cylp.py.modeling¶
Module provide a facility to model a linear program. Although the current usage is to pass the resulting LP to CLP to solve it is independant of the solver.
Usage (single dimension, using CyLPModel indirectly)
>>> import numpy as np
>>> from cylp.cy import CyClpSimplex
>>> from cylp.py.modeling.CyLPModel import CyLPArray
>>>
>>> s = CyClpSimplex()
>>>
>>> # Add variables
>>> x = s.addVariable('x', 3)
>>> y = s.addVariable('y', 2)
>>>
>>> # Create coefficients and bounds
>>> A = np.array([[1., 2., 0],[1., 0, 1.]])
>>> B = np.array([[1., 0, 0], [0, 0, 1.]])
>>> D = np.array([[1., 2.],[0, 1]])
>>> a = CyLPArray([5, 2.5])
>>> b = CyLPArray([4.2, 3])
>>> x_u= CyLPArray([2., 3.5])
>>>
>>> # Add constraints
>>> s += A @ x <= a
>>> s += 2 <= B @ x + D @ y <= b
>>> s += y >= 0
>>> s += 1.1 <= x[1:3] <= x_u
>>>
>>> # Set the objective function
>>> c = CyLPArray([1., -2., 3.])
>>> s.objective = c * x + 2 * y.sum()
>>>
>>> # Solve using primal Simplex
>>> s.primal()
'optimal'
>>> s.primalVariableSolution['x']
array([0.2, 2. , 1.1])
>>> s.primalVariableSolution['y']
array([0. , 0.9])
>>> s += x[2] + y[1] >= 2.1
>>> s.primal()
'optimal'
>>> s.primalVariableSolution['x']
array([0. , 2. , 1.1])
>>> s.primalVariableSolution['y']
array([0., 1.])
Usage (single dimension, using CyLPModel directly, depending on the size could be faster than the indirect approach)
>>> import numpy as np
>>> from cylp.cy import CyClpSimplex
>>> from cylp.py.modeling.CyLPModel import CyLPModel, CyLPArray
>>>
>>> model = CyLPModel()
>>>
>>> # Add variables
>>> x = model.addVariable('x', 3)
>>> y = model.addVariable('y', 2)
>>>
>>> # Create coefficients and bounds
>>> A = np.array([[1., 2., 0],[1., 0, 1.]])
>>> B = np.array([[1., 0, 0], [0, 0, 1.]])
>>> D = np.array([[1., 2.],[0, 1]])
>>> a = CyLPArray([5, 2.5])
>>> b = CyLPArray([4.2, 3])
>>> x_u= CyLPArray([2., 3.5])
>>>
>>> # Add constraints
>>> model += A @ x <= a
>>> model += 2 <= B @ x + D @ y <= b
>>> model += y >= 0
>>> model += 1.1 <= x[1:3] <= x_u
>>>
>>> # Set the objective function
>>> c = CyLPArray([1., -2., 3.])
>>> model.objective = c * x + 2 * y.sum()
>>>
>>> # Create a CyClpSimplex instance from the model
>>> s = CyClpSimplex(model)
>>>
>>> # Solve using primal Simplex
>>> s.primal()
'optimal'
>>> s.primalVariableSolution['x']
array([0.2, 2. , 1.1])
>>> s.primalVariableSolution['y']
array([0. , 0.9])
>>> s += x[2] + y[1] >= 2.1
>>> s.primal()
'optimal'
>>> s.primalVariableSolution['x']
array([0. , 2. , 1.1])
>>> s.primalVariableSolution['y']
array([0., 1.])
Usage (multi-dimensions, using CyLPModel indirectly)
>>> from cylp.cy import CyClpSimplex
>>> from cylp.py.modeling.CyLPModel import CyLPArray
>>> s = CyClpSimplex()
>>> x = s.addVariable('x', (5, 3, 6))
>>> s += 2 * x[2, :, 3].sum() + 3 * x[0, 1, :].sum() >= 5
>>> s += 0 <= x <= 1
>>> c = CyLPArray(range(18))
>>> s.objective = c * x[2, :, :] + c * x[0, :, :]
>>> s.primal()
'optimal'
>>> sol = s.primalVariableSolution['x']
>>> abs(sol[0, 1, 0] - 1) <= 10**-6
True
>>> abs(sol[2, 0, 3] - 1) <= 10**-6
True
CyLPVar¶
- class cylp.py.modeling.CyLPModel.CyLPVar(name, dim, isInt=False, fromInd=None, toInd=None)[source]¶
Contains variable information such as its name, dimension, bounds, and whether or not it is an integer. We don’t creat instances directly but rather use
CyLPModel.addVariable()to create one.See the modeling example.
Index a variable as you would a NumPy array to use some of its elements:
an integer selects one element. Negative integers count from the end, so
x[-1]is the last element.a slice selects a range of elements. It can leave out its start or its stop, and have a step, as in
x[:3]orx[::2].a list or an array of integers selects those elements, as in
x[[0, 2]].a variable with several dimensions takes one of these for each dimension, as in
y[1, :]ory[-1, ::2].
An index that is out of range raises
IndexError. Theindicesof the result are the positions of the elements it selects:>>> from cylp.cy import CyClpSimplex >>> s = CyClpSimplex() >>> x = s.addVariable('x', 5) >>> y = s.addVariable('y', (3, 4)) >>> x[::2].indices.tolist() [0, 2, 4] >>> x[-2:].indices.tolist() [3, 4] >>> y[-1, ::2].indices.tolist() [8, 10]
CyLPModel¶
- class cylp.py.modeling.CyLPModel.CyLPModel[source]¶
Hold all the necessary information to create a linear program, i.e. variables, constraints, and the objective function.
See the modeling example.
- addVariable(name, dim, isInt=False)[source]¶
Create a new instance of
CyLPVarusing the given arguments and add it to current model’s variable list.
- addConstraint(cons, consName='', addMpsNames=True)[source]¶
Add constraint
consto theCyLPModel. Argumentconsmust be an expresion made using instances ofCyLPVar,CyLPArray, Numpy matrices, or sparse matrices. It can use normal operators such as>=,<=,==,+,-,*.See the modeling example.
CyLPArray¶
- class cylp.py.modeling.CyLPModel.CyLPArray(input_array, info=None)[source]¶
It is a tweaked Numpy array. It allows user to define objects comparability to an array. If we have an instance of
CyLPVarcalledxand a numpy arrayb, thenb >= xreturns an array of the same dimension asbwith all elements equal toFalse, which has no sense at all. On the constraryCyLPArraywill returnNotImplementedif it doesn’t know how to compare. This is particularly helpful when we define LP constraints.See the modeling example.
IndexFactory¶
- class cylp.py.modeling.CyLPModel.IndexFactory[source]¶
A small class that keeps track of the indices of variables and constraints that you are adding to a problem gradually.
Usage
>>> import numpy as np >>> from cylp.py.modeling.CyLPModel import IndexFactory >>> i = IndexFactory() >>> i.addVar('x', 10) >>> i.addVar('y', 5) >>> i.hasVar('x') True >>> (i.varIndex['y'] == np.arange(10, 15)).all() True >>> i.addConst('Manpower', 4) >>> i.addConst('Gold', 10) >>> (i.constIndex['Manpower'] == np.arange(4)).all() True >>> (i.constIndex['Gold'] == np.arange(4, 14)).all() True