Constraints
Summary
A general algebraic constraint |
|
A linear constraint |
|
A tuple-style container for objects with category type IConstraint |
|
A list-style container for objects with category type IConstraint |
|
A dict-style container for objects with category type IConstraint |
|
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:
_MutableBoundsConstraintMixin,IConstraintA 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
exprproperty on the constraint. When this keyword is used, values for thebody,lb,ub, andrhsattributes are automatically determined based on the relational expression type. Default value isNone.body – Sets the body of the constraint. Can be updated later by assigning to the
bodyproperty on the constraint. Default isNone. This keyword should not be used in combination with theexprkeyword.lb – Sets the lower bound of the constraint. Can be updated later by assigning to the
lbproperty on the constraint. Default isNone, which is equivalent to-inf. This keyword should not be used in combination with theexprkeyword.ub – Sets the upper bound of the constraint. Can be updated later by assigning to the
ubproperty on the constraint. Default isNone, which is equivalent to+inf. This keyword should not be used in combination with theexprkeyword.rhs – Sets the right-hand side of the constraint. Can be updated later by assigning to the
rhsproperty on the constraint. The default value ofNoneimplies that this keyword is ignored. Otherwise, use of this keyword implies that theequalityproperty is set toTrue. This keyword should not be used in combination with theexprkeyword.
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)
- property body
The body of the constraint
- property 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:
_MutableBoundsConstraintMixin,IConstraintA 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
variablesproperty 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
coefficientsproperty on the constraint.terms (list) – An alternative way of initializing the
variablesandcoefficientslists using an iterable of (variable, coefficient) tuples. Can be updated later by assigning to thetermsproperty on the constraint. This keyword should not be used in combination with thevariablesorcoefficientskeywords.lb – Sets the lower bound of the constraint. Can be updated later by assigning to the
lbproperty on the constraint. Default isNone, which is equivalent to-inf.ub – Sets the upper bound of the constraint. Can be updated later by assigning to the
ubproperty on the constraint. Default isNone, which is equivalent to+inf.rhs – Sets the right-hand side of the constraint. Can be updated later by assigning to the
rhsproperty on the constraint. The default value ofNoneimplies that this keyword is ignored. Otherwise, use of this keyword implies that theequalityproperty is set toTrue.
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)
- property body
The body of the constraint
- canonical_form(compute_values=True)[source]
Build a canonical representation of the body of this constraints
- property 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)
Bases:
TupleContainerA tuple-style container for objects with category type IConstraint
- class pyomo.core.kernel.constraint.constraint_list(*args, **kwds)
Bases:
ListContainerA list-style container for objects with category type IConstraint
- class pyomo.core.kernel.constraint.constraint_dict(*args, **kwds)
Bases:
DictContainerA 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]
Bases:
constraint_tupleA 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.
- property A
A read-only view of the constraint matrix
- property equality
The array of boolean entries indicating the indices that are equality constraints
- property lb
The array of constraint lower bounds
- property lslack
Lower slack (body - lb)
- property 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
Trueon every index. Assigning to this property implicitly sets the equality property toTrueon every index.
- property slack
min(lslack, uslack)
- property sparse
Boolean indicating whether or not the underlying matrix uses sparse storage
- property ub
The array of constraint upper bounds
- property uslack
Upper slack (ub - body)
- property x
The list of variables associated with the columns of the constraint matrix