-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_sim_plot.m
199 lines (167 loc) · 6.85 KB
/
generate_sim_plot.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
% Copyright © 2023 Martin Schonger
% This software is licensed under the GPLv3.
function [fig] = generate_sim_plot(output_root_path, exp_id, plot_id, enable_legend)
arguments
output_root_path;
exp_id;
plot_id;
enable_legend = false;
end
[options, result, f, V, B] = read_exp(exp_id);
lasa_idx = options.dataset_opts.idx;
rd = RefData;
rd.loadLasa(lasa_idx);
x_min = min(rd.Data(1, :));
x_max = max(rd.Data(1, :));
x_range = x_max - x_min;
y_min = min(rd.Data(2, :));
y_max = max(rd.Data(2, :));
y_range = y_max - y_min;
xy_range = max(x_range, y_range);
xy_range = xy_range * 2.1;
x_lowerlim = x_min - 0.5 * (xy_range - x_range);
x_upperlim = x_max + 0.5 * (xy_range - x_range);
y_lowerlim = y_min - 0.5 * (xy_range - y_range);
y_upperlim = y_max + 0.5 * (xy_range - y_range);
limits = [x_lowerlim, x_upperlim, y_lowerlim, y_upperlim];
initial_set_center = rd.xi0_mean;
initial_set_radius = 0.1;
switch lasa_idx
case 1
unsafe_set_center = [-0.51; 0.3];
unsafe_set_radius = 0.15;
case 3
unsafe_set_center = [-0.31; 0.33];
unsafe_set_radius = 0.15;
ellipse_center_position = [-0.1234, 0.3208];
ellipse_axes_length = [0.1746*2*2, 0.0824*2];
ellipse_orientation = -5 / 180 * pi;
case 4
unsafe_set_center = [-0.81; -0.58];
unsafe_set_radius = 0.125;
case 5
unsafe_set_center = [-0.2; 0.25];
unsafe_set_radius = 0.1;
case 11
ellipse_center_position = [-0.3925, 0.0253];
ellipse_axes_length = [0.3593*2, 0.1441*2];
ellipse_orientation = 78 / 180 * pi;
case 14
unsafe_set_center = [-0.2; -0.09];
unsafe_set_radius = 0.075;
case 15
limits = [-1.62, 1.62, -1.63, 1.63];
unsafe_set_center = [-0.21; 0.37];
unsafe_set_radius = 0.175;
ellipse_center_position = [-0.2631, -0.3640];
ellipse_axes_length = [0.5181*2, 0.1203*2];
ellipse_orientation = 99 / 180 * pi;
initial_set_radius = 0.15;
case 19
unsafe_set_center = [-0.43; 0.14];
unsafe_set_radius = 0.1;
case 22
ellipse_center_position = [0.4054, 0.5528];
ellipse_axes_length = [0.1978*2*1.5, 0.0877*2];
ellipse_orientation = -5 / 180 * pi;
case 24
unsafe_set_center = [-0.5; 0.07];
unsafe_set_radius = 0.1;
ellipse_center_position = [-0.495, 0];
ellipse_axes_length = [0.07*2, 0.2*2];
ellipse_orientation = 5 / 180 * pi;
case 25
ellipse_center_position = [-0.255, 0.09];
ellipse_axes_length = [0.4*2, 0.08*2];
ellipse_orientation = 96 / 180 * pi;
otherwise
warning('Unknown unsafe set parameters for LASA %s.', num2str(lasa_idx));
unsafe_set_center = [1; 1];
unsafe_set_radius = 1e-4;
ellipse_center_position = [1,1];
ellipse_axes_length = [0.01, 0.01];
ellipse_orientation = 0;
end
xi = sdpvar(rd.M, 1);
% initial/safe set
r1 = initial_set_radius;
r21 = r1 * r1;
center1 = initial_set_center;
initial_set = {};
initial_set{1} = r21 - sum((xi-center1).^2, 1);
% unsafe set
unsafe_set = {};
if options.enable_barrier
switch options.dataset_opts.obstacle_repr
case "circle"
r = unsafe_set_radius;
r2 = r * r;
center = unsafe_set_center;
unsafe_set{1} = r2 - sum((xi-center).^2, 1);
case "ellipse_eq"
unsafe_set{1} = 1 - (xi-C)' * A * (xi-C);
case "ellipse_axis"
ell = ellipse(ellipse_center_position, ellipse_axes_length, ellipse_orientation);
unsafe_set{1} = ell(xi);
end
end
plot_dim1 = 1;
plot_dim2 = 2;
[fig, ax] = setup_figure(1.627635606409449, 1.627635606409449, enable_legend, true);
set(ax,'xtick',[],'ytick',[],'ztick',[],'xcolor','black','ycolor','black');
box on;
ax.LineWidth=0.5;
% reference trajectories and equilibrium
scatter([], [], 0.001, 'o', 'sizedata', 0.001, 'markeredgecolor', 'black', 'markerfacecolor', 'black', 'linewidth', 0.001, 'displayname', '$\xi^*$');
plot_objs = rd.plotLines(plot_dim1, plot_dim2, {'sizedata', 20, 'handlevisibility', 'off'}, {'linewidth', 0.5});
quiver(100,100,1,0, 'black', 'linewidth', 0.5, 'displayname', '$f(\xi)$');
% axis limits (depending on ref data)
xlim([limits(1), limits(2)]);
ylim([limits(3), limits(4)]);
axis_limits = axis;
% plot DS
streamlines_plt = plot_streamlines_for_f(f, axis_limits, 200, 1);
set(streamlines_plt(1), 'displayname', '$f(\xi)$');
set(streamlines_plt(1), 'handlevisibility', 'off');
set(streamlines_plt(2:end), 'handlevisibility', 'off');
resolution = 0.01;
[X, Y] = meshgrid(axis_limits(1)-resolution:resolution:axis_limits(2)+resolution, axis_limits(3)-resolution:resolution:axis_limits(4)+resolution);
XY = [X(:)'; Y(:)'];
% plot Barrier zero-level curve
if options.enable_barrier
% plot initial set
for p = 1:length(initial_set)
fgptilde = sdpvar2fun(initial_set{p}, xi);
Fgptilde = reshape(fgptilde(XY), size(X));
[cctilde, hhtilde] = contourf(X, Y, Fgptilde, [0, 0], 'linewidth', 1, 'color', 'cyan', 'facecolor', 'cyan', 'facealpha', 0.35, 'displayname', strcat('$\mathcal{X}_{0}$'), 'handlevisibility', 'off');
hold on;
end
fill([10 10 100 100],[10 100 100 10], 'cyan', 'edgecolor', 'cyan', 'facecolor', 'cyan', 'facealpha', 0.35, 'displayname', '$\mathcal{X}_{0}$');
% plot unsafe set
for m = 1:length(unsafe_set)
fgm = sdpvar2fun(unsafe_set{m}, xi);
Fgm = reshape(fgm(XY), size(X));
[cc, hh] = contourf(X, Y, Fgm, [0, 0], 'linewidth', 1, 'color', 'black', 'facecolor', 'black', 'facealpha', 0.35, 'displayname', strcat('$\mathcal{X}_{u}$'), 'handlevisibility', 'off');
hold on;
end
fill([10 10 100 100],[10 100 100 10], 'black', 'edgecolor', 'black', 'facecolor', 'black', 'facealpha', 0.35, 'displayname', '$\mathcal{X}_{u}$');
Bfun_eval = reshape(B(XY), size(X));
contour(X, Y, Bfun_eval, [0, 0], 'linewidth', 1, 'color', '#d41919', 'displayname', '$B^{-1}(0)$');
hold on;
end
% generate sample trajectories
initial_set_center_est = rd.Data(1:rd.M, rd.indivTrajStartIndices(1:end-1));
rd_est = RefData;
[Data_est, Target_est, indivTrajStartIndices_est, Timestamps_est] = generateRefData(f, initial_set_center_est);
rd_est.directInit(Data_est, Target_est, indivTrajStartIndices_est, Timestamps_est, false);
plt_sampled_traj = rd_est.plotLines(plot_dim1, plot_dim2, {'markeredgecolor', 'none', 'markerfacecolor', 'none', 'handlevisibility', 'off'}, {'color', '#ff00ff', 'displayname', '$\xi^{\mathrm{sim}}$'});
if enable_legend
leg = findobj(fig, 'Type', 'Legend');
leg.ItemTokenSize(1) = 5;
% flushLegend(leg, ax, 'northwest'); % [OPTIONAL] https://de.mathworks.com/matlabcentral/fileexchange/57962-flush-legend?s_tid=blogs_rc_6
leg.Position = leg.Position + [+0.001, 0, 0, 0];
end
filename = strcat(output_root_path, plot_id, '.pdf');
print(fig, filename, '-dpdf', '-r2400');
close(fig);
end