Skip to content

Instantly share code, notes, and snippets.

@kanungo
Created November 17, 2016 16:18
Show Gist options
  • Select an option

  • Save kanungo/3534e163fbaf92f6db5099281ab71b78 to your computer and use it in GitHub Desktop.

Select an option

Save kanungo/3534e163fbaf92f6db5099281ab71b78 to your computer and use it in GitHub Desktop.
wk-12-Fall 2016-pulp-Assignment-Hint
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"import pulp as pu"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def readLPData(fn):\n",
" \"\"\"\n",
" Input = name of JSON file\n",
" Returns = JSON object\n",
" \"\"\"\n",
" import json\n",
"\n",
" try:\n",
" with open(fn, 'r') as f:\n",
" LPdata = json.load(f)\n",
" return LPdata\n",
" except:\n",
" return None"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Read the JSON file into a variable\n",
"modelData = readLPData(\"introLP.json\") # if needed, provide full path name for file"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def createLP(LPdata):\n",
" \n",
" #initialise the model\n",
" if LPdata['objective'] == \"MIN\":\n",
" my_model = pu.LpProblem('My Model', pu.LpMinimize)\n",
" elif LPdata['objective'] == \"MAX\":\n",
" my_model = pu.LpProblem('My Model', pu.LpMaximize)\n",
" else:\n",
" print (\"Neither max nor min\")\n",
" exit(0)\n",
" \n",
" # make a list of decision variables\n",
" decVars = LPdata['variables'] \n",
"\n",
" # create a dictionary of pulp variables with keys from input vars\n",
" # the default lower bound is -inf, make it 0\n",
" x = pu.LpVariable.dict('x_%s', decVars, lowBound = 0)\n",
"\n",
" # Objective function data\n",
" objCoeffList = LPdata[\"objCoeffs\"]\n",
" objective = dict(zip(decVars, objCoeffList))\n",
" \n",
" # create the objective\n",
" my_model += sum( [objective[i] * x[i] for i in decVars])\n",
"\n",
" # create the constraints\n",
" constraintKeys = LPdata[\"LHS\"].keys()\n",
" \n",
" for key in constraintKeys:\n",
" LHScoeffs = dict(zip(decVars,LPdata[\"LHS\"][key]))\n",
" \n",
" if LPdata[\"conditions\"][key] == '<=':\n",
" my_model += sum([LHScoeffs[j]*x[j] for j in decVars]) <= LPdata['RHS'][key]\n",
" elif LPdata[\"conditions\"][key] == '>=':\n",
" my_model += sum([LHScoeffs[j]*x[j] for j in decVars]) >= LPdata['RHS'][key]\n",
" elif LPdata[\"conditions\"][key] == '==':\n",
" my_model += sum([LHScoeffs[j]*x[j] for j in decVars]) == LPdata['RHS'][key]\n",
" else:\n",
" print (\"Problems with constraint {}\".format(key))\n",
" exit(0)\n",
" return (my_model)\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"# Create the LP problem from the data that were read\n",
"myLP = createLP(modelData)"
]
}
],
"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