linear_constraint

(class from pyomo.core.kernel.constraint)

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

Bases: _MutableBoundsConstraintMixin, 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)
__init__(variables=None, coefficients=None, terms=None, lb=None, ub=None, rhs=None)[source]

Methods

__init__([variables, coefficients, terms, ...])

activate()

Activate this object.

canonical_form([compute_values])

Build a canonical representation of the body of this constraints

clone()

Returns a copy of this object with the parent pointer set to None.

deactivate()

Deactivate this object.

getname([fully_qualified, name_buffer, ...])

Dynamically generates a name for this object.

has_lb()

Returns False when the lower bound is None or negative infinity

has_ub()

Returns False when the upper bound is None or positive infinity

to_bounded_expression([evaluate_bounds])

Attributes

active

The active status of this object.

body

The body of the constraint

bounds

The bounds of the constraint as a tuple (lb, ub)

ctype

The object's category type.

equality

Returns True when this is an equality constraint.

expr

Get the expression on this constraint.

lb

The value of the lower bound of the constraint

local_name

The object's local name within the context of its parent.

lower

The expression for the lower bound of the constraint

lslack

Lower slack (body - lb).

name

The object's fully qualified name.

parent

The object's parent (possibly None).

rhs

The right-hand side of the constraint

slack

min(lslack, uslack).

storage_key

The object's storage key within its parent

terms

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

ub

The value of the upper bound of the constraint

upper

The expression for the upper bound of the constraint

uslack

Upper slack (ub - body).

Member Documentation

activate()

Activate this object.

canonical_form(compute_values=True)[source]

Build a canonical representation of the body of this constraints

clone()

Returns a copy of this object with the parent pointer set to None.

A clone is almost equivalent to deepcopy except that any categorized objects encountered that are not descendents of this object will reference the same object on the clone.

deactivate()

Deactivate this object.

getname(fully_qualified=False, name_buffer={}, convert=<class 'str'>, relative_to=None)

Dynamically generates a name for this object.

Parameters:
  • fully_qualified (bool) – Generate a full name by iterating through all ancestor containers. Default is False.

  • convert (function) – A function that converts a storage key into a string representation. Default is the built-in function str.

  • relative_to (object) – When generating a fully qualified name, generate the name relative to this block.

Returns:

If a parent exists, this method returns a string representing the name of the object in the context of its parent; otherwise (if no parent exists), this method returns None.

has_lb()

Returns False when the lower bound is None or negative infinity

has_ub()

Returns False when the upper bound is None or positive infinity

property active

The active status of this object.

property body

The body of the constraint

property bounds

The bounds of the constraint as a tuple (lb, ub)

property ctype

The object’s category type.

property equality

Returns True when this is an equality constraint.

Disable equality by assigning False. Equality can only be activated by assigning a value to the .rhs property.

property expr

Get the expression on this constraint.

property lb

The value of the lower bound of the constraint

property local_name

The object’s local name within the context of its parent. Alias for obj.getname(fully_qualified=False).

property lower

The expression for the lower bound of the constraint

property lslack

Lower slack (body - lb). Returns None if a value for the body can not be computed.

property name

The object’s fully qualified name. Alias for obj.getname(fully_qualified=True).

property parent

The object’s parent (possibly None).

property rhs

The right-hand side of the constraint

property slack

min(lslack, uslack). Returns None if a value for the body can not be computed.

property storage_key

The object’s storage key within its parent

property terms

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

property ub

The value of the upper bound of the constraint

property upper

The expression for the upper bound of the constraint

property uslack

Upper slack (ub - body). Returns None if a value for the body can not be computed.