LpConstraint¶
A class representing a linear constraint
Attributes¶
lhs: LpExpression
property
writable
¶
Getter for lhs expression
lower_bound: Union[int, float]
property
readonly
¶
Returns the lower bound on the shifted expression
rhs: LpExpression
property
writable
¶
Getter for rhs expression
sense: str
property
writable
¶
Getter for the sense of the constraint
slack: bool
property
writable
¶
Getter for slack indicator
slack_penalty: Union[int, float]
property
writable
¶
Getter for slack penalty of the constraint
slack_variable: LpVariable
property
readonly
¶
Getter for the slack variable of the problem. Sets if does not exist.
upper_bound: Union[int, float]
property
readonly
¶
Returns the upper bound on the shifted expression
Methods¶
breakdown(self)
¶
Breaks down a equality constraint into an upper bound and a lower bound constraint
Source code in flipy/lp_constraint.py
def breakdown(self) -> List['LpConstraint']:
""" Breaks down a equality constraint into an upper bound and a lower bound constraint """
if self.sense != 'eq':
return [self]
upper_bound_constraint = LpConstraint(
name=self.name + '_ub',
lhs=LpExpression(expression=copy.copy(self.lhs.expr)),
rhs=LpExpression(expression=copy.copy(self.rhs.expr)),
sense='leq',
slack=self.slack,
slack_penalty=self.slack_penalty,
)
lower_bound_constraint = self
lower_bound_constraint.sense = 'geq'
lower_bound_constraint.name += '_lb'
return [lower_bound_constraint, upper_bound_constraint]
check(self)
¶
Checks if the constraint is satisfied given variable assignments
Source code in flipy/lp_constraint.py
def check(self) -> bool:
""" Checks if the constraint is satisfied given variable assignments """
return {'leq': operator.le,
'eq': operator.eq,
'geq': operator.ge}[self.sense](self.lhs.evaluate() +
(0 if not self.slack else self.slack_variable.evaluate() *
(-1 if self.sense == 'leq' else 1)),
self.rhs.evaluate())
to_lp_terms(self)
¶
Returns the constraint as a list of terms like ['5', 'a', '<=', '10']
Returns:
Type | Description |
---|---|
list(str) |
List of terms in string |
Source code in flipy/lp_constraint.py
def to_lp_terms(self):
"""
Returns the constraint as a list of terms like ['5', 'a', '<=', '10']
Returns
-------
list(str)
List of terms in string
"""
new_expr, const = self._shift_variables()
if not any(new_expr.expr.values()):
return []
if self.sense.lower() == 'leq':
sense = '<='
elif self.sense.lower() == 'geq':
sense = '>='
else:
sense = '='
terms = []
terms += new_expr.to_lp_terms()
terms.append(sense)
terms.append(str(const))
return terms