Last active
November 7, 2016 22:07
-
-
Save kanungo/1e171fd7f20ece4a9ee6c66b7a6843cc to your computer and use it in GitHub Desktop.
wk-11a-Fall 2016-pulp-example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"cells": [ | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"import pulp as plp" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# Testing the module\n", | |
"# plp.puplTestAll()" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": true | |
}, | |
"outputs": [], | |
"source": [ | |
"# A new LP problem\n", | |
"myProblem = plp.LpProblem(\"Shader\", plp.LpMaximize)" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Define decision variables\n", | |
"x = plp.LpVariable(\"x\", 0)\n", | |
"y = plp.LpVariable(\"y\", 0)\n", | |
" \n", | |
"# Define objective function\n", | |
"myProblem += 7*x + 5*y, \"obj\"\n", | |
"\n", | |
"# define constraints\n", | |
"myProblem += 4*x + 3*y <= 240, \"c1\"\n", | |
"myProblem += 2*x + 1*y <= 100, \"c2\"" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Save and solve the problem\n", | |
"myProblem.writeLP(\"Shader.lp\")\n", | |
"# myProblem.solve() # This will work too because it will use COIN solver\n", | |
"myProblem.solve(plp.GLPK()) # This uses GLPK solver\n", | |
"print (\"Status:\", plp.LpStatus[myProblem.status])" | |
] | |
}, | |
{ | |
"cell_type": "code", | |
"execution_count": null, | |
"metadata": { | |
"collapsed": false | |
}, | |
"outputs": [], | |
"source": [ | |
"# Print the results\n", | |
"for v in myProblem.variables():\n", | |
" print (v.name, \"=\", v.varValue)\n", | |
"print (\"objective =\", plp.value(myProblem.objective))\n", | |
"print (\"\\nSensitivity Analysis\\nConstraint\\t\\tShadow Price\\tSlack\")\n", | |
"for name, c in myProblem.constraints.items():\n", | |
" print (name, \":\", c, \"\\t\", c.pi, \"\\t\\t\", c.slack)" | |
] | |
} | |
], | |
"metadata": { | |
"anaconda-cloud": {}, | |
"kernelspec": { | |
"display_name": "Python [conda root]", | |
"language": "python", | |
"name": "conda-root-py" | |
}, | |
"language_info": { | |
"codemirror_mode": { | |
"name": "ipython", | |
"version": 3 | |
}, | |
"file_extension": ".py", | |
"mimetype": "text/x-python", | |
"name": "python", | |
"nbconvert_exporter": "python", | |
"pygments_lexer": "ipython3", | |
"version": "3.5.2" | |
} | |
}, | |
"nbformat": 4, | |
"nbformat_minor": 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment