[495079]: examples / knapsack / run.py Maximize Restore History

Download this file

run.py    30 lines (25 with data), 775 Bytes

 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
#!/usr/bin/python
# run.py
from __future__ import with_statement
from pyke import knowledge_engine
from pyke import krb_traceback
def run(pantry, capacity):
engine = knowledge_engine.engine(__file__)
engine.activate('knapsack')
max = 0
ans = None
with engine.prove_goal(
'knapsack.legal_knapsack($pantry, $capacity, $knapsack)',
pantry=pantry,
capacity=capacity) \
as gen:
for vars, no_plan in gen:
knapsack = vars['knapsack']
calories = sum(map(lambda x: x[2], knapsack))
if calories > max:
max = calories
ans = knapsack
return max, ans
if __name__ == "__main__":
import sys
print run(eval(sys.argv[1]), int(sys.argv[2]))