Common Model Interface

Generally speaking, the following APIs are common to all optimizers except for adding constraint because different optimizers may support different types of constraints.

Model

Get/set model attributes

model.set_model_attribute(attr, value)

set the value of a model attribute

Parameters:
model.get_model_attribute(attr)

get the value of a model attribute

Parameters:

attr (pyoptinterface.ModelAttribute) – the attribute to get

Returns:

the value of the attribute

Variable

Add a variable to the model

model.add_variable([lb=-inf, ub=+inf, domain=pyoptinterface.VariableDomain.Continuous, name=""])

add a variable to the model

Parameters:
  • lb (float) – the lower bound of the variable, optional, defaults to \(-\infty\)

  • ub (float) – the upper bound of the variable, optional, defaults to \(+\infty\)

  • domain (pyoptinterface.VariableDomain) – the domain of the variable, optional, defaults to continuous

  • name (str) – the name of the variable, optional

Returns:

the handle of the variable

Add multidimensional variables to the model as pyoptinterface.tupledict

model.add_variables(*coords[, lb=-inf, ub=+inf, domain=pyoptinterface.VariableDomain.Continuous, name=""])

add a multidimensional variable to the model

Parameters:
  • coords – the coordinates of the variable, can be a list of Iterables

  • lb (float) – the lower bound of the variable, optional, defaults to \(-\infty\)

  • ub (float) – the upper bound of the variable, optional, defaults to \(+\infty\)

  • domain (pyoptinterface.VariableDomain) – the domain of the variable, optional, defaults to continuous

  • name (str) – the name of the variable, optional

Returns:

the multi-dimensional variable

Return type:

pyoptinterface.tupledict

Add multidimensional variables to the model as numpy.ndarray

model.add_m_variables(shape[, lb=-inf, ub=+inf, domain=pyoptinterface.VariableDomain.Continuous, name=""])

add a multidimensional variable to the model as numpy.ndarray

Parameters:
  • shape – the shape of the variable, can be a tuple of integers or an integer

  • lb (float) – the lower bound of the variable, optional, defaults to \(-\infty\)

  • ub (float) – the upper bound of the variable, optional, defaults to \(+\infty\)

  • domain (pyoptinterface.VariableDomain) – the domain of the variable, optional, defaults to continuous

  • name (str) – the name of the variable, optional

Returns:

the multidimensional variable

Return type:

numpy.ndarray

Get/set variable attributes

model.set_variable_attribute(var, attr, value)

set the value of a variable attribute

Parameters:
model.get_variable_attribute(var, attr)

get the value of a variable attribute

Parameters:
Returns:

the value of the attribute

Delete variable

model.delete_variable(var)

delete a variable from the model

Parameters:

var – the handle of the variable

model.is_variable_active(var)

query whether a variable is active

Parameters:

var – the handle of the variable

Returns:

whether the variable is active

Return type:

bool

Modify the bounds of variable

model.set_variable_bounds(var, lb, ub)

set the lower and upper bounds of a variable

Parameters:
  • var – the handle of the variable

  • lb (float) – the new lower bound value

  • ub (float) – the new upper bound value

Expression

Get the value of an expression (including variable)

model.get_value(expr_or_var)

get the value of an expression or a variable after optimization

Parameters:

expr_or_var – the handle of the expression or the variable

Returns:

the value of the expression or the variable

Return type:

float

Pretty print expression (including variable)

model.pprint(expr_or_var)

pretty print an expression in a human-readable format

Parameters:

expr_or_var – the handle of the expression or the variable

Returns:

the human-readable format of the expression

Return type:

str

Constraint

Add a constraint to the model

Add linear constraints as matrix form to the model

model.add_m_linear_constraints(A, vars, sense, b[, name=""])

add linear constraints as matrix form to the model \(Ax \le b\) or \(Ax = b\) or \(Ax \ge b\)

Parameters:
  • A – the matrix of coefficients, can be a dense numpy.ndarray or a sparse matrix scipy.sparse.sparray

  • vars – the variables in the constraints, can be a list or a 1-d numpy.ndarray returned by add_m_variables

  • sense (pyoptinterface.ConstraintSense) – the sense of the constraints

  • b – the right-hand side of the constraints, should be a 1-d numpy.ndarray

  • name (str) – the name of the constraints, optional

Returns:

the handles of linear constraints

Return type:

numpy.ndarray

Get/set constraint attributes

model.set_constraint_attribute(con, attr, value)

set the value of a constraint attribute

Parameters:
model.get_constraint_attribute(con, attr)

get the value of a constraint attribute

Parameters:
Returns:

the value of the attribute

Delete constraint

model.delete_constraint(con)

delete a constraint from the model

Parameters:

con – the handle of the constraint

model.is_constraint_active(con)

query whether a constraint is active

Parameters:

con – the handle of the constraint

Returns:

whether the variable is active

Return type:

bool

Modify constraint

model.set_normalized_rhs(con, value)

set the right-hand side of a normalized constraint

Parameters:
  • con – the handle of the constraint

  • value – the new right-hand side value

model.get_normalized_rhs(con)

get the right-hand side of a normalized constraint

Parameters:

con – the handle of the constraint

Returns:

the right-hand side value

model.set_normalized_coefficient(con, var, value)

set the coefficient of a variable in a normalized constraint

Parameters:
  • con – the handle of the constraint

  • var – the handle of the variable

  • value – the new coefficient value

model.get_normalized_coefficient(con, var)

get the coefficient of a variable in a normalized constraint

Parameters:
  • con – the handle of the constraint

  • var – the handle of the variable

Returns:

the coefficient value

Objective

Set the objective function

model.set_objective(expr[, sense=pyoptinterface.ObjectiveSense.Minimize])

set the objective function of the model

Parameters:
  • expr – the handle of the expression

  • sense (pyoptinterface.ObjectiveSense) – the sense of the objective function (Minimize/Maximize), defaults to Minimize

Modify the linear part of the objective function

model.set_objective_coefficient(var, value)

modify the coefficient of a variable in the linear part of the objective function

Parameters:
  • var – the handle of the variable

  • value (float) – the new coefficient value

model.get_objective_coefficient(var)

get the coefficient of a variable in the linear part of the objective function

Parameters:

var – the handle of the variable

Returns:

the coefficient value