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.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])
>>>
>>> # 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.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])
>>>
>>> # 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.

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 CyLPVar using the given arguments and add it to current model’s variable list.

addConstraint(cons, consName='', addMpsNames=True)[source]

Add constraint cons to the CyLPModel. Argument cons must be an expresion made using instances of CyLPVar, CyLPArray, Numpy matrices, or sparse matrices. It can use normal operators such as >=, <=, ==, +, -, *.

See the modeling example.

CyLPArray

class cylp.py.modeling.CyLPModel.CyLPArray[source]

It is a tweaked Numpy array. It allows user to define objects comparability to an array. If we have an instance of CyLPVar called x and a numpy array b, then b >= x returns an array of the same dimension as b with all elements equal to False, which has no sense at all. On the constrary CyLPArray will return NotImplemented if 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
reverseVarSearch(ind)[source]

Take an index and return the corresponding variable name.