pyscipopt
importModel, quicksum, multidict
10 def diet(F, N, a, b, c, d):
11 """diet -- model for the modern diet problem 15 - a[i]: minimum intake of nutrient i 16 - b[i]: maximum intake of nutrient i 17 - c[j]: cost of food j 18 - d[j][i]: amount of nutrient i in food j 19 Returns a model, ready to be solved. 22model = Model(
"modern diet")
27x[j] = model.addVar(vtype=
"I", name=
"x(%s)"% j)
28y[j] = model.addVar(vtype=
"B", name=
"y(%s)"% j)
30z[i] = model.addVar(lb=a[i], ub=b[i], name=
"z(%s)"% j)
31v = model.addVar(vtype=
"C", name=
"v")
35model.addCons(
quicksum(d[j][i] * x[j]
forj
inF) == z[i], name=
"Nutr(%s)"% i)
37model.addCons(
quicksum(c[j] * x[j]
forj
inF) == v, name=
"Cost")
40model.addCons(y[j] <= x[j], name=
"Eat(%s)"% j)
43model.setObjective(
quicksum(y[j]
forj
inF),
"maximize")
44model.data = x, y, z, v
50 """make_inst: prepare data for the diet model""" 52 "QPounder": [1.84, {
"Cal": 510,
"Carbo": 34,
"Protein": 28,
53 "VitA": 15,
"VitC": 6,
"Calc": 30,
"Iron": 20}],
54 "McLean": [2.19, {
"Cal": 370,
"Carbo": 35,
"Protein": 24,
"VitA": 15,
55 "VitC": 10,
"Calc": 20,
"Iron": 20}],
56 "Big Mac": [1.84, {
"Cal": 500,
"Carbo": 42,
"Protein": 25,
57 "VitA": 6,
"VitC": 2,
"Calc": 25,
"Iron": 20}],
58 "FFilet": [1.44, {
"Cal": 370,
"Carbo": 38,
"Protein": 14,
59 "VitA": 2,
"VitC": 0,
"Calc": 15,
"Iron": 10}],
60 "Chicken": [2.29, {
"Cal": 400,
"Carbo": 42,
"Protein": 31,
61 "VitA": 8,
"VitC": 15,
"Calc": 15,
"Iron": 8}],
62 "Fries": [.77, {
"Cal": 220,
"Carbo": 26,
"Protein": 3,
63 "VitA": 0,
"VitC": 15,
"Calc": 0,
"Iron": 2}],
64 "McMuffin": [1.29, {
"Cal": 345,
"Carbo": 27,
"Protein": 15,
65 "VitA": 4,
"VitC": 0,
"Calc": 20,
"Iron": 15}],
66 "1% LFMilk": [.60, {
"Cal": 110,
"Carbo": 12,
"Protein": 9,
67 "VitA": 10,
"VitC": 4,
"Calc": 30,
"Iron": 0}],
68 "OrgJuice": [.72, {
"Cal": 80,
"Carbo": 20,
"Protein": 1,
69 "VitA": 2,
"VitC": 120,
"Calc": 2,
"Iron": 2}],
73 "Cal": [2000,
None],
75 "Protein": [55,
None],
76 "VitA": [100,
None],
77 "VitC": [100,
None],
78 "Calc": [100,
None],
79 "Iron": [100,
None],
82 returnF, N, a, b, c, d
85 if__name__ ==
"__main__":
89 forb[
"Cal"]
in[
None, 3500, 3000, 2500]:
91print(
"\nDiet for a maximum of {0} calories".format(b[
"Cal"]
ifb[
"Cal"] !=
None else "unlimited"))
92model =
diet(F, N, a, b, c, d)
96print(
"Optimal value:", model.getObjVal())
97x, y, z, v = model.data
99 ifmodel.getVal(x[j]) > 0:
100print(
"{0:30s}: {1:3.1f} dishes --> {2:4.2f} added to objective".format(j, model.getVal(x[j]),
102print(
"amount spent:", model.getObjVal())
104print(
"amount of nutrients:")
106print(
"{0:30s}: {1:4.2f}".format(i, model.getVal(z[i])))
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4