PySCIPOpt  5.1.1
Python Interface for the SCIP Optimization Suite
diet.py
Go to the documentation of this file.
1 
3 """
4 Copyright (c) by Joao Pedro PEDROSO and Mikio KUBO, 2012
5 """
6 # todo: can we make it work as "from pyscipopt import *"?
7 from pyscipopt import Model, quicksum, multidict
8 
9 
10 def diet(F, N, a, b, c, d):
11  """diet -- model for the modern diet problem
12  Parameters:
13  - F: set of foods
14  - N: set of nutrients
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.
20  """
21 
22  model = Model("modern diet")
23 
24  # Create variables
25  x, y, z = {}, {}, {}
26  for j in F:
27  x[j] = model.addVar(vtype="I", name="x(%s)" % j)
28  y[j] = model.addVar(vtype="B", name="y(%s)" % j)
29  for i in N:
30  z[i] = model.addVar(lb=a[i], ub=b[i], name="z(%s)" % j)
31  v = model.addVar(vtype="C", name="v")
32 
33  # Constraints:
34  for i in N:
35  model.addCons(quicksum(d[j][i] * x[j] for j in F) == z[i], name="Nutr(%s)" % i)
36 
37  model.addCons(quicksum(c[j] * x[j] for j in F) == v, name="Cost")
38 
39  for j in F:
40  model.addCons(y[j] <= x[j], name="Eat(%s)" % j)
41 
42  # Objective:
43  model.setObjective(quicksum(y[j] for j in F), "maximize")
44  model.data = x, y, z, v
45 
46  return model
47 
48 
49 def make_inst():
50  """make_inst: prepare data for the diet model"""
51  F, c, d = multidict({ # cost # composition
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}],
70  })
71 
72  N, a, b = multidict({ # min,max intake
73  "Cal": [2000, None],
74  "Carbo": [350, 375],
75  "Protein": [55, None],
76  "VitA": [100, None],
77  "VitC": [100, None],
78  "Calc": [100, None],
79  "Iron": [100, None],
80  })
81 
82  return F, N, a, b, c, d
83 
84 
85 if __name__ == "__main__":
86 
87  F, N, a, b, c, d = make_inst()
88 
89  for b["Cal"] in [None, 3500, 3000, 2500]:
90 
91  print("\nDiet for a maximum of {0} calories".format(b["Cal"] if b["Cal"] != None else "unlimited"))
92  model = diet(F, N, a, b, c, d)
93  model.hideOutput() # silent mode
94  model.optimize()
95 
96  print("Optimal value:", model.getObjVal())
97  x, y, z, v = model.data
98  for j in x:
99  if model.getVal(x[j]) > 0:
100  print("{0:30s}: {1:3.1f} dishes --> {2:4.2f} added to objective".format(j, model.getVal(x[j]),
101  model.getVal(y[j])))
102  print("amount spent:", model.getObjVal())
103 
104  print("amount of nutrients:")
105  for i in z:
106  print("{0:30s}: {1:4.2f}".format(i, model.getVal(z[i])))
pyscipopt.expr.quicksum
def quicksum(termlist)
Definition: expr.pxi:357
diet
Definition: diet.py:1
diet.diet
def diet(F, N, a, b, c, d)
Definition: diet.py:10
diet.make_inst
def make_inst()
Definition: diet.py:49
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3