-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBoxPlanner.ml
169 lines (156 loc) · 4.47 KB
/
BoxPlanner.ml
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
open DomainTypes
let actions = [
{ name = "pick";
arity = 3; (* crane, from, box *)
preconditions = [
Pattern("Free", [1]);
Pattern("Top", [3]);
Pattern("On", [2; 3])
];
adds = [
Pattern("Top", [2]);
Pattern("Holding", [1; 3])
];
dels = [
Pattern("Free", [1]);
Pattern("Top", [3]);
Pattern("On", [2; 3])
];
};
{ name = "pickbot";
arity = 2; (* crane, box *)
preconditions = [
Pattern("Free", [1]);
Pattern("Top", [2]);
Pattern("Bot", [2])
];
adds = [
Pattern("Holding", [1; 2])
];
dels = [
Pattern("Free", [1]);
Pattern("Top", [2]);
Pattern("Bot", [2])
];
};
{ name = "drop";
arity = 2; (* crane, box *)
preconditions = [
Pattern("Holding", [1; 2]);
];
adds = [
Pattern("Free", [1]);
Pattern("Top", [2]);
Pattern("Bot", [2])
];
dels = [
Pattern("Holding", [1; 2]);
];
};
{ name = "stack";
arity = 3; (* crane, from, box *)
preconditions = [
Pattern("Top", [2]);
Pattern("Holding", [1; 3])
];
adds = [
Pattern("Free", [1]);
Pattern("Top", [3]);
Pattern("On", [2; 3])
];
dels = [
Pattern("Top", [2]);
Pattern("Holding", [1; 3])
];
};
]
let goal = List.fold_right State.add [
Bound("On", ["Rect1"; "Rect2"]);
Bound("On", ["Rect2"; "Rect3"]);
Bound("On", ["Rect3"; "Rect4"]);
] State.empty
let debug f = Printf.ksprintf (fun s -> Firebug.console##log(Js.string s)) f
let name_to_ix : (string, int) Hashtbl.t = Hashtbl.create 10
let expected_state = ref State.empty
let next_actions = ref []
module S = Set.Make(struct type t = string let compare = compare end)
let all_objs = ref S.empty
let initialize_planner labels =
let arr = Js.str_array labels in
for i = 0 to arr##length - 1 do
ignore(Js.Optdef.map (Js.array_get arr i)
(fun s -> let s = Js.to_string s in
debug "Defined object %s" s;
all_objs := S.add s !all_objs;
Hashtbl.add name_to_ix s i))
done
let reconstruct_state observed_state =
let obs = Array.map (fun fact ->
Array.map Js.to_string (Js.to_array (Js.str_array fact))
) (Js.to_array (Js.Unsafe.coerce observed_state)) in
let state = ref State.empty
and holding = ref false
and tops = ref !all_objs
and bottoms = ref !all_objs in
Array.iter (fun fact ->
match fact.(0) with
| "holding" ->
holding := true;
state := State.add (Bound("Holding", ["Crane"; fact.(1)])) !state;
tops := S.remove fact.(1) !tops;
bottoms := S.remove fact.(1) !bottoms;
| "on" ->
state := State.add (Bound("On", [fact.(1); fact.(2)])) !state;
tops := S.remove fact.(1) !tops;
bottoms := S.remove fact.(2) !bottoms;
| _ -> ()
) obs;
if not !holding then
state := State.add (Bound("Free", ["Crane"])) !state;
S.iter (fun s ->
state := State.add (Bound("Top", [s])) !state;
) !tops;
S.iter (fun s ->
state := State.add (Bound("Bot", [s])) !state;
) !bottoms;
!state
let replan state =
debug "Replanning";
match try Planner.make_plan actions state goal with _ -> None with
| Some(cost, _target, actions, visited) ->
debug "Found new plan wiht cost %d (Visited nodes: %d)" cost visited;
next_actions := actions
| None ->
debug "Found no plan";
next_actions := []
let perform state (action, binding) =
let pick nr = (4, (1, Hashtbl.find name_to_ix (Binding.find nr binding))) in
expected_state := Domain.apply_action state action binding;
debug "Performing action %s" action.name;
match action.name with
| "pick" -> pick 3
| "pickbot" -> pick 2
| "stack" ->
(4, (2, Hashtbl.find name_to_ix (Binding.find 2 binding)))
| "drop" ->
(4, (2, -1))
| _ ->
failwith "Internal Error"
let run_planner observed_state =
let state = reconstruct_state observed_state in
if State.subset goal state then
(3, (0,0))
else (
if not (State.equal state !expected_state) then
replan state;
match !next_actions with
| [] ->
debug "No plan going forward";
(3, (0,0))
| act::acts ->
next_actions := acts;
perform state act
)
let _ =
(Js.Unsafe.coerce Dom_html.window)##initializePlanner <- Js.wrap_callback initialize_planner;
(Js.Unsafe.coerce Dom_html.window)##runPlanner <- Js.wrap_callback run_planner