我正在使用Pyomo对MIP进行建模,我想用gurobi进行灵敏度分析,以获得仍然确保最佳组合解决方案的系数范围(决策变量是二元的)。
据我所知,必须修复所有决策变量的值,并重新求解模型,求解器给出系数gm
的范围。
我尝试了不同的编码(case1,case2)
这是我模型的一些部分:
from __future__ import division
from pyomo.environ import *
from pyomo.opt import SolverFactory
#from gurobipy import *
model = AbstractModel ()
model.f = Param(within=PositiveIntegers)
model.F = RangeSet(1, model.f)
model.v = Param(within=PositiveIntegers)
model.V = RangeSet(1, model.v)
model.m = Param(within=PositiveIntegers)
model.M = RangeSet(1, model.m)
...
model.yw = Var(model.M, within=Binary)
model.ys = Var(model.index_fvm, within=Binary)
model.bs = Param(model.index_fvm, within=Binary, default=0)
def Obj_rule(model):
expr = 0.0
for (f,v,m) in model.index_fvm:
expr += model.g[m] * model.ys[f,v,m]
for m in model.M:
expr += model.g[m] * model.yw[m]
return expr
model.OBJ = Objective(rule=Obj_rule, sense=maximize)
...
def C10_constraint_rule (model, m):
expr= 0.0
for (f,v) in model.index_fv:
expr += model.ys[f,v,m]
expr += model.yw[m]
return expr <= 1
model.C10Constraint = Constraint(model.M, rule=C10_constraint_rule)
...
# Pyomo doc 5.1.1. P82-83
# case 1
opt = SolverFactory("gurobi")
instance = model.create_instance()
results = opt.solve(instance)
instance.display()
instance.load(results)
if instance.ys[f,v,m] == 0:
instance.ys[f,v,m].setlb(1)
instance.ys[f,v,m].setub(1)
else:
instance.ys[f,v,m].setlb(0)
instance.ys[f,v,m].setub(0)
instance.preprocess()
results = opt.solve(instance)
# case 2
#http://www.gurobi.com/documentation/7.0/examples.pdf
if len(sys.argv) < 2:
print('Usage: sensitivity.py filename')
quit()
# Read and solve model
model = read(sys.argv[1])
if model.IsMIP == 0:
print('Model is not a MIP')
exit(0)
model.optimize()
if model.status != GRB.Status.OPTIMAL:
print('Optimization ended with status %d' % model.status)
exit(0)
# Store the optimal solution
origObjVal = model.ObjVal
for v in model.getVars():
v._origX = v.X
# Disable solver output for subsequent solves
model.Params.outputFlag = 0
# Iterate through unfixed, binary variables in model
for v in model.getVars():
if (v.LB == 0 and v.UB == 1 \
and (v.VType == GRB.BINARY or v.VType == GRB.INTEGER)):
# Set variable to 1-X, where X is its value in optimal solution
if v._origX < 0.5:
v.LB = v.Start = 1
else:
v.UB = v.Start = 0
# Update MIP start for the other variables
for vv in model.getVars():
if not vv.sameAs(v):
vv.Start = vv._origX
# Solve for new value and capture sensitivity information
model.optimize()
if model.status == GRB.Status.OPTIMAL:
print('Objective sensitivity for variable %s is %g' % \
(v.VarName, model.ObjVal - origObjVal))
else:
print('Objective sensitivity for variable %s is infinite' % \
v.VarName)
# Restore the original variable bounds
v.LB = 0
v.UB = 1
我在案例1中收到此错误:
ERROR: Unexpected exception while loading model:
Error evaluating Param value (f):
The Param value is undefined and no default value is specified
我在案例2中收到此错误:
ERROR: Unexpected exception while loading model:
__init__() takes exactly 3 positional arguments (2 given)
gm.dat看起来像这样:
param g:=
1 7
7 6
54 7
52 7
53 1
44 7
45 7
56 7
46 7
48 7
70 7
71 7
72 7
73 7
100 7
112 4
;
有没有人知道继续解决这个问题并且知道如何进行敏感度分析?
提前谢谢。
拉拉