MATLAB Answers

How to modify this code for true result?

1 view (last 30 days)
IBM watson
IBM watson on 21 Oct 2018
Commented: IBM watson on 31 Oct 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.

Here is my code:

    myset=[1 2 3 4 2];
  while q < 5   %make sure all boxes will be assigned.
      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.
  %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)


Show 4 older comments
IBM watson
IBM watson on 23 Oct 2018
  • 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 on 30 Oct 2018
If you haven't solved this yet, try to fill in the code below:
boxes=[1 2 3 4 5 6];
container=cell(numel(boxes),1);%will be trimmed
%put your algorithm implementation here
container(space_left==capacity)=[];%remove unused
IBM watson
IBM watson on 31 Oct 2018
I did solve it. Thanks for your help.

Sign in to comment.

Accepted Answer

Rik on 21 Oct 2018
Edited: Rik on 21 Oct 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
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];
container=cell(numel(boxes),1);%will be trimmed
for n=1:numel(boxes)
%select biggest box and remove from list
%put in the box with the least space left
container{ind}=[container{ind} current_vol];
container(space_left==capacity)=[];%remove unused

  1 Comment

IBM watson
IBM watson on 21 Oct 2018
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.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by