Search code examples
pyomo

Access Duals for Variable Bounds in Pyomo


Is it possible to access dual information related to the variable bounds in Pyomo? For constraints, you can declare a Suffix, but is there an equivalent for the variable bounds?


Solution

  • You can declare a suffix named rc (reduced cost) to obtain this from the following interfaces:

    • Gurobi: LP, MPS, Python
    • Cplex: LP, MPS, Python
    • Glpk: LP, MPS

    Xpress might also be on that list, but I have no way of verifying that.

    The Gurobi and Cplex solvers for AMPL do not return this information as suffixes (and I don't know why), so you can not get these through the NL-file interface to these solvers in Pyomo.

    Also, for Ipopt you can get this by declaring suffixes named ipopt_zL_out and ipopt_zU_out for the duals of the lower bounds and upper bounds, respectively. See this example for a better explanation.

    The above list is only what I am aware of. There are likely other AMPL solvers that provide this information through suffixes, so you would be able to access that solution information through Pyomo's NL-file interface as long is you know the name of the suffix.

    Update: Here is an example with gurobi:

    import pyomo.environ as aml
    
    model = aml.ConcreteModel()
    model.x = aml.Var(bounds=(0,1))
    model.o = aml.Objective(expr=model.x)
    model.c = aml.Constraint(expr=model.x >= -1)
    
    model.rc = aml.Suffix(direction=aml.Suffix.IMPORT)
    
    gurobi = aml.SolverFactory("gurobi",
                               solver_io="lp")
    results = gurobi.solve(model)
    assert str(results.solver.termination_condition) == "optimal"
    
    print(model.rc[model.x])
    

    As I explained above, you can set solver_io in this example to "lp", "mps", or "python" with Gurobi.