Advertisement
pongfactory

update 20May2016 - 2

May 20th, 2016
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 5.29 KB | None | 0 0
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %%%%%%%%%%%%% One-dimensional bin packing problem %%%%%%%%%%%%%%%%%%%%
  3. %%%%%%%%%%%%%%%%%%% Project II | 1 May 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 1 - 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. % input_input = u
  31. % b = sort(u, 'descend');
  32. % u = b
  33. %
  34. % end
  35.  
  36.  
  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. start = 1;
  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.        
  47.         fprintf('copy object %d', i);
  48.         copy(i) = input(' Insert your copy object value : ');
  49.         fprintf('copy %d', i);
  50.         fprintf(' = %d\n', copy(i));
  51.        
  52.         for j=start:(start+copy(i)-1),
  53.             input_all(j) = u(i);
  54.         end
  55.         start = start+copy(i);
  56.    
  57.     input_all
  58.    % input_input = u
  59.     u = input_all; %input
  60.     b = sort(u, 'descend');
  61.     u = b
  62.  
  63.  
  64. end
  65.  
  66. %u = [1 4 2 1 2 3 5 5 6 3 2 4];  
  67. n = length(u);
  68.  
  69. %fprintf('BIN = %d', i);
  70. C = input(' Insert your BIN value : ');
  71. %fprintf('bin size = %d\n', C);
  72. disp('................................................');
  73. r = C*ones(1,n);
  74. w = zeros(n);
  75.  
  76. [ud1,idec]=sort(-u); % sort obj.
  77. ud=-ud1;
  78. [tmp,irec]=sort(idec);
  79.  
  80. disp('................................................');
  81. disp('One dimensional bin-packing heuristic algorithms');
  82. disp('Press 0 - default choice;');
  83. disp('Press 1 - FFD method;');
  84. %disp('Press 2 - first fit decreasing method;');
  85. disp('Press 2 - BFD method;');
  86. %disp('Press 4 - best fit decreasing method;');
  87. chosen=input('Enter your choice : ');
  88.  
  89.  
  90.  
  91.  
  92. % ****************************  FFD method  ****************************** %
  93. if chosen==0 | chosen==1,  % first fit method
  94. wff=w; rff=r+0.01; % initialize for first fit method
  95. for i=1:n,
  96.    idx=min(find([u(i)*ones(1,n)<=rff]));
  97.    wff(i,idx)=1;
  98.    rff(idx)=rff(idx)-u(i);  % update the state
  99. end
  100. disp('% *******************************');
  101. disp('the assignment are:')
  102. disp(wff)
  103.  
  104. % B = wff(:,1)
  105. % A = input_input'
  106. % B = logical(B);
  107. % A(B)
  108.  
  109. N_feature = size(wff,2);
  110.  
  111. input_temp = [u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u'];
  112. % Q = [input_temp(:,1:4)]
  113. % C = wff(:,1:4);
  114. % C = logical(C);
  115. % Q(C)
  116. %show_out = input_temp(C)
  117.  
  118. for k=1:N_feature,
  119.     fprintf('Data in Bin %d', k);
  120.     B = wff(:,k);
  121.     %A = input_input';
  122.     A = input_temp(:,k);
  123.     B = logical(B);
  124.     output_ans = A(B)
  125.    
  126.    % output_ans(k) = A(B);
  127. end
  128.  
  129.  
  130. disp('First fit (FFD) method: ')
  131. occpff=u*wff;
  132. disp(['Total # of bins used = ' int2str(sum([occpff > 0]))]);
  133. nbinff=sum([occpff > 0]);
  134. disp('the occupancy of each bin after packing are:');
  135. disp(occpff(1:nbinff))
  136. end
  137. % ******************************************************************************* %
  138.  
  139.  
  140.  
  141.  
  142. % ****************************   BFD method  ****************************** %
  143. if chosen==0 | chosen==2,  % best fit d method
  144. wbfd=w; rbfd=r+0.01; % initialize for best fit method
  145.     for i=1:n,
  146.      
  147.        idx1=find([ud(i)*ones(1,n) <= rbfd]);
  148.        [tmp,idx2]=min(rbfd(idx1)-ud(i));    
  149.        wbfd(i,idx1(idx2))=1; rbfd(idx1(idx2))=tmp; % update the state
  150.  
  151.     end
  152. wbfd=wbfd(irec,:);
  153. disp('% *******************************');
  154. disp('the assignment are:')
  155. disp(wbfd)
  156.  
  157.  
  158. N_feature = size(wbfd,2);
  159.  
  160. input_temp = [u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u' u'];
  161. % Q = [input_temp(:,1:4)]
  162. % C = wff(:,1:4);
  163. % C = logical(C);
  164. % Q(C)
  165. %show_out = input_temp(C)
  166.  
  167. for k=1:N_feature,
  168.     fprintf('Data in Bin %d', k);
  169.     B = wbfd(:,k);
  170.     %A = input_input';
  171.     A = input_temp(:,k);
  172.     B = logical(B);
  173.     output_ans = A(B)
  174.    
  175.    % output_ans(k) = A(B);
  176. end
  177.  
  178.  
  179.  
  180.  
  181. disp('Best fit decreasing (BFD) method: ')
  182. occpbfd=u*wbfd;
  183. nbinbfd=sum([occpbfd > 0]);
  184. disp(['Total # of bins used = ' int2str(nbinbfd)]);
  185. disp('the occupancy of each bin after packing are:');
  186. disp(occpbfd(1:nbinbfd))
  187.  
  188. end %
  189.    
  190.  
  191. % ******************************************************************************* %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement