How to modify this code for true result?

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 件のコメント

IBM watson
IBM watson 2018 年 10 月 22 日
I will try to make it clear. And can you please add some explanations to your last code like Rik.
Rik
Rik 2018 年 10 月 22 日
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
IBM watson 2018 年 10 月 22 日
編集済み: IBM watson 2018 年 10 月 22 日
I did some modifications on it. Now it is better and even works for first 3 elements of the vector. Then i get an out of bound error. If i add another element to boxes vector no error shows up but it doesnt assign the last element. Except this it seems working.
Code:
myset=[1 2 3 4 2 5]; % 5 is added
capacity=6;
passedall=[];
passed=[];
n=5;
q=0
while q < 5
q=q+1
passedall = [passedall passed]
myset= setdiff(myset,passedall)
n=n-size(passedall,2);
passed=[];
capacity=6;
for b= 1:n
if (myset(b) <= capacity);
passed(b) = myset(b)
capacity= capacity - myset(b)
end
end
end
Results:
q = 0
q = 1
passedall = [](0x0)
myset =
1 2 3 4 5
passed = 1
capacity = 5
passed =
1 2
capacity = 3
passed =
1 2 3
capacity = 0
q = 2
passedall =
1 2 3
myset =
4 5
passed = 4
capacity = 2
q = 3
passedall =
1 2 3 4
myset = 5
q = 4
passedall =
1 2 3 4
myset = 5
q = 5
passedall =
1 2 3 4
myset = 5
But myset should be [ ]
Rik
Rik 2018 年 10 月 23 日
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
IBM watson 2018 年 10 月 23 日
  • I cant use your data structure because;
  • The way your algorithm thinks is different from the original idea. I tried but couldnt modify your code for my solution.
  • I should have done with 1st room before going 2nd one. But in your algorithm is not like that.
Rik
Rik 2018 年 10 月 30 日
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
IBM watson 2018 年 10 月 31 日
I did solve it. Thanks for your help.

サインインしてコメントする。

 採用された回答

Rik
Rik 2018 年 10 月 21 日
編集済み: Rik 2018 年 10 月 21 日

1 投票

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 件のコメント

IBM watson
IBM watson 2018 年 10 月 21 日
編集済み: IBM watson 2018 年 10 月 21 日
But i need to make this algorithm work. It doesnt matter if i get the optimal or not because i will use a heuristic method to find the optimal. Many thanks to you for your answer though.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

質問済み:

2018 年 10 月 21 日

コメント済み:

2018 年 10 月 31 日

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by