# Constraints¶

## Summary¶

 pyomo.core.kernel.constraint.constraint([…]) A general algebraic constraint pyomo.core.kernel.constraint.linear_constraint([…]) A linear constraint pyomo.core.kernel.constraint.constraint_tuple(…) A tuple-style container for objects with category type IConstraint pyomo.core.kernel.constraint.constraint_list(…) A list-style container for objects with category type IConstraint pyomo.core.kernel.constraint.constraint_dict(…) A dict-style container for objects with category type IConstraint pyomo.core.kernel.matrix_constraint.matrix_constraint(A) A container for constraints of the form lb <= Ax <= ub.

## Member Documentation¶

class pyomo.core.kernel.constraint.constraint(expr=None, body=None, lb=None, ub=None, rhs=None)[source]

Bases: pyomo.core.kernel.constraint._MutableBoundsConstraintMixin, pyomo.core.kernel.constraint.IConstraint

A general algebraic constraint

Algebraic constraints store relational expressions composed of linear or nonlinear functions involving decision variables.

Parameters: expr – Sets the relational expression for the constraint. Can be updated later by assigning to the expr property on the constraint. When this keyword is used, values for the body, lb, ub, and rhs attributes are automatically determined based on the relational expression type. Default value is None. body – Sets the body of the constraint. Can be updated later by assigning to the body property on the constraint. Default is None. This keyword should not be used in combination with the expr keyword. lb – Sets the lower bound of the constraint. Can be updated later by assigning to the lb property on the constraint. Default is None, which is equivalent to -inf. This keyword should not be used in combination with the expr keyword. ub – Sets the upper bound of the constraint. Can be updated later by assigning to the ub property on the constraint. Default is None, which is equivalent to +inf. This keyword should not be used in combination with the expr keyword. rhs – Sets the right-hand side of the constraint. Can be updated later by assigning to the rhs property on the constraint. The default value of None implies that this keyword is ignored. Otherwise, use of this keyword implies that the equality property is set to True. This keyword should not be used in combination with the expr keyword.

Examples

>>> import pyomo.kernel as pmo
>>> # A decision variable used to define constraints
>>> x = pmo.variable()
>>> # An upper bound constraint
>>> c = pmo.constraint(0.5*x <= 1)
>>> # (equivalent form)
>>> c = pmo.constraint(body=0.5*x, ub=1)
>>> # A range constraint
>>> c = pmo.constraint(lb=-1, body=0.5*x, ub=1)
>>> # An nonlinear equality constraint
>>> c = pmo.constraint(x**2 == 1)
>>> # (equivalent form)
>>> c = pmo.constraint(body=x**2, rhs=1)

body

The body of the constraint

expr

Get or set the expression on this constraint.

class pyomo.core.kernel.constraint.linear_constraint(variables=None, coefficients=None, terms=None, lb=None, ub=None, rhs=None)[source]

Bases: pyomo.core.kernel.constraint._MutableBoundsConstraintMixin, pyomo.core.kernel.constraint.IConstraint

A linear constraint

A linear constraint stores a linear relational expression defined by a list of variables and coefficients. This class can be used to reduce build time and memory for an optimization model. It also increases the speed at which the model can be output to a solver.

Parameters: variables (list) – Sets the list of variables in the linear expression defining the body of the constraint. Can be updated later by assigning to the variables property on the constraint. coefficients (list) – Sets the list of coefficients for the variables in the linear expression defining the body of the constraint. Can be updated later by assigning to the coefficients property on the constraint. terms (list) – An alternative way of initializing the variables and coefficients lists using an iterable of (variable, coefficient) tuples. Can be updated later by assigning to the terms property on the constraint. This keyword should not be used in combination with the variables or coefficients keywords. lb – Sets the lower bound of the constraint. Can be updated later by assigning to the lb property on the constraint. Default is None, which is equivalent to -inf. ub – Sets the upper bound of the constraint. Can be updated later by assigning to the ub property on the constraint. Default is None, which is equivalent to +inf. rhs – Sets the right-hand side of the constraint. Can be updated later by assigning to the rhs property on the constraint. The default value of None implies that this keyword is ignored. Otherwise, use of this keyword implies that the equality property is set to True.

Examples

>>> import pyomo.kernel as pmo
>>> # Decision variables used to define constraints
>>> x = pmo.variable()
>>> y = pmo.variable()
>>> # An upper bound constraint
>>> c = pmo.linear_constraint(variables=[x,y], coefficients=[1,2], ub=1)
>>> # (equivalent form)
>>> c = pmo.linear_constraint(terms=[(x,1), (y,2)], ub=1)
>>> # (equivalent form using a general constraint)
>>> c = pmo.constraint(x + 2*y <= 1)

body

The body of the constraint

canonical_form(compute_values=True)[source]

Build a canonical representation of the body of this constraints

terms

An iterator over the terms in the body of this constraint as (variable, coefficient) tuples

class pyomo.core.kernel.constraint.constraint_tuple(*args, **kwds)

A tuple-style container for objects with category type IConstraint

class pyomo.core.kernel.constraint.constraint_list(*args, **kwds)

A list-style container for objects with category type IConstraint

class pyomo.core.kernel.constraint.constraint_dict(*args, **kwds)

A dict-style container for objects with category type IConstraint

class pyomo.core.kernel.matrix_constraint.matrix_constraint(A, lb=None, ub=None, rhs=None, x=None, sparse=True)[source]

A container for constraints of the form lb <= Ax <= ub.

Parameters: A – A scipy sparse matrix or 2D numpy array (always copied) lb – A scalar or array with the same number of rows as A that defines the lower bound of the constraints ub – A scalar or array with the same number of rows as A that defines the upper bound of the constraints rhs – A scalar or array with the same number of rows as A that defines the right-hand side of the constraints (implies equality constraints) x – A list with the same number of columns as A that stores the variable associated with each column sparse – Indicates whether or not sparse storage (CSR format) should be used to store A. Default is True.
A

A read-only view of the constraint matrix

equality

The array of boolean entries indicating the indices that are equality constraints

lb

The array of constraint lower bounds

lslack

Lower slack (body - lb)

rhs

The array of constraint right-hand sides. Can be set to a scalar or a numpy array of the same dimension. This property can only be read when the equality property is True on every index. Assigning to this property implicitly sets the equality property to True on every index.

slack

min(lslack, uslack)

sparse

Boolean indicating whether or not the underlying matrix uses sparse storage

ub

The array of constraint upper bounds

uslack

Upper slack (ub - body)

x

The list of variables associated with the columns of the constraint matrix