Source file trajectory.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
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
209
210
211
212
213
214
type ('obs, 'act) t = {
observations : 'obs array;
actions : 'act array;
rewards : float array;
terminateds : bool array;
truncateds : bool array;
infos : Info.t array;
log_probs : float array option;
values : float array option;
}
let create ~observations ~actions ~rewards ~terminateds ~truncateds
?(infos = [||]) ?log_probs ?values () =
let n = Array.length observations in
if
n <> Array.length actions
|| n <> Array.length rewards
|| n <> Array.length terminateds
|| n <> Array.length truncateds
then invalid_arg "Trajectory.create: arrays must have same length";
let infos =
if Array.length infos = 0 then Array.make n Info.empty
else if Array.length infos = n then infos
else
invalid_arg "Trajectory.create: infos array must be empty or same length"
in
let log_probs =
match log_probs with
| Some arr when Array.length arr <> n ->
invalid_arg "Trajectory.create: log_probs must have same length"
| Some arr -> Some arr
| None -> None
in
let values =
match values with
| Some arr when Array.length arr <> n ->
invalid_arg "Trajectory.create: values must have same length"
| Some arr -> Some arr
| None -> None
in
{
observations;
actions;
rewards;
terminateds;
truncateds;
infos;
log_probs;
values;
}
let length t = Array.length t.observations
let concat trajectories =
match trajectories with
| [] -> invalid_arg "Trajectory.concat: empty list"
| [ t ] -> t
| ts ->
let observations = Array.concat (List.map (fun t -> t.observations) ts) in
let actions = Array.concat (List.map (fun t -> t.actions) ts) in
let rewards = Array.concat (List.map (fun t -> t.rewards) ts) in
let terminateds = Array.concat (List.map (fun t -> t.terminateds) ts) in
let truncateds = Array.concat (List.map (fun t -> t.truncateds) ts) in
let infos = Array.concat (List.map (fun t -> t.infos) ts) in
let log_probs =
if List.for_all (fun t -> Option.is_some t.log_probs) ts then
Some (Array.concat (List.map (fun t -> Option.get t.log_probs) ts))
else None
in
let values =
if List.for_all (fun t -> Option.is_some t.values) ts then
Some (Array.concat (List.map (fun t -> Option.get t.values) ts))
else None
in
{
observations;
actions;
rewards;
terminateds;
truncateds;
infos;
log_probs;
values;
}
let collect env ~policy ~n_steps =
let observations = ref [] in
let actions = ref [] in
let rewards = ref [] in
let terminateds = ref [] in
let truncateds = ref [] in
let infos = ref [] in
let log_probs = ref [] in
let values = ref [] in
let obs, _info = Env.reset env () in
let current_obs = ref obs in
let steps_collected = ref 0 in
while !steps_collected < n_steps do
let action, log_prob_opt, value_opt = policy !current_obs in
observations := !current_obs :: !observations;
actions := action :: !actions;
(match log_prob_opt with
| Some lp -> log_probs := lp :: !log_probs
| None -> ());
(match value_opt with Some v -> values := v :: !values | None -> ());
let transition = Env.step env action in
rewards := transition.Env.reward :: !rewards;
terminateds := transition.Env.terminated :: !terminateds;
truncateds := transition.Env.truncated :: !truncateds;
infos := transition.Env.info :: !infos;
current_obs := transition.Env.observation;
steps_collected := !steps_collected + 1;
if transition.Env.terminated || transition.Env.truncated then
let obs, _info = Env.reset env () in
current_obs := obs
done;
let log_probs_arr =
if List.length !log_probs = n_steps then
Some (Array.of_list (List.rev !log_probs))
else None
in
let values_arr =
if List.length !values = n_steps then
Some (Array.of_list (List.rev !values))
else None
in
create
~observations:(Array.of_list (List.rev !observations))
~actions:(Array.of_list (List.rev !actions))
~rewards:(Array.of_list (List.rev !rewards))
~terminateds:(Array.of_list (List.rev !terminateds))
~truncateds:(Array.of_list (List.rev !truncateds))
~infos:(Array.of_list (List.rev !infos))
?log_probs:log_probs_arr ?values:values_arr ()
let collect_episodes env ~policy ~n_episodes ?(max_steps = 1000) () =
let episodes = ref [] in
for _ = 1 to n_episodes do
let observations = ref [] in
let actions = ref [] in
let rewards = ref [] in
let terminateds = ref [] in
let truncateds = ref [] in
let infos = ref [] in
let log_probs = ref [] in
let values = ref [] in
let obs, _info = Env.reset env () in
let current_obs = ref obs in
let steps = ref 0 in
let done_flag = ref false in
while !steps < max_steps && not !done_flag do
let action, log_prob_opt, value_opt = policy !current_obs in
observations := !current_obs :: !observations;
actions := action :: !actions;
(match log_prob_opt with
| Some lp -> log_probs := lp :: !log_probs
| None -> ());
(match value_opt with Some v -> values := v :: !values | None -> ());
let transition = Env.step env action in
rewards := transition.Env.reward :: !rewards;
terminateds := transition.Env.terminated :: !terminateds;
truncateds := transition.Env.truncated :: !truncateds;
infos := transition.Env.info :: !infos;
current_obs := transition.Env.observation;
steps := !steps + 1;
done_flag := transition.Env.terminated || transition.Env.truncated
done;
let n = !steps in
let log_probs_arr =
if List.length !log_probs = n then
Some (Array.of_list (List.rev !log_probs))
else None
in
let values_arr =
if List.length !values = n then Some (Array.of_list (List.rev !values))
else None
in
let episode =
create
~observations:(Array.of_list (List.rev !observations))
~actions:(Array.of_list (List.rev !actions))
~rewards:(Array.of_list (List.rev !rewards))
~terminateds:(Array.of_list (List.rev !terminateds))
~truncateds:(Array.of_list (List.rev !truncateds))
~infos:(Array.of_list (List.rev !infos))
?log_probs:log_probs_arr ?values:values_arr ()
in
episodes := episode :: !episodes
done;
List.rev !episodes