LP and MILP API Reference#
- class cuopt.linear_programming.problem.Problem(model_name='')#
A Problem defines a Linear Program or Mixed Integer Program Variable can be be created by calling addVariable() Constraints can be added by calling addConstraint() The objective can be set by calling setObjective() The problem data is formed when calling solve().
- Parameters:
- model_namestr, optional
Name of the model. Default is an empty string.
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=-2.0, ub=8.0, vtype=INTEGER) >>> y = problem.addVariable(name="Var2") >>> problem.addConstraint(2*x - 3*y <= 10, name="Constr1") >>> expr = 3*x + y >>> problem.addConstraint(expr + x == 20, name="Constr2") >>> problem.setObjective(x + y, sense=MAXIMIZE) >>> problem.solve()
Attributes
Name
(str) Name of the model.
ObjSense
(sense) Objective sense (MINIMIZE or MAXIMIZE).
ObjConstant
(float) Constant term in the objective.
Status
(int) Status of the problem after solving.
SolveTime
(float) Time taken to solve the problem.
SolutionStats
(object) Solution statistics for LP or MIP problem.
ObjValue
(float) Objective value of the problem.
IsMIP
(bool) Indicates if the problem is a Mixed Integer Program.
NumVariables
(int) Number of Variables in the problem.
NumConstraints
(int) Number of constraints in the problem.
NumNZs
(int) Number of non-zeros in the problem.
Methods
addConstraint
(constr[, name])Adds a constraint to the problem defined by constraint object and name.
addVariable
([lb, ub, obj, vtype, name])Adds a variable to the problem defined by lower bound, upper bound, type and name.
getCSR
()Computes and returns the CSR representation of the constraint matrix.
getConstraint
(identifier)Get a Constraint by its index or name.
Get a list of all the Constraints in a problem.
Get the Objective expression of the problem.
getVariable
(identifier)Get a Variable by its index or name.
Get a list of all the variables in the problem.
get_incumbent_values
(solution, vars)Extract incumbent values of the vars from a problem solution.
Note: Applicable to only LP.
readMPS
(mps_file)Initiliaze a problem from an MPS file.
relax
()Relax a MIP problem into an LP problem and return the relaxed model.
setObjective
(expr[, sense])Set the Objective of the problem with an expression that needs to be MINIMIZED or MAXIMIZED.
solve
([settings])Optimizes the LP or MIP problem with the added variables, constraints and objective.
update
()Update the problem.
updateConstraint
(constr[, coeffs, rhs])Updates a previously added constraint.
updateObjective
([coeffs, constant, sense])Updates the objective of the problem.
writeMPS
(mps_file)Write the problem into an MPS file.
- property Obj#
- addConstraint(constr, name='')#
Adds a constraint to the problem defined by constraint object and name. A constraint is generated using LinearExpression, Sense and RHS.
- Parameters:
- constr
Constraint
Constructed using LinearExpressions (See Examples)
- namestring
Name of the variable. Optional.
- constr
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=-2.0, ub=8.0, vtype=INTEGER) >>> y = problem.addVariable(name="Var2") >>> problem.addConstraint(2*x - 3*y <= 10, name="Constr1") >>> expr = 3*x + y >>> problem.addConstraint(expr + x == 20, name="Constr2")
- addVariable(
- lb=0.0,
- ub=inf,
- obj=0.0,
- vtype=VType.CONTINUOUS,
- name='',
Adds a variable to the problem defined by lower bound, upper bound, type and name.
- Parameters:
- lbfloat
Lower bound of the variable. Defaults to 0.
- ubfloat
Upper bound of the variable. Defaults to infinity.
- vtypeenum
VType
vtype.CONTINUOUS or vtype.INTEGER. Defaults to CONTINUOUS.
- namestring
Name of the variable. Optional.
- Returns:
- variable
Variable
Variable object added to the problem.
- variable
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=-2.0, ub=8.0, vtype=INTEGER, name="Var1")
- getCSR()#
Computes and returns the CSR representation of the constraint matrix.
- getConstraint(identifier)#
Get a Constraint by its index or name.
- getConstraints()#
Get a list of all the Constraints in a problem.
- getObjective()#
Get the Objective expression of the problem.
- getVariable(identifier)#
Get a Variable by its index or name.
- getVariables()#
Get a list of all the variables in the problem.
- get_incumbent_values(solution, vars)#
Extract incumbent values of the vars from a problem solution.
- get_pdlp_warm_start_data()#
Note: Applicable to only LP. Allows to retrieve the warm start data from the PDLP solver once the problem is solved. This data can be used to warmstart the next PDLP solve by setting it in
cuopt.linear_programming.solver_settings.SolverSettings.set_pdlp_warm_start_data()
# noqaExamples
>>> problem = problem.Problem.readMPS("LP.mps") >>> problem.solve() >>> warmstart_data = problem.get_pdlp_warm_start_data() >>> settings.set_pdlp_warm_start_data(warmstart_data) >>> updated_problem = problem.Problem.readMPS("updated_LP.mps") >>> updated_problem.solve(settings)
- classmethod readMPS(mps_file)#
Initiliaze a problem from an MPS file. # noqa
Examples
>>> problem = problem.Problem.readMPS("model.mps")
- relax()#
Relax a MIP problem into an LP problem and return the relaxed model. The relaxed model has all variable types set to CONTINUOUS.
Examples
>>> mip_problem = problem.Problem.readMPS("MIP.mps") >>> lp_problem = problem.relax()
- setObjective(expr, sense=sense.MINIMIZE)#
Set the Objective of the problem with an expression that needs to be MINIMIZED or MAXIMIZED.
- Parameters:
- expr
LinearExpression
orVariable
or Constant Objective expression that needs maximization or minimization.
- senseenum
sense
Sets whether the problem is a maximization or a minimization problem. Values passed can either be MINIMIZE or MAXIMIZE. Defaults to MINIMIZE.
- expr
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=-2.0, ub=8.0, vtype=INTEGER) >>> y = problem.addVariable(name="Var2") >>> problem.addConstraint(2*x - 3*y <= 10, name="Constr1") >>> expr = 3*x + y >>> problem.addConstraint(expr + x == 20, name="Constr2") >>> problem.setObjective(x + y, sense=MAXIMIZE)
- solve(
- settings=<cuopt.linear_programming.solver_settings.solver_settings.SolverSettings object>,
Optimizes the LP or MIP problem with the added variables, constraints and objective.
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=-2.0, ub=8.0, vtype=INTEGER) >>> y = problem.addVariable(name="Var2") >>> problem.addConstraint(2*x - 3*y <= 10, name="Constr1") >>> expr = 3*x + y >>> problem.addConstraint(expr + x == 20, name="Constr2") >>> problem.setObjective(x + y, sense=MAXIMIZE) >>> problem.solve()
- update()#
Update the problem. This is mandatory if attributes of existing Variables, Constraints or Objective has been modified.
- updateConstraint(constr, coeffs=[], rhs=None)#
Updates a previously added constraint. Values that can be updated are constraint coefficients and RHS.
- Parameters:
- constr
Constraint
Constraint to be updated.
- coeffsList[Tuple[
Variable
, coefficient]] List of Tuples containing variable and corresponding coefficient. Optional.
- rhsint|float
New RHS value for the constraint.
- constr
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=0.0, vtype=INTEGER) >>> y = problem.addVariable(lb=0.0, vtype=INTEGER) >>> c1 = problem.addConstraint(2 * x + y <= 7, name="c1") >>> c2 = problem.addConstraint(x + y <= 5, name="c2") >>> problem.updateConstraint(c1, coeffs=[(x, 1)], rhs=10)
- updateObjective(coeffs=[], constant=None, sense=None)#
Updates the objective of the problem. Values that can be updated are objective coefficients, constant and sense.
- Parameters:
Examples
>>> problem = problem.Problem("MIP_model") >>> x = problem.addVariable(lb=0.0, vtype=INTEGER) >>> y = problem.addVariable(lb=0.0, vtype=INTEGER) >>> problem.setObjective(4*x + y + 4, MAXIMIZE) >>> problem.updateObjective(coeffs=[(x1, 1.0), (x2, 3.0)], constant=5, sense=MINIMIZE)
- class cuopt.linear_programming.problem.Variable(lb=0.0, ub=inf, obj=0.0, vtype=VType.CONTINUOUS, vname='')#
cuOpt variable object initialized with details of the variable such as lower bound, upper bound, type and name. Variables are always associated with a problem and can be created using
Problem.addVariable()
.- Parameters:
- lbfloat
Lower bound of the variable. Defaults to 0.
- ubfloat
Upper bound of the variable. Defaults to infinity.
- vtypeenum
CONTINUOUS or INTEGER. Defaults to CONTINUOUS.
- objfloat
Coefficient of the Variable in the objective.
- namestr
Name of the variable. Optional.
Attributes
VariableName
(str) Name of the Variable.
VariableType
(CONTINUOUS or INTEGER) Variable type.
LB
(float) Lower Bound of the Variable.
UB
(float) Upper Bound of the Variable.
Obj
(float) Coefficient of the variable in the Objective function.
Value
(float) Value of the variable after solving.
ReducedCost
(float) Reduced Cost after solving an LP problem.
Methods
getIndex
()Get the index position of the variable in the problem.
Returns the lower bound of the variable.
Returns the objective coefficient of the variable.
Returns the upper bound of the variable.
getValue
()Returns the Value of the variable computed in current solution.
Returns the name of the variable.
Returns the type of the variable.
setLowerBound
(val)Sets the lower bound of the variable.
Sets the objective cofficient of the variable.
setUpperBound
(val)Sets the upper bound of the variable.
setVariableName
(val)Sets the name of the variable.
setVariableType
(val)Sets the variable type of the variable.
- getIndex()#
Get the index position of the variable in the problem.
- getLowerBound()#
Returns the lower bound of the variable.
- getObjectiveCoefficient()#
Returns the objective coefficient of the variable.
- getUpperBound()#
Returns the upper bound of the variable.
- getValue()#
Returns the Value of the variable computed in current solution. Defaults to 0
- getVariableName()#
Returns the name of the variable.
- getVariableType()#
Returns the type of the variable.
- setLowerBound(val)#
Sets the lower bound of the variable.
- setObjectiveCoefficient(val)#
Sets the objective cofficient of the variable.
- setUpperBound(val)#
Sets the upper bound of the variable.
- setVariableName(val)#
Sets the name of the variable.
- setVariableType(val)#
Sets the variable type of the variable. Variable types can be either CONTINUOUS or INTEGER.
- class cuopt.linear_programming.problem.LinearExpression(vars, coefficients, constant)#
LinearExpressions contain a set of variables, the coefficients for the variables, and a constant. LinearExpressions can be used to create constraints and the objective in the Problem. LinearExpressions can be added and subtracted with other LinearExpressions and Variables and can also be multiplied and divided by scalars. LinearExpressions can be compared with scalars, Variables, and other LinearExpressions to create Constraints.
- Parameters:
- varsList
List of Variables in the linear expression.
- coefficientsList
List of coefficients corresponding to the variables.
- constantfloat
Constant of the linear expression.
Methods
Gets the coefficient of the variable at ith index of the linear expression.
Returns all the coefficients in the linear expression.
Returns the constant in the linear expression.
getValue
()Returns the value of the expression computed with the current solution.
getVariable
(i)Gets Variable at ith index in the linear expression.
Returns all the variables in the linear expression.
zipVarCoefficients
- getCoefficient(i)#
Gets the coefficient of the variable at ith index of the linear expression.
- getCoefficients()#
Returns all the coefficients in the linear expression.
- getConstant()#
Returns the constant in the linear expression.
- getValue()#
Returns the value of the expression computed with the current solution.
- getVariable(i)#
Gets Variable at ith index in the linear expression.
- getVariables()#
Returns all the variables in the linear expression.
- zipVarCoefficients()#
- class cuopt.linear_programming.problem.Constraint(expr, sense, rhs, name='')#
cuOpt constraint object containing a linear expression, the sense of the constraint, and the right-hand side of the constraint. Constraints are associated with a problem and can be created using
Problem.addConstraint()
.- Parameters:
- exprLinearExpression
Linear expression corresponding to a problem.
- senseenum
Sense of the constraint. Either LE for <=, GE for >= or EQ for == .
- rhsfloat
Constraint right-hand side value.
- namestr, Optional
Name of the constraint. Optional.
Attributes
ConstraintName
(str) Name of the constraint.
Sense
(LE, GE or EQ) Row sense. LE for >=, GE for <= or EQ for == .
RHS
(float) Constraint right-hand side value.
Slack
(float) Computed LHS - RHS with current solution.
DualValue
(float) Constraint dual value in the current solution.
Methods
getCoefficient
(var)Returns the coefficient of a variable in the constraint.
Returns the name of the constraint.
getRHS
()Returns the right-hand side value of the constraint.
getSense
()Returns the sense of the constraint.
- getCoefficient(var)#
Returns the coefficient of a variable in the constraint.
- getConstraintName()#
Returns the name of the constraint.
- getRHS()#
Returns the right-hand side value of the constraint.
- getSense()#
Returns the sense of the constraint. Constraint sense can be LE(<=), GE(>=) or EQ(==).
- class cuopt.linear_programming.solver_settings.SolverSettings#
Methods
Return callback class object
get_parameter
(name)Get the value of a parameter used by cuOpt's LP/MIP solvers.
Returns the warm start data.
set_mip_callback
(callback)Note: Only supported for MILP
set_optimality_tolerance
(eps_optimal)NOTE: Not supported for MILP, absolute is fixed to 1e-4,
set_parameter
(name, value)Set the value of a parameter used by cuOpt's LP/MIP solvers.
set_pdlp_warm_start_data
(pdlp_warm_start_data)Set the pdlp warm start data.
- get_mip_callbacks()#
Return callback class object
- get_parameter(name)#
Get the value of a parameter used by cuOpt’s LP/MIP solvers.
- Parameters:
- namestr
The name of the parameter to get.
- Returns:
- valuefloat, int, bool, or str
The value of the parameter.
Notes
For a list of availabe parameters, their descriptions, default values, and acceptable ranges, see the cuOpt documentation parameter.rst.
- get_pdlp_warm_start_data()#
Returns the warm start data. See set_pdlp_warm_start_data for more details.
- Returns:
- pdlp_warm_start_data:
- set_mip_callback(callback)#
Note: Only supported for MILP
Set the callback to receive incumbent solution.
- Parameters:
- callbackclass for function callback
Callback class that inherits from GetSolutionCallback or SetSolutionCallback.
Examples
>>> # Callback for incumbent solution >>> class CustomGetSolutionCallback(GetSolutionCallback): >>> def __init__(self): >>> super().__init__() >>> self.n_callbacks = 0 >>> self.solutions = [] >>> >>> def get_solution(self, solution, solution_cost): >>> self.n_callbacks += 1 >>> assert len(solution) > 0 >>> assert len(solution_cost) == 1 >>> >>> self.solutions.append( >>> { >>> "solution": solution.copy_to_host(), >>> "cost": solution_cost.copy_to_host()[0], >>> } >>> ) >>> >>> class CustomSetSolutionCallback(SetSolutionCallback): >>> def __init__(self, get_callback): >>> super().__init__() >>> self.n_callbacks = 0 >>> self.get_callback = get_callback >>> >>> def set_solution(self, solution, solution_cost): >>> self.n_callbacks += 1 >>> if self.get_callback.solutions: >>> solution[:] = >>> self.get_callback.solutions[-1]["solution"] >>> solution_cost[0] = float( >>> self.get_callback.solutions[-1]["cost"] >>> ) >>> >>> get_callback = CustomGetSolutionCallback() >>> set_callback = CustomSetSolutionCallback(get_callback) >>> settings.set_mip_callback(get_callback) >>> settings.set_mip_callback(set_callback)
- set_optimality_tolerance(eps_optimal)#
NOTE: Not supported for MILP, absolute is fixed to 1e-4,
Set both absolute and relative tolerance on the primal feasibility, dual feasibility, and gap. Changing this value has a significant impact on accuracy and runtime.
Optimality is computed as follows:
- dual_feasibility < absolute_dual_tolerance + relative_dual_tolerance
norm_objective_coefficient (l2_norm(c))
- primal_feasibility < absolute_primal_tolerance
relative_primal_tolerance * norm_constraint_bounds (l2_norm(b))
- duality_gap < absolute_gap_tolerance + relative_gap_tolerance
(abs(primal_objective) + abs(dual_objective))
If all three conditions hold, optimality is reached.
- Parameters:
- eps_optimalfloat64
Tolerance to optimality
Notes
Default value is 1e-4. To set each absolute and relative tolerance, use the provided setters.
- set_parameter(name, value)#
Set the value of a parameter used by cuOpt’s LP/MIP solvers.
- Parameters:
- namestr
The name of the parameter to set.
- valuestr
The value the parameter should take.
- For a list of availabe parameters, their descriptions, default values,
- and acceptable ranges, see the cuOpt documentation `parameter.rst`.
- set_pdlp_warm_start_data(pdlp_warm_start_data)#
Set the pdlp warm start data. This allows to restart PDLP with a previous solution context.
This should be used when you solve a new problem which is similar to the previous one.
- Parameters:
- pdlp_warm_start_dataPDLPWarmStartData
PDLP warm start data obtained from a previous solve. Refer
cuopt.linear_programming.problem.Problem.get_pdlp_warm_start_data()
# noqa
Notes
For now, the problem must have the same number of variables and constraints as the one found in the previous solution.
Only supported solver modes are Stable2 and Fast1.
Examples
>>> settings.set_pdlp_warm_start_data(pdlp_warm_start_data)
- class cuopt.linear_programming.problem.VType(*values)#
The type of a variable is either continuous or integer. Variable Types can be directly used as a constant. CONTINUOUS is VType.CONTINUOUS INTEGER is VType.INTEGER
- CONTINUOUS = 'C'#
- INTEGER = 'I'#