**PuLP in SolverStudio
**PuLP is a modelling environment for building linear and integer programmes within Python.To learn PuLP, read the documentation, visit this tutorial or watch the Datacamp PuLP Video series.

SolverStudio can run PuLP both using the built-in IronPython (via the SolverStudio language “PuLP (IronPython)” — this is the recommended choice) or using your own external (i.e. manually installed outside SolverStudio) Python installation (via the SolverStudio language “Python (external)”. (Read more about external Python in SolverStudio.)

PuLP comes by default with SolverStudio – you do not need to install it.

**Using PuLP with IronPython**

Please see the example below, and the “PuLP Examples.xlsx” example spreadsheet, for samples that illustrate using PuLP. Please also see this SolverStudio IronPython page for more details of IronPython’s ability to access the spreadsheet using the standard Excel objects* Application*, *ActiveSheet* and *ActiveWorkbook*.

**Using PuLP with External Python
**If you run PuLP using Python (External), you need to start your file with:

*from SolverStudio import **

which (1) gives you read and write access to the SovlerStudio Data Items on the sheet, (2) gives you access to the standard Excel objects

*Application*,

*ActiveSheet*and

*ActiveWorkbook*, and (3) adds SolverStudio’s PuLP files the external Python’s path, allowing you to then say:

*from pulp import **

in the normal way. PuLP works with Python 2.7 and 3.x. Don’t forget that you need to install Python, but not PuLP. (When using SolverStudio, your external Python installation will use the PuLP included in SolverStudio.)

**PuLP Solvers under both IronPython and Python (external)
**PuLP supports a range of solvers. Of these, you can use the “_CMD” solvers under both the “PuLP (IronPython)” and “Python (external) languages, including CBC (i.e. COIN_CMD()), Gurobi (GUROBI_CMD()) and Cplex (CPLEX_CMD()). The other solvers, such as GUROBI(), are only available under “Python(external).”

The default PuLP solver is CBC, which is included with SolverStudio. You can solve using CBC with any of the following commands:

1 2 3 4 |
prob.solve() # Solve using PuLP's default Solver (usually CBC) prob.solve(COIN_CMD()) # Solve using CBC prob.solve(COIN_CMD(msg=1)) # Solve using CBC with logging prob.solve(COIN()) # COIN is an alias for COIN_CMD |

Other options for CBC include `COIN_CMD`

(*path=None*, *keepFiles=0*, *mip=1*, *msg=0*, *cuts=None*, *presolve=None*, *dual=None*, *strong=None*, *options=[]*, *fracGap=None*, *maxSeconds=None*, *threads=None*)

If you have installed the Gurobi or Cplex solver, PuLP can use it (under the “PuLP (IronPython)” language) using one of

1 2 3 |
prob.solve(GUROBI_CMD()) # Solve using Gurobi prob.solve(COIN_CMD(msg=1)) # Solve using CBC with logging prob.solve(CPLEX_CMD()) # Solve using Cplex |

The “PuLP Examples.xlsx” workbook (included in the SolverStudio download) has code on the “IronPython Solvers” sheet to list the available solvers.

**PuLP Solvers under Python (External)**

If you run PuLP using the “Python (External)” language (i.e. CPython), you can use any of the installed solvers. The “PuLP Examples.xlsx” workbook (included in the SolverStudio download) has code on the “Python (external) Solvers” sheet to list the available solvers.

**SolverStudio with Cut/Column Generation using DIPPY under Python (External)**

SolverStudio can work with Dippy, the ‘big brother’ of PuLP that allows column and cut generation. Be sure to run under the *Python (External)* language.

**Advanced SolverStudio Usage
**SolverStudio can work with Dippy, the ‘big brother’ of PuLP that allows column and cut generation. Dippy is a joint collaboration between Ted Ralphs and Michael O’Sullivan. Ted has some excellent SolverStudio examples using Dippy on his COIN-Fest 2015 page. Be sure to run under the

*Python (External)*language.

COIN Fest 2015: Workshop on Modeling and the COIN-OR Optimization Suite

**PuLP Resources
**To work with PuLP in SolverStudio, you need some understanding of Python.

The PuLP developer, Stu Mitchell, has some useful resources including:

PuLP on PyPi – the definitive site for installing PuLP

PuLP Package Documentation from PyPi – the definitive documentation

A comprehensive introduction to PuLP;

Tips for efficient Python modelling using PuLP;

Pulp a linear programming interpreter for Python;

PuLP: A Linear Programming Toolkit for Python (PDF paper);

An Introduction to PuLP for Python Programmers (PDF).

See also Pulp and GLPK.

The latest version of PuLP is available on PyPi.

If you have any PuLP questions, please direct them to the PuLP Google Forum.

**PuLP Example using IronPython
**

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
""" The Beer Distribution Problem for the PuLP Modeller Authors: Antony Phillips, Dr Stuart Mitchell 2007 SolverStudio version: Andrew Mason """ # Import PuLP modeller functions from pulp import * # Creates the 'prob' variable to contain the problem data prob = LpProblem("Beer Distribution Problem",LpMinimize) # Creates a list of tuples containing all the possible routes for transport Routes = [(w,b) for w in Warehouses for b in Bars] # A dictionary called 'Vars' is created to contain the referenced variables (the routes) vars = LpVariable.dicts("Route",(Warehouses,Bars),0,None,LpInteger) # The objective function is added to 'prob' first prob += lpSum([vars[w][b]*costs[w,b] for (w,b) in Routes]), "Sum_of_Transporting_Costs" # The supply maximum constraints are added to prob for each supply node (warehouse) for w in Warehouses: prob += lpSum([vars[w][b] for b in Bars])<=supply[w], "Sum_of_Products_out_of_Warehouse_%s"%w # The demand minimum constraints are added to prob for each demand node (bar) for b in Bars: prob += lpSum([vars[w][b] for w in Warehouses])>=demand[b], "Sum_of_Products_into_Bar%s"%b # The problem data is written to an .lp file prob.writeLP("BeerDistributionProblem.lp") # The problem is solved using PuLP's choice of Solver prob.solve(COIN_CMD(msg=1)) # The status of the solution is printed to the screen print "Status:", LpStatus[prob.status] # Each of the variables is printed with it's resolved optimum value for v in prob.variables(): print v.name, "=", v.varValue # The optimised objective function value is printed to the screen print "Total Cost of Transportation = ", value(prob.objective) # Copy values of decision variables into data item "flow" on the sheet for (w,b) in Routes: flow[w,b]=vars[w][b].varValue SolverResult = LpStatus[prob.status] |

Note: To run this under an external CPython, please add “from SolverStudio import *” before the “from pulp import *” line.

## 61 thoughts on “SolverStudio & PuLP”