从pyomo中具有SOS2约束的优化问题中获取双变量值

时间:2019-10-06 16:52:08

标签: pyomo gurobi

我正在使用pyomo和gurobi解决具有SOS2约束的优化问题。

但是,当gurobi面临SOS2约束时,它将问题视为MIP,并且不返回任何对偶。这是MWE:

import pyomo.environ as pyo

breakpoints = {
    1: [0, 5, 10],
    2: [0, 5, 10],
}

c_values = {
    1: [0, 10, 5],
    2: [1, 10, 4],
}

model = pyo.ConcreteModel()
model.x = pyo.Var([1, 2], domain=pyo.NonNegativeReals, bounds=(0, 10))
model.cost = pyo.Var([1, 2])
model.pcws_cost = pyo.Piecewise([1, 2], model.cost, model.x,
                                pw_pts=breakpoints,
                                f_rule=c_values,
                                pw_constr_type='LB')

model.obj = pyo.Objective(expr=model.cost[1] + model.cost[2])
model.constraint = pyo.Constraint(expr=model.x[1] + model.x[2] >= 12)

model.dual = pyo.Suffix(direction=pyo.Suffix.IMPORT)

instance = model
opt = pyo.SolverFactory('gurobi')
results = opt.solve(instance, tee=True)

# should display all duals, but it will raise a KeyError instead
print("Duals")
for c in instance.component_objects(pyo.Constraint, active=True):
    print("   Constraint", c)
    for index in c:
        print("      ", index, instance.dual[c[index]])

但是,gurobipy enables the user to fix the integer variables in a model和我能够通过对pyomo的代码进行一些可疑的修改来使用此功能,效果很好,现在我确实具有固定的SOS2约束的双重变量值:

Duals
   Constraint constraint
       None 2.0
   Constraint pcws_cost[1].SOS2_constraint
       1 -2.0
       2 1.0
       3 0.0
   Constraint pcws_cost[2].SOS2_constraint
       1 1.2
       2 1.0
       3 16.0

pyomo是否提供一种正确的方法来“修复” SOS2约束或模型中的所有整数变量?

0 个答案:

没有答案