{"id":213,"date":"2012-09-17T02:24:58","date_gmt":"2012-09-17T02:24:58","guid":{"rendered":"http:\/\/solverstudio.org\/?page_id=213"},"modified":"2019-10-15T22:09:21","modified_gmt":"2019-10-15T22:09:21","slug":"pulp","status":"publish","type":"page","link":"https:\/\/solverstudio.org\/languages\/pulp\/","title":{"rendered":"SolverStudio &#038; PuLP"},"content":{"rendered":"<p><strong>PuLP in SolverStudio<br \/>\n<\/strong>PuLP is a modelling environment for building linear and integer programmes within Python.To learn PuLP, <a href=\"https:\/\/pypi.org\/project\/PuLP\/\">read the documentation<\/a>, visit <a href=\"https:\/\/pythonhosted.org\/PuLP\/\">this tutorial<\/a> or watch the <a href=\"https:\/\/www.datacamp.com\/courses\/supply-chain-analytics-in-python\">Datacamp PuLP Video series<\/a>.<\/p>\n<p>SolverStudio can run PuLP both using the built-in IronPython (via the SolverStudio language &#8220;PuLP (IronPython)&#8221; &#8212; this is the recommended choice) or using your own external (i.e. manually installed outside SolverStudio) Python installation (via the SolverStudio language &#8220;Python (external)&#8221;. (<a href=\"http:\/\/solverstudio.org\/cpython\/\">Read more about external Python in SolverStudio<\/a>.)<\/p>\n<p>PuLP comes by default with SolverStudio &#8211; you do not need to install it.<\/p>\n<p><strong>Using PuLP with IronPython<\/strong><br \/>\nPlease see the example below, and the &#8220;PuLP Examples.xlsx&#8221; example spreadsheet, for samples that illustrate using PuLP. Please also see <a title=\"SolverStudio &amp; IronPython\" href=\"http:\/\/solverstudio.org\/ironpython\/\">this SolverStudio IronPython page<\/a> for more details of IronPython&#8217;s ability to access the spreadsheet using the standard Excel objects<em> <a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ff194565.aspx\">Application<\/a><\/em>, <em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174742%28v=office.11%29.aspx\">ActiveSheet<\/a><\/em> and <em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174744%28v=office.11%29.aspx\">ActiveWorkbook<\/a><\/em>.<\/p>\n<p><strong>Using PuLP with External Python<br \/>\n<\/strong>If you run PuLP using Python (External), you need to start your file with:<br \/>\n<em>from SolverStudio import *<\/em><br \/>\nwhich (1) gives you read and write access to the SovlerStudio Data Items on the sheet, (2) gives you access to the standard Excel objects\u00a0 <em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/ff194565.aspx\">Application<\/a><\/em>, <em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174742%28v=office.11%29.aspx\">ActiveSheet<\/a><\/em> and <em><a href=\"https:\/\/msdn.microsoft.com\/en-us\/library\/office\/aa174744%28v=office.11%29.aspx\">ActiveWorkbook<\/a><\/em>, and (3) adds SolverStudio&#8217;s PuLP files the external Python&#8217;s path, allowing you to then say:<br \/>\n<em>from pulp import *<\/em><br \/>\nin the normal way. PuLP works with Python 2.7 and 3.x. Don&#8217;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.)<\/p>\n<p><strong>PuLP Solvers under both IronPython and Python (external)<br \/>\n<\/strong>PuLP supports a <a href=\"http:\/\/pythonhosted.org\/PuLP\/solvers.html\" target=\"_blank\" rel=\"noopener noreferrer\">range of solvers<\/a>. Of these, you can use the &#8220;_CMD&#8221; solvers under both the &#8220;PuLP (IronPython)&#8221; and &#8220;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 &#8220;Python(external).&#8221;<\/p>\n<p>The default PuLP solver is CBC, which is included with SolverStudio. You can solve using CBC with any of the following commands:<\/p>\n<pre class=\"lang:python decode:true\" title=\"PuLP Example\">prob.solve() # Solve using PuLP's default Solver (usually CBC)\nprob.solve(COIN_CMD()) # Solve using CBC\nprob.solve(COIN_CMD(msg=1)) # Solve using CBC with logging\nprob.solve(COIN()) # COIN is an alias for COIN_CMD\n<\/pre>\n<p>Other options for CBC include <code class=\"descname\">COIN_CMD<\/code><span class=\"sig-paren\">(<\/span><em>path=None<\/em>, <em>keepFiles=0<\/em>, <em>mip=1<\/em>, <em>msg=0<\/em>, <em>cuts=None<\/em>, <em>presolve=None<\/em>, <em>dual=None<\/em>, <em>strong=None<\/em>, <em>options=[]<\/em>, <em>fracGap=None<\/em>, <em>maxSeconds=None<\/em>, <em>threads=None<\/em><span class=\"sig-paren\">)<\/span><\/p>\n<p>If you have installed the <a href=\"http:\/\/www.gurobi.com\/\" target=\"_blank\" rel=\"noopener noreferrer\">Gurobi<\/a> or <a href=\"http:\/\/www-01.ibm.com\/software\/commerce\/optimization\/cplex-optimizer\/\" target=\"_blank\" rel=\"noopener noreferrer\">Cplex<\/a> solver, PuLP can use it (under the &#8220;PuLP (IronPython)&#8221; language) using one of<\/p>\n<pre class=\"lang:python decode:true\" title=\"PuLP Example\">prob.solve(GUROBI_CMD()) # Solve using Gurobi\nprob.solve(COIN_CMD(msg=1)) # Solve using CBC with logging\nprob.solve(CPLEX_CMD()) # Solve using Cplex\n<\/pre>\n<p>The &#8220;PuLP Examples.xlsx&#8221; workbook (included in the SolverStudio download) has code on the &#8220;IronPython Solvers&#8221; sheet to list the available solvers.<\/p>\n<p><strong>PuLP Solvers under Python (External)<\/strong><br \/>\nIf you run PuLP using the &#8220;Python (External)&#8221; language (i.e. CPython), you can use any of the installed solvers. The &#8220;PuLP Examples.xlsx&#8221; workbook (included in the SolverStudio download) has code on the &#8220;Python (external) Solvers&#8221; sheet to list the available solvers.<\/p>\n<p><strong>SolverStudio with Cut\/Column Generation using DIPPY under Python (External)<\/strong><br \/>\nSolverStudio can work with <a href=\"https:\/\/projects.coin-or.org\/Dip\/wiki\/DipPy\">Dippy<\/a>, the &#8216;big brother&#8217; of PuLP that allows column and cut generation. Be sure to run under the <em>Python (External)<\/em> language.<\/p>\n<p><strong>Advanced SolverStudio Usage<br \/>\n<\/strong>SolverStudio can work with <a href=\"https:\/\/projects.coin-or.org\/Dip\/wiki\/DipPy\">Dippy<\/a>, the &#8216;big brother&#8217; of PuLP that allows column and cut generation. Dippy is a joint collaboration between Ted Ralphs and Michael O&#8217;Sullivan. Ted has some excellent SolverStudio examples using Dippy on his <a href=\"http:\/\/coral.ie.lehigh.edu\/~ted\/teaching\/coin-or\/\">COIN-Fest 2015 page<\/a>. Be sure to run under the <em>Python (External)<\/em> language.<\/p>\n<blockquote data-secret=\"8pwsj0m2Zi\" class=\"wp-embedded-content\"><p><a href=\"http:\/\/coral.ie.lehigh.edu\/~ted\/teaching\/coin-or\/\">COIN Fest 2015: Workshop on Modeling and the COIN-OR Optimization Suite<\/a><\/p><\/blockquote>\n<p><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" src=\"http:\/\/coral.ie.lehigh.edu\/~ted\/teaching\/coin-or\/embed\/#?secret=8pwsj0m2Zi\" data-secret=\"8pwsj0m2Zi\" width=\"600\" height=\"338\" title=\"&#8220;COIN Fest 2015: Workshop on Modeling and the COIN-OR Optimization Suite&#8221; &#8212; Ted Ralphs\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe><\/p>\n<p><strong>PuLP Resources<br \/>\n<\/strong>To work with <a href=\"http:\/\/packages.python.org\/PuLP\/\" target=\"_blank\" rel=\"noopener noreferrer\">PuLP<\/a> in SolverStudio, you need some understanding of <a title=\"Python Programming Language \u2013 Official Website\" href=\"http:\/\/python.org\/\" target=\"_blank\" rel=\"noopener noreferrer\">Python<\/a>.<\/p>\n<p>The PuLP developer, Stu Mitchell, has some useful resources including:<br \/>\n<a href=\"https:\/\/pypi.python.org\/pypi\/PuLP\">PuLP on PyPi<\/a> &#8211; the definitive site for installing PuLP<br \/>\n<a href=\"http:\/\/pythonhosted.org\/PuLP\/\">PuLP Package Documentation from PyPi<\/a> &#8211; the definitive documentation<a href=\"http:\/\/www.coin-or.org\/PuLP\/\"><br \/>\n<\/a><a href=\"http:\/\/www.coin-or.org\/PuLP\/\">A comprehensive introduction to PuLP<\/a>; <a href=\"http:\/\/www.stuartmitchell.com\/journal\/2012\/2\/3\/my-top-n-tips-for-python-coding-in-optimisation-1.html\"><br \/>\nTips for efficient Python modelling using PuLP<\/a>; <a href=\"https:\/\/launchpad.net\/pulp-or\"><br \/>\nPulp a linear programming interpreter for Python<\/a>;<br \/>\n<a href=\"http:\/\/www.optimization-online.org\/DB_FILE\/2011\/09\/3178.pdf\">PuLP: A Linear Programming Toolkit for Python<\/a> (PDF paper); <a href=\"http:\/\/ojs.pythonpapers.org\/index.php\/tppm\/article\/download\/111\/112\" target=\"_blank\" rel=\"noopener noreferrer\"><br \/>\nAn Introduction to PuLP for Python Programmers<\/a> (PDF).<\/p>\n<p>See also <a href=\"http:\/\/www.slideshare.net\/sucha\/tutorial-python-pulp-and-glpk\">Pulp and GLPK<\/a>.<\/p>\n<p>The latest version of PuLP is available on <a href=\"https:\/\/pypi.python.org\/pypi\/PuLP\/\" target=\"_blank\" rel=\"noopener noreferrer\">PyPi<\/a>.<\/p>\n<p>If you have any PuLP questions, please direct them to the <a href=\"https:\/\/groups.google.com\/forum\/#!forum\/pulp-or-discuss\">PuLP Google Forum<\/a>.<\/p>\n<p><strong>PuLP Example using IronPython<br \/>\n<\/strong><\/p>\n<pre class=\"lang:python decode:true\" title=\"PuLP Example\">\"\"\"\nThe Beer Distribution Problem for the PuLP Modeller\nAuthors: Antony Phillips, Dr Stuart Mitchell  2007\nSolverStudio version: Andrew Mason\n\"\"\"\n# Import PuLP modeller functions\nfrom pulp import *\n\n# Creates the 'prob' variable to contain the problem data\nprob = LpProblem(\"Beer Distribution Problem\",LpMinimize)\n\n# Creates a list of tuples containing all the possible routes for transport\nRoutes = [(w,b) for w in Warehouses for b in Bars]\n\n# A dictionary called 'Vars' is created to contain the referenced variables (the routes)\nvars = LpVariable.dicts(\"Route\",(Warehouses,Bars),0,None,LpInteger)\n\n# The objective function is added to 'prob' first\nprob += lpSum([vars[w][b]*costs[w,b] for (w,b) in Routes]), \"Sum_of_Transporting_Costs\"\n\n# The supply maximum constraints are added to prob for each supply node (warehouse)\nfor w in Warehouses:\n    prob += lpSum([vars[w][b] for b in Bars])&lt;=supply[w], \"Sum_of_Products_out_of_Warehouse_%s\"%w\n\n# The demand minimum constraints are added to prob for each demand node (bar)\nfor b in Bars:\n    prob += lpSum([vars[w][b] for w in Warehouses])&gt;=demand[b], \"Sum_of_Products_into_Bar%s\"%b\n                   \n# The problem data is written to an .lp file\nprob.writeLP(\"BeerDistributionProblem.lp\")\n\n# The problem is solved using PuLP's choice of Solver\nprob.solve(COIN_CMD(msg=1)) \n\n# The status of the solution is printed to the screen\nprint \"Status:\", LpStatus[prob.status]\n\n# Each of the variables is printed with it's resolved optimum value\nfor v in prob.variables():\n    print v.name, \"=\", v.varValue\n\n# The optimised objective function value is printed to the screen    \nprint \"Total Cost of Transportation = \", value(prob.objective)\n\n# Copy values of decision variables into data item \"flow\" on the sheet\nfor (w,b) in Routes:\n  flow[w,b]=vars[w][b].varValue\n\nSolverResult = LpStatus[prob.status]<\/pre>\n<p>Note: To run this under an external CPython, please add &#8220;from SolverStudio import *&#8221; before the &#8220;from pulp import *&#8221; line.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 &#8220;PuLP (IronPython)&#8221; &#8212; this is the recommended choice) or using your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"parent":201,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-213","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/213","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/comments?post=213"}],"version-history":[{"count":25,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/213\/revisions"}],"predecessor-version":[{"id":1110,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/213\/revisions\/1110"}],"up":[{"embeddable":true,"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/pages\/201"}],"wp:attachment":[{"href":"https:\/\/solverstudio.org\/wp-json\/wp\/v2\/media?parent=213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}