PySCIPOpt  4.3.0
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 def diet(F,N,a,b,c,d):
10  """diet -- model for the modern diet problem
11  Parameters:
12  - F: set of foods
13  - N: set of nutrients
14  - a[i]: minimum intake of nutrient i
15  - b[i]: maximum intake of nutrient i
16  - c[j]: cost of food j
17  - d[j][i]: amount of nutrient i in food j
18  Returns a model, ready to be solved.
19  """
20 
21  model = Model("modern diet")
22 
23  # Create variables
24  x,y,z = {},{},{}
25  for j in F:
26  x[j] = model.addVar(vtype="I", name="x(%s)"%j)
27  y[j] = model.addVar(vtype="B", name="y(%s)"%j)
28  for i in N:
29  z[i] = model.addVar(lb=a[i], ub=b[i], name="z(%s)"%j)
30  v = model.addVar(vtype="C", name="v")
31 
32  # Constraints:
33  for i in N:
34  model.addCons(quicksum(d[j][i]*x[j] for j in F) == z[i], name="Nutr(%s)"%i)
35 
36  model.addCons(quicksum(c[j]*x[j] for j in F) == v, name="Cost")
37 
38  for j in F:
39  model.addCons(y[j] <= x[j], name="Eat(%s)"%j)
40 
41  # Objective:
42  model.setObjective(quicksum(y[j] for j in F), "maximize")
43  model.data = x,y,z,v
44 
45  return model
46 
47 
48 def make_inst():
49  """make_inst: prepare data for the diet model"""
50  F,c,d = multidict({ # cost # composition
51  "QPounder" : [ 1.84, {"Cal":510, "Carbo":34, "Protein":28,
52  "VitA":15, "VitC": 6, "Calc":30, "Iron":20}],
53  "McLean" : [ 2.19, {"Cal":370, "Carbo":35, "Protein":24, "VitA":15,
54  "VitC": 10, "Calc":20, "Iron":20}],
55  "Big Mac" : [ 1.84, {"Cal":500, "Carbo":42, "Protein":25,
56  "VitA": 6, "VitC": 2, "Calc":25, "Iron":20}],
57  "FFilet" : [ 1.44, {"Cal":370, "Carbo":38, "Protein":14,
58  "VitA": 2, "VitC": 0, "Calc":15, "Iron":10}],
59  "Chicken" : [ 2.29, {"Cal":400, "Carbo":42, "Protein":31,
60  "VitA": 8, "VitC": 15, "Calc":15, "Iron": 8}],
61  "Fries" : [ .77, {"Cal":220, "Carbo":26, "Protein": 3,
62  "VitA": 0, "VitC": 15, "Calc": 0, "Iron": 2}],
63  "McMuffin" : [ 1.29, {"Cal":345, "Carbo":27, "Protein":15,
64  "VitA": 4, "VitC": 0, "Calc":20, "Iron":15}],
65  "1% LFMilk": [ .60, {"Cal":110, "Carbo":12, "Protein": 9,
66  "VitA":10, "VitC": 4, "Calc":30, "Iron": 0}],
67  "OrgJuice" : [ .72, {"Cal": 80, "Carbo":20, "Protein": 1,
68  "VitA": 2, "VitC":120, "Calc": 2, "Iron": 2}],
69  })
70 
71  N,a,b = multidict({ # min,max intake
72  "Cal" : [ 2000, None ],
73  "Carbo" : [ 350, 375 ],
74  "Protein" : [ 55, None ],
75  "VitA" : [ 100, None ],
76  "VitC" : [ 100, None ],
77  "Calc" : [ 100, None ],
78  "Iron" : [ 100, None ],
79  })
80 
81  return F,N,a,b,c,d
82 
83 
84 if __name__ == "__main__":
85 
86  F,N,a,b,c,d = make_inst()
87 
88  for b["Cal"] in [None,3500,3000,2500]:
89 
90  print("\nDiet for a maximum of {0} calories".format(b["Cal"] if b["Cal"] != None else "unlimited"))
91  model = diet(F,N,a,b,c,d)
92  model.hideOutput() # silent mode
93  model.optimize()
94 
95  print("Optimal value:",model.getObjVal())
96  x,y,z,v = model.data
97  for j in x:
98  if model.getVal(x[j]) > 0:
99  print("{0:30s}: {1:3.1f} dishes --> {2:4.2f} added to objective".format(j,model.getVal(x[j]),model.getVal(y[j])))
100  print("amount spent:",model.getObjVal())
101 
102  print("amount of nutrients:")
103  for i in z:
104  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:9
diet.make_inst
def make_inst()
Definition: diet.py:48
pyscipopt.Multidict.multidict
def multidict(D)
Definition: Multidict.py:3