File: diet.py

package info (click to toggle)
python-glpk 0.4.45-1
  • links: PTS
  • area: main
  • in suites: wheezy
  • size: 308 kB
  • sloc: python: 1,002; ansic: 873; makefile: 49
file content (35 lines) | stat: -rw-r--r-- 1,035 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from glpk import *

# create an LP as an instance of class "glpk",
# based on the model "diet.mod" (coming in glpk's "examples")
problem = glpk("models/diet.mod")

# solve
problem.solve()
print "\n\nsolution:", problem.solution()

# define/modify problem data:
# "F" and "a" are, respectively, a "set" and a "param" in "diet.mod";
# now, they can be accessed in Python as members of the "glpk" instance
problem.F += ['Bacalhau']	# add a new food
problem.a['Bacalhau','Calorie']    = 87.3	# nutrients
problem.a['Bacalhau','Protein']	   = 98.7
problem.a['Bacalhau','Calcium']	   = 17.3
problem.a['Bacalhau','Iron']	   = 12.3
problem.a['Bacalhau','Vitamin-A']  = 12.3	

# update and solve
problem.update()
problem.solve()
print "\n\nsolution:", problem.solution()

# one more update
problem.F.remove('Bacalhau')	# remove food
for key in problem.a.keys():
    if 'Bacalhau' in key:
        del problem.a[key]

# update and solve
problem.update()
problem.solve()
print "\n\nsolution:", problem.solution()