forked from garydoranjr/pyddl
/
forward_search_block_stacking.py
92 lines (77 loc) · 2.51 KB
/
forward_search_block_stacking.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
from pyddl import Domain, Problem, State, Action, neg, backwards_planner, planner
import visualize_search
"""
Logic space for planner
"""
# Object types
type_gripper = "gripper"
type_block = "block"
# logic states predicates
focus = "focus" # set focus on a block block
in_hand = "in_hand" # a block is in hand (block, gripper)
block_free = "block_free" # condition a block is graspable
hand_empty = "hand_empty" # condition a hand is free
b_on_b = "block_on_block" # condition block is on block
actions = [Action("Grab_Block",
parameters=(
(type_gripper, "g"),
(type_block, "b"),
),
preconditions=(
(hand_empty, "g"),
(block_free, "b")
),
effects=(
neg((hand_empty, "g")),
(in_hand, "b", "g"),
),
unique=True),
Action("Place_Block",
parameters=(
(type_gripper, "g"),
(type_block, "b"),
(type_block, "b_placed")
),
preconditions=(
(in_hand, "b", "g"),
(block_free, "b_placed")
),
effects=(
(b_on_b, "b", "b_placed"),
neg((block_free, "b_placed")),
neg((in_hand, "b", "g")),
(hand_empty, "g"),
),
unique=True)
]
# setup config and get frame names
block_names = [f"b{x}" for x in range(1, 4)]
gripper_name = "R_gripper"
# put all objects into one dictionary with types
scene_obj = {
type_block: block_names,
type_gripper: (gripper_name,)
}
# get simple action from all controllers
domain = Domain(actions)
# goal is for now numerical order of block placed on each other, unless specified otherwise
goal = [(b_on_b, scene_obj[type_block][i], scene_obj[type_block][i + 1])\
for i in range(len(scene_obj[type_block]) - 1)]
# also append free hand
goal.append((hand_empty, scene_obj[type_gripper][0]))
print(goal)
# normal initial conditions
init_free_hand = (hand_empty, scene_obj[type_gripper][0])
init_free_blocks = [(block_free, block) for block in scene_obj[type_block]]
# extend all initial conditions
init = [init_free_hand]
init.extend(init_free_blocks)
# define problem here
prob = Problem(
domain,
scene_obj,
init=init,
goal=goal
)
plan, G = planner(prob, monotone=False)
visualize_search.draw_search_graph([], [], G, "simple_path.png")