-
Notifications
You must be signed in to change notification settings - Fork 11
/
missionaries_cannibals_example.py
executable file
·137 lines (133 loc) · 4.06 KB
/
missionaries_cannibals_example.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#!/usr/bin/env python
"""
Example of using PyDDL to solve the "Missionaries and Cannibals" Problem.
A boat must transport a group of 3 missionaries and 3 cannibals across a river,
but at no time can the cannibals outnumber the missionaries at either side of
the river.
"""
from __future__ import print_function
from pyddl import Domain, Problem, Action, neg, planner
def problem(verbose):
domain = Domain((
Action(
'cross-right',
preconditions=(
('at', 'left-bank'),
('>', ('occupants',), 0),
),
effects=(
neg(('at', 'left-bank')),
('at', 'right-bank'),
),
),
Action(
'cross-left',
preconditions=(
('at', 'right-bank'),
('>', ('occupants',), 0),
),
effects=(
neg(('at', 'right-bank')),
('at', 'left-bank'),
),
),
Action(
'onboard-cannibal',
parameters=(
('location', 'l'),
),
preconditions=(
('at', 'l'),
('>', ('cannibals', 'l'), 0),
('<', ('occupants',), 2),
),
effects=(
('-=', ('cannibals', 'l'), 1),
('+=', ('cannibals', 'boat'), 1),
('+=', ('occupants',), 1),
),
),
Action(
'onboard-missionary',
parameters=(
('location', 'l'),
),
preconditions=(
('at', 'l'),
('>', ('missionaries', 'l'), 0),
('>', ('missionaries', 'l'), ('cannibals', 'l')),
('<', ('occupants',), 2),
),
effects=(
('-=', ('missionaries', 'l'), 1),
('+=', ('missionaries', 'boat'), 1),
('+=', ('occupants',), 1),
),
),
Action(
'offboard-cannibal',
parameters=(
('location', 'l'),
),
preconditions=(
('at', 'l'),
('>', ('cannibals', 'boat'), 0),
('>', ('missionaries', 'l'), ('cannibals', 'l')),
),
effects=(
('-=', ('cannibals', 'boat'), 1),
('-=', ('occupants',), 1),
('+=', ('cannibals', 'l'), 1),
),
),
Action(
'offboard-missionary',
parameters=(
('location', 'l'),
),
preconditions=(
('at', 'l'),
('>', ('missionaries', 'boat'), 0),
),
effects=(
('-=', ('missionaries', 'boat'), 1),
('-=', ('occupants',), 1),
('+=', ('missionaries', 'l'), 1),
),
),
))
problem = Problem(
domain,
{
'location': ('left-bank', 'right-bank'),
},
init=(
('at', 'left-bank'),
('=', ('missionaries', 'boat'), 0),
('=', ('cannibals', 'boat'), 0),
('=', ('occupants',), 0),
('=', ('missionaries', 'left-bank'), 3),
('=', ('cannibals', 'left-bank'), 3),
('=', ('missionaries', 'right-bank'), 0),
('=', ('cannibals', 'right-bank'), 0),
),
goal=(
('=', ('missionaries', 'right-bank'), 3),
('=', ('cannibals', 'right-bank'), 3),
)
)
plan = planner(problem, verbose=verbose)
if plan is None:
print('No Plan!')
else:
for action in plan:
print(action)
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser(usage="Usage: %prog [options]")
parser.add_option('-q', '--quiet',
action='store_false', dest='verbose', default=True,
help="don't print statistics to stdout")
# Parse arguments
opts, args = parser.parse_args()
problem(opts.verbose)