Variable¶
Variable represents a decision variable in the optimization problem. It can be created by calling the add_variable
method of the model:
import pyoptinterface as poi
from pyoptinterface import highs
model = highs.Model()
x = model.add_variable(lb=0, ub=1.0, domain=poi.VariableDomain.Continuous, name="x")
Variable Attributes¶
After a variable is created, we can query or modify its attributes. The following table lists the standard variable attributes:
Attribute name |
Type |
---|---|
Name |
str |
LowerBound |
float |
UpperBound |
float |
Domain |
|
PrimalStart |
float |
Value |
float |
# set the lower bound of the variable
model.set_variable_attribute(x, poi.VariableAttribute.LowerBound, 0.0)
# set the upper bound of the variable
model.set_variable_attribute(x, poi.VariableAttribute.UpperBound, 1.0)
# For mixed-integer programming, we can set the initial value of the variable
model.set_variable_attribute(x, poi.VariableAttribute.PrimalStart, 0.5)
# get the value of the variable after optimization
x_value = model.get_variable_attribute(x, poi.VariableAttribute.Value)
The most common operation is to set the bounds of the variable. We can use the set_variable_bounds
method of the model to set the lower and upper bounds of the variable at the same time:
x = model.add_variable(name="x")
model.set_variable_bounds(x, 0.0, 1.0)
Delete variable¶
We can delete a variable by calling the delete_variable
method of the model:
model.delete_variable(x)
After a variable is deleted, it cannot be used in the model anymore, otherwise an exception will be raised.
We can query whether a variable is active by calling the is_variable_active
method of the model:
is_active = model.is_variable_active(x)