How to modify this code for true result?

조회 수: 10 (최근 30일)
IBM watson
IBM watson 2018년 10월 21일
댓글: IBM watson 2018년 10월 31일

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
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일
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개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by