-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEXP_EKF.m
351 lines (301 loc) · 11.6 KB
/
EXP_EKF.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
%% RAL Experiment: How do measurement noise and # shapes affect performance?
% Dataset: pascal+car
% Constants: K, N, spiral trajectory
% Independent variable: measurement noise, L
% Dependent variables: runtime, duality gap, accuracy (p, R, c)
%
% Lorenzo Shaikewitz for SPARK Lab
clc; clear; close all
%% Experiment settings
indepVar = "noiseSigmaSqrt";
savename = "pascalaeroplane_EKFGT_TEST_" + indepVar;
lengthScale = 0.2; % smallest dimension
domain = 0.025:0.025:0.5; % 0:0.025:1
Ldomain = [4,8,12]; % 2: 3:12;
num_repeats = 500; % 2: 50
%% Loop
results = cell(length(domain),1);
parfor (index = 1:length(domain)) % PAR, 20)
iv = domain(index);
resultsIV = struct();
resultsIV.(indepVar) = iv;
resultsIV.R_err_ours = zeros(num_repeats,length(Ldomain));
resultsIV.R_err_ekf = zeros(num_repeats,1);
resultsIV.R_err_pace = zeros(num_repeats,1);
resultsIV.R_err_castp = zeros(num_repeats,1);
resultsIV.p_err_ours = zeros(num_repeats,length(Ldomain));
resultsIV.p_err_ekf = zeros(num_repeats,1);
resultsIV.p_err_pace = zeros(num_repeats,1);
resultsIV.p_err_castp = zeros(num_repeats,1);
resultsIV.c_err_ours = zeros(num_repeats,length(Ldomain));
resultsIV.c_err_pace = zeros(num_repeats,1);
resultsIV.c_err_castp = zeros(num_repeats,1);
resultsIV.gap_ours = zeros(num_repeats,length(Ldomain));
resultsIV.gap_pace = zeros(num_repeats,1);
resultsIV.gap_castp = zeros(num_repeats,1);
resultsIV.time_ours = zeros(num_repeats,length(Ldomain));
resultsIV.time_pace = zeros(num_repeats,1);
resultsIV.time_castp = zeros(num_repeats,1);
disp("Starting " + indepVar + "=" + string(iv));
for j = 1:num_repeats
problem = struct();
problem.category = "aeroplane";
problem.L = max(Ldomain); % nr of keyframes in horizon
problem.outlierRatio = 0.0;
problem.noiseSigmaSqrt = iv*lengthScale; % [m]
% MLE parameters
problem.accelerationNoiseBoundSqrt = 0.05*lengthScale;
problem.rotationKappa = 1/(0.05*lengthScale)^2*1/2;
problem.covar_measure_base = problem.noiseSigmaSqrt^2;
problem.covar_velocity_base = problem.accelerationNoiseBoundSqrt^2;
problem.kappa_rotrate_base = problem.rotationKappa;
% for EKF
% problem.covar_measure_position = 5e-2*ones(1,3);
problem.covar_measure_position = iv/100*ones(1,3);
problem.covar_measure_rotation = problem.covar_measure_position*2;
problem.noiseBound = 3*iv*lengthScale; %3*iv for 8 reg
problem.translationBound = 10.0;
problem.velocityBound = 5.0;
problem.dt = 1.0;
problem.velprior = "body"; % constant body frame velocity
% add shape, measurements, outliers
problem = gen_pascal_tracking(problem);
lambda = 0;
problem.lambda = lambda;
% Solve!
pace = struct();
pace.p = problem.p_gt + sqrt(problem.covar_measure_position(1))*randn(3,1,problem.L);
for l = 1:problem.L
rnoise = 1*sqrt(problem.covar_measure_rotation(1))*randn(1,3);
pace.R(:,:,l) = problem.R_gt(:,:,l)*axang2rotm([rnoise./norm(rnoise), norm(rnoise)]);
end
paceekf = pace_ekf2(problem,pace); % try pace_py_ukf (issues with parfor)
% Save solutions: only use last error
% rotation error
R_err_ekf = getAngularError(problem.R_gt(:,:,end), paceekf.R(:,:,end));
R_err_pace = getAngularError(problem.R_gt(:,:,end), pace.R(:,:,end));
% position error
p_err_ekf = norm(problem.p_gt(:,:,end) - paceekf.p(:,:,end));
p_err_pace = norm(problem.p_gt(:,:,end) - pace.p(:,:,end));
% save
resultsIV.R_err_ekf(j) = R_err_ekf;
resultsIV.R_err_pace(j) = R_err_pace;
resultsIV.p_err_ekf(j) = p_err_ekf;
resultsIV.p_err_pace(j) = p_err_pace;
end
results{index} = resultsIV;
end
results = [results{:}];
% save
save("../datasets/results/" + savename + ".mat","results")
%%
load("../datasets/results/" + savename + ".mat","results")
%% Display Results
% data settings
Llist = [1,2,3];
displayRange = 1:length(results);
% visual settings
tile = true;
settings.PACEEKF = {'x-.','DisplayName', 'PACE-EKF', 'Color', "#D95319",'LineWidth',2};
settings.PACERAW = {'x:','DisplayName', 'PACE-RAW', 'Color', "#EDB120",'LineWidth',2};
settings.OURS = {{'x-','DisplayName', 'CAST','LineWidth',2.5,'Color','338eca','MarkerSize',10};...
{'square-','DisplayName', 'CAST','LineWidth',2.5,'Color','005b97','MarkerSize',5};...
{'.-','DisplayName', 'CAST','LineWidth',2.5,'Color','002e4c','MarkerSize',20}};
settings.ours_colors = ["#338eca","#005b97","#002e4c"];
settings.Llist = Llist;
settings.Ldomain = Ldomain;
% restrict domain and normalize positions
resultsAdj = results(:,displayRange);
for j = 1:length(resultsAdj)
resultsAdj(j).p_err_ekf = resultsAdj(j).p_err_ekf/lengthScale;
resultsAdj(j).p_err_castp = resultsAdj(j).p_err_castp/lengthScale;
resultsAdj(j).p_err_pace = resultsAdj(j).p_err_pace/lengthScale;
resultsAdj(j).p_err_ours = resultsAdj(j).p_err_ours/lengthScale;
end
% created tiled figure
if tile
figure
t=tiledlayout(1,5);
% title(t,'Measurement Noise')
end
% Positions
if (tile); nexttile; else; figure; end
plotvariable(resultsAdj, indepVar, "p_err", settings)
yscale log;% xscale log
xlabel(indepVar); ylabel("Position Error (normalized)");
title("Position Errors")
if tile
lg = legend('Orientation','horizontal');
lg.Layout.Tile = 'south';
end
% Rotations
if (tile); nexttile; else; figure; end
plotvariable(resultsAdj, indepVar, "R_err", settings)
yscale log;% xscale log
xlabel(indepVar); ylabel("Rotation Error (deg)");
title("Rotation Errors")
% Shape
settings=rmfield(settings,"PACEEKF");
if (tile); nexttile; else; figure; end
plotvariable(resultsAdj, indepVar, "c_err", settings)
yscale log;% xscale log
xlabel(indepVar); ylabel("Shape Error");
title("Shape Errors")
% Gap
if (tile); nexttile; else; figure; end
plotvariable(resultsAdj, indepVar, "gap", settings)
yscale log;% xscale log
xlabel(indepVar); ylabel("Gap");
title("Suboptimality Gaps")
% Time
if (tile); nexttile; else; figure; end
plotvariable(resultsAdj, indepVar, "time", settings)
yscale log;% xscale log
xlabel(indepVar); ylabel("Time (s)");
title("Run Times")
%% Display Results
% process into displayable form
% settings.OURS = {'DisplayName', 'OURS','LineWidth',3};
% ours_colors = ["#000000", "#002e4c", "#00395f", "#004471", "#005084",...
% "#005b97", "#0067aa","#0072bd", "#1a80c4", "#338eca",...
% "#4d9cd1","#66aad7","#80b9de"];
ours_colors = ["#002e4c", "#005b97","#338eca","#80b9de"];
settings.PACEEKF = {'DisplayName', 'PACE-EKF', 'Color', "#D95319"};
settings.PACERAW = {'DisplayName', 'PACE-RAW', 'Color', "#EDB120"};
figure
t=tiledlayout(2,2);
title(t,'Measurement Noise')
set(0,'DefaultLineLineWidth',1)
% display_range = 2:length(domain);
% display_range = 1:17;
% results = results(:,display_range);
Llist = [1,2,3];
% Rotation figure
nexttile
hold on
c=plot([results.(indepVar)],median([results.R_err_pace]),'x-',settings.PACERAW{:});
errorshade([results.(indepVar)],[results.R_err_pace],get(c,'Color'));
% b=plot([results.(indepVar)],median([results.R_err_ekf]),'x-',settings.PACEEKF{:});
% errorshade([results.(indepVar)],[results.R_err_ekf],get(b,'Color'));
res = [results.R_err_ours];
for lidx = Llist
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
plotsettings = {'DisplayName', "OURS-" + string(L),'LineWidth',3,'Color',ours_colors(length(Ldomain)-lidx+1)};
a=plot([results.(indepVar)],median(res(:,lrange)),'x-',plotsettings{:});
errorshade([results.(indepVar)],res(:,lrange),get(a,'Color'));
end
yscale log;% xscale log
xlabel(indepVar); ylabel("Rotation Error (deg)");
title("Rotation Errors")
% position figure
nexttile
hold on
b=plot([results.(indepVar)],median([results.p_err_ekf])/lengthScale,'x-',settings.PACEEKF{:});
errorshade([results.(indepVar)],[results.p_err_ekf]/lengthScale,get(b,'Color'));
c=plot([results.(indepVar)],median([results.p_err_pace])/lengthScale,'x-',settings.PACERAW{:});
errorshade([results.(indepVar)],[results.p_err_pace]/lengthScale,get(c,'Color'));
res = [results.p_err_ours];
for lidx = Llist
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
plotsettings = {'DisplayName', "OURS-" + string(L),'LineWidth',3,'Color',ours_colors(length(Ldomain)-lidx+1)};
a=plot([results.(indepVar)],median(res(:,lrange))/lengthScale,'x-',plotsettings{:});
errorshade([results.(indepVar)],res(:,lrange)/lengthScale,get(a,'Color'));
end
yscale log;% xscale log
xlabel(indepVar); ylabel("Position Error (normalized)");
title("Position Errors")
lg = legend('Orientation','horizontal');
lg.Layout.Tile = 'south';
% shape figure
nexttile
hold on
b=plot([results.(indepVar)],median([results.c_err_pace]),'x-',settings.PACERAW{:});
errorshade([results.(indepVar)],[results.c_err_pace],get(b,'Color'));
res = [results.c_err_ours];
for lidx = Llist
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
plotsettings = {'DisplayName', "OURS-" + string(L),'LineWidth',3,'Color',ours_colors(length(Ldomain)-lidx+1)};
a=plot([results.(indepVar)],median(res(:,lrange)),'x-',plotsettings{:});
errorshade([results.(indepVar)],res(:,lrange),get(a,'Color'));
end
yscale log;% xscale log
xlabel(indepVar); ylabel("Shape Error (normalized)");
title("Shape Errors")
% gap figure
nexttile
b=plot([results.(indepVar)],median([results.gap_pace]),'x-',settings.PACERAW{:});
hold on
errorshade([results.(indepVar)],[results.gap_pace],get(b,'Color'));
res = [results.gap_ours];
% res(res < 0) = 0;
% res(res > 1) = 1; % only affects first round
for lidx = Llist
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
plotsettings = {'DisplayName', "OURS-" + string(L),'LineWidth',3,'Color',ours_colors(length(Ldomain)-lidx+1)};
a=semilogy([results.(indepVar)],median(res(:,lrange)),'x-',plotsettings{:});
errorshade([results.(indepVar)],res(:,lrange),get(a,'Color'));
end
yscale log;% xscale log
xlabel(indepVar); ylabel("Gap");
title("Suboptimality Gaps")
% time figure
nexttile
hold on
b=plot([results.(indepVar)],median([results.time_pace]),'x-',settings.PACERAW{:});
hold on
errorshade([results.(indepVar)],[results.time_pace],get(b,'Color'));
res = [results.time_ours];
for lidx = Llist
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
plotsettings = {'DisplayName', "OURS-" + string(L),'LineWidth',3,'Color',ours_colors(length(Ldomain)-lidx+1)};
a=plot([results.(indepVar)],median(res(:,lrange)),'x-',plotsettings{:});
errorshade([results.(indepVar)],res(:,lrange),get(a,'Color'));
end
yscale log;% xscale log
xlabel(indepVar); ylabel("Time (s)");
title("Solve Time")
%% Box and Whisker Display
% parameters
nBins = length(domain);
% make bins
domain = [results.(indepVar)];
edges = linspace(domain(1),domain(end),nBins+1);
bins = discretize(domain,edges);
% generate figure
figure
% t=tiledlayout(2,2);
% title(t,'Measurement Noise')
% rotation figure
% nexttile
hold on
L = Ldomain(lidx);
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
res = [results.p_err_ours];
boxplot(res(:,lrange), bins, 'Positions', 1:nBins ,'Symbol','','Colors',hex2rgb('#1a80c4'),'PlotStyle','compact','Widths',0.25)
boxplot([results.p_err_pace], bins, 'Positions', (1:nBins) + 0.3,'Symbol','','Colors',hex2rgb('#D95319'),'PlotStyle','compact','Widths',0.25)
hold off
yscale log
% set(gca,'xticklabel',{'#1','#2','#3','#4','#5','#6','#7','#8'})
%% Second Box and Whisker Option
tbl = table;
domain = [results.(indepVar)];
tbl.method = repelem(["OURS", "PACE"], num_repeats*length(domain))';
nBins = length(domain);
edges = linspace(domain(1),domain(end),nBins+1);
bins = discretize(domain,edges);
tbl.domain = repmat(repelem(bins,num_repeats)',[2,1]);
% positions
res = [results.p_err_ours];
lidx = 1;
lrange = lidx + length(Ldomain)*(0:length(display_range)-1);
p_err_ours = res(:,lrange)/lengthScale;
p_err_pace = [results.p_err_pace]/lengthScale;
tbl.p = [p_err_ours(:); p_err_pace(:)];
figure
b=boxchart(tbl.domain,tbl.p,'GroupByColor',tbl.method, 'JitterOutliers','on','MarkerStyle','.', 'Notch','off');
yscale log