PersistentSolver

(class from pyomo.solvers.plugins.solvers.persistent_solver)

class pyomo.solvers.plugins.solvers.persistent_solver.PersistentSolver(**kwds)[source]

Bases: DirectOrPersistentSolver

A base class for persistent solvers. Direct solver interfaces do not use any file io. Rather, they interface directly with the python bindings for the specific solver. Persistent solver interfaces are similar except that they “remember” their model. Thus, persistent solver interfaces allow incremental changes to the solver model (e.g., the gurobi python model or the cplex python model). Note that users are responsible for notifying the persistent solver interfaces when changes are made to the corresponding pyomo model.

Keyword Arguments:
  • type (str) – String indicating the class type of the solver instance.

  • name (str) – String representing either the class type of the solver instance or an assigned name.

  • doc (str) – Documentation for the solver

  • options (dict) – Dictionary of solver options

__init__(**kwds)

Constructor

Methods

__init__(**kwds)

Constructor

add_block(block)

Add a single Pyomo Block to the solver's model.

add_column(model, var, obj_coef, ...)

Add a column to the solver's and Pyomo model

add_constraint(con)

Add a single constraint to the solver's model.

add_sos_constraint(con)

Add a single SOS constraint to the solver's model (if supported).

add_var(var)

Add a single variable to the solver's model.

available([exception_flag])

True if the solver is available.

config_block([init])

default_variable_value()

has_capability(cap)

Returns a boolean value representing whether a solver supports a specific feature.

has_instance()

True if set_instance has been called and this solver interface has a pyomo model and a solver model.

license_is_valid()

True if the solver is present and has a valid license (if applicable)

load_vars([vars_to_load])

Load the values from the solver's variables into the corresponding pyomo variables.

problem_format()

Returns the current problem format.

remove_block(block)

Remove a single block from the solver's model.

remove_constraint(con)

Remove a single constraint from the solver's model.

remove_sos_constraint(con)

Remove a single SOS constraint from the solver's model.

remove_var(var)

Remove a single variable from the solver's model.

reset()

Reset the state of the solver

results_format()

Returns the current results format.

set_callback(name[, callback_fn])

Set the callback function for a named callback.

set_instance(model, **kwds)

This method is used to translate the Pyomo model provided to an instance of the solver's Python model.

set_objective(obj)

Set the solver's objective.

set_options(istr)

set_problem_format(format)

Set the current problem format (if it's valid) and update the results format to something valid for this problem format.

set_results_format(format)

Set the current results format (if it's valid for the current problem format).

solve(*args, **kwds)

Solve the model.

update_var(var)

Update a variable in the solver's model.

version()

Returns a 4-tuple describing the solver executable version.

warm_start_capable()

True is the solver can accept a warm-start solution

Attributes

keepfiles

log_file

soln_file

suffixes

symbolic_solver_labels

tee

warm_start_file_name

warm_start_solve

results

A results object return from the solve method.

Member Documentation

add_block(block)[source]

Add a single Pyomo Block to the solver’s model.

This will keep any existing model components intact.

Parameters:

block (Block (scalar Block or single BlockData))

add_column(model, var, obj_coef, constraints, coefficients)[source]

Add a column to the solver’s and Pyomo model

This will add the Pyomo variable var to the solver’s model, and put the coefficients on the associated constraints in the solver model. If the obj_coef is not zero, it will add obj_coef*var to the objective of both the Pyomo and solver’s model.

Parameters:
  • model (pyomo ConcreteModel to which the column will be added)

  • var (Var (scalar Var or single VarData))

  • obj_coef (float, pyo.Param)

  • constraints (list of scalar Constraints of single ConstraintDatas)

  • coefficients (list of the coefficient to put on var in the associated constraint)

add_constraint(con)[source]

Add a single constraint to the solver’s model.

This will keep any existing model components intact.

Parameters:

con (Constraint (scalar Constraint or single ConstraintData))

add_sos_constraint(con)[source]

Add a single SOS constraint to the solver’s model (if supported).

This will keep any existing model components intact.

Parameters:

con (SOSConstraint)

add_var(var)[source]

Add a single variable to the solver’s model.

This will keep any existing model components intact.

Parameters:

var (Var)

available(exception_flag=True)

True if the solver is available.

has_capability(cap)

Returns a boolean value representing whether a solver supports a specific feature. Defaults to ‘False’ if the solver is unaware of an option. Expects a string.

Example: # prints True if solver supports sos1 constraints, and False otherwise print(solver.has_capability(‘sos1’)

# prints True is solver supports ‘feature’, and False otherwise print(solver.has_capability(‘feature’)

Parameters:

cap (str) – The feature

Returns:

val – Whether or not the solver has the specified capability.

Return type:

bool

has_instance()[source]

True if set_instance has been called and this solver interface has a pyomo model and a solver model.

Returns:

tmp

Return type:

bool

license_is_valid()

True if the solver is present and has a valid license (if applicable)

load_vars(vars_to_load=None)

Load the values from the solver’s variables into the corresponding pyomo variables.

Parameters:

vars_to_load (list of Var)

problem_format()

Returns the current problem format.

remove_block(block)[source]

Remove a single block from the solver’s model.

This will keep any other model components intact.

WARNING: Users must call remove_block BEFORE modifying the block.

Parameters:

block (Block (scalar Block or a single BlockData))

remove_constraint(con)[source]

Remove a single constraint from the solver’s model.

This will keep any other model components intact.

Parameters:

con (Constraint (scalar Constraint or single ConstraintData))

remove_sos_constraint(con)[source]

Remove a single SOS constraint from the solver’s model.

This will keep any other model components intact.

Parameters:

con (SOSConstraint)

remove_var(var)[source]

Remove a single variable from the solver’s model.

This will keep any other model components intact.

Parameters:

var (Var (scalar Var or single VarData))

reset()

Reset the state of the solver

results_format()

Returns the current results format.

set_callback(name, callback_fn=None)

Set the callback function for a named callback.

A call-back function has the form:

def fn(solver, model):

pass

where ‘solver’ is the native solver interface object and ‘model’ is a Pyomo model instance object.

set_instance(model, **kwds)[source]

This method is used to translate the Pyomo model provided to an instance of the solver’s Python model. This discards any existing model and starts from scratch.

Parameters:

model (ConcreteModel) – The pyomo model to be used with the solver.

Keyword Arguments:
  • symbolic_solver_labels (bool) – If True, the solver’s components (e.g., variables, constraints) will be given names that correspond to the Pyomo component names.

  • skip_trivial_constraints (bool) – If True, then any constraints with a constant body will not be added to the solver model. Be careful with this. If a trivial constraint is skipped then that constraint cannot be removed from a persistent solver (an error will be raised if a user tries to remove a non-existent constraint).

  • output_fixed_variable_bounds (bool) – If False then an error will be raised if a fixed variable is used in one of the solver constraints. This is useful for catching bugs. Ordinarily a fixed variable should appear as a constant value in the solver constraints. If True, then the error will not be raised.

set_objective(obj)[source]

Set the solver’s objective. Note that, at least for now, any existing objective will be discarded. Other than that, any existing model components will remain intact.

Parameters:

obj (Objective)

set_problem_format(format)

Set the current problem format (if it’s valid) and update the results format to something valid for this problem format.

set_results_format(format)

Set the current results format (if it’s valid for the current problem format).

solve(*args, **kwds)[source]

Solve the model.

Keyword Arguments:
  • suffixes (list of str) – The strings should represent suffixes support by the solver. Examples include ‘dual’, ‘slack’, and ‘rc’.

  • options (dict) – Dictionary of solver options. See the solver documentation for possible solver options.

  • warmstart (bool) – If True, the solver will be warmstarted.

  • keepfiles (bool) – If True, the solver log file will be saved.

  • logfile (str) – Name to use for the solver log file.

  • load_solutions (bool) – If True and a solution exists, the solution will be loaded into the Pyomo model.

  • report_timing (bool) – If True, then timing information will be printed.

  • tee (bool) – If True, then the solver log will be printed.

update_var(var)[source]

Update a variable in the solver’s model. This will update bounds, fix/unfix the variable as needed, and update the variable type.

Parameters:

var (Var)

version()

Returns a 4-tuple describing the solver executable version.

warm_start_capable()

True is the solver can accept a warm-start solution

results

A results object return from the solve method.