How to modify this code for true result?
Ältere Kommentare anzeigen
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 Kommentare
IBM watson
am 22 Okt. 2018
Rik
am 22 Okt. 2018
You are not keeping track of the boxes in your code. Thinking about how you should store your data is half the work. You should be able to adapt my answer to your algorithm. I don't have a lot of time now to fix it for you, so give it a try and post the result here if you don't manage to get it to work.
IBM watson
am 22 Okt. 2018
Bearbeitet: IBM watson
am 22 Okt. 2018
Rik
am 23 Okt. 2018
Even with your comments I still don't understand what your code is doing. It is of course your own choice to use whatever code you want, but why do you insist on not using my data structure? You only have to replace everything within the for-loop to implement you own algorithm.
IBM watson
am 23 Okt. 2018
Rik
am 30 Okt. 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
IBM watson
am 31 Okt. 2018
Akzeptierte Antwort
Weitere Antworten (0)
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!