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.

パラメータ:

arg (Constraint)

戻り値:

A new ConstraintList containing both constraints.

戻り値の型:

ConstraintList

__eq__(self, arg: object, /) bool

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

パラメータ:

arg (object)

戻り値の型:

bool

__imul__(self, arg: float, /) Constraint

Multiply the weight of the constraint by the coefficient.

パラメータ:

arg (float)

戻り値の型:

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.

パラメータ:

arg (float)

戻り値の型:

Constraint

__mul__(self, arg: float, /) Constraint

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

パラメータ:

arg (float)

戻り値の型:

Constraint

__ne__(self, arg: object, /) bool

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

パラメータ:

arg (object)

戻り値の型:

bool

__radd__(self, arg: int, /) Constraint
パラメータ:

arg (int)

戻り値の型:

Constraint

__repr__(self) str
戻り値の型:

str

__rmul__(self, arg: float, /) Constraint

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

パラメータ:

arg (float)

戻り値の型:

Constraint

__str__(self) str
戻り値の型:

str

__truediv__(self, arg: float, /) Constraint

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

パラメータ:

arg (float)

戻り値の型:

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.

戻り値:

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

戻り値の型:

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

property label

Constraint label used for identification.

戻り値の型:

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.

戻り値:

Penalty expression used for this constraint.

The polynomial that the conversion adds to the objective function.

戻り値の型:

Poly

例外:

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.

戻り値の型:

float

例外:

ValueError -- The setter received a negative weight.