Search code examples
pyomo

Set initial primal and dual values for variables pyomo


I wan't to set initial primal and dual values in a program's variables. Is there a specific way to do this. I can see there is a initialize option in the Var object but i'm not sure how to use it in this manner


Solution

  • If you want to set the value of a variable when you declare it, you can use the initialize keyword. E.g.,

    model.x = Var(initialize=1.0)
    

    Alternatively, you can set the .value attribute on a variable anytime before the solve. If you are starting with an AbstractModel be sure to only do this on the instance that gets returned by the create_instance method. Here is an example using a ConcreteModel:

    model = ConcreteModel()
    model.x = Var()
    model.X = Var([1,2,3])
    
    model.x.value = 5.0
    model.X[1].value = 1.0
    

    The NL file interface will always include the current value of all model variables in the solver input file. For other interfaces (e.g., the LP file interface), adding the keyword warmstart=True to the solve method will create a warmstart file that includes values of any binary or integer variables for a MIP warmstart.

    To set a dual solution, you must declare a Suffix on your model with the name dual. Note that the only interface that currently supports exporting suffix information is the NL file interface (solvers that work with AMPL). However, most interfaces support importing suffix information from the solver (dual especially). Setting the dual value of a particular constraint might look like:

    model = ConcreteModel()
    model.dual = Suffix(direction=Suffix.IMPORT_EXPORT)
    model.c = Constraint(...)
    model.dual[model.c] = 1.0
    

    More information about the Suffix component can be found in the online documentation for Pyomo.