CyClpSimplex is a Cython interface to CLP. Not all methods are available but they are being added gradually.
Its constructor can create an empty object if no argument is provided. However, if a CyLPModel object is given then the resulting CyClpSimplex object will be build from it. For an example of the latter case see CyLP's modeling tool.
An easy example of how to read and solve an LP
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.initialSolve()
'optimal'
Add a constraint to the problem, CLP style. See CLP documentation. Not commonly used in CyLP. For CyLP modeling tool see CyLP.python.modeling.CyLPModel.
Add a variable to the problem, CLP style. See CLP documentation. For CyLP modeling tool see CyLP.python.modeling.CyLPModel.
Delete constraints indexed by which from the LP.
Delete variables indexed by which from the LP.
get the status of a variable
Return type: | int |
---|
set the status of a variable
Adds constraints cons to the problem. Example for the value of cons is 0 <= A * x <= b where A is a Numpy matrix and b is a CyLPArray.
Add number constraints at once, CLP style. For CyLP modeling tool see CyLP.python.modeling.CyLPModel.
Add variable var to the problem.
Add number variables at once, CLP style. For CyLP modeling tool see CyLP.python.modeling.CyLPModel.
The coefficient matrix. A scipy sparse matrix.
Constraints.
Constraints lower bounds
Return type: | Numpy array |
---|
Constraints upper bounds
Return type: | Numpy array |
---|
Take in a character array containing 0-1 specifying whether or not a variable is integer
Return the current point.
Return type: | Numpy array |
---|
Runs CLP dual simplex.
Usage Example
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.dual()
'optimal'
Dual variables’ solution
Return type: | Numpy array |
---|
Variables’ reduced costs
Return type: | Numpy array |
---|
Returns 1 if variable index varInd is flagged.
Return type: | int \(\in \{0, 1\}\) |
---|
The index set of variables that are free or superbasic.
Gets column ncol of A and store it in colArray.
Compute \(A_B^{-1}A_{col}\) and store the result in cl.
Return \(A_B^{-1}_{col}\) and store the result in cl.
Run initialSolve, return a CyCbcModel object that can be used to add cuts, run B&B and ...
Get the status of a constraint.
Return the index set of the basic variables.
Return type: | Numpy array |
---|
Take a spare array, rhs, and store the current right-hand-side in it.
Get the probelm status as defined in CLP. Return value could be:
Return the problem status in string using the code from getStatusCode()
Return the value of useCustomPrimal. See useCustomPrimal().
Return type: | int \(\in \{0, 1\}\) |
---|
Return the variable name. (e.g. that was set in the mps file)
Get the status of a variable.
Run CLP’s initalPrimalSolve. The same as initalSolve() but force the use of dual Simplex.
Usage example
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.initialDualSolve()
'optimal'
>>> round(s.objectiveValue, 4)
2520.5717
Run CLP’s initalPrimalSolve. The same as initalSolve() but force the use of primal Simplex.
Usage example
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.initialPrimalSolve()
'optimal'
>>> round(s.objectiveValue, 4)
2520.5717
Run CLP’s initialSolve. It does a presolve and uses primal or dual Simplex to solve a problem.
Usage example
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getMpsExample
>>> s = CyClpSimplex()
>>> f = getMpsExample()
>>> s.readMps(f)
0
>>> s.initialSolve()
'optimal'
>>> round(s.objectiveValue, 4)
2520.5717
A binary list of size nVariables that specifies whether a variable is integer or not. (ClpModel::integerInformation())
Return type: | Numpy array |
---|
Returns True if the variable index ind is integer.
Number of iterations.
Set the coefficient matrix, constraint bounds, and variable bounds based on the data in cyLPModel which should be and object of CyLPModel class.
This method is usually called from CyClpSimplex’s constructor. But in a case that the CyClpSimplex instance is created before we have the CyLPModel we use this method to load the LP, for example:
>>> import numpy as np
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex, getModelExample
>>>
>>> s = CyClpSimplex()
>>> model = getModelExample()
>>> s.loadFromCyLPModel(model)
>>>
>>> s.primal()
'optimal'
>>> sol_x = s.primalVariableSolution['x']
>>> (abs(sol_x -
... np.array([0.2, 2, 1.1]) ) <= 10**-6).all()
True
The coefficient matrix. A CyCoinPackedMatrix.
Number of columns, variables.
Number of constraints, rows.
Number of rows, constraints.
Number of variables, columns.
The index set of variables that are not basic or fixed.
Set the objective function using this property. See the modeling example.
The objective value. Readonly.
Perform partial pricing from variable start to variable end. Stop when numberWanted variables good variable checked.
Return the index of the constraint corresponding to the (basic) leaving variable.
Return type: | int |
---|
Solve the problem using the primal simplex algorithm. See this usage example.
startFinishOptions is a string containing one or more of the following characters: ‘x’: do not delete work areas ‘f’: use old factorization if possible ‘s’: skip initialization of work areas So one might call self.primal(startFinishOptions='sx')
Slack variables’ solution
Return type: | Numpy array |
---|
Solution to the primal variables.
Return type: | Numpy array |
---|
Solution to the primal variables. Including the slacks.
Return type: | Numpy array |
---|
Read an mps file. See this modeling example.
The reduced costs. A Numpy array.
Return type: | Numpy array |
---|
Removes constraint named name from the problem.
Removes variable named name from the problem.
Resize the problem. After a call to resize the problem will have newNumberRows constraints and newNumberColumns variables.
Return the index of the entering variable.
Return type: | int |
---|
Set the lower bound of variable index ind to val.
Set the upper bound of variable index ind to val.
Set var1 as the complementary variable of var2. These arguments may be integers signifying indices, or CyLPVars.
Set the name of constraint index constInd to name.
Parameters: |
|
---|
Set the status of a constraint.
Parameters: |
|
---|
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex
>>> s = CyClpSimplex()
>>> x = s.addVariable('x', 4)
>>> s.addConstraint(0 <= x[0] + x[1] <= 1, 'const1')
>>> # Using constraint name:
>>> s.setConstraintStatus('const1', 'atUpperBound')
>>> s.getConstraintStatus('const1')
'atUpperBound'
>>> # Using constraint index directly
>>> s.setConstraintStatus(0, 'atLowerBound')
>>> s.getConstraintStatus('const1')
'atLowerBound'
Takes a python object and sets it as the dual pivot rule. dualPivotObjectMethod should implement DualPivotPythonBase. See how to use custom dual Python pivots to solve LPs.
Set variables index varInd flagged.
if arg is an integer: mark variable index arg as integer. if arg is a CyLPVar object: mark variable arg as integer. Here is an example of the latter:
>>> import numpy as np
>>> from CyLP.cy import CyClpSimplex
>>> from CyLP.py.modeling.CyLPModel import CyLPModel, CyLPArray
>>> model = CyLPModel()
>>>
>>> x = model.addVariable('x', 3)
>>> y = model.addVariable('y', 2)
>>>
>>> A = np.matrix([[1., 2., 0],[1., 0, 1.]])
>>> B = np.matrix([[1., 0, 0], [0, 0, 1.]])
>>> D = np.matrix([[1., 2.],[0, 1]])
>>> a = CyLPArray([5, 2.5])
>>> b = CyLPArray([4.2, 3])
>>> x_u= CyLPArray([2., 3.5])
>>>
>>> model += A*x <= a
>>> model += 2 <= B * x + D * y <= b
>>> model += y >= 0
>>> model += 1.1 <= x[1:3] <= x_u
>>>
>>> c = CyLPArray([1., -2., 3.])
>>> model.objective = c * x + 2 * y.sum()
>>>
>>>
>>> s = CyClpSimplex(model)
>>> s.setInteger(x[1:3])
>>>
>>> cbcModel = s.getCbcModel()
>>> cbcModel.branchAndBound()
'solution'
>>>
>>> sol_x = cbcModel.primalVariableSolution['x']
>>> (abs(sol_x -
... np.array([0.5, 2, 2]) ) <= 10**-6).all()
True
>>> sol_y = cbcModel.primalVariableSolution['y']
>>> (abs(sol_y -
... np.array([0, 0.75]) ) <= 10**-6).all()
True
Set the objective coefficients using sparse vector elements elementIndex and elementValue.
Perturb the problem by value.
Takes a python object and sets it as the primal simplex pivot rule. pivotObjectMethod should implement PivotPythonBase. See how to use custom Python pivots to solve LPs.
Set the v‘th variable of the basis as the leaving variable.
Set primal simplex’s pivot rule to the Cython implementation of positive edge
Set primal simplex’s pivot rule to the Cython implementation of Wolfe’s rule used to solve QPs.
Set the lower bound of constraint index ind to val.
Set the upper bound of constraint index ind to val.
Set the variable index v as the entering variable.
Set the name of variable index varInd to name.
Parameters: |
|
---|
Set the status of a variable.
Parameters: |
|
---|
Example:
>>> from CyLP.cy.CyClpSimplex import CyClpSimplex
>>> s = CyClpSimplex()
>>> x = s.addVariable('x', 4)
>>> # Using CyLPVars:
>>> s.setVariableStatus(x[1:3], 'basic')
>>> s.getVariableStatus(x[1])
'basic'
>>> # Using a variable index directly
>>> s.setVariableStatus(1, 'atLowerBound')
>>> s.getVariableStatus(x[1])
'atLowerBound'
Return the current point.
Return type: | Numpy array |
---|
A Numpy array of all the variables’ status
Compute \(x * scalar * A + y\) and store the result in z.
Compute \(y_{which} - pi^{T}A_{which}\) where which is a variable index set. Store the result in y.
Same as transposeTimesSubset() but here which can also address slack variables.
Determines if CyLP.python.pivot.PivotPythonBase.isPivotAcceptable() should be called just before each pivot is performed (right after the entering and leaving variables are obtained.
The index set of variables that are at their lower bound.
The index set of variables that are at their upper bound.
The index set of variables that are basic.
The index set of variables that are fixed.
The index set of variables that are flagged.
The index set of variables that are free.
The index set of variables that are superbasic.
The index set of variables that are NOT at their lower bound.
The index set of variables that are NOT at their upper bound.
The index set of variables that are NOT basic.
The index set of variables that are NOT fixed.
The index set of variables that are NOT flagged.
The index set of variables that are NOT free.
The index set of variables that are NOT superbasic.
variable names
Variables.
Variables lower bounds
Return type: | Numpy array |
---|
Variables upper bounds
Return type: | Numpy array |
---|
Compute \(vec A_B^{-1}\) and store it in vec.