Variable API Reference
Variable
A decision variable.
Variables represent unknowns in an optimization problem that are determined by the optimization process. Each variable has a name, type, optional bounds, and belongs to an environment.
Variables can be combined using arithmetic operations (+, -, *, **)
to create Expression objects, and compared using relational operators (==, <=, >=)
to create Constraint objects.
Parameters:
-
name(str) –The unique name identifying this variable within its environment.
-
vtype(Vtype, default:Vtype.BINARY) –The type of the variable. Must be one of:
Vtype.BINARY: Variable can be 0 or 1Vtype.SPIN: Variable can be -1 or +1Vtype.INTEGER: Variable can be any integerVtype.REAL: Variable can be any real number
If Vtype.INVERTED_BINARY is used an error is raised.
-
bounds(Bounds, default:None) –The bounds constraining the variable's value.
-
env(Environment, default:None) –The environment managing this variable. If
None, requires an active environment context.
Attributes:
-
id(int) –Unique integer identifier for this variable within its environment.
-
name(str) –The name of the variable.
-
bounds(Bounds) –The lower and upper bounds of the variable.
-
vtype(Vtype) –The type of the variable.
-
environment(Environment) –The environment containing this variable.
Examples:
Create binary variables:
>>> from luna_model import Environment, Variable, Vtype
>>> with Environment():
... x1 = Variable("x1") # Binary by default
... x2 = Variable("x2", vtype=Vtype.BINARY)
Create integer variables with bounds:
>>> from luna_model import Variable, Vtype, Bounds
>>> env = Environment()
>>> y = Variable("y", vtype=Vtype.INTEGER, bounds=Bounds(0, 10), env=env)
Create expressions using arithmetic:
>>> with Environment():
... x = Variable("x")
... y = Variable("y")
>>> expr = 3 * x + 2 * y - 5 # Creates an Expression
Create constraints using comparisons:
>>> with Environment():
... x = Variable("x")
... y = Variable("y")
>>> constraint = x + y <= 1 # Creates a Constraint
id: int
property
Get the unique identifier of the variable.
Returns:
-
int–Unique integer identifier within the variable's environment.
Notes
Variable IDs are assigned sequentially within an environment.
name: str
property
bounds: VBounds
property
Get the bounds of the variable.
Returns:
-
VBounds–The variable's bounds with a guaranteed lower and upper value.
vtype: Vtype
property
environment: Environment
property
Get the environment containing this variable.
Returns:
-
Environment–The environment containing this variable's metadata.
is_equal(other: Variable) -> bool
Check if this variable is the same as another variable.
Parameters:
-
other(Variable) –The variable to compare with.
Returns:
-
bool–Trueif both variables refer to the same underlying variable (same ID and environment),Falseotherwise.
Notes
This compares variable identity, not just name equality. Two variables with the same name in different environments are not equal.
inv() -> Variable
Get an inverted version of this variable.
Only a binary variable (Vtype.BINARY) can be inverted. Inverting a
variable of any other vtype will raise an UnsupportedOperationError.
If the inverted variable already exists it is not re-created but returned
directly.
Returns:
-
Variable–The inverted variable.
Raises:
-
UnsupportedOperationError–If any variable is inverted that is not of type
Vtype.BINARY
Notes
Inverted variables maintain a relationship with their original variable in the environment.
__add__(other: Expression | Variable | float) -> Expression
Add this variable to another term.
Parameters:
-
other(Expression or Variable or float) –The term to add to this variable.
Returns:
-
Expression–A new expression representing the sum.
__sub__(other: Expression | Variable | float) -> Expression
Subtract another term from this variable.
Parameters:
-
other(Expression or Variable or float) –The term to subtract from this variable.
Returns:
-
Expression–A new expression representing the difference.
__mul__(other: Expression | Variable | float) -> Expression
Multiply this variable by another term.
Parameters:
-
other(Expression or Variable or float) –The term to multiply with this variable.
Returns:
-
Expression–A new expression representing the product.
__radd__(other: Expression | Variable | float) -> Expression
Add this variable to another term (right operand).
Parameters:
-
other(Expression or Variable or float) –The term to add this variable to.
Returns:
-
Expression–A new expression representing the sum.
__rsub__(other: Expression | Variable | float) -> Expression
Subtract this variable from another term (right operand).
Parameters:
-
other(Expression or Variable or float) –The term to subtract this variable from.
Returns:
-
Expression–A new expression representing the difference.
__rmul__(other: Expression | Variable | float) -> Expression
Multiply another term by this variable (right operand).
Parameters:
-
other(Expression or Variable or float) –The term to multiply by this variable.
Returns:
-
Expression–A new expression representing the product.
__pow__(val: int) -> Expression
Raise this variable to a power.
Parameters:
-
val(int) –The exponent (must be a positive integer).
Returns:
-
Expression–A new expression representing this variable raised to the power.
__neg__() -> Expression
Negate this variable.
Returns:
-
Expression–A new expression representing the negation of this variable.
__invert__() -> Variable
Get an inverted version of this variable.
Only a binary variable (Vtype.BINARY) can be inverted. Inverting a
variable of any other vtype will raise an UnsupportedOperationError.
If the inverted variable already exists it is not re-created but returned
directly.
Returns:
-
Variable–The inverted variable.
Raises:
-
UnsupportedOperationError–If any variable is inverted that is not of type
Vtype.BINARY
Notes
Inverted variables maintain a relationship with their original variable in the environment.
__eq__(other: Expression | Variable | float) -> Constraint | bool
Check equality or create an equality constraint.
When comparing with another Variable, performs identity comparison. When comparing with an Expression or float, creates an equality constraint.
Parameters:
-
other(Expression or Variable or float) –The term to compare with.
Returns:
-
Constraint or bool–boolif comparing with another Variable (identity check)Constraintif comparing with Expression or float (constraint creation)
__le__(other: Expression | Variable | float) -> Constraint
Create a less-than-or-equal-to constraint.
Parameters:
-
other(Expression or Variable or float) –The right-hand side of the inequality.
Returns:
-
Constraint–A constraint representing
self <= other.
__ge__(other: Expression | Variable | float) -> Constraint
Create a greater-than-or-equal-to constraint.
Parameters:
-
other(Expression or Variable or float) –The right-hand side of the inequality.
Returns:
-
Constraint–A constraint representing
self >= other.
__hash__() -> int
Compute hash.
__str__() -> str
Get human-readable string representation.
__repr__() -> str
Get debug string representation.
Vtype
Bases: Enum
Enumeration of variable types.
The variable type determines the domain of values a variable can take during optimization.
Attributes:
-
BINARY(str) –Binary variable that can be 0 or 1. Used for yes/no decisions.
-
INVERTED_BINARY(str) –Inverted binary variable. Created by inverting a BINARY variable. Creating a variable with
vtype=Vtype.INVERTED_BINARYwill raise an error. -
SPIN(str) –Spin variable that can be -1 or +1. Common in quantum computing formulations.
-
INTEGER(str) –Integer variable that can be any integer value within bounds.
-
REAL(str) –Real-valued (continuous) variable that can be any floating-point value within bounds.
Bounds
Bounds for constraining variable values.
Bounds specify the lower and upper limits on the values a variable can take during optimization. Bounds can be finite values or unbounded (infinite).
Parameters:
-
lower(float or type[Unbounded], default:None) –The lower bound. Use
Unboundedfor negative infinity. UseNonefor the default value based on a variable's vtype. -
upper(float or type[Unbounded], default:None) –The upper bound. Use
Unboundedfor positive infinity. UseNonefor the default value based on a variable's vtype.
Attributes:
-
lower(float or type[Unbounded] or None) –The lower bound value. If set to
Nonethe lower bound will be determined based on the variable's vtype this bounds object is be associated with during variable creation. -
upper(float or type[Unbounded] or None) –The upper bound value. If set to
Nonethe upper bound will be determined based on the variable's vtype this bounds object is be associated with during variable creation.