Debian Codegraph @

doc/KPyCon2009/code/whiskas.py

raw symbols SCIP
1 """
2 Example problem file that solves the whiskas blending problem
3 """
4 import pulp
5
6 # initialise the model
7 whiskas_model = pulp.LpProblem("The Whiskas Problem", pulp.LpMinimize)
8 # make a list of ingredients
9 ingredients = ["chicken", "beef", "mutton", "rice", "wheat", "gel"]
10 # create a dictionary of pulp variables with keys from ingredients
11 # the default lower bound is -inf
12 x = pulp.LpVariable.dict("x_%s", ingredients, lowBound=0)
13
14 # cost data
15 cost = dict(zip(ingredients, [0.013, 0.008, 0.010, 0.002, 0.005, 0.001]))
16 # create the objective
17 whiskas_model += sum([cost[i] * x[i] for i in ingredients])
18
19 # ingredient parameters
20 protein = dict(zip(ingredients, [0.100, 0.200, 0.150, 0.000, 0.040, 0.000]))
21 fat = dict(zip(ingredients, [0.080, 0.100, 0.110, 0.010, 0.010, 0.000]))
22 fibre = dict(zip(ingredients, [0.001, 0.005, 0.003, 0.100, 0.150, 0.000]))
23 salt = dict(zip(ingredients, [0.002, 0.005, 0.007, 0.002, 0.008, 0.000]))
24
25 # note these are constraints and not an objective as there is a equality/inequality
26 whiskas_model += sum([protein[i] * x[i] for i in ingredients]) >= 8.0
27 whiskas_model += sum([fat[i] * x[i] for i in ingredients]) >= 6.0
28 whiskas_model += sum([fibre[i] * x[i] for i in ingredients]) <= 2.0
29 whiskas_model += sum([salt[i] * x[i] for i in ingredients]) <= 0.4
30
31 # problem is then solved with the default solver
32 whiskas_model.solve()
33
34 # print the result
35 for ingredient in ingredients:
36 print("The mass of %s is %s grams per can" % (ingredient, x[ingredient].value()))