Skip to content

Instantly share code, notes, and snippets.

@dandye
Last active August 29, 2015 14:06
Show Gist options
  • Select an option

  • Save dandye/3f3631dfcf69211bc560 to your computer and use it in GitHub Desktop.

Select an option

Save dandye/3f3631dfcf69211bc560 to your computer and use it in GitHub Desktop.
learning PuLP
{
"metadata": {
"name": ""
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"!pip install pulp"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"Downloading/unpacking pulp\n",
" Running setup.py (path:c:\\users\\ddye\\appdata\\local\\temp\\pip_build_ddye\\pulp\\setup.py) egg_info for package pulp\n",
" \n",
"Requirement already satisfied (use --upgrade to upgrade): pyparsing<=1.9.9 in c:\\users\\ddye\\appdata\\local\\continuum\\anaconda\\lib\\site-packages (from pulp)\n",
"Installing collected packages: pulp\n",
" Running setup.py install for pulp\n",
" \n",
" Installing pulpdoctest-script.py script to C:\\Users\\ddye\\AppData\\Local\\Continuum\\Anaconda\\Scripts\n",
" Installing pulpdoctest.exe script to C:\\Users\\ddye\\AppData\\Local\\Continuum\\Anaconda\\Scripts\n",
" Installing pulptest-script.py script to C:\\Users\\ddye\\AppData\\Local\\Continuum\\Anaconda\\Scripts\n",
" Installing pulptest.exe script to C:\\Users\\ddye\\AppData\\Local\\Continuum\\Anaconda\\Scripts\n",
"Successfully installed pulp\n",
"Cleaning up...\n"
]
}
],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"import pulp\n",
"pulp.pulpTestAll()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\t Testing zero subtraction\n",
"\t Testing continuous LP solution\n",
"\t Testing maximize continuous LP solution"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing unbounded continuous LP solution"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing Long Names"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing repeated Names"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing zero constraint\n",
"\t Testing zero objective"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing LpVariable (not LpAffineExpression) objective"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing MIP solution"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing MIP relaxation"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing feasibility problem (no objective)"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing an infeasible problem"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing an integer infeasible problem\n",
"\t Testing column based modelling"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing dual variables and slacks reporting\n",
"\t Testing fractional constraints"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing elastic constraints (no change)\n",
"\t Testing elastic constraints (freebound)"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"\t Testing elastic constraints (penalty unchanged)\n",
"\t Testing elastic constraints (penalty unbounded)"
]
},
{
"output_type": "stream",
"stream": "stdout",
"text": [
"\n",
"* Solver pulp.solvers.PULP_CBC_CMD passed.\n",
"Solver pulp.solvers.CPLEX_DLL unavailable\n",
"Solver pulp.solvers.CPLEX_CMD unavailable\n",
"Solver pulp.solvers.CPLEX_PY unavailable\n",
"Solver pulp.solvers.COIN_CMD unavailable\n",
"Solver pulp.solvers.COINMP_DLL unavailable\n",
"Solver pulp.solvers.GLPK_CMD unavailable\n",
"Solver pulp.solvers.XPRESS unavailable\n",
"Solver pulp.solvers.GUROBI unavailable\n",
"Solver pulp.solvers.GUROBI_CMD unavailable\n",
"Solver pulp.solvers.PYGLPK unavailable\n",
"Solver pulp.solvers.YAPOSIB unavailable\n"
]
}
],
"prompt_number": 4
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"# Creates the 'prob' variable to contain the problem data \n",
"prob = pulp.LpProblem(\"Egg Cheese Mix Problem\", pulp.LpMaximize)\n",
"\n",
"eggs = pulp.LpVariable('eggs', lowBound = 1, upBound = 4, cat='Integer')\n",
"cheese = pulp.LpVariable('cheese',lowBound = .33, upBound = 1, cat='Continuous')\n",
"\n",
"# Creates the objective function\n",
"prob += pulp.lpSum([eggs*70 + cheese*110*3]), \"Total Calories\"\n",
"\n",
"# Objective function: keep calories under 350. Each egg is 70 calories. Each 1/3 cup of cheese is 110 calories\n",
"prob += pulp.lpSum([eggs*70 + cheese*110*3]) <= 350., \"MaxCalorie\"\n",
"\n",
"prob += (pulp.lpSum([eggs/2.]) >= pulp.lpSum([cheese/.33])*.75), \"Min Cheese Ratio: at least .75*1/3 cup for every 2 eggs\"\n",
"\n",
"prob += (pulp.lpSum([eggs/2.]) <= pulp.lpSum([cheese/.33])*1.25), \"Max Cheese Ratio: at most 1.25*1/3 cup for every 2 eggs\"\n",
"\n",
"# The problem data is written to an .lp file\n",
"prob.writeLP(\"EggCheeseMix.lp\")\n",
"\n",
"# The problem is solved using PuLP's choice of Solver\n",
"prob.solve()\n",
"\n",
"# The status of the solution is printed to the screen\n",
"print(\"Status:\", pulp.LpStatus[prob.status])\n",
"\n",
"# Each of the variables is printed with it's resolved optimum value\n",
"for v in prob.variables():\n",
" print(v.name, \"=\", v.varValue)\n",
"\n",
"# The optimised objective function value is printed to the screen \n",
"print(\"Total Calories= \", pulp.value(prob.objective))\n",
"\n"
],
"language": "python",
"metadata": {},
"outputs": [
{
"output_type": "stream",
"stream": "stdout",
"text": [
"('Status:', 'Optimal')\n",
"('cheese', '=', 0.42424242)\n",
"('eggs', '=', 3.0)\n",
"('Total Calories= ', 349.9999986)\n"
]
}
],
"prompt_number": 57
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment