I will try to make it clear. And can you please add some explanations to your last code like Rik.
How to modify this code for true result?
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
IBM watson
el 21 de Oct. de 2018
Comentada: IBM watson
el 31 de Oct. de 2018
The objective is to place some boxes into rooms. Each box has its own volume (given in the 'boxes' matrix) and rooms capacities are 6. We should use min rooms. And the algorithm goes like this:

All rooms are identical. Not the boxes.
1)Place boxes(1) to room1. 2)Calculate capacity for room1 (capacity = capacity - boxes(1)) 3)Try to place boxes(2) to room1 and calcualte capacity 4)Do it for all boxes before opening room2. 5)After trying for all boxes, now move on to room2. 6)...
Here is my code:
myset=[1 2 3 4 2];
capacity=6;
passedall=[];
passed=[];
n=5;
q=0
while q < 5 %make sure all boxes will be assigned.
q=q+1
passedall = [passedall passed] %this is for storing all assigned boxes
myset= setdiff(myset,passedall) %for not assign same boxes repeatedly
n=n-size(passedall,2); %reducing n with boxes vector size for avoiding out of bounds errors.
passed=[]; %for every new room start with blank passed vector.
capacity=6; %for every room refresh the capacity.
for b= 1:n %try all boxes for one room. if (myset(b) <= capacity); %given in the problem definition.
passed(b) = myset(b) %store assigned boxes for a room. Then we will store all in the allpassed vector.
capacity= capacity - myset(b) %given in the problem definition. end
end
end
%For the objective function. It should store 'passed' vectors for every loop
%as an element of a cluster. The number of elements of this cluster will be
%the obj function. The less is the better.I know the problem could have been solved with different algorithms. But the main purpose is fixing this code.
(explanations added)
7 comentarios
Rik
el 30 de Oct. de 2018
If you haven't solved this yet, try to fill in the code below:
boxes=[1 2 3 4 5 6];
capacity=6;
container=cell(numel(boxes),1);%will be trimmed
%put your algorithm implementation here
space_left=capacity-cellfun(@sum,container);
container(space_left==capacity)=[];%remove unused
Respuesta aceptada
Rik
el 21 de Oct. de 2018
Editada: Rik
el 21 de Oct. de 2018
Your algorithm might not always result in an optimal packing. You should start with the biggest box and go down in volume. That way you can fill the small left-over gaps with the smaller boxes, instead of packing all small boxes into the first and then having all other containers having gaps.
container1: 1 2 3
container2: 4
container3: 5
container4: 6
or
container1: 6
container2: 5 1
container3: 4 2
container4: 3
Even if you need the same number of containers for the second strategy, there are no gaps except for the last one.
boxes=[1 2 3 4 5 6];
capacity=6;
container=cell(numel(boxes),1);%will be trimmed
space_left=capacity-cellfun(@sum,container);
boxes_temp=sort(boxes,'descend');
for n=1:numel(boxes)
%select biggest box and remove from list
current_vol=boxes_temp(1);boxes_temp(1)=[];
%put in the box with the least space left
fits_here=find(space_left>=current_vol);
[~,least_room_left_position]=min(space_left(fits_here));
ind=fits_here(least_room_left_position);
container{ind}=[container{ind} current_vol];
space_left=capacity-cellfun(@sum,container);
end
container(space_left==capacity)=[];%remove unused
1 comentario
Más respuestas (0)
Ver también
Categorías
Más información sobre Loops and Conditional Statements en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!