0
线性规划的影子价格(shadow price)|对偶价格(dual price) 与 gurobi 实现
source link: https://blog.csdn.net/oqqYuan1234567890/article/details/121066537
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
在列生成的问题中,大量用到 dual price, 有一些文章会翻译为影子价格,有一些则是对偶价格,叫知乎有一个写得比较好的文章
什么是影子价格?—— 线性规划的对偶解,及拉格朗日乘数
模型为:
下面是 gurobi 的实现, gurobi 像现在可以直接 pip 安装,有一个非商业的 licensepip install gurobipy
from gurobipy import *
# Create a new model
m = Model("test1")
# Create variables
x1 = m.addVar(name="x1")
x2 = m.addVar(name="x2")
# Set objective
m.setObjective(50 * x1 + 100*x2, GRB.MAXIMIZE)
m.addConstr(x1 + x2 <= 300, "c0")
m.addConstr(2 * x1 + x2 <= 400, "c1")
m.addConstr(x2 <= 250, "c2")
m.optimize()
dualArray = []
c = m.getConstrs()
for v in m.getVars():
print('%s = %g' % (v.varName, v.x))
for i in range(m.getAttr(GRB.Attr.NumConstrs)):
dualArray.append(c[i].getAttr(GRB.Attr.Pi))
print('Obj: %g' % m.objVal)
print('dual price:', dualArray)
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK