-
Notifications
You must be signed in to change notification settings - Fork 2
/
mGP_cmaes_full.m
executable file
·357 lines (307 loc) · 12.5 KB
/
mGP_cmaes_full.m
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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
% a planning example
close all
clear all
clear
clc
root_folder = pwd;
% Random number generator
% matlab_parameters.seed_num = 3;
% rng(matlab_parameters.seed_num, 'twister');
%% Environment
model_name = 'cylinder'; % cylinder, boeing747
model.name = model_name;
% mesh
data_mesh = load([model_name, '_mesh.mat']);
model.TR = data_mesh.TR;
model.valid_faces = data_mesh.valid_faces;
TR = data_mesh.TR;
% occupancy
data_occupancy = load([model_name, '_map_occupancy']);
model.occupancy = data_occupancy.occupancy;
% esdf
data_esdf = load([model_name, '_map_esdf']);
model.esdf = data_esdf.esdf;
% true temperature field
data_temperature_field = load([model_name, '_temperature_field']);
model.temperature_field = data_temperature_field.F_value;
%% Parameters
[map_parameters, sensor_parameters, planning_parameters, optimization_parameters, ...
matlab_parameters] = load_parameteres(model);
%% Ground truth and initial map
dim_x_env = map_parameters.dim_x_env;
dim_y_env = map_parameters.dim_y_env;
dim_z_env = map_parameters.dim_z_env;
dim_xyz_plot = [dim_x_env, dim_y_env, 0, dim_z_env(2)];
% dim_xyz_plot = [dim_x_env, dim_y_env, dim_z_env];
% dim_xyz_plot = [-9 9 -9 9 0 25];
% dim_xyz_plot = [-8 80 -40 40 -4 16];
ground_truth_faces_map = create_ground_truth_map(map_parameters);
faces_map = create_initial_map(map_parameters);
P_prior = diag(faces_map.P);
if (matlab_parameters.visualize_map)
figure;
subplot(2, 4, 1)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title('Ground truth map')
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), ground_truth_faces_map, 'EdgeAlpha', 0);
caxis([0, 1]);
colormap jet
subplot(2, 4, 2)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title('Mean - prior')
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), faces_map.m, 'EdgeAlpha', 0);
caxis([0, 1]);
colormap jet
subplot(2, 4, 6)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title(['Var. - prior. Trace = ', num2str(trace(faces_map.P), 5)])
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), P_prior, 'EdgeAlpha', 0);
var_max = max(P_prior);
caxis([0 var_max]);
end
%% Take first measurement
viewpoint_init = [-7.0711 -7.0711 4.0000 0.7854]; %[10, 0, 4, -pi]
% viewpoint_init = [-4 0 0 0];
% comment if not taking a first measurement
faces_map = take_measurement_at_viewpoint(viewpoint_init, faces_map, ...
ground_truth_faces_map, map_parameters, sensor_parameters);
P_post = diag(faces_map.P);
P_trace_init = trace(faces_map.P);
P_prior = P_post;
if (matlab_parameters.visualize_map)
subplot(2, 4, 3)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title('Mean - init ')
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), faces_map.m, 'EdgeAlpha', 0);
caxis([0 1]);
colormap jet
subplot(2, 4, 7)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title(['Var. - init Trace = ', num2str(trace(faces_map.P), 5)])
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), P_post, 'EdgeAlpha', 0);
caxis([0 var_max]);
end
%% Lattice viewpoints
data_lattice = load([model_name, '_lattice_viewpoints.mat']);
lattice_viewpoints = data_lattice.lattice_viewpoints;
num_lattice_viewpoints = size(lattice_viewpoints, 1);
%% Planning-Execution:
P_trace_prev = P_trace_init;
viewpoint_prev = viewpoint_init;
time_elapsed = 0;
metrics = initialize_metrics_inspect();
while (time_elapsed < planning_parameters.time_budget)
%% Step 1. Grid search on the lattice viewpoints
path = search_lattice_viewpoints(viewpoint_prev, lattice_viewpoints, ...
faces_map, map_parameters, sensor_parameters, planning_parameters);
obj = compute_objective_inspect(path, faces_map, map_parameters, sensor_parameters, ...
planning_parameters, optimization_parameters);
disp(['Objective before optimization: ', num2str(obj)]);
%% STEP 2. CMA-ES optimization, only optimize position now.
path_optimized = optimize_with_cmaes_inspect(path, faces_map, map_parameters, ...
sensor_parameters, planning_parameters, optimization_parameters);
%% Plan Execution %%
% Create polynomial path through the control points.
trajectory = plan_path_waypoints(path_optimized(:,1:3), ...
planning_parameters.max_vel, planning_parameters.max_acc);
% Find best yaw for each control point if not optimizing
if (optimization_parameters.opt_yaw)
control_yaws = path_optimized(:,4);
else
control_yaws = zeros(size(path_optimized, 1), 1);
for i = 1 : size(path_optimized,1)
control_yaws(i) = get_best_yaw(path_optimized(i,1:3), map_parameters);
end
end
% Also create the yaw trajectory
segment_time = zeros(trajectory.num_elements, 1);
for i = 2 : trajectory.num_elements
segment_time(i) = trajectory.segments(i-1).time;
end
yaw_trajectory = plan_yaw_waypoints(control_yaws, segment_time);
% Sample trajectory to find locations to take measurements at.
[times_meas, points_meas, ~, ~] = ...
sample_trajectory(trajectory, 1/planning_parameters.measurement_frequency);
[~, yaws_meas, ~, ~] = sample_trajectory(yaw_trajectory, ...
1/planning_parameters.measurement_frequency);
trajectory_time = get_trajectory_total_time(trajectory);
% Alternatively, spercify a yaw to the measurement point
if planning_parameters.plan_yaw == 0
for i = 1 : size(points_meas,1)
yaws_meas(i) = get_best_yaw(points_meas(i,1:3), map_parameters);
end
end
% Remove the viewpoints beyond budget
add_end_viewpoint = 0;
idx_in_budget = find(times_meas <= planning_parameters.time_budget-time_elapsed);
if length(idx_in_budget) < length(times_meas)
% reduce trajectory time
trajectory_time = planning_parameters.time_budget - time_elapsed;
% add endtime viewpoint also as a measurement
add_end_viewpoint = 1;
[times_temp, points_temp, ~, ~] = sample_trajectory(trajectory, 0.1);
[~, yaws_temp, ~, ~] = sample_trajectory(yaw_trajectory, 0.1);
idx_temp = find(times_temp <= planning_parameters.time_budget-time_elapsed);
times_meas_last = planning_parameters.time_budget - time_elapsed;
points_meas_last = points_temp(idx_temp(end),:);
yaws_meas_last = yaws_temp(idx_temp(end),:);
end
times_meas = times_meas(idx_in_budget);
points_meas = points_meas(idx_in_budget,:);
yaws_meas = yaws_meas(idx_in_budget,:);
% Add endtime viewpoint also as a measurement
if add_end_viewpoint
times_meas = [times_meas, times_meas_last];
points_meas = [points_meas; points_meas_last];
yaws_meas = [yaws_meas; yaws_meas_last];
end
% Combine the viewpoints
num_points_meas = size(points_meas,1);
viewpoints_meas = [points_meas, yaws_meas];
% Take measurements along path.
for i = 1:num_points_meas
faces_map = take_measurement_at_viewpoint(viewpoints_meas(i,:), faces_map, ...
ground_truth_faces_map, map_parameters, sensor_parameters);
metrics.faces_map_m = [metrics.faces_map_m; faces_map.m'];
metrics.faces_map_P_diag = [metrics.faces_map_P_diag; diag(faces_map.P)'];
metrics.P_traces = [metrics.P_traces; trace(faces_map.P)];
metrics.rmses = [metrics.rmses; compute_rmse(faces_map.m(map_parameters.valid_faces), ...
ground_truth_faces_map(map_parameters.valid_faces))];
metrics.wrmses = [metrics.wrmses; compute_wrmse(faces_map.m(map_parameters.valid_faces), ...
ground_truth_faces_map(map_parameters.valid_faces))];
metrics.mlls = [metrics.mlls; compute_mll(faces_map, ground_truth_faces_map)];
metrics.wmlls = [metrics.wmlls; compute_wmll(faces_map, ground_truth_faces_map)];
end
disp(['Trace after execution: ', num2str(trace(faces_map.P))]);
disp(['Time after execution: ', num2str(get_trajectory_total_time(trajectory))]);
gain = P_trace_init - trace(faces_map.P);
if (strcmp(planning_parameters.obj, 'rate'))
cost = max(get_trajectory_total_time(trajectory), 1/planning_parameters.measurement_frequency);
disp(['Objective after optimization: ', num2str(-gain/cost)]);
elseif (strcmp(planning_parameters.obj, 'exponential'))
cost = get_trajectory_total_time(trajectory);
disp(['Objective after optimization: ', num2str(-gain*exp(-planning_parameters.lambda*cost))]);
end
metrics.viewpoints_meas = [metrics.viewpoints_meas; viewpoints_meas];
metrics.times = [metrics.times; time_elapsed + times_meas'];
metrics.path_travelled = [metrics.path_travelled; path_optimized];
metrics.trajectory_travelled = [metrics.trajectory_travelled; trajectory];
P_trace_prev = trace(faces_map.P);
viewpoint_prev = [path_optimized(end,1:3),control_yaws(end)]; % End of trajectory (not last meas. point!)
time_elapsed = time_elapsed + trajectory_time;
disp(['Time elapsed: ', num2str(time_elapsed)]);
end
if (matlab_parameters.visualize_map)
subplot(2, 4, 4)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title('Mean - final ')
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), metrics.faces_map_m(end,:)', 'EdgeAlpha', 0);
caxis([0 1]);
colormap jet
subplot(2, 4, 8)
hold on;
axis(dim_xyz_plot);
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
title(['Var. - final Trace = ', num2str(metrics.P_traces(end,:), 5)])
daspect([1 1 1]);
view(3);
trisurf(TR.ConnectivityList, TR.Points(:,1), TR.Points(:,2), ...
TR.Points(:,3), metrics.faces_map_P_diag(end,:)', 'EdgeAlpha', 0);
caxis([0 var_max]);
end
if (matlab_parameters.visualize_path)
fig_path = figure;
hold on;
xlabel('x [m]');
ylabel('y [m]');
zlabel('z [m]');
ax_path = fig_path.CurrentAxes;
daspect(ax_path, [1 1 1]);
view(ax_path, 3);
% mesh object
h_mesh = trimesh(TR);
h_mesh.FaceColor = 'w';
h_mesh.FaceAlpha = 1;
h_mesh.EdgeColor = 'c';
h_mesh.LineWidth = 0.5;
h_mesh.LineStyle = '-';
num_path_segments = size(metrics.trajectory_travelled, 1);
% path and viewpoints
axis(dim_xyz_plot);
plot_path_viewpoints(ax_path, num_path_segments, metrics.path_travelled, ...
metrics.trajectory_travelled, metrics.viewpoints_meas);
% camera fov
if (matlab_parameters.visualize_cam)
for i = 1 : size(metrics.viewpoints_meas, 1)
% pause;
cam_pos = metrics.viewpoints_meas(i, 1:3)';
cam_roll = sensor_parameters.cam_roll;
cam_pitch = sensor_parameters.cam_pitch;
cam_yaw = sensor_parameters.cam_yaw + metrics.viewpoints_meas(i,4);
plot_camera_fov(ax_path, cam_pos, cam_roll, cam_pitch, cam_yaw, ...
sensor_parameters.fov_x, sensor_parameters.fov_y, ...
sensor_parameters.fov_range_max, 'r');
[F_visible, faces_visible] = get_visible_faces(map_parameters.num_faces, ...
map_parameters.F_points, map_parameters.F_center, ...
map_parameters.F_normal, cam_pos, cam_roll, cam_pitch, cam_yaw, sensor_parameters);
for iFace = 1 : map_parameters.num_faces
if F_visible(iFace) == 1
patch(ax_path, 'XData', map_parameters.F_points(iFace, 1, :), ...
'YData', map_parameters.F_points(iFace, 2, :), ...
'ZData', map_parameters.F_points(iFace, 3, :), ...
'FaceColor', 'b', ...
'FaceAlpha', 0.5, ...
'EdgeColor', 'b');
end
end
end
end
end
figure;
plot_metrics(metrics);
save([root_folder, '/logs/cylinder/', model_name, '_cmaes_', 'kernel_', ...
num2str(map_parameters.kernel_choice), '_metrics.mat'], 'metrics');