-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathencode.py
More file actions
55 lines (38 loc) · 1.42 KB
/
encode.py
File metadata and controls
55 lines (38 loc) · 1.42 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from krrt.sat.CNF import Formula, Not as neg
from krrt.planning.strips.representation import parse_problem
USAGE_STRING = "\n Usage: python encode.py [action|fluent] domain.pddl problem.pddl output.cnf\n"
def encode(mode, dfile, pfile, ofile):
print "Parsing ground pddl..."
# Load and set up the problem
F, A, I, G = parse_problem(dfile, pfile)
A = A.values()
print "Building CNF model..."
adders = {}
for f in F:
adders[f] = set([])
for a in A:
for f in a.adds:
adders[f].add(a)
formula = Formula()
# For it to be a deadend, one of the goal fluents must be disabled
formula.addClause(list(G))
# If a fluent is disabled, then so is all of its adders
for f in F:
for a in adders[f]:
if 'action' == mode:
formula.addClause([neg(f), a])
else:
formula.addClause([neg(f)] + list(a.precond))
if 'action' == mode:
# If an action is disabled, then one of it's preconditions must be
for a in A:
formula.addClause([neg(a)] + list(a.precond))
print "Writing files..."
formula.writeCNF(ofile)
formula.writeMapping(ofile+'.map')
if __name__ == '__main__':
import sys
if 5 != len(sys.argv) or sys.argv[1] not in ['action', 'fluent']:
print USAGE_STRING
sys.exit(1)
encode(sys.argv[1], sys.argv[2], sys.argv[3], sys.argv[4])