Advertisement
pongfactory

Project II - Two Bin (Wanchai and Amnoi Kuyban)

Apr 17th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 12.89 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%% Two-dimensional bin packing problem %%%%%%%%%%%%%%%%%%%%
  3. %%%%%%%%%%%%%%%%%%% Project II | 17 April 2016%%%%%%%%%%%%%%%%%%%%%%%%%  
  4. %%%%%%%%%%%%%%%% WANCHAI SUKSRINUAN & AMNUY kUYBAN %%%%%%%%%%%%%%%%%%%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6. % main box sizes:
  7. a=2;
  8. b=1;
  9.  
  10. Nb=30; % number of boxes
  11.  
  12. % random boxes sizes:
  13. mab=mean([a b]);
  14. aa=0.05*mab+0.3*mab*rand(1,Nb);
  15. bb=0.05*mab+0.3*mab*rand(1,Nb);
  16. m2=min([aa bb]/2); % smallest half-size
  17. AA=aa.*bb; % boxes areas
  18.  
  19. penalty=0.2*a*b;
  20. nac=0.8; % negative area coefficient
  21.  
  22.  
  23. N=500; % population size
  24. ng=5000; % number of generations
  25. pmpe=0.05; % places exchange mutation probability
  26. pmbj=0.01; % big gauss jump
  27. pmsj=0.02; % small gauss jump
  28. pmrr=0.05; % random rotation
  29. pmvi=0.05; % random visible/invisible
  30. pmne=0.1; % move to nearest adge
  31.  
  32. figure;
  33. %ha1=axes;
  34. ha1=subplot(2,1,1);
  35. plot([0 a a 0 0], [0 0 b b 0],'b-');
  36. xlim([-0.1*a 1.1*a]);
  37. ylim([-0.1*b 1.1*b]);
  38. set(ha1,'NextPlot','add');
  39. ht=title(ha1,'start');
  40. ha2=subplot(2,1,2);
  41. drawnow;
  42.  
  43.  
  44. set_cl; % set color table cl to plot boxes with different colors
  45.  
  46.  
  47.  
  48. % random initial population:
  49. G=zeros(N,4*Nb);
  50. Gch=zeros(N,4*Nb); % children
  51. for Nc=1:N % for each individual
  52.     G1=zeros(4,Nb); % one individual
  53.     % G1(1,i)=1 if i-box is visible
  54.     % G1(2,i)=1 if i-box is rotated at 90 degrees
  55.     % G1(3,i) - x-coordinate of i-box center
  56.     % G1(4,i) - y-coordinate of i-box center
  57.  
  58.     G1(1,:)=double(rand(1,Nb)<0.2);
  59.     G1(2,:)=double(rand(1,Nb)<0.5);
  60.    
  61.     G1(3,:)=m2+(a-m2)*rand(1,Nb);
  62.     G1(4,:)=m2+(b-m2)*rand(1,Nb);
  63.    
  64.    
  65.     G(Nc,:)=(G1(:))'; % (G1(:))' converts matrix to row-vector
  66. end
  67.  
  68. hi=imagesc(G,'parent',ha2);
  69. drawnow;
  70.  
  71.  
  72.  
  73. Gpr1=zeros(4,Nb);
  74. Gpr2=zeros(4,Nb); % two parents
  75. Gch1=zeros(4,Nb);
  76. Gch2=zeros(4,Nb); % two children
  77. for ngc=1:ng % generations counting
  78.     % find fitnesses:
  79.     fitnesses=zeros(N,1);
  80.     for Nc=1:N % for each individual
  81.         G1(:)=(G(Nc,:))';
  82.         vis=G1(1,:);
  83.         ind=find(vis);
  84.         L=length(ind);
  85.         if L>0
  86.             % only visible:
  87.             rot=G1(2,ind);
  88.             x=G1(3,ind);
  89.             y=G1(4,ind);
  90.             if L==1
  91.                 aaa=aa(ind);
  92.                 bbb=bb(ind);
  93.                 if rot
  94.                     tmp=aaa;
  95.                     aaa=bbb;
  96.                     bbb=tmp;
  97.                 end
  98.                 A0=AA(ind); % box area
  99.                 x1=max([x-aaa/2  0]);
  100.                 y1=max([y-bbb/2  0]);
  101.                 x2=min([x+aaa/2  a]);
  102.                 y2=min([y+bbb/2  b]);
  103.                 % x1 - x2,  y1 - y2 is box (part of current box) that inside main box
  104.                 if (x1>=x2)||(y1>=y2)
  105.                     A=0; % box that inside main box area
  106.                 else
  107.                     A=(x2-x1)*(y2-y1); % box that inside main box area
  108.                 end
  109.                 %if A<A0 % if not fully inside main box
  110.                 if (aaa/2<=x)&&(x<=a-aaa/2)&&(bbb/2<=y)&&(y<=b-bbb/2) % if filly inside
  111.                     fitness=A;
  112.                 else
  113.                     fitness=A-nac*(A0-A)-penalty;
  114.                 end
  115.                    
  116.             else
  117.                 fitness=0;
  118.                 ispen=false; % true if penality
  119.                
  120.                 % check cross with main box:
  121.                 % add boxes arreas and strong subtract out areas:
  122.                 for n=1:L % for each box
  123.                     ind1=ind(n);
  124.                     aaa=aa(ind1);
  125.                     bbb=bb(ind1);
  126.                     if rot(n)
  127.                         tmp=aaa;
  128.                         aaa=bbb;
  129.                         bbb=tmp;
  130.                     end
  131.                     A0=AA(ind1); % box area
  132.                     x1=max([x(n)-aaa/2  0]);
  133.                     y1=max([y(n)-bbb/2  0]);
  134.                     x2=min([x(n)+aaa/2  a]);
  135.                     y2=min([y(n)+bbb/2  b]);
  136.                     % x1 - x2,  y1 - y2 is box (part of current box) that inside main box
  137.                     if (x1>=x2)||(y1>=y2)
  138.                         A=0; % box that inside main box area
  139.                     else
  140.                         A=(x2-x1)*(y2-y1); % box that inside main box area
  141.                     end
  142.                     %if A<A0 % if not fully inside main box
  143.                         %fitness=fitness + A-nac*(A0-A);
  144.                         %ispen=true; % penality
  145.                     %else
  146.                         %fitness=fitness + A;
  147.                     %end
  148.                    
  149.                     if (aaa/2<=x(n))&&(x(n)<=a-aaa/2)&&(bbb/2<=y(n))&&(y(n)<=b-bbb/2) % if filly inside
  150.                         fitness=fitness + A;
  151.                     else
  152.                         fitness=fitness + A-nac*(A0-A);
  153.                         ispen=true; % penality
  154.                     end
  155.                    
  156.                 end
  157.                
  158.                 % for each pair of boxes:
  159.                 for n1=1:L-1
  160.                     ind1=ind(n1);
  161.                     aaa1=aa(ind1);
  162.                     bbb1=bb(ind1);
  163.                     if rot(n1)
  164.                         tmp=aaa1;
  165.                         aaa1=bbb1;
  166.                         bbb1=tmp;
  167.                     end
  168.                     A1=AA(ind1);
  169.                     x1=x(n1);
  170.                     y1=y(n1); % position of 1st box of pair
  171.                     for n2=n1+1:L
  172.                         ind2=ind(n2);
  173.                         aaa2=aa(ind2);
  174.                         bbb2=bb(ind2);
  175.                         if rot(n2)
  176.                             tmp=aaa2;
  177.                             aaa2=bbb2;
  178.                             bbb2=tmp;
  179.                         end
  180.                         A2=AA(ind2);
  181.                         x2=x(n2);
  182.                         y2=y(n2); % position of 2nd box of pair
  183.                         dx=abs(x1-x2);
  184.                         dy=abs(y1-y2); % distancies
  185.                         a12=(aaa1/2+aaa2/2);
  186.                         b12=(bbb1/2+bbb2/2);
  187.                         if (dx<a12)&&(dy<b12) % if cross
  188.                             ispen=true;
  189.                             Ac=(a12-dx)*(b12-dy); % area of cross
  190.                             fitness=fitness-Ac-Ac; % becuse area of n1 and n2 was added fully
  191.                             fitness=fitness-2*nac*Ac;
  192.                         end
  193.  
  194.                     end
  195.                 end
  196.                
  197.                 if ispen
  198.                     fitness=fitness-penalty;
  199.                 end
  200.        
  201.             end
  202.         else
  203.             fitness=0;
  204.         end
  205.         fitnesses(Nc)=fitness;
  206.     end
  207.    
  208.     [fb bi]=max(fitnesses); % best
  209.    
  210.     % plot best:
  211.     G1(:)=(G(bi,:))';
  212.     Gb=G(bi,:); % best
  213.     if mod(ngc,10)==0
  214.         cla(ha1);
  215.         Atmp=0;
  216.         for Nbc=1:Nb
  217.             vis1=G1(1,Nbc);
  218.             if vis1
  219.                 rot1=G1(2,Nbc);
  220.                 aaa=aa(Nbc);
  221.                 bbb=bb(Nbc);
  222.                 if rot1
  223.                     tmp=aaa;
  224.                     aaa=bbb;
  225.                     bbb=tmp;
  226.                 end
  227.                 x=G1(3,Nbc);
  228.                 y=G1(4,Nbc);
  229.                 plot([x-aaa/2  x+aaa/2  x+aaa/2  x-aaa/2  x-aaa/2],...
  230.                      [y-bbb/2  y-bbb/2  y+bbb/2  y+bbb/2  y-bbb/2],...
  231.                      '-','color',cl(Nbc,:),...
  232.                      'parent',ha1);
  233.                 hold on;
  234.                 Atmp=Atmp+aaa*bbb;
  235.             end
  236.         end
  237.         plot([0 a a 0 0], [0 0 b b 0],'b-','parent',ha1);
  238.         xlim(ha1,[-0.1*a 1.1*a]);
  239.         ylim(ha1,[-0.1*b 1.1*b]);
  240.        
  241.         set(hi,'Cdata',G);
  242.        
  243.         nvb=length(find(G1(1,:))); % number of visible boxes
  244.        
  245.         set(ht,'string',[' generation: ' num2str(ngc)  ', boxes: ' num2str(nvb) ', area: ' num2str(fb)]);
  246.        
  247.         drawnow;
  248.     end
  249.    
  250.    
  251.     % prepare for crossover, selection:
  252.     fmn=min(fitnesses);
  253.     fst=std(fitnesses);
  254.     if fst<1e-7
  255.         fst=1e-7;
  256.     end
  257.     fmn1=fmn-0.01*fst; % little low then minimum
  258.     P=fitnesses-fmn1; % positive values
  259.     p=P/sum(P); % probabilities
  260.     ii=roulette_wheel_indexes(N,p);
  261.     Gp=G(ii,:); % parents
  262.    
  263.     % crossover:
  264.     for n=1:2:N
  265.         pr1=Gp(n,:);
  266.         pr2=Gp(n+1,:); % two parents
  267.         % in matrix form:
  268.         Gpr1(:)=pr1';
  269.         Gpr2(:)=pr2';
  270.        
  271.         for Nbc=1:Nb
  272.            
  273.             % visibility:
  274.             if rand<0.5
  275.                 Gch1(1,Nbc)=Gpr1(1,Nbc);
  276.             else
  277.                 Gch1(1,Nbc)=Gpr2(1,Nbc);
  278.             end
  279.             if rand<0.5
  280.                 Gch2(1,Nbc)=Gpr1(1,Nbc);
  281.             else
  282.                 Gch2(1,Nbc)=Gpr2(1,Nbc);
  283.             end
  284.            
  285.             % rotation:
  286.             if rand<0.5
  287.                 Gch1(2,Nbc)=Gpr1(2,Nbc);
  288.             else
  289.                 Gch1(2,Nbc)=Gpr2(2,Nbc);
  290.             end
  291.             if rand<0.5
  292.                 Gch2(2,Nbc)=Gpr1(2,Nbc);
  293.             else
  294.                 Gch2(2,Nbc)=Gpr2(2,Nbc);
  295.             end
  296.            
  297.             % position:
  298.             % child 1:
  299.             %i3=ceil(3*rand);
  300.             %i3=roulette_wheel_indexes(1,[0.2 0.4 0.4]);
  301.             i3=1+ceil(2*rand);
  302.             switch i3
  303.                 case 1 % get mean position
  304.                     Gch1(3,Nbc)=(Gpr1(3,Nbc)+Gpr2(3,Nbc))/2;
  305.                     Gch1(4,Nbc)=(Gpr1(4,Nbc)+Gpr2(4,Nbc))/2;
  306.                 case 2 %get position of parent 1
  307.                     Gch1(3,Nbc)=Gpr1(3,Nbc);
  308.                     Gch1(4,Nbc)=Gpr1(4,Nbc);
  309.                 case 3 %get position of parent 2
  310.                     Gch1(3,Nbc)=Gpr2(3,Nbc);
  311.                     Gch1(4,Nbc)=Gpr2(4,Nbc);
  312.             end
  313.             % child 2:
  314.             %i3=ceil(3*rand);
  315.             %i3=roulette_wheel_indexes(1,[0.2 0.4 0.4]);
  316.             i3=1+ceil(2*rand);
  317.             switch i3
  318.                 case 1 % get mean position
  319.                     Gch2(3,Nbc)=(Gpr1(3,Nbc)+Gpr2(3,Nbc))/2;
  320.                     Gch2(4,Nbc)=(Gpr1(4,Nbc)+Gpr2(4,Nbc))/2;
  321.                 case 2 %get position of parent 1
  322.                     Gch2(3,Nbc)=Gpr1(3,Nbc);
  323.                     Gch2(4,Nbc)=Gpr1(4,Nbc);
  324.                 case 3 %get position of parent 2
  325.                     Gch2(3,Nbc)=Gpr2(3,Nbc);
  326.                     Gch2(4,Nbc)=Gpr2(4,Nbc);
  327.             end
  328.            
  329.            
  330.         end
  331.         ch1=(Gch1(:))';
  332.         ch2=(Gch2(:))';
  333.         Gch(n,:)=ch1;
  334.         Gch(n+1,:)=ch2;
  335.        
  336.        
  337.     end
  338.     G=Gch; % now children
  339.    
  340.     % mutations:
  341.     % places exchange
  342.     for Nc=1:N % for each individual
  343.         if rand<pmpe
  344.             G1(:)=(G(Nc,:))';
  345.             ir1=ceil(Nb*rand);
  346.             ir2=ceil(Nb*rand);
  347.             tmp1=G1(3:4,ir1);
  348.             G1(3:4,ir1)=G1(3:4,ir2);
  349.             G1(3:4,ir2)=tmp1;
  350.             G(Nc,:)=(G1(:))';
  351.         end
  352.     end
  353.    
  354.     % big gauss jump:
  355.     for Nc=1:N % for each individual
  356.         if rand<pmbj
  357.             G1(:)=(G(Nc,:))';
  358.             ir=ceil(Nb*rand);
  359.             G1(3:4,ir)=G1(3:4,ir)+[0.05*a*randn;
  360.                                    0.05*b*randn];
  361.             G(Nc,:)=(G1(:))';
  362.         end
  363.     end
  364.    
  365.     % small gauss jump:
  366.     for Nc=1:N % for each individual
  367.         if rand<pmsj
  368.             G1(:)=(G(Nc,:))';
  369.             ir=ceil(Nb*rand);
  370.             G1(3:4,ir)=G1(3:4,ir)+[0.005*a*randn;
  371.                                    0.005*b*randn];
  372.             G(Nc,:)=(G1(:))';
  373.         end
  374.     end
  375.    
  376.     % random rotation:
  377.     for Nc=1:N % for each individual
  378.         if rand<pmrr
  379.             G1(:)=(G(Nc,:))';
  380.             ir=ceil(Nb*rand);
  381.             G1(2,ir)=double(rand<0.5);
  382.             G(Nc,:)=(G1(:))';
  383.         end
  384.     end
  385.    
  386.     % random visible/invisible:
  387.     for Nc=1:N % for each individual
  388.         if rand<pmvi
  389.             G1(:)=(G(Nc,:))';
  390.             ir=ceil(Nb*rand);
  391.             G1(1,ir)=double(rand<0.5);
  392.             G(Nc,:)=(G1(:))';
  393.         end
  394.     end
  395.    
  396.     % move to nearest edge:
  397.     for Nc=1:N % for each individual
  398.         if rand<pmne
  399.             G1(:)=(G(Nc,:))';
  400.             ir=ceil(Nb*rand); % random small box
  401.             rv=find((G1(1,:))&((1:Nb)~=Nc)); % find rest visible
  402.             if rand<0.5
  403.                 % to veritcile edge
  404.                 eax=[G1(3,rv)-aa(rv)/2  G1(3,rv)+aa(rv)/2  0  a]; % edge xs
  405.                 deax=[(G1(3,ir)-aa(ir)/2) - eax  (G1(3,ir)+aa(ir)/2) - eax]; % distancies
  406.                 [dmn indm]=min(abs(deax));
  407.                 G1(3,ir)=G1(3,ir)-deax(indm);
  408.             else
  409.                 % to horizontal edge
  410.                 eay=[G1(4,rv)-bb(rv)/2  G1(4,rv)+bb(rv)/2  0  b]; % edge ys
  411.                 deay=[(G1(4,ir)-bb(ir)/2) - eay  (G1(4,ir)+bb(ir)/2) - eay]; % distancies
  412.                 [dmn indm]=min(abs(deay));
  413.                 G1(4,ir)=G1(4,ir)-deay(indm);
  414.             end
  415.         end
  416.     end
  417.    
  418.    
  419.    
  420.     % ellitism:
  421.     G(1,:)=Gb;
  422.    
  423.  
  424.    
  425.    
  426.    
  427. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement