File: driver.py

package info (click to toggle)
pyke 1.1.1-1
  • links: PTS, VCS
  • area: main
  • in suites: squeeze
  • size: 4,196 kB
  • ctags: 1,159
  • sloc: python: 12,866; sh: 446; xml: 203; sql: 39; makefile: 5
file content (29 lines) | stat: -rw-r--r-- 778 bytes parent folder | download | duplicates (3)
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

# driver.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]))