Mixed-Integer Linear Programming Basics: Problem-Based
全文翻譯自Matlab官方教程:Mixed-Integer Linear Programming Basics: Problem-Based
這個例子展示瞭如何解決一個混合整數線性問題。雖然不復雜,但示例顯示了使用基於問題的方法來構造問題的典型步驟。有關顯示此示例的視頻,請參見 Mixed-Integer Linear Programming Basics: Sovler-Based。
Problem Description
你想把不同化學成分的鋼混合起來,得到25噸具有特定化學成分的鋼。結果應含有5%的碳和5%的鉬(按重量計),即25噸*5%=1.25噸碳和1.25噸鉬。其目的是儘量降低鋼的混合成本。
這個問題摘自Carl Henrik Westerberg,Bengt Bjorklund和Eskil Hultman,“瑞典鋼鐵廠混合整數規劃的應用”,接口1977年2月第7卷,第2期,第39-43頁,摘要見https://doi.org/10.1287/inte.7.2.39。
有四塊鋼錠可供購買,每個鋼錠只有一個可用。
可購買三級合金鋼和一級廢鋼,合金鋼和廢鋼可以少量購買。
Formulate Problem
要解決這個問題,首先要確定控制變量。取變量表示購買錠1,表示不購買錠1。類似地,變量到是指示您是否購買錠2到錠4的二進制變量。
變量到合金是以噸爲單位購買的合金1、2和3的數量。是您購買的廢鋼數量。
steelprob = optimproblem;
ingots = optimvar('ingots',4,'Type','integer','LowerBound',0,'UpperBound',1);
alloys = optimvar('alloys',3,'LowerBound',0);
scrap = optimvar('scrap','LowerBound',0);
創建cost的函數。
weightIngots = [5,3,4,6];
costIngots = weightIngots.*[350,330,310,280];
costAlloys = [500,450,400];
costScrap = 100;
cost = costIngots*ingots + costAlloys*alloys + costScrap*scrap;
把cost作爲目標函數包含在問題中。
steelprob.Objective = cost;
這個問題有三個等式約束。第一個限制是總重量是25噸,計算鋼的重量:
totalWeight = weightIngots*ingots + sum(alloys) + scrap;
第二個限制是碳的重量是25噸的5%,即1.25噸。計算鋼中碳的重量:
carbonIngots = [5,4,5,3]/100;
carbonAlloys = [8,7,6]/100;
carbonScrap = 3/100;
totalCarbon = (weightIngots.*carbonIngots)*ingots + carbonAlloys*alloys + carbonScrap*scrap;
第三個限制是鉬的重量是1.25噸。計算鋼中鉬的重量:
molybIngots = [3,3,4,4]/100;
molybAlloys = [6,7,8]/100;
molybScrap = 9/100;
totalMolyb = (weightIngots.*molybIngots)*ingots + molybAlloys*alloys + molybScrap*scrap;
在問題中包含約束:
steelprob.Constraints.conswt = totalWeight == 25;
steelprob.Constraints.conscarb = totalCarbon == 1.25;
steelprob.Constraints.consmolyb = totalMolyb == 1.25;
Solve Problem
調用求解器:
[sol,fval] = solve(steelprob);
輸出:
Solving problem using intlinprog.
LP: Optimal objective value is 8125.600000.
Cut Generation: Applied 3 mir cuts.
Lower bound is 8495.000000.
Relative gap is 0.00%.
Optimal solution found.
Intlinprog stopped at the root node because the objective value is within a gap
tolerance of the optimal value, options.AbsoluteGapTolerance = 0 (the default
value). The intcon variables are integer within tolerance,
options.IntegerTolerance = 1e-05 (the default value).
查看結果:
sol.ingots
ans = 4×1
1.0000
1.0000
0
1.0000
sol.alloys
ans = 3×1
7.2500
0
0.2500
sol.scrap
sol.fval
ans = 3.5000
fval = 8.4950e+03
最佳購買價格爲8495美元。購買1號、2號和4號鋼錠,但不要購買3號鋼錠,購買7.25噸合金1、0.25噸合金3和3.5噸廢鋼。