Xpress¶
Initial setup¶
from pyoptinterface import xpress
model = xpress.Model()
You need to follow the instructions in Getting Started to set up the optimizer correctly.
If you want to manage the license of Xpress manually, you can create a xpress.Env object and pass it to the constructor of the xpress.Model object, otherwise we will initialize an implicit global xpress.Env object automatically and use it.
env = xpress.Env()
model = xpress.Model(env)
For users who want to release the license immediately after the optimization, you can call the close method of all models created and the xpress.Env object.
env = xpress.Env()
model = xpress.Model(env)
# do something with the model
model.close()
env.close()
The capability of xpress.Model¶
Supported constraints¶
Supported model attribute¶
Attribute |
Get |
Set |
|---|---|---|
Name |
✅ |
✅ |
ObjectiveSense |
✅ |
✅ |
DualStatus |
✅ |
❌ |
PrimalStatus |
✅ |
❌ |
RawStatusString |
✅ |
❌ |
TerminationStatus |
✅ |
❌ |
BarrierIterations |
✅ |
❌ |
DualObjectiveValue |
✅ |
❌ |
NodeCount |
✅ |
❌ |
NumberOfThreads |
✅ |
✅ |
ObjectiveBound |
✅ |
❌ |
ObjectiveValue |
✅ |
❌ |
RelativeGap |
✅ |
✅ |
Silent |
✅ |
✅ |
SimplexIterations |
✅ |
❌ |
SolverName |
✅ |
❌ |
SolverVersion |
✅ |
❌ |
SolveTimeSec |
✅ |
❌ |
TimeLimitSec |
✅ |
✅ |
Supported variable attribute¶
Attribute |
Get |
Set |
|---|---|---|
Value |
✅ |
❌ |
LowerBound |
✅ |
✅ |
UpperBound |
✅ |
✅ |
Domain |
✅ |
✅ |
PrimalStart |
✅ |
✅ |
Name |
✅ |
✅ |
IISLowerBound |
✅ |
❌ |
IISUpperBound |
✅ |
❌ |
Supported constraint attribute¶
Attribute |
Get |
Set |
|---|---|---|
Name |
✅ |
✅ |
Primal |
✅ |
❌ |
Dual |
✅ |
❌ |
IIS |
✅ |
❌ |
Solver-specific operations¶
Controls and Attributes¶
Xpress uses different terminology than PyOptInterface:
Controls govern the solution procedure and output format (similar to PyOptInterface parameters)
Attributes are read-only properties of the problem and solution
PyOptInterface maps these as follows:
PyOptInterface parameters correspond to Xpress controls
PyOptInterface attributes may access Xpress controls, attributes, or variable/constraint properties through dedicated methods
Controls (Parameters)¶
For solver-specific controls, we provide get_raw_control and set_raw_control methods.
model = xpress.Model()
# Get the value of a control
value = model.get_raw_control("XPRS_TIMELIMIT")
# Set the value of a control
model.set_raw_control("XPRS_TIMELIMIT", 10.0)
Attributes¶
For problem attributes, we provide get_raw_attribute method.
# Get number of columns in the problem
cols = model.get_raw_attribute("XPRS_COLS")
We also provide xpress.XPRS to contain common constants from the Xpress C API.
# Using constants
value = model.get_raw_control_dbl_by_id(xpress.XPRS.TIMELIMIT)
Variable and Constraint Properties¶
Common variable and constraint properties are provided through PyOptInterface dedicated methods:
Variable methods:
Bounds:
set_variable_lowerbound,get_variable_lowerbound,set_variable_upperbound,get_variable_upperbound,set_variable_boundsObjective:
set_objective_coefficient,get_objective_coefficientType and name:
set_variable_type,get_variable_type,set_variable_name,get_variable_nameSolution values:
get_variable_value,get_variable_rc,get_variable_primal_rayBasis status:
is_variable_basic,is_variable_nonbasic_lb,is_variable_nonbasic_ub,is_variable_superbasicIIS information:
is_variable_lowerbound_IIS,is_variable_upperbound_IIS
Constraint methods:
Definition:
set_constraint_sense,get_constraint_sense,set_constraint_rhs,get_constraint_rhs,set_constraint_name,get_constraint_nameCoefficients:
set_normalized_coefficient,get_normalized_coefficient,set_normalized_rhs,get_normalized_rhsSolution values:
get_constraint_dual,get_constraint_slack,get_constraint_dual_rayBasis status:
is_constraint_basic,is_constraint_nonbasic_lb,is_constraint_nonbasic_ub,is_constraint_superbasicIIS information:
is_constraint_in_IIS
Usage examples:
Variable properties:
# Bounds
model.set_variable_lowerbound(variable, 0.0)
lb = model.get_variable_lowerbound(variable)
model.set_variable_upperbound(variable, 10.0)
ub = model.get_variable_upperbound(variable)
# Objective coefficient
model.set_objective_coefficient(variable, 2.0)
coef = model.get_objective_coefficient(variable)
# Type and name
model.set_variable_type(variable, VariableDomain.Integer)
vtype = model.get_variable_type(variable)
model.set_variable_name(variable, "x")
name = model.get_variable_name(variable)
# Solution values
value = model.get_variable_value(variable)
rc = model.get_variable_rc(variable)
ray = model.get_variable_primal_ray(variable)
# Basis status
if model.is_variable_basic(variable):
...
Constraint properties:
# Sense and RHS
model.set_constraint_sense(constraint, ConstraintSense.LessEqual)
sense = model.get_constraint_sense(constraint)
model.set_constraint_rhs(constraint, 5.0)
rhs = model.get_constraint_rhs(constraint)
# Name
model.set_constraint_name(constraint, "c1")
name = model.get_constraint_name(constraint)
# Solution values
dual = model.get_constraint_dual(constraint)
slack = model.get_constraint_slack(constraint)
ray = model.get_constraint_dual_ray(constraint)
# Basis status
if model.is_constraint_basic(constraint):
...