-
Notifications
You must be signed in to change notification settings - Fork 1
/
hierarchical_task_network_planner.py
205 lines (167 loc) · 8.27 KB
/
hierarchical_task_network_planner.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
import pyhop
class HierarchicalTaskNetworkPlanner:
"""
Automated planner for hierarchical tasks (with pre-conditions, sub-tasks and effects).
Deterministic, with backtracking search. Elements:
1. Compound tasks (Methods): Compositions of simpler tasks.
2. Primitive tasks (Operators): Base tasks.
3. Helper methods.
"""
def __init__(self, init_world_model):
self.failure_reason = ""
self.planner_world_model = init_world_model.current_world_model.planner
self.verbose = self.planner_world_model["verbose"]
# Helper methods
def is_within_bounds(location1, min_bounds, max_bounds):
for i in range(len(location1)):
if location1[i] < min_bounds[i] or location1[i] > max_bounds[i]:
return False
return True
# Operators (primitive tasks)
def move_arm(state, to_):
arm_min_bounds = state.min_bounds["xyz"]
arm_max_bounds = state.max_bounds["xyz"]
if to_ == "target_object" or to_ == "container":
xyz = state.xyz[to_]
if not is_within_bounds(xyz, arm_min_bounds, arm_max_bounds):
self.failure_reason = "Can't move arm to {}: {} outside of bound min: {}, max: {}."\
.format(to_, xyz, arm_min_bounds, arm_max_bounds)
return False
return state
return state
def move_arm_above(state, to_):
return move_arm(state, to_)
def move_arm_up(state, to_):
return move_arm(state, to_)
def close_hand(state):
gripper_min_bounds = state.min_bounds["object_side_length"]
gripper_max_bounds = state.max_bounds["object_side_length"]
distance = state.size['object_side_length']
if distance < gripper_min_bounds or distance > gripper_max_bounds:
self.failure_reason = "Can't close hand to width {}, outside of min: {}, max: {}."\
.format(distance, gripper_min_bounds, gripper_max_bounds)
return False
return state
def open_hand(state):
gripper_max_bounds = state.max_bounds["object_side_length"]
distance = state.size['object_side_length']
if distance > gripper_max_bounds:
self.failure_reason = "Can't open hand to width {}, outside of max: {}."\
.format(distance, gripper_max_bounds)
return False
return state
def initialize(state, actor):
if actor == "arm":
state.initialized["arm"] = True
return state
self.failure_reason = "{} can't initialize".format(actor)
return False
pyhop.declare_operators(initialize, move_arm, move_arm_above, move_arm_up, close_hand, open_hand)
if self.verbose > 0:
pyhop.print_operators()
# Methods (compound tasks)
def put_grabbed(state, actor, actee, from_, to_):
if actor == "arm":
if state.grabbed["target_object"]:
return [
('move_arm_above', to_),
('open_hand',)
]
return False
def full_transfer(state, actor, actee, from_, to_):
if actor == "arm":
return [('initialize', actor),
('open_hand',),
('move_arm_above', 'target_object'),
('move_arm', 'target_object'),
('close_hand',),
('move_arm_up', 'target_object'),
('move_arm_above', to_),
('open_hand',)
]
return False
def transfer(state, actor, actee, from_, to_):
if actor == "arm":
if state.initialized["arm"]:
return [('move_arm_above', 'target_object'),
('move_arm', 'target_object'),
('close_hand',),
('move_arm_up', 'target_object'),
('move_arm_above', to_),
('open_hand',)
]
return False
# TODO: nesting of more than 2 levels deep
pyhop.declare_methods('transfer_target_object_to_container', transfer, put_grabbed, full_transfer)
def get_plans(self, world_model, goal):
"""
Returns all the suggested plans, given a world model (state object), goal (list) and an internal collection of
primitive and compound tasks.
:param world_model: The current perceived world state (json object).
:param goal: The end goal we try to achieve (tuple).
:return: List of suggested plans.
"""
if goal is not "":
return pyhop.pyhop(world_model, goal, verbose=self.verbose, all_plans=True, sort_asc=True)
else:
return ""
if __name__ == '__main__':
end_goal = [('transfer_target_object_to_container', 'arm', 'target_object', 'table', 'container')]
intentions = end_goal # I := I0; Initial Intentions
from world_model import WorldModel
beliefs = WorldModel() # B := B0; Initial Beliefs
htn_planner = HierarchicalTaskNetworkPlanner(beliefs)
print()
print("GOAL:\n{}".format(end_goal))
print()
pyhop.print_operators()
print()
beliefs.current_world_model.xyz["target_object"] = [-10, -10, 0]
beliefs.current_world_model.grabbed["target_object"] = True
htn_plans = htn_planner.get_plans(beliefs.current_world_model, intentions) # π := plan(B, I); MEANS_END REASONING
if not htn_plans:
print("== No valid plan. Failure_reason: {}".format(htn_planner.failure_reason))
else:
beliefs.current_world_model.plans = htn_plans
print("== BEST PLAN:\n", beliefs.current_world_model.plans[0])
if len(beliefs.current_world_model.plans) > 1:
print("-- Alternative PLAN: ")
for action in beliefs.current_world_model.plans[1]:
print("{}, ".format(action))
print()
beliefs.current_world_model.xyz["target_object"] = [-510, -10, 0]
beliefs.current_world_model.grabbed["target_object"] = False
htn_plans = htn_planner.get_plans(beliefs.current_world_model, intentions) # π := plan(B, I); MEANS_END REASONING
if not htn_plans:
print("== No valid plan. Failure_reason:\n{}".format(htn_planner.failure_reason))
else:
beliefs.current_world_model.plans = htn_plans
print("Best current_world_model.plan: ", beliefs.current_world_model.plans[0])
print()
beliefs.current_world_model.xyz["target_object"] = [-10, -10, 0]
beliefs.current_world_model.xyz["container"] = [-310, -10, 0]
beliefs.current_world_model.grabbed["target_object"] = False
htn_plans = htn_planner.get_plans(beliefs.current_world_model, intentions) # π := plan(B, I); MEANS_END REASONING
if not htn_plans:
print("== No valid plan. Failure_reason:\n{}".format(htn_planner.failure_reason))
else:
beliefs.current_world_model.plans = htn_plans
print("Best current_world_model.plan: ", beliefs.current_world_model.plans[0])
print()
beliefs.current_world_model.xyz["container"] = [-10, -10, 0]
beliefs.current_world_model.xyz["target_object"] = [-10, -10, 0]
beliefs.current_world_model.size['object_side_length'] = 20.0
htn_plans = htn_planner.get_plans(beliefs.current_world_model, intentions) # π := plan(B, I); MEANS_END REASONING
if not htn_plans:
print("== No valid plan. Failure_reason:\n{}".format(htn_planner.failure_reason))
else:
beliefs.current_world_model.plans = htn_plans
print("Best current_world_model.plan: ", beliefs.current_world_model.plans[0])
print()
beliefs.current_world_model.size['object_side_length'] = 0.2
htn_plans = htn_planner.get_plans(beliefs.current_world_model, intentions) # π := plan(B, I); MEANS_END REASONING
if not htn_plans:
print("== No valid plan. Failure_reason:\n{}".format(htn_planner.failure_reason))
else:
beliefs.current_world_model.plans = htn_plans
print("Best current_world_model.plan: ", beliefs.current_world_model.plans[0])