2
\$\begingroup\$

I have a fitness function, where inputs x1 to x14 have values between 1:4. Is there a way to make this more efficient? Any suggestions are useful. I need to get the computation time down. A smaller scale pseudocode is below:

%Help improve computation time

 function y = Reduce(x1,x2,x3,x4,x5,x6,x7,x8,x9,x10,x11,x12,x13,x14)

P= [ 0.3           0.2          0.01      0;
 0.4           0.1          0.02      0;
 0.7           0.5          0.2       0;
 0.5           0.3          0.17      0;
 0.3           0.2          0.09      0;
 0.4           0.3          0.17      0;
 0.4           0.1          0.11      0;
 0.3           0.1          0.11      0;
 0.23          0.14         0.04      0;
 0.25          0.13         0.02      0;
 0.625         0.589        0.29      0;
 0.6           0.38         0.2       0;
 0.23          0.15         0.05      0;
 0.3           0.01         0.001     0];
 Q= [ 0              0            0         0;
 0              0            0         0;
 0              0            0         0;
 0              .11          .11       .11;
 0              0            0         0;
 0              0            0         0;
 0              0            0         0;
 0              0            0         0;
 0              0            0         0;
 0              0            0         0;
 0.178          0.1          0.01      0;
 0.43           0.32         0.07      0;
 0              0.13         0.08      0;
 0              0            0         0];

 N=1-P-Q;

 P= [P, Q];

%Make grid for all the pets
web={0:1,0:1,0:2,0:1,0:1,0:1,0:1,0:1,0:1,0:2,0:2,0:2,0:1,0:1};
[aa bb cc dd ee ff gg hh ii jj kk ll mm nn ] = ndgrid(web{:});
RaNk = [aa(:) bb(:) cc(:) dd(:) ee(:) ff(:) gg(:) hh(:) ii(:) jj(:) kk(:) ll(:) mm(:) nn(:)];

%function to get weight proportion
function result = gailv(a,b,c,d,e)
result = d; 
result(c==4 & b>0) = 0; 
result(c==4 & b==0) = 1;
result(c<4 & b==1) = a;
result(c<4 & b==2) = e;

end


%% Loop over all weight proportion possibilities
y = 0;


for j=1:size(RaNk,1)

Proportion=gailv(P(1,x1),RaNk(j,1),x1,N(1,x1),Q(1,x1))*gailv(P(2,x2),RaNk(j,2),x2,N(2,x2),Q(2,x2))*gailv(P(3,x3),RaNk(j,3),x3,N(3,x3),Q(3,x4))*gailv(P(4,x4),RaNk(j,4),x4,N(4,x4),Q(4,x4))*gailv(P(5,x5),RaNk(j,5),x5,N(5,x5),Q(5,x5))*gailv(P(6,x6),RaNk(j,6),x6,N(6,x6),Q(6,x6))*gailv(P(7,x7),RaNk(j,7),x7,N(7,x7),Q(7,x7))*gailv(P(8,x8),RaNk(j,8),x8,N(8,x8),Q(8,x8))*gailv(P(9,x9),RaNk(j,9),x9,N(9,x9),Q(9,x9))*gailv(P(10,x10),RaNk(j,10),x10,N(10,x10),Q(10,x10))*gailv(P(11,x11),RaNk(j,11),x11,N(11,x11),Q(11,x11))*gailv(P(12,x12),RaNk(j,12),x12,N(12,x12),Q(12,x12))*gailv(P(13,x13),RaNk(j,13),x13,N(13,x13),Q(13,x13))*gailv(P(14,x14),RaNk(j,14),x14,N(14,x14),Q(14,x14));

 weightCATS(aa(j)==0 & bb(j)==0)=0;
 weightCATS(bb(j)==1)=2;
 weightCATS(aa(j)==1 & bb(j)==0)=1;
 weightCOWS(cc(j)==0 & dd(j)==0 & ee(j)==0 & ff(j)==0 & gg(j)==0 & hh(j)==0)=0;
 weightCOWS(ff(j)==1)=7;
 weightCOWS(ff(j)~=1 & gg(j)==1)=6;
 weightCOWS(ff(j)~=1 & gg(j)==0 & hh(j)==1)=5;
 weightCOWS(cc(j)==1 & ff(j)~=1 & gg(j)==0 & hh(j)==0)=4;
 weightCOWS(cc(j)==0 & ff(j)==2 & gg(j)==0 & hh(j)==0)=3;
 weightCOWS(cc(j)==0 & ee(j)==1 & ff(j)==0 & gg(j)==0 & hh(j)==0)=2;
 weightCOWS(cc(j)==0 & dd(j)==1 & ee(j)==0 & ff(j)==0 & gg(j)==0 & hh(j)==0)=1;
 weightDOGS(ii(j)==0 & jj(j)==0 & kk(j)==0)=0;
 weightDOGS(jj(j)==1)=3;
 weightDOGS(ii(j)==1 & jj(j)==0)=2;
 weightDOGS(ii(j)==0 & jj(j)==0 & kk(j)==1)=1;
 weightDUCKS(ll(j)==0 & mm(j)==0 & nn(j)==0)=0;
 weightDUCKS(ll(j)==1 | mm(j)==1 | nn(j)==1)=2;
 weightDUCKS(ll(j)==0 & ((mm(j)==2 & nn(j)==2) | (mm(j)==2 & nn(j)==0) | (mm(j)==0 &     nn(j)==2)))=1;
 weight=[weightCATS(:) weightCOWS(:) weightDOGS(:) weightDUCKS(:) ];
 weight=sscanf(sprintf('%d',weight),'%d');

  % get weight from search function
    weight_get= [00000  10000   20000   00001   30000   10001   40000   20001   00002   30001   50000   00003   ;
 0  1.636626462 2.277517454 2.767758977 2.86906291  4.415328506 4.667984412 5.062564392 5.476217888 5.651094723 5.743576203 6.659943334];
    WEI = weight_get(2,weight_get(1,:)==weight);   

  y=y-Proportion*WEI;


  end

end
\$\endgroup\$
  • \$\begingroup\$ Please try to profile your code. That gives you and others a starting point for improvements. \$\endgroup\$ – Dennis Jaheruddin Aug 26 '13 at 12:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.