Issue with combining two loops in a Cutting Stock Problem with Multiple Stock (Two)

7 views (last 30 days)
I'm trying to run a cutting stock problem (based on Matlab refrence code) but with two stock lenghts instead one. I'm having multiple issues in combining the loops of both stock 1 and stock 2 to reach an overal optimum cuts. I need to run each of the loops ones (i.e. to include the two stock lengths once in a loop). This is what I have right now:
% Cutting Stock Problem: Solver-Based
railLength1 = 80;
railLength2 = 40;
% lengthlist = [24.04; 29.91; 36.59; 38.28;44.61];
% quantity = [64; 38; 61; 54; 42];
% nLengths = length(lengthlist);
lengthlist = [24.04;29.91;36.59;38.28];
quantity = [64; 38; 61; 54];
nLengths = length(lengthlist);
nLengths = length(lengthlist);
patterns1 = diag(floor(railLength1./lengthlist));
nPatterns1 = size(patterns1,2);
patterns2 = diag(floor(railLength2./lengthlist));
nPatterns2 = size(patterns2,2);
lb2 = zeros(nLengths,1);
A2_1 = lengthlist';
b2_1 = railLength1;
A2_2 = lengthlist';
b2_2 = railLength2;
lpopts = optimoptions('linprog','Display','off');
ipopts = optimoptions('intlinprog',lpopts);
% Initialize the variables for the loop.
reducedCost1 = -Inf;
reducedCostTolerance1 = -0.0001;
reducedCost2 = -Inf;
reducedCostTolerance2 = -0.0001;
exitflag = 1;
while reducedCost1 < reducedCostTolerance1 && exitflag > 0
lb1 = zeros(nPatterns1,1);
f1 = lb1 + 1;
A1 = -patterns1;
b1 = -quantity;
[values1,nrails1,exitflag,~,lambda1] = linprog(f1,A1,b1,[],[],lb1,[],lpopts);
if exitflag > 0
fprintf('Using %g rails_1\n',nrails1);
% Now generate a new pattern, if possible
f2_1 = -lambda1.ineqlin;
[values1,reducedCost1,pexitflag] = intlinprog(f2_1,1:nLengths,A2_1,b2_1,[],[],lb2,[],ipopts);
reducedCost1 = 1 + reducedCost1; % continue if this reducedCost is negative
newpattern = round(values1);
if pexitflag > 0 && reducedCost1 < reducedCostTolerance1
patterns1 = [patterns1 newpattern];
nPatterns1 = nPatterns1 + 1;
end
end
end
exitflag1 = 1;
while reducedCost2 < reducedCostTolerance2 && exitflag1 > 0
lb = zeros(nPatterns2,1);
f2 = lb + 1;
A2 = -patterns2;
b2 = -quantity;
[values2,nrails2,exitflag1,~,lambda2] = linprog(f2,A2,b2,[],[],lb,[],lpopts);
if exitflag1 > 0
fprintf('Using %g rail_2 \n',nrails2);
% Now generate a new pattern, if possible
f2_2 = -lambda2.ineqlin;
[values2,reducedCost2,pexitflag] = intlinprog(f2_2,1:nLengths,A2_2,b2_2,[],[],lb2,[],ipopts);
reducedCost2 = 1 + reducedCost2; % continue if this reducedCost is negative
newpattern = round(values2);
if pexitflag > 0 && reducedCost2 < reducedCostTolerance2
patterns2 = [patterns2 newpattern];
nPatterns2 = nPatterns2 + 1;
end
end
end
if exitflag <= 0
disp('Error in column generation phase')
else
[values1,railsUsed1,exitflag] = intlinprog(f1,1:length(lb1),A1,b1,[],[],lb1,[],[],ipopts);
[values2,railsUsed2,exitflag] = intlinprog(f2,1:length(lb),A2,b2,[],[],lb,[],[],ipopts);
if exitflag > 0
values1 = round(values1);
values2 = round(values2);
railsUsed1 = round(railsUsed1);
railsUsed2 = round(railsUsed2);
fprintf('Optimal solution For Stock 1 uses %g First Type rails\n', railsUsed1);
fprintf('Optimal solution For Stock 2 uses %g Second Type rails\n', railsUsed2);
totalwaste = sum((patterns1*values1 - quantity).*lengthlist); % waste due to overproduction
totalwaste1 = sum((patterns2*values2 - quantity).*lengthlist); % waste due to overproduction
for j = 1:size(values1)
if values1(j) > 0
fprintf('Cut from Stock 1 %g rails with pattern\n',values1(j));
for w = 1:size(patterns1,1)
if patterns1(w,j) > 0
fprintf(' %d cut(s) of length %d\n', patterns1(w,j),lengthlist(w));
end
end
wastej = railLength1 - dot(patterns1(:,j),lengthlist); % waste due to pattern inefficiency
totalwaste = totalwaste + wastej;
fprintf(' Waste of this pattern is %g\n', wastej);
end
end
for j = 1:size(values2)
if values2(j) > 0
fprintf('Cut from Stock 2 %g rails with pattern\n',values2(j));
for w = 1:size(patterns2,1)
if patterns2(w,j) > 0
fprintf(' %d cut(s) of length %d\n', patterns2(w,j),lengthlist(w));
end
end
wastej = railLength2 - dot(patterns2(:,j),lengthlist); % waste due to pattern inefficiency
totalwaste1 = totalwaste1 + wastej;
fprintf(' Waste of this pattern is %g\n', wastej);
end
end
fprintf('Total waste in this problem for stock1 is %g.\n',totalwaste);
fprintf('Total waste in this problem for stock2 is %g.\n',totalwaste1);
else
disp('Error in final optimization')
end
end

Accepted Answer

N/A
N/A on 25 Mar 2021
The solution for this can be found by correcclty defining the cutting stock problem constrainits uisng Matlab's optimization tool, as can be seen below:
const1 = dot(lengthlist,cuts) <= railLength1 ;
const2 = dot(lengthlist,cuts) <= railLength2 ;
subproblem.Constraints.const1 = const1;
subproblem.Constraints.const2 = const2;

More Answers (0)

Categories

Find more on Linear Programming and Mixed-Integer Linear Programming in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!