Constraint

class Constraint

Constraint class representing equality or inequality conditions on polynomial expressions.

Methods

__init__

Construct a constraint from bounds and a penalty polynomial.

is_satisfied

Check whether the constraint is satisfied.

Attributes

conditional

Constraint condition as (left, op, right) tuple.

label

Constraint label used for identification.

penalty

Penalty polynomial associated with this constraint.

weight

Constraint weight.

__add__(self, arg: Constraint, /) ConstraintList

Combine two constraints into a ConstraintList.

Parameters:

arg (Constraint)

Returns:

A new ConstraintList containing both constraints.

Return type:

ConstraintList

__eq__(self, arg: object, /) bool

Tell whether the two constraints have the same condition, the same weight, and the same label.

Parameters:

arg (object)

Return type:

bool

__imul__(self, arg: float, /) Constraint

Multiply the weight of the constraint by the coefficient.

Parameters:

arg (float)

Return type:

Constraint

__init__(
self,
left: Poly,
bounds: tuple[float | None, float | None],
penalty: Poly,
label: str = '',
threshold: float = 0.0,
) None
__init__(self, left: Poly, eq: float, penalty: Poly, label: str = '', threshold: float = 0.0) None
__init__(self, left: Poly, le: float, penalty: Poly, label: str = '', threshold: float = 0.0) None
__init__(self, left: Poly, ge: float, penalty: Poly, label: str = '', threshold: float = 0.0) None

Construct a constraint from bounds and a penalty polynomial.

Overload 1.

Args:
  • left (amplify.Poly): Left-hand side polynomial.

  • bounds (tuple[float | None, float | None]): Bounds as (lower, upper). At least one must be specified.

  • penalty (amplify.Poly): Penalty polynomial.

  • label (str): Constraint label. Defaults to ''.

  • threshold (float): Satisfaction threshold. Defaults to 0.0.

Overload 2.

Args:
  • left (amplify.Poly): Left-hand side polynomial.

  • eq (float): Right-hand side value for equality.

  • penalty (amplify.Poly): Penalty polynomial.

  • label (str): Constraint label. Defaults to ''.

  • threshold (float): Satisfaction threshold. Defaults to 0.0.

Overload 3.

Args:
  • left (amplify.Poly): Left-hand side polynomial.

  • le (float): Upper bound value.

  • penalty (amplify.Poly): Penalty polynomial.

  • label (str): Constraint label. Defaults to ''.

  • threshold (float): Satisfaction threshold. Defaults to 0.0.

Overload 4.

Args:
  • left (amplify.Poly): Left-hand side polynomial.

  • ge (float): Lower bound value.

  • penalty (amplify.Poly): Penalty polynomial.

  • label (str): Constraint label. Defaults to ''.

  • threshold (float): Satisfaction threshold. Defaults to 0.0.

__itruediv__(self, arg: float, /) Constraint

Divide the weight of the constraint by the coefficient.

Parameters:

arg (float)

Return type:

Constraint

__mul__(self, arg: float, /) Constraint

Give a new constraint whose weight is the weight of this one times the coefficient.

Parameters:

arg (float)

Return type:

Constraint

__ne__(self, arg: object, /) bool

Tell whether the two constraints differ. This is the negation of ==.

Parameters:

arg (object)

Return type:

bool

__radd__(self, arg: int, /) Constraint
Parameters:

arg (int)

Return type:

Constraint

__repr__(self) str
Return type:

str

__rmul__(self, arg: float, /) Constraint

Give a new constraint whose weight is the weight of this one times the coefficient.

Parameters:

arg (float)

Return type:

Constraint

__str__(self) str
Return type:

str

__truediv__(self, arg: float, /) Constraint

Give a new constraint whose weight is the weight of this one divided by the coefficient.

Parameters:

arg (float)

Return type:

Constraint

is_satisfied(self, values: Values) bool
is_satisfied(
self,
mapping: Mapping[Poly, float | int] | Mapping[Variable, float | int],
) bool

Check whether the constraint is satisfied.

Overload 1.

Args:
  • values (amplify.Values): Values for variables, typically result.best.values.

Returns:

bool: True if the condition is satisfied.

Overload 2.

Args:
  • mapping (collections.abc.Mapping[amplify.Poly, float | int] | collections.abc.Mapping[amplify.Variable, float | int]): Mapping from variable to numeric value.

Returns:

bool: True if the condition is satisfied.

property conditional

Constraint condition as (left, op, right) tuple.

Returns:

Left polynomial, operator literal, and right-hand side value(s).

Return type:

tuple[Poly, Literal[‘EQ’, ‘GE’, ‘GT’, ‘LE’, ‘LT’, ‘BW’], float | tuple[float, float]]

property label

Constraint label used for identification.

Return type:

str

property penalty

Penalty polynomial associated with this constraint.

The penalty function is zero when the constraint holds and positive otherwise. An inequality over a left-hand side whose range is unknown has no exact penalty function, so it needs a relaxation formulation.

Returns:

Penalty expression used for this constraint.

The polynomial that the conversion adds to the objective function.

Return type:

Poly

Raises:

RuntimeError – The constraint is an inequality, the range of its left-hand side is unknown, and the penalty formulation is not a relaxation.

property weight

Constraint weight.

Larger values enforce this constraint more strongly when penalty methods are used. The setter refuses a negative value.

Return type:

float

Raises:

ValueError – The setter received a negative weight.