Constraint

class Constraint

Bases: object

A class for a user-defined constraint.

Methods

__init__

Initialize constraint.

is_satisfied

Return whether a given input (solution of the optimization) meets the constraint.

to_amplify_constraint

Convert the constraint to the Amplify SDK's constraint.

unify_variables

Unify duplicate variables used in the constraint and across different constraints.

Attributes

left

A l.h.s.

op

An equality or inequality operator symbol of a constraint.

penalty_formulation

The Amplify SDK's penalty_formulation parameter.

right

A r.h.s.

weight

A constraint weight.

__init__(left: Poly | VariableListBase, op: str, right: float | tuple) None

Initialize constraint.

Parameters:
  • left (Poly | VariableListBase) – A polynomial of variables. If a ‘variable list’ is directly given, its sum is considered as the l.h.s. polynomial.

  • op (str) – An equality or inequality operator of the constraint.

  • right (float | tuple) – R.h.s. value.

__repr__() str

Return repr(self).

__str__() str

Return a human-readable constraint expression.

Returns:

A constraint expression.

Return type:

str

is_satisfied(solution_dict: FlatSolutionDict) bool

Return whether a given input (solution of the optimization) meets the constraint.

Parameters:

solution_dict (FlatSolutionDict) – A solution.

Raises:
  • ValueError – If a variable used in the constraint is not found in the solution_dict .

  • ValueError – If an operator is invalid.

Returns:

True if the constraint is met.

Return type:

bool

to_amplify_constraint() Constraint

Convert the constraint to the Amplify SDK’s constraint.

Raises:

ValueError – If an operator is invalid.

Returns:

A converted constraint.

Return type:

amplify.Constraint

unify_variables(var_dict_universe: dict[str, Any]) None

Unify duplicate variables used in the constraint and across different constraints. Intended for multi-objective optimizations where the same variable is used in different black-box objective functions. Calling this for single-objective optimization has no effect.

Parameters:

var_dict_universe (dict[str, Any]) – A dictionary of all variable names and variables appearing in black-box functions. In the same form as Variables.var_dict (structured).

__dict__ = mappingproxy({'__module__': 'amplify_bbopt.constraint', '__doc__': 'A class for a user-defined constraint.', '__init__': <function Constraint.__init__>, 'unify_variables': <function Constraint.unify_variables>, 'left': <property object>, 'op': <property object>, 'right': <property object>, 'weight': <property object>, 'penalty_formulation': <property object>, 'to_amplify_constraint': <function Constraint.to_amplify_constraint>, 'is_satisfied': <function Constraint.is_satisfied>, '__str__': <function Constraint.__str__>, '__repr__': <function Constraint.__repr__>, '__dict__': <attribute '__dict__' of 'Constraint' objects>, '__weakref__': <attribute '__weakref__' of 'Constraint' objects>, '__annotations__': {}})
__weakref__

list of weak references to the object (if defined)

property left: Poly

A l.h.s. of the constraint (a polynomial of variables).

property op: str

An equality or inequality operator symbol of a constraint.

property penalty_formulation: str

The Amplify SDK’s penalty_formulation parameter.

Set this to “Relaxation” when you:
  1. impose an inequality constraint with real variables (when coefficients in the constraint formulation is likely to yield real numbers), AND

  2. the specified solver (e.g Amplify Annealing Engine) is a QUBO solver that does not support real variables, until the Amplify SDK used internally in Amplify-BBOpt supports the real-to-binary variable conversion. See: https://amplify.fixstars.com/ja/docs/amplify/v1/penalty.html#ineq-penalty

property right: int | float | tuple

A r.h.s. value of the constraint.

property weight: float

A constraint weight.