-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks_world_examples.py
208 lines (148 loc) · 5.28 KB
/
blocks_world_examples.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
"""
Blocks-world test data for Pyhop 1.1.
Author: Dana Nau <[email protected]>, November 15, 2012
This file should work correctly in both Python 2.7 and Python 3.2.
"""
from __future__ import print_function
from pyhop import *
import blocks_world_operators
print('')
print_operators()
import blocks_world_methods
print('')
print_methods()
############# beginning of tests ################
print("""
****************************************
First, test pyhop on some of the operators and smaller tasks
****************************************
""")
print("- Define state1: a on b, b on table, c on table")
"""
A state is a collection of all of the state variables and their values. Every state variable in the domain should have a value.
"""
state1 = State('state1')
state1.pos={'a':'b', 'b':'table', 'c':'table'}
state1.clear={'c':True, 'b':False,'a':True}
state1.holding=False
print_state(state1)
print('')
print('- these should fail:')
pyhop(state1,[('pickup','a')], verbose=1)
pyhop(state1,[('pickup','b')], verbose=1)
print('- these should succeed:')
pyhop(state1,[('pickup','c')], verbose=1)
pyhop(state1,[('unstack','a','b')], verbose=1)
pyhop(state1,[('get','a')], verbose=1)
print('- this should fail:')
pyhop(state1,[('get','b')], verbose=1)
print('- this should succeed:')
pyhop(state1,[('get','c')], verbose=1)
print("""
****************************************
Run pyhop on two block-stacking problems, both of which start in state1.
The goal for the 2nd problem omits some of the conditions in the goal
of the 1st problem, but those conditions will need to be achieved
anyway, so both goals should produce the same plan.
****************************************
""")
print("- Define goal1a:")
"""
A goal is a collection of some (but not necessarily all) of the state variables and their desired values.
Below, both goal1a and goal1b specify c on b, and b on a.
The difference is that goal1a also specifies that a is on table and the hand is empty.
"""
goal1a = Goal('goal1a')
goal1a.pos={'c':'b', 'b':'a', 'a':'table'}
goal1a.clear={'c':True, 'b':False, 'a':False}
goal1a.holding=False
print_goal(goal1a)
print('')
print("- Define goal1b:")
goal1b = Goal('goal1b')
goal1b.pos={'c':'b', 'b':'a'}
print_goal(goal1b)
### goal1b omits some of the conditions of goal1a,
### but those conditions will need to be achieved anyway
pyhop(state1,[('move_blocks', goal1a)], verbose=1)
pyhop(state1,[('move_blocks', goal1b)], verbose=1)
print("""
****************************************
Run pyhop on two more planning problems. As before, the 2nd goal omits
some of the conditions in the 1st goal, but both goals should produce
the same plan.
****************************************
""")
print("- Define state 2:")
state2 = State('state2')
state2.pos={'a':'c', 'b':'d', 'c':'table', 'd':'table'}
state2.clear={'a':True, 'c':False,'b':True, 'd':False}
state2.holding=False
print_state(state2)
print('')
print("- Define goal2a:")
goal2a = Goal('goal2a')
goal2a.pos={'b':'c', 'a':'d', 'c':'table', 'd':'table'}
goal2a.clear={'a':True, 'c':False,'b':True, 'd':False}
goal2a.holding=False
print_goal(goal2a)
print('')
print("- Define goal2b:")
goal2b = Goal('goal2b')
goal2b.pos={'b':'c', 'a':'d'}
print_goal(goal2b)
print('')
### goal2b omits some of the conditions of goal2a,
### but those conditions will need to be achieved anyway.
pyhop(state2,[('move_blocks', goal2a)], verbose=1)
pyhop(state2,[('move_blocks', goal2b)], verbose=1)
print("""
****************************************
Test pyhop on planning problem bw_large_d from the SHOP distribution.
****************************************
""")
print("- Define state3:")
state3 = State('state3')
state3.pos = {1:12, 12:13, 13:'table', 11:10, 10:5, 5:4, 4:14, 14:15, 15:'table', 9:8, 8:7, 7:6, 6:'table', 19:18, 18:17, 17:16, 16:3, 3:2, 2:'table'}
state3.clear = {x:False for x in range(1,20)}
state3.clear.update({1:True, 11:True, 9:True, 19:True})
state3.holding = False
print_state(state3)
print('')
print("- Define goal3:")
goal3 = Goal('goal3')
goal3.pos = {15:13, 13:8, 8:9, 9:4, 4:'table', 12:2, 2:3, 3:16, 16:11, 11:7, 7:6, 6:'table'}
goal3.clear = {17:True, 15:True, 12:True}
print_goal(goal3)
print('')
pyhop(state3,[('move_blocks', goal3)], verbose=1)
print("""
****************************************
Load a modified version of the blocks_world methods, in which the
method for 'get' is replaced with two methods that will sometimes
cause backtracking.
****************************************
""")
import blocks_world_methods2
print_methods()
print("""\n=== In the next call to pyhop, it should backtrack:
the recursion depth should go up, then down, then up again.===\n""")
# verbose=2 tells pyhop to print out a message at each recursion depth
pyhop(state1,[('get', 'a')], verbose=2)
print("""\n=== This time it shouldn't backtrack.===\n""")
pyhop(state1,[('get', 'c')], verbose=2)
print("""\n=== This time it should fail.===\n""")
pyhop(state1,[('get', 'b')], verbose=2)
print("""
****************************************
demonstrate different levels of verbosity
****************************************
""")
print('- verbosity 0:')
pyhop(state1,[('get','a')], verbose=0)
print('- verbosity 1:')
pyhop(state1,[('get','a')], verbose=1)
print('- verbosity 2:')
pyhop(state1,[('get','a')], verbose=2)
print('- verbosity 3:')
pyhop(state1,[('get','a')], verbose=3)