|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": 1, |
| 6 | + "metadata": {}, |
| 7 | + "outputs": [], |
| 8 | + "source": [ |
| 9 | + "from pulp import *" |
| 10 | + ] |
| 11 | + }, |
| 12 | + { |
| 13 | + "cell_type": "markdown", |
| 14 | + "metadata": {}, |
| 15 | + "source": [ |
| 16 | + "## Small cat food problem" |
| 17 | + ] |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": 4, |
| 22 | + "metadata": {}, |
| 23 | + "outputs": [], |
| 24 | + "source": [ |
| 25 | + "# Create the 'prob' variable to contain the problem data\n", |
| 26 | + "prob = LpProblem(\"The Whiskas Problem\",LpMinimize)" |
| 27 | + ] |
| 28 | + }, |
| 29 | + { |
| 30 | + "cell_type": "code", |
| 31 | + "execution_count": 5, |
| 32 | + "metadata": {}, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "x1=LpVariable(\"ChickenPercent\",0,100,LpInteger)\n", |
| 36 | + "x2=LpVariable(\"BeefPercent\",0,100)" |
| 37 | + ] |
| 38 | + }, |
| 39 | + { |
| 40 | + "cell_type": "code", |
| 41 | + "execution_count": 6, |
| 42 | + "metadata": {}, |
| 43 | + "outputs": [], |
| 44 | + "source": [ |
| 45 | + "# The objective function is added to 'prob' first\n", |
| 46 | + "prob += 0.013*x1 + 0.008*x2, \"Total Cost of Ingredients per can\"" |
| 47 | + ] |
| 48 | + }, |
| 49 | + { |
| 50 | + "cell_type": "code", |
| 51 | + "execution_count": 7, |
| 52 | + "metadata": {}, |
| 53 | + "outputs": [], |
| 54 | + "source": [ |
| 55 | + "prob += x1 + x2 == 100, \"PercentagesSum\"\n", |
| 56 | + "prob += 0.100*x1 + 0.200*x2 >= 8.0, \"ProteinRequirement\"\n", |
| 57 | + "prob += 0.080*x1 + 0.100*x2 >= 6.0, \"FatRequirement\"\n", |
| 58 | + "prob += 0.001*x1 + 0.005*x2 <= 2.0, \"FibreRequirement\"\n", |
| 59 | + "prob += 0.002*x1 + 0.005*x2 <= 0.4, \"SaltRequirement\"" |
| 60 | + ] |
| 61 | + }, |
| 62 | + { |
| 63 | + "cell_type": "code", |
| 64 | + "execution_count": 8, |
| 65 | + "metadata": {}, |
| 66 | + "outputs": [], |
| 67 | + "source": [ |
| 68 | + "# The problem data is written to an .lp file\n", |
| 69 | + "prob.writeLP(\"WhiskasModel.lp\")" |
| 70 | + ] |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": 9, |
| 75 | + "metadata": {}, |
| 76 | + "outputs": [ |
| 77 | + { |
| 78 | + "data": { |
| 79 | + "text/plain": [ |
| 80 | + "1" |
| 81 | + ] |
| 82 | + }, |
| 83 | + "execution_count": 9, |
| 84 | + "metadata": {}, |
| 85 | + "output_type": "execute_result" |
| 86 | + } |
| 87 | + ], |
| 88 | + "source": [ |
| 89 | + "# The problem is solved using PuLP's choice of Solver\n", |
| 90 | + "prob.solve()" |
| 91 | + ] |
| 92 | + }, |
| 93 | + { |
| 94 | + "cell_type": "code", |
| 95 | + "execution_count": 10, |
| 96 | + "metadata": {}, |
| 97 | + "outputs": [ |
| 98 | + { |
| 99 | + "name": "stdout", |
| 100 | + "output_type": "stream", |
| 101 | + "text": [ |
| 102 | + "Status: Optimal\n" |
| 103 | + ] |
| 104 | + } |
| 105 | + ], |
| 106 | + "source": [ |
| 107 | + "# The status of the solution is printed to the screen\n", |
| 108 | + "print(\"Status:\", LpStatus[prob.status])" |
| 109 | + ] |
| 110 | + }, |
| 111 | + { |
| 112 | + "cell_type": "code", |
| 113 | + "execution_count": 11, |
| 114 | + "metadata": {}, |
| 115 | + "outputs": [ |
| 116 | + { |
| 117 | + "name": "stdout", |
| 118 | + "output_type": "stream", |
| 119 | + "text": [ |
| 120 | + "BeefPercent = 66.0\n", |
| 121 | + "ChickenPercent = 34.0\n" |
| 122 | + ] |
| 123 | + } |
| 124 | + ], |
| 125 | + "source": [ |
| 126 | + "# Each of the variables is printed with it's resolved optimum value\n", |
| 127 | + "for v in prob.variables():\n", |
| 128 | + " print(v.name, \"=\", v.varValue)" |
| 129 | + ] |
| 130 | + }, |
| 131 | + { |
| 132 | + "cell_type": "code", |
| 133 | + "execution_count": 12, |
| 134 | + "metadata": {}, |
| 135 | + "outputs": [ |
| 136 | + { |
| 137 | + "name": "stdout", |
| 138 | + "output_type": "stream", |
| 139 | + "text": [ |
| 140 | + "Total Cost of Ingredients per can = 0.97\n" |
| 141 | + ] |
| 142 | + } |
| 143 | + ], |
| 144 | + "source": [ |
| 145 | + "# The optimised objective function value is printed to the screen\n", |
| 146 | + "print(\"Total Cost of Ingredients per can = \", value(prob.objective))" |
| 147 | + ] |
| 148 | + }, |
| 149 | + { |
| 150 | + "cell_type": "markdown", |
| 151 | + "metadata": {}, |
| 152 | + "source": [ |
| 153 | + "## Large cat food problem" |
| 154 | + ] |
| 155 | + }, |
| 156 | + { |
| 157 | + "cell_type": "code", |
| 158 | + "execution_count": 13, |
| 159 | + "metadata": {}, |
| 160 | + "outputs": [], |
| 161 | + "source": [ |
| 162 | + "# Creates a list of the Ingredients\n", |
| 163 | + "Ingredients = ['CHICKEN', 'BEEF', 'MUTTON', 'RICE', 'WHEAT', 'GEL']\n", |
| 164 | + "\n", |
| 165 | + "# A dictionary of the costs of each of the Ingredients is created\n", |
| 166 | + "costs = {'CHICKEN': 0.013, \n", |
| 167 | + " 'BEEF': 0.008, \n", |
| 168 | + " 'MUTTON': 0.010, \n", |
| 169 | + " 'RICE': 0.002, \n", |
| 170 | + " 'WHEAT': 0.005, \n", |
| 171 | + " 'GEL': 0.001}\n", |
| 172 | + "\n", |
| 173 | + "# A dictionary of the protein percent in each of the Ingredients is created\n", |
| 174 | + "proteinPercent = {'CHICKEN': 0.100, \n", |
| 175 | + " 'BEEF': 0.200, \n", |
| 176 | + " 'MUTTON': 0.150, \n", |
| 177 | + " 'RICE': 0.000, \n", |
| 178 | + " 'WHEAT': 0.040, \n", |
| 179 | + " 'GEL': 0.000}\n", |
| 180 | + "\n", |
| 181 | + "# A dictionary of the fat percent in each of the Ingredients is created\n", |
| 182 | + "fatPercent = {'CHICKEN': 0.080, \n", |
| 183 | + " 'BEEF': 0.100, \n", |
| 184 | + " 'MUTTON': 0.110, \n", |
| 185 | + " 'RICE': 0.010, \n", |
| 186 | + " 'WHEAT': 0.010, \n", |
| 187 | + " 'GEL': 0.000}\n", |
| 188 | + "\n", |
| 189 | + "# A dictionary of the fibre percent in each of the Ingredients is created\n", |
| 190 | + "fibrePercent = {'CHICKEN': 0.001, \n", |
| 191 | + " 'BEEF': 0.005, \n", |
| 192 | + " 'MUTTON': 0.003, \n", |
| 193 | + " 'RICE': 0.100, \n", |
| 194 | + " 'WHEAT': 0.150, \n", |
| 195 | + " 'GEL': 0.000}\n", |
| 196 | + "\n", |
| 197 | + "# A dictionary of the salt percent in each of the Ingredients is created\n", |
| 198 | + "saltPercent = {'CHICKEN': 0.002, \n", |
| 199 | + " 'BEEF': 0.005, \n", |
| 200 | + " 'MUTTON': 0.007, \n", |
| 201 | + " 'RICE': 0.002, \n", |
| 202 | + " 'WHEAT': 0.008, \n", |
| 203 | + " 'GEL': 0.000}" |
| 204 | + ] |
| 205 | + }, |
| 206 | + { |
| 207 | + "cell_type": "code", |
| 208 | + "execution_count": 14, |
| 209 | + "metadata": {}, |
| 210 | + "outputs": [], |
| 211 | + "source": [ |
| 212 | + "# Create the 'prob' variable to contain the problem data\n", |
| 213 | + "prob = LpProblem(\"The Whiskas Problem\", LpMinimize)" |
| 214 | + ] |
| 215 | + }, |
| 216 | + { |
| 217 | + "cell_type": "code", |
| 218 | + "execution_count": 15, |
| 219 | + "metadata": {}, |
| 220 | + "outputs": [], |
| 221 | + "source": [ |
| 222 | + "# A dictionary called 'ingredient_vars' is created to contain the referenced Variables\n", |
| 223 | + "ingredient_vars = LpVariable.dicts(\"Ingr\",Ingredients,0)" |
| 224 | + ] |
| 225 | + }, |
| 226 | + { |
| 227 | + "cell_type": "code", |
| 228 | + "execution_count": 16, |
| 229 | + "metadata": {}, |
| 230 | + "outputs": [ |
| 231 | + { |
| 232 | + "data": { |
| 233 | + "text/plain": [ |
| 234 | + "{'BEEF': Ingr_BEEF,\n", |
| 235 | + " 'CHICKEN': Ingr_CHICKEN,\n", |
| 236 | + " 'GEL': Ingr_GEL,\n", |
| 237 | + " 'MUTTON': Ingr_MUTTON,\n", |
| 238 | + " 'RICE': Ingr_RICE,\n", |
| 239 | + " 'WHEAT': Ingr_WHEAT}" |
| 240 | + ] |
| 241 | + }, |
| 242 | + "execution_count": 16, |
| 243 | + "metadata": {}, |
| 244 | + "output_type": "execute_result" |
| 245 | + } |
| 246 | + ], |
| 247 | + "source": [ |
| 248 | + "ingredient_vars" |
| 249 | + ] |
| 250 | + }, |
| 251 | + { |
| 252 | + "cell_type": "code", |
| 253 | + "execution_count": 17, |
| 254 | + "metadata": {}, |
| 255 | + "outputs": [], |
| 256 | + "source": [ |
| 257 | + "# The objective function is added to 'prob' first\n", |
| 258 | + "prob += lpSum([costs[i]*ingredient_vars[i] for i in Ingredients]), \"Total Cost of Ingredients per can\"" |
| 259 | + ] |
| 260 | + }, |
| 261 | + { |
| 262 | + "cell_type": "code", |
| 263 | + "execution_count": 18, |
| 264 | + "metadata": {}, |
| 265 | + "outputs": [], |
| 266 | + "source": [ |
| 267 | + "# The five constraints are added to 'prob'\n", |
| 268 | + "prob += lpSum([ingredient_vars[i] for i in Ingredients]) == 100, \"PercentagesSum\"\n", |
| 269 | + "prob += lpSum([proteinPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 8.0, \"ProteinRequirement\"\n", |
| 270 | + "prob += lpSum([fatPercent[i] * ingredient_vars[i] for i in Ingredients]) >= 6.0, \"FatRequirement\"\n", |
| 271 | + "prob += lpSum([fibrePercent[i] * ingredient_vars[i] for i in Ingredients]) <= 2.0, \"FibreRequirement\"\n", |
| 272 | + "prob += lpSum([saltPercent[i] * ingredient_vars[i] for i in Ingredients]) <= 0.4, \"SaltRequirement\"" |
| 273 | + ] |
| 274 | + }, |
| 275 | + { |
| 276 | + "cell_type": "code", |
| 277 | + "execution_count": 19, |
| 278 | + "metadata": {}, |
| 279 | + "outputs": [], |
| 280 | + "source": [ |
| 281 | + "# The problem data is written to an .lp file\n", |
| 282 | + "prob.writeLP(\"WhiskasModelBig.lp\")" |
| 283 | + ] |
| 284 | + }, |
| 285 | + { |
| 286 | + "cell_type": "code", |
| 287 | + "execution_count": 20, |
| 288 | + "metadata": {}, |
| 289 | + "outputs": [ |
| 290 | + { |
| 291 | + "data": { |
| 292 | + "text/plain": [ |
| 293 | + "1" |
| 294 | + ] |
| 295 | + }, |
| 296 | + "execution_count": 20, |
| 297 | + "metadata": {}, |
| 298 | + "output_type": "execute_result" |
| 299 | + } |
| 300 | + ], |
| 301 | + "source": [ |
| 302 | + "# The problem is solved using PuLP's choice of Solver\n", |
| 303 | + "prob.solve()" |
| 304 | + ] |
| 305 | + }, |
| 306 | + { |
| 307 | + "cell_type": "code", |
| 308 | + "execution_count": 21, |
| 309 | + "metadata": {}, |
| 310 | + "outputs": [ |
| 311 | + { |
| 312 | + "name": "stdout", |
| 313 | + "output_type": "stream", |
| 314 | + "text": [ |
| 315 | + "Ingr_BEEF = 60.0\n", |
| 316 | + "Ingr_CHICKEN = 0.0\n", |
| 317 | + "Ingr_GEL = 40.0\n", |
| 318 | + "Ingr_MUTTON = 0.0\n", |
| 319 | + "Ingr_RICE = 0.0\n", |
| 320 | + "Ingr_WHEAT = 0.0\n" |
| 321 | + ] |
| 322 | + } |
| 323 | + ], |
| 324 | + "source": [ |
| 325 | + "# Each of the variables is printed with it's resolved optimum value\n", |
| 326 | + "for v in prob.variables():\n", |
| 327 | + " print(v.name, \"=\", v.varValue)" |
| 328 | + ] |
| 329 | + }, |
| 330 | + { |
| 331 | + "cell_type": "code", |
| 332 | + "execution_count": 22, |
| 333 | + "metadata": {}, |
| 334 | + "outputs": [ |
| 335 | + { |
| 336 | + "name": "stdout", |
| 337 | + "output_type": "stream", |
| 338 | + "text": [ |
| 339 | + "Total Cost of Ingredients per can = 0.52\n" |
| 340 | + ] |
| 341 | + } |
| 342 | + ], |
| 343 | + "source": [ |
| 344 | + "# The optimised objective function value is printed to the screen\n", |
| 345 | + "print(\"Total Cost of Ingredients per can = \", value(prob.objective))" |
| 346 | + ] |
| 347 | + }, |
| 348 | + { |
| 349 | + "cell_type": "code", |
| 350 | + "execution_count": null, |
| 351 | + "metadata": {}, |
| 352 | + "outputs": [], |
| 353 | + "source": [] |
| 354 | + } |
| 355 | + ], |
| 356 | + "metadata": { |
| 357 | + "kernelspec": { |
| 358 | + "display_name": "Python 3", |
| 359 | + "language": "python", |
| 360 | + "name": "python3" |
| 361 | + }, |
| 362 | + "language_info": { |
| 363 | + "codemirror_mode": { |
| 364 | + "name": "ipython", |
| 365 | + "version": 3 |
| 366 | + }, |
| 367 | + "file_extension": ".py", |
| 368 | + "mimetype": "text/x-python", |
| 369 | + "name": "python", |
| 370 | + "nbconvert_exporter": "python", |
| 371 | + "pygments_lexer": "ipython3", |
| 372 | + "version": "3.6.2" |
| 373 | + }, |
| 374 | + "latex_envs": { |
| 375 | + "LaTeX_envs_menu_present": true, |
| 376 | + "autoclose": false, |
| 377 | + "autocomplete": true, |
| 378 | + "bibliofile": "biblio.bib", |
| 379 | + "cite_by": "apalike", |
| 380 | + "current_citInitial": 1, |
| 381 | + "eqLabelWithNumbers": true, |
| 382 | + "eqNumInitial": 1, |
| 383 | + "hotkeys": { |
| 384 | + "equation": "Ctrl-E", |
| 385 | + "itemize": "Ctrl-I" |
| 386 | + }, |
| 387 | + "labels_anchors": false, |
| 388 | + "latex_user_defs": false, |
| 389 | + "report_style_numbering": false, |
| 390 | + "user_envs_cfg": false |
| 391 | + } |
| 392 | + }, |
| 393 | + "nbformat": 4, |
| 394 | + "nbformat_minor": 2 |
| 395 | +} |
0 commit comments