BinaryVariable¶
- class BinaryVariable¶
Bases:
VariableBase
A class for the binary variable.
Methods
Constructor.
Converts binary to value index.
Discretization information.
Decode values of the the Amplify SDK variables (i.e. binary variables) to a value of this variable.
Encode the value to values of the Amplify SDK variables (i.e. binary variables) if necessary.
Generate Amplify SDK's constraints related to the conversion of a non-binary variable.
Generate a random value compatible with the variable.
Converts a value index to a binary vector.
Converts a value index to a value of the variable.
Issue the Amplify SDK's variables (amplify.PolyArray) for the variable only when VariableBase.poly_array is None.
Issue the Amplify SDK variables relevant to this variable.
Make poly_array = None.
Converts the variable to the Amplify SDK's polynomial.
Convert the variable to a polynomial with a unity coefficient.
Converts a value of the variable to a value index.
Attributes
Return the lower and upper bounds that the variable can take.
Return the distance between discretization points.
Return the number of the variables.
Return the encoding method.
Return the name of the variable.
Return the number of discretization bins.
Return the number of the Amplify SDK variables encoded from the variable.
The Amplify SDK's PolyArray that represents this variable.
Return the type of the variable.
- __add__(other: Any) Poly ¶
Add the variable to another variable or a polynomial.
- Parameters:
other – A variable or a polynomial to add.
- Raises:
ValueError – If other is not a variable or a polynomial.
- Returns:
The resulting polynomial.
- Return type:
- __mul__(other: Any) Poly ¶
Multiply the variable by a value.
If this is directly called for a variable list class, the sum of all the variables applied __mul__ is considered.
- Parameters:
other – A value to multiply.
- Returns:
The resulting polynomial of the variable with a coefficient being the value.
- Return type:
- __radd__(other: Any) Poly ¶
Add the variable to another variable or a polynomial.
- Parameters:
other – A variable or a polynomial to add.
- Raises:
ValueError – If other is not a variable or a polynomial.
- Returns:
The resulting polynomial.
- Return type:
- __rmul__(other: Any) Poly ¶
Multiply the variable by a value.
If this is directly called for a variable list class, the sum of all the variables applied __rmul__ is considered.
- Parameters:
other – A value to multiply.
- Returns:
The resulting polynomial of the variable with a coefficient being the value.
- Return type:
- __rsub__(other: Any) Poly ¶
Subtract another variable or a polynomial from the variable.
- Parameters:
other – A variable or a polynomial to subtract.
- Raises:
ValueError – If other is not a variable or a polynomial.
- Returns:
The resulting polynomial.
- Return type:
- __str__() str ¶
Return a human-readable information of the variables.
- Returns:
The human-readable information of the variables.
- Return type:
- __sub__(other: Any) Poly ¶
Subtract another variable or a polynomial from the variable.
- Parameters:
other – A variable or a polynomial to subtract.
- Raises:
ValueError – If other is not a variable or a polynomial.
- Returns:
The resulting polynomial.
- Return type:
- binary_to_idx(binary: list[int]) int ¶
Converts binary to value index. Only for
BinaryVariable
, value == idx == binary[0]. Note that returned binary from Amplify AE is list[float], hence the int().
- construct_discretize_table() ndarray ¶
Discretization information.
- Returns:
Discretization table.
- Return type:
np.ndarray
- decode(amplify_value_vector: list[int | Any]) Any ¶
Decode values of the the Amplify SDK variables (i.e. binary variables) to a value of this variable.
- encode(value: Any) list[int | Any] ¶
Encode the value to values of the Amplify SDK variables (i.e. binary variables) if necessary.
- generate_amplify_constraint() ConstraintList ¶
Generate Amplify SDK’s constraints related to the conversion of a non-binary variable.
- Raises:
RuntimeError – If the poly_array is not set.
- Returns:
The resulting constraints.
- Return type:
amplify.ConstraintList
- generate_random_value( ) Any ¶
Generate a random value compatible with the variable.
If ref_value specified, ensure that the return value value != ref_value. User-defined constraints (if there’s any) are not considered in this value.
- Parameters:
rng (np.random.Generator) – NumPy’s random generator.
ref_value (Any | None, optional) – A reference value. Defaults to None.
find_neighbour (bool, optional) – True to generate a random value neighbour to the reference value. Defaults to False.
- Returns:
The resulting value of the variable.
- Return type:
Any
- idx_to_value(idx: int) bool ¶
Converts a value index to a value of the variable.
Only for
BinaryVariable
, value == idx == binary[0].
- issue_amplify_variable(
- generator: VariableGenerator,
- var_counter: dict[VariableType, int],
- var_name: dict[VariableType, str],
Issue the Amplify SDK’s variables (amplify.PolyArray) for the variable only when VariableBase.poly_array is None.
- Parameters:
generator (amplify.VariableGenerator) – A variable generator.
var_counter (dict[amplify.VariableType, int]) – Counter counts how many amplify variables of each amplify.VariableType are issued.
var_name (dict[amplify.VariableType, str]) – Name prefix of amplify variables of each amplify.VariableType.
- issue_amplify_variable_impl(
- generator: VariableGenerator,
- var_counter: dict[VariableType, int],
- var_name: dict[VariableType, str],
Issue the Amplify SDK variables relevant to this variable.
- to_amplify_poly() Poly ¶
Converts the variable to the Amplify SDK’s polynomial.
- Raises:
RuntimeError – If poly_array is not set for the variable.
- Returns:
The resulting polynomial.
- Return type:
amplify.Poly
- to_poly() Poly ¶
Convert the variable to a polynomial with a unity coefficient.
If this is directly called for a variable list class instance, the sum of all the variables in the list is considered.
- Returns:
The constructed polynomial.
- Return type:
- value_to_idx(value: bool) int ¶
Converts a value of the variable to a value index.
Only for
BinaryVariable
, value == idx == binary[0].
- __abstractmethods__ = frozenset({})¶
- __dict__ = mappingproxy({'__module__': 'amplify_bbopt.variable', '__doc__': 'A class for the binary variable.', '__init__': <function BinaryVariable.__init__>, 'value_to_idx': <function BinaryVariable.value_to_idx>, 'idx_to_value': <function BinaryVariable.idx_to_value>, 'to_amplify_poly': <function BinaryVariable.to_amplify_poly>, 'issue_amplify_variable_impl': <function BinaryVariable.issue_amplify_variable_impl>, 'binary_to_idx': <function BinaryVariable.binary_to_idx>, 'idx_to_binary': <function BinaryVariable.idx_to_binary>, '__abstractmethods__': frozenset(), '_abc_impl': <_abc._abc_data object>, '__annotations__': {'_name': 'str | None', '_poly_array': 'amplify.PolyArray | None'}})¶
- __slots__ = ()¶
- __weakref__¶
list of weak references to the object (if defined)
- property bounds: tuple¶
Return the lower and upper bounds that the variable can take.
- Returns:
The lower and upper bounds that the variable can take.
- property delta: float¶
Return the distance between discretization points.
- Returns:
The distance between discretization points.
- property len: int¶
Return the number of the variables. This is significant for variable list classes.
- Returns:
The number of the variables.
- Return type:
- property method: str | None¶
Return the encoding method.
- Returns:
The encoding method. None is no encoding is necessary.
- Return type:
str | None
- property name: str | None¶
Return the name of the variable.
- Returns:
The variable name.
- Return type:
- property nbins: int¶
Return the number of discretization bins.
- Returns:
The number of discretization bins.
- Return type:
- property num_amplify_variables: int¶
Return the number of the Amplify SDK variables encoded from the variable.
- Returns:
The number of the Amplify SDK variables.
- Return type: