Advertisement
pongfactory

Project II - One Bin (New)(9 April 2016)

Apr 9th, 2016
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.97 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%% One-dimensional bin packing problem %%%%%%%%%%%%%%%%%%%%
  3. %%%%%%%%%%%%%%%%%%% Project II | 9 April 2016%%%%%%%%%%%%%%%%%%%%%%%%%  
  4. %%%%%%%%%%%%%%%% WANCHAI SUKSRINUAN & AMNUY kUYBAN %%%%%%%%%%%%%%%%%%%
  5. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  6.  
  7. clc;
  8. clear;
  9. disp('................................................');
  10. disp('One dimensional bin-packing heuristic algorithms');
  11. disp('Select Input Type');
  12. disp('Press 1 - Auto Input;');
  13. disp('Press 2 - Manual Input;');
  14.  
  15.  
  16. chosen=input('Enter your choice : ');
  17. if chosen==1,
  18. %u = [5 3 4 9 1 8 7 8 11 5];
  19. %u = [4 11 8 5 2 8 7 2 2 11];
  20. r_keyboard = input('Number of object : ');
  21. number_object = r_keyboard;
  22. u = round(rand(1,number_object)*10)+1;
  23. %round(rand(1)*100);
  24.     for i=1:number_object,
  25.        
  26.         fprintf('object %d', i);
  27.         fprintf(' = %d\n', u(i));
  28.     end
  29. end
  30.  
  31. if chosen==2,
  32. r_keyboard = input('Number of object : ');
  33. %u = [5 3 4 9 1 8 7 8 11 5];
  34. number_object = r_keyboard;
  35.  
  36.     for i=1:number_object,
  37.         fprintf('object %d', i);
  38.          u(i) = input(' Insert your object value : ');
  39.         fprintf('object %d', i);
  40.         fprintf(' = %d\n', u(i));
  41.     end
  42. end
  43.  
  44. %u = [1 4 2 1 2 3 5 5 6 3 2 4];  
  45. n = length(u);
  46. C = 20;
  47. fprintf('bin size = %d\n', C);
  48. disp('................................................');
  49. r = C*ones(1,n);
  50. w = zeros(n);
  51.  
  52. [ud1,idec]=sort(-u); % sort obj.
  53. ud=-ud1;
  54. [tmp,irec]=sort(idec);
  55.  
  56. disp('................................................');
  57. disp('One dimensional bin-packing heuristic algorithms');
  58. disp('Press 0 - default choice;');
  59. disp('Press 1 - first fit method;');
  60. disp('Press 2 - best fit method;');
  61. disp('Press 3 - first fit decreasing method;');
  62. disp('Press 4 - best fit decreasing method;');
  63. chosen=input('Enter your choice : ');
  64.  
  65.  
  66.  
  67.  
  68. % ****************************  First fit method  ****************************** %
  69. if chosen==0 | chosen==1,  % first fit method
  70. wff=w; rff=r; % initialize for first fit method
  71. for i=1:n,
  72.    idx=min(find([u(i)*ones(1,n)<=rff]));
  73.    wff(i,idx)=1;
  74.    rff(idx)=rff(idx)-u(i);  % update the state
  75. end
  76. disp('% *******************************');
  77. disp('the assignment are:')
  78. disp(wff)
  79. disp('First fit (FF) method: ')
  80. occpff=u*wff;
  81. nbinff=sum([occpff > 0]);
  82. disp('the occupancy of each bin after packing are:');
  83. disp(occpff(1:nbinff))
  84. end
  85. % ******************************************************************************* %
  86.  
  87.  
  88.  
  89.  
  90.  
  91.  
  92.  
  93. % ****************************   Best fit method  ****************************** %
  94. if chosen==0 | chosen==2,  % first fit method
  95. wbf=w; rbf=r; % initialize for best fit method
  96. for i=1:n,
  97.    idx1=find([u(i)*ones(1,n) <= rbf]);
  98.    [tmp,idx2]=min(rbf(idx1)-u(i));  
  99.    wbf(i,idx1(idx2))=1; rbf(idx1(idx2))=tmp; % update the state
  100. end
  101. disp('% *******************************');
  102. disp('the assignment are:')
  103. disp(wbf)
  104. disp('Best fit (BF) method: ')
  105. occpbf=u*wbf;
  106. nbinbf=sum([occpbf > 0]);
  107. disp('the occupancy of each bin after packing are:');
  108. disp(occpbf(1:nbinbf))
  109. end
  110. % ******************************************************************************* %
  111.  
  112.  
  113.  
  114.  
  115.  
  116.  
  117.  
  118.  
  119. % ******************   first fit decreasing methodt method  ********************* %
  120. if chosen==0 | chosen==3,  % first fit method
  121. wffd=w; rffd=r; % initialize for first fit method
  122. for i=1:n,
  123.    % disp(['ud(' int2str(i) ') = ' int2str(ud(i))]);
  124.    % disp('r = '); disp(rffd);
  125.    idx=min(find([ud(i)*ones(1,n)<=rffd]));
  126.    % disp(['u(' int2str(i) ') is assigned to b(' int2str(idx) ');']);
  127.    wffd(i,idx)=1; rffd(idx)=rffd(idx)-ud(i);  % update the state
  128. end
  129. wffd=wffd(irec,:); % sort it back to the original assignment order
  130. %disp('% *******************************');
  131. %disp('First fit decreasing (FFD) method: ')
  132. disp('the assignment are:')
  133. disp(wffd)
  134. occpffd=u*wffd;
  135. nbinffd=sum([occpffd > 0]);
  136. disp(['Total # of bins used = ' int2str(nbinffd)]);
  137. disp('the occupancy of each bin after packing are:');
  138. disp(occpffd(1:nbinffd))
  139. %disp(idx)
  140. % disp('Press any key to continue to Best fit method ...'); pause
  141.    
  142. end
  143. % ******************************************************************************* %
  144.  
  145.  
  146.  
  147.  
  148.  
  149.  
  150.  
  151. % *******************   Best fit decreasing method  ****************************** %
  152. if chosen==0 | chosen==4,
  153. wbfd=w; rbfd=r; % initialize for best fit method
  154.     for i=1:n,
  155.      
  156.        idx1=find([ud(i)*ones(1,n) <= rbfd]);
  157.        [tmp,idx2]=min(rbfd(idx1)-ud(i));    
  158.        wbfd(i,idx1(idx2))=1; rbfd(idx1(idx2))=tmp; % update the state
  159.  
  160.     end
  161. wbfd=wbfd(irec,:);
  162. disp('% *******************************');
  163. disp('the assignment are:')
  164. disp(wbdf)
  165. disp('Best fit decreasing (BFD) method: ')
  166. occpbfd=u*wbfd;
  167. nbinbfd=sum([occpbfd > 0]);
  168. disp(['Total # of bins used = ' int2str(nbinbfd)]);
  169. disp('the occupancy of each bin after packing are:');
  170. disp(occpbfd(1:nbinbfd))
  171.  
  172. end %
  173. % ******************************************************************************* %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement