VariableGenerator¶
- class VariableGenerator¶
Provides functionality to generate a decision variable or an array of decision variables.
Methods
Generate an array of variables.
Generate a quadratic polynomial in coefficient-matrix form.
Generate a scalar variable.
Attributes
List of all variables created.
- __repr__(self) str¶
Return a developer-friendly string representation of the generator.
- Return type:
- array(
- self,
- type: Literal['Binary', 'Ising', 'Integer', 'Real'] | VariableType,
- shape: tuple[int, ...] | int,
- bounds: tuple[float | None, ...] = (None, None),
- name: str = '',
- array(
- self,
- type: Literal['Binary', 'Ising', 'Integer', 'Real'] | VariableType,
- *shape: int,
Generate an array of variables.
Variables are generated in an n-dimensional array. Each variable has the same type and bounds (if any).
Overload 1.
- Args:
type (typing.Literal[‘Binary’, ‘Ising’, ‘Integer’, ‘Real’] | amplify.VariableType): The type of variables.
- shape (tuple[int, …] | int): The NumPy-like shape of the variable array.
For example,
(3, 4)generates a two-dimensional variable array with 3 rows and 4 columns.
- bounds (tuple[float | None, …]): The lower and upper bounds of the variables.
If either of the bounds is not specified, it will be unbounded in that direction. A Binary or an Ising variable carries no bound. Giving one that narrows its value set raises a
ValueError. Defaults to(None, None).
- name (str): The name of the variable array.
Each variable is automatically assigned a name consisting of this string and a specific index. Defaults to
''.
- Returns:
amplify.PolyArray[Dim]: Generated variable array.
An array of the given shape. Every element is a polynomial of one new variable.
Overload 2.
- Args:
type (typing.Literal[‘Binary’, ‘Ising’, ‘Integer’, ‘Real’] | amplify.VariableType): The type of variables.
*shape (int): The NumPy-like shape of the variable array, given as one integer for each axis.
- Returns:
amplify.PolyArray[Dim]: Generated variable array.
- matrix(
- self,
- type: Literal['Binary', 'Ising', 'Integer', 'Real'] | VariableType,
- shape: int | tuple[int, ...],
- bounds: tuple[float | None, ...] = (None, None),
- name: str = '',
- matrix(
- self,
- type: Literal['Binary', 'Ising', 'Integer', 'Real'] | VariableType,
- *shape: int,
Generate a quadratic polynomial in coefficient-matrix form.
A variable array of the specified shape is generated together with the coefficient matrix. The polynomial represented by the coefficient matrix is defined as \((x^T) Q x + (p^T) x + c\), where Q is the quadratic coefficient matrix, p is the linear coefficient array, c is the constant term, and x is the variable array.
Overload 1.
- Args:
type (typing.Literal[‘Binary’, ‘Ising’, ‘Integer’, ‘Real’] | amplify.VariableType): The type of variables.
- shape (int | tuple[int, …]): The shape of the array. It can be specified in two ways:
A single integer, e.g., 5, which will generate a one-dimensional variable array with 5 elements.
A NumPy-like shape, e.g.,
(3, 4), which will generate a two-dimensional variable array with 3 rows and 4 columns.
- bounds (tuple[float | None, …]): The lower and upper bounds of the variables.
If either bound is omitted, the variable is unbounded in that direction. A Binary or an Ising variable carries no bound. Giving one that narrows its value set raises a
ValueError. Defaults to(None, None).
- name (str): The name of the variable array.
Each variable is automatically assigned a name consisting of this string and a specific index. Defaults to
''.
- Returns:
amplify.Matrix: Coefficient matrix.
A matrix whose coefficients are all zero. Its
variable_arrayholds the new variables that the matrix uses.
Overload 2.
- Args:
type (typing.Literal[‘Binary’, ‘Ising’, ‘Integer’, ‘Real’] | amplify.VariableType): The type of variables.
- *shape (int): The shape of the array.
This overload is provided for backward compatibility.
- Returns:
amplify.Matrix: Coefficient matrix.
- scalar(
- self,
- type: Literal['Binary', 'Ising', 'Integer', 'Real'] | VariableType,
- bounds: tuple[float | None, ...] = (None, None),
- name: str = '',
Generate a scalar variable.
The variable will have the specified type and bounds (if any).
- Parameters:
type (Literal['Binary', 'Ising', 'Integer', 'Real'] | amplify.VariableType) – The type of the variable.
bounds (tuple[float | None, ...]) – The lower and upper bounds of the variable. A Binary or an Ising variable carries no bound. Giving one that narrows its value set raises a
ValueError. Defaults to(None, None).name (str) – The name of the variable. Defaults to
''.
- Returns:
Generated variable.
A polynomial of one new variable.
- Return type: