全文翻译自Matlab官方教程:Mixed-Integer Linear Programming Basics: Problem-Based,侵删。
这个例子展示了如何解决一个混合整数线性问题。虽然不复杂,但示例显示了使用基于问题的方法来构造问题的典型步骤。有关显示此示例的视频,请参见 Mixed-Integer Linear Programming Basics: Sovler-Based。
你想把不同化学成分的钢混合起来,得到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。
有四种钢锭可供购买,每种钢锭只有一个。钢锭需要整个整个地购买。
要解决这个问题,首先要确定控制变量。取变量 i n g o t ( 1 ) = 1 ingot(1)=1 ingot(1)=1表示购买锭1, i n g o t ( 1 ) = 0 ingot(1)=0 ingot(1)=0表示不购买锭1。类似地,变量 i n g o t ( 2 ) ingot(2) ingot(2)到 i n g o t ( 4 ) ingot(4) ingot(4)是指示您是否购买锭2到锭4的二进制变量。
变量 a l l o y s ( 1 ) alloys(1) alloys(1)到合金 a l l o y s ( 3 ) alloys(3) alloys(3)是以吨为单位购买的合金1、2和3的数量。 s c r a p scrap scrap是您购买的废钢数量。
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;
调用求解器:
[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吨废钢。