-
Notifications
You must be signed in to change notification settings - Fork 11
/
eight_puzzle_example.py
executable file
·179 lines (169 loc) · 4.95 KB
/
eight_puzzle_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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#!/usr/bin/env python
"""
Example of using PyDDL to solve an eight-puzzle. Each number is a tile that
can slide vertically or horizontally to fill in the blank space. This "hard"
instance (requiring the maximum of 31 steps) is taken from the following paper:
Reinefeld, Alexander. "Complete Solution of the Eight-Puzzle and the Benefit of
Node Ordering in IDA*." International Joint Conference on Artificial
Intelligence. 1993.
Initial State:
+---+---+---+
| 8 7 6 |
| 4 1 |
| 2 5 3 |
+---+---+---+
Goal State:
+---+---+---+
| 1 2 |
| 3 4 5 |
| 6 7 8 |
+---+---+---+
"""
from __future__ import print_function
from pyddl import Domain, Problem, Action, neg, planner
def problem(verbose):
domain = Domain((
Action(
'move-up',
parameters=(
('tile', 't'),
('position', 'px'),
('position', 'py'),
('position', 'by'),
),
preconditions=(
('dec', 'by', 'py'),
('blank', 'px', 'by'),
('at', 't', 'px', 'py'),
),
effects=(
neg(('blank', 'px', 'by')),
neg(('at', 't', 'px', 'py')),
('blank', 'px', 'py'),
('at', 't', 'px', 'by'),
),
),
Action(
'move-down',
parameters=(
('tile', 't'),
('position', 'px'),
('position', 'py'),
('position', 'by'),
),
preconditions=(
('inc', 'by', 'py'),
('blank', 'px', 'by'),
('at', 't', 'px', 'py'),
),
effects=(
neg(('blank', 'px', 'by')),
neg(('at', 't', 'px', 'py')),
('blank', 'px', 'py'),
('at', 't', 'px', 'by'),
),
),
Action(
'move-left',
parameters=(
('tile', 't'),
('position', 'px'),
('position', 'py'),
('position', 'bx'),
),
preconditions=(
('dec', 'bx', 'px'),
('blank', 'bx', 'py'),
('at', 't', 'px', 'py'),
),
effects=(
neg(('blank', 'bx', 'py')),
neg(('at', 't', 'px', 'py')),
('blank', 'px', 'py'),
('at', 't', 'bx', 'py'),
),
),
Action(
'move-right',
parameters=(
('tile', 't'),
('position', 'px'),
('position', 'py'),
('position', 'bx'),
),
preconditions=(
('inc', 'bx', 'px'),
('blank', 'bx', 'py'),
('at', 't', 'px', 'py'),
),
effects=(
neg(('blank', 'bx', 'py')),
neg(('at', 't', 'px', 'py')),
('blank', 'px', 'py'),
('at', 't', 'bx', 'py'),
),
),
))
problem = Problem(
domain,
{
'tile': (1, 2, 3, 4, 5, 6, 7, 8),
'position': (1, 2, 3),
},
init=(
('inc', 1, 2),
('inc', 2, 3),
('dec', 3, 2),
('dec', 2, 1),
('at', 8, 1, 1),
('at', 7, 2, 1),
('at', 6, 3, 1),
('blank', 1, 2),
('at', 4, 2, 2),
('at', 1, 3, 2),
('at', 2, 1, 3),
('at', 5, 2, 3),
('at', 3, 3, 3),
),
goal=(
('blank', 1, 1),
('at', 1, 2, 1),
('at', 2, 3, 1),
('at', 3, 1, 2),
('at', 4, 2, 2),
('at', 5, 3, 2),
('at', 6, 1, 3),
('at', 7, 2, 3),
('at', 8, 3, 3),
)
)
def to_coordinates(state):
grid = {}
for p in state:
if p[0] == 'at':
grid[p[1]] = (p[2], p[3])
return grid
goal_coords = to_coordinates(problem.goals)
def manhattan_distance_heuristic(state):
state_coords = to_coordinates(state.predicates)
dist = 0
for k in goal_coords.keys():
c1, r1 = goal_coords[k]
c2, r2 = state_coords[k]
dist += (abs(c1 - c2) + abs(r1 - r2))
return dist
plan = planner(problem, heuristic=manhattan_distance_heuristic, 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)