Advertisement
pongfactory

Project 2 - Decreasing

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