How to iterate through all combinations within a FOR loop?
Show older comments
Hi,
I want to set up a loop where items in one array is subtracted from from elements in another array while minimising the leftovers.
A simplified example
Six rolls of three different lengths (10, 12, 15) are in one array roll_list. The first row is the lengths and the second row is the corresponding quantities.
Ten different parts of lengths (7, 6, 6, 5, 4, 7, 5, 4, 6, 5) need to be made from the six available rolls.
The parts need to be produced in the order as shown in the array part_list, but the rolls may be used in any order.
How can I set up a loop to iterate all the possible combination and find out the least amount of waste? I have managed to do one possible iteration sequential to the roll_list array, but I am struggling to figure out how to attempt all possible combinations.
Appreciate any suggestions.
----EDIT ----
- One part cannot be made using more than one roll. So Part A cannot include material from Roll X and Y.
- Once a roll enters production, it needs to be used straight away. It cannot be removed from production and brought back to be reused.
roll_list = [10 12 15; 3 1 2];
roll_tot=sum(roll_list(1,:).*roll_list(2,:));
part_list = [7 6 6 5 4 7 5 4 6 5];
parts_total_length = sum(part_list);
b=1;
c=1;
waste=0;
for a = 1:length(part_list)
if roll_list(1,b) >= part_list(a) && roll_list(2,c) >= 1
roll_list(1,b) = roll_list(1,b) - part_list(a);
else
waste = waste + roll_list(1,b);
roll_list(2,c) = roll_list(2,c)-1;
roll_list(1,:) = [10 12 15];
if roll_list(2,c) == 0
b=b+1;
c=c+1;
end
roll_list(1,b) = roll_list(1,b) - part_list(a);
end
end
16 Comments
Abhi Ramesh
on 5 Jan 2022
Matt J
on 5 Jan 2022
The parts need to be produced in the order as shown in the array part_list
I don't see how the order matters. The solution is a 10x6 table listing how much of each roll is allocated to each part correct? For any solution, you should be able to reorder the rows of the table any way you wish.
Catalytic
on 5 Jan 2022
How can I set up a loop to iterate all the possible combination and find out the least amount of waste?
The amount of waste will always be the same. It has to be
waste = roll_tot - sum(part_list)
Abhi Ramesh
on 5 Jan 2022
Edited: Abhi Ramesh
on 5 Jan 2022
But what about @Catalytic's question? Why would the waste ever vary? Is the remainder of a previously used roll unusable?
Abhi Ramesh
on 5 Jan 2022
Abhi Ramesh
on 5 Jan 2022
Stephen23
on 6 Jan 2022
"... but the system can be improved by optimising it and I am not sure how best to do it."
Abhi Ramesh
on 6 Jan 2022
Edited: Abhi Ramesh
on 6 Jan 2022
Correct. Once the roll is removed from production and another roll is introduced, we cannot go back to the old roll.
That's not what I asked, though. Suppose one part is made with 2.5 rolls, in other words one of the 3 rolls used to make the part is only partially expended. When you move onto the next part, can you continue to use what is left over from the partially-expended roll?
Abhi Ramesh
on 6 Jan 2022
Edited: Abhi Ramesh
on 6 Jan 2022
So each part is to be made from exactly one roll? If so how can the 6 rolls in your example ever produce more than a total of 6 parts?
I suggest you demonstrate one possible solution to your 10 part example, not necessarily the optimal solution with the least waste.
Abhi Ramesh
on 6 Jan 2022
Edited: Abhi Ramesh
on 6 Jan 2022
Matt J
on 6 Jan 2022
The actual problem involves tens of roll lengths, hundreds of quantities, and thousands of parts.
I don't think you're going to be able to handle thousands of parts. The number of combinations grows exponentially with the number of parts, something like,
N=size(roll_list,2);
M=numel(part_list);
numberofCombinations=N.^M
So, if M is in the 1000s, I don't think there's any hope. As a compromise, you can decompose the parts list into sub-lists and optimize over each one sequentially.
Abhi Ramesh
on 6 Jan 2022
Edited: Abhi Ramesh
on 7 Jan 2022
Accepted Answer
More Answers (0)
Categories
Find more on Entering Commands in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
