Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%% One-dimensional bin packing problem %%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%% Project II | 1 May 2016%%%%%%%%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%% WANCHAI SUKSRINUAN & AMNUY kUYBAN %%%%%%%%%%%%%%%%%%%
- %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
- clc;
- clear;
- disp('................................................');
- disp('One dimensional bin-packing heuristic algorithms');
- disp('Select Input Type');
- %disp('Press 1 - Auto Input;');
- %disp('Press 1 - Manual Input;');
- %chosen=input('Enter your choice : ');
- % if chosen==1,
- % %u = [5 3 4 9 1 8 7 8 11 5];
- % %u = [4 11 8 5 2 8 7 2 2 11];
- % r_keyboard = input('Number of object : ');
- % number_object = r_keyboard;
- % u = round(rand(1,number_object)*10)+1;
- % %round(rand(1)*100);
- % for i=1:number_object,
- %
- % fprintf('object %d', i);
- % fprintf(' = %d\n', u(i));
- % end
- % %sort(u)
- % input_input = u
- % b = sort(u, 'descend');
- % u = b
- %
- % end
- r_keyboard = input('Number of object : ');
- %u = [5 3 4 9 1 8 7 8 11 5];
- number_object = r_keyboard;
- start = 1;
- for i=1:number_object,
- fprintf('object %d', i);
- u(i) = input(' Insert your object value : ');
- fprintf('object %d', i);
- fprintf(' = %d\n', u(i));
- fprintf('copy object %d', i);
- copy(i) = input(' Insert your copy object value : ');
- fprintf('copy %d', i);
- fprintf(' = %d\n', copy(i));
- for j=start:(start+copy(i)-1),
- input_all(j) = u(i);
- end
- start = start+copy(i);
- input_all
- % input_input = u
- u = input_all; %input
- b = sort(u, 'descend');
- u = b
- end
- %u = [1 4 2 1 2 3 5 5 6 3 2 4];
- n = length(u);
- %fprintf('BIN = %d', i);
- C = input(' Insert your BIN value : ');
- %fprintf('bin size = %d\n', C);
- disp('................................................');
- r = C*ones(1,n);
- w = zeros(n);
- [ud1,idec]=sort(-u); % sort obj.
- ud=-ud1;
- [tmp,irec]=sort(idec);
- disp('................................................');
- disp('One dimensional bin-packing heuristic algorithms');
- disp('Press 0 - default choice;');
- disp('Press 1 - FFD method;');
- %disp('Press 2 - first fit decreasing method;');
- disp('Press 2 - BFD method;');
- %disp('Press 4 - best fit decreasing method;');
- chosen=input('Enter your choice : ');
- % **************************** FFD method ****************************** %
- if chosen==0 | chosen==1, % first fit method
- wff=w; rff=r+0.01; % initialize for first fit method
- for i=1:n,
- idx=min(find([u(i)*ones(1,n)<=rff]));
- wff(i,idx)=1;
- rff(idx)=rff(idx)-u(i); % update the state
- end
- disp('% *******************************');
- disp('the assignment are:')
- disp(wff)
- % B = wff(:,1)
- % A = input_input'
- % B = logical(B);
- % A(B)
- N_feature = size(wff,2);
- 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'];
- % Q = [input_temp(:,1:4)]
- % C = wff(:,1:4);
- % C = logical(C);
- % Q(C)
- %show_out = input_temp(C)
- for k=1:N_feature,
- fprintf('Data in Bin %d', k);
- B = wff(:,k);
- %A = input_input';
- A = input_temp(:,k);
- B = logical(B);
- output_ans = A(B)
- % output_ans(k) = A(B);
- end
- disp('First fit (FFD) method: ')
- occpff=u*wff;
- disp(['Total # of bins used = ' int2str(sum([occpff > 0]))]);
- nbinff=sum([occpff > 0]);
- disp('the occupancy of each bin after packing are:');
- disp(occpff(1:nbinff))
- end
- % ******************************************************************************* %
- % **************************** BFD method ****************************** %
- if chosen==0 | chosen==2, % best fit d method
- wbfd=w; rbfd=r+0.01; % initialize for best fit method
- for i=1:n,
- idx1=find([ud(i)*ones(1,n) <= rbfd]);
- [tmp,idx2]=min(rbfd(idx1)-ud(i));
- wbfd(i,idx1(idx2))=1; rbfd(idx1(idx2))=tmp; % update the state
- end
- wbfd=wbfd(irec,:);
- disp('% *******************************');
- disp('the assignment are:')
- disp(wbfd)
- N_feature = size(wbfd,2);
- 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'];
- % Q = [input_temp(:,1:4)]
- % C = wff(:,1:4);
- % C = logical(C);
- % Q(C)
- %show_out = input_temp(C)
- for k=1:N_feature,
- fprintf('Data in Bin %d', k);
- B = wbfd(:,k);
- %A = input_input';
- A = input_temp(:,k);
- B = logical(B);
- output_ans = A(B)
- % output_ans(k) = A(B);
- end
- disp('Best fit decreasing (BFD) method: ')
- occpbfd=u*wbfd;
- nbinbfd=sum([occpbfd > 0]);
- disp(['Total # of bins used = ' int2str(nbinbfd)]);
- disp('the occupancy of each bin after packing are:');
- disp(occpbfd(1:nbinbfd))
- end %
- % ******************************************************************************* %
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement