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.
Sign in to answer this question.