-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTrajectory.cpp
317 lines (276 loc) · 10.5 KB
/
Trajectory.cpp
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
// -*- Mode: c++ -*-
// copyright (c) 2006 by Christos Dimitrakakis <[email protected]>
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cassert>
#include <list>
#include <vector>
#include "Trajectory.h"
#include <time.h>
/// Return a point
Point Trajectory::GetPoint(Segment& s, float w) {
float v = 1.0f - w;
return Point(w * s.left.x + v * s.right.x,
w * s.left.y + v * s.right.y,
w * s.left.z + v * s.right.z);
}
#define EXP_COST
#undef DBG_OPTIMISE
/// Optimise a track trajectory
void Trajectory::Optimise(SegmentList track, int max_iter, float alpha, const char* fname, bool reset) {
int N = track.size();
clock_t start_time = clock();
int min_iter = max_iter / 2; // minimum number of iterations to do
float time_limit = 2.0f; // if more than min_iter have been done, exit when time elapsed is larger than the time limit
float beta = 0.75f; // amount to reduce alpha to when it seems to be too large
w.resize(N);
dw.resize(N);
dw2.resize(N);
indices.resize(N);
accel.resize(N);
// initialise vectors
int i;
for (i = 0; i < N; ++i) {
if (reset) {
w[i] = 0.5f;
}
dw2[i] = 1.0f;
indices[i] = i;
}
// Shuffle thoroughly
#if 1
srand(12358);
for (i = 0; i < N - 1; ++i) {
int z = rand() % (N - i);
int tmp = indices[i];
indices[i] = indices[z + i];
indices[z + i] = tmp;
}
#endif
//float prevC = 0.0f;
float Z = 10.0f;
float lambda = 0.9f;
float delta_C = 0.0f;
float prev_dCdw2 = 0.0f;
for (int iter = 0; iter < max_iter; iter++) {
float C = 0.0f;
float P = 0.0f;
float dCdw2 = 0.0f;
float EdCdw = 0.0f;
float direction = 0.0;
for (int j = 0; j < N - 1; ++j) {
int i = indices[j]; //rand()%(N-3) + 3;
int i_p3 = i - 3;
if (i_p3 < 0) i_p3 += N;
int i_p2 = i - 2;
if (i_p2 < 0) i_p2 += N;
int i_p1 = i - 1;
if (i_p1 < 0) i_p1 += N;
//int i_n3 = (i + 3)%N;
int i_n2 = (i + 2) % N;
int i_n1 = (i + 1) % N;
//Segment s_prv3 = track[i_p3];
//Segment s_prv2 = track[i_p2];
//Segment s_prv = track[i_p1];
Segment s_cur = track[i];
//Segment s_nxt = track[i_n1];
//Segment s_nxt2 = track[i_n2];
//Point prv3 = GetPoint(track[i_p3], w[i_p3]);
Point prv2 = GetPoint(track[i_p2], w[i_p2]);
Point prv = GetPoint(track[i_p1], w[i_p1]);
Point cur = GetPoint(track[i], w[i]);
Point nxt = GetPoint(track[i_n1], w[i_n1]);
Point nxt2 = GetPoint(track[i_n2], w[i_n2]);
//Point u_prv2 = prv2 - prv3;
Point u_prv = prv - prv2;
Point u_cur = cur - prv;
Point u_nxt = nxt - cur;
Point u_nxt2 = nxt2 - nxt;
u_prv.Normalise();
u_cur.Normalise();
u_nxt.Normalise();
u_nxt2.Normalise();
//float l_prv2 = (prv2 - prv3).Length();
float l_prv = (prv - prv2).Length();
float l_cur = (cur - prv).Length();
float l_nxt = (nxt - cur).Length();
#if 1
Point a_prv = (u_cur - u_prv) / l_prv;
Point a_cur = (u_nxt - u_cur) / l_cur;
Point a_nxt = (u_nxt2 - u_nxt) / l_nxt;
#else
Point a_prv = (u_prv - u_prv2) / l_prv2;
Point a_cur = (u_cur - u_prv) / l_prv;
Point a_nxt = (u_nxt - u_cur) / l_cur;
#endif
float current_cost = a_prv.Length() * a_prv.Length()
+ a_cur.Length() * a_cur.Length()
+ a_nxt.Length() * a_nxt.Length();
//accel[i] = +a_nxt.Length();
accel[i] = (a_prv.Length() + a_cur.Length() + a_nxt.Length()) / 3.0f;
C += current_cost;
float dCdw = 0.0;
if (1) {
// Done only for a_cur, ignoring other costs.
{
Point lr = s_cur.left - s_cur.right;
Point d = cur - prv;
float dnorm2 = d.x * d.x + d.y * d.y;
float dnorm = sqrt(dnorm2);
float dxdynorm = d.x * d.y / dnorm;
#ifdef EXP_COST
float tmp = exp(a_cur.x * a_cur.x + a_cur.y * a_cur.y);
dCdw += tmp * a_cur.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw += tmp * a_cur.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#else
dCdw += a_cur.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw += a_cur.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#endif
}
{
Point lr = s_cur.left - s_cur.right;
Point d = nxt - cur;
float dnorm2 = d.x * d.x + d.y * d.y;
float dnorm = sqrt(dnorm2);
float dxdynorm = d.x * d.y / dnorm;
#ifdef EXP_COST
float tmp = exp(a_cur.x * a_cur.x + a_cur.y * a_cur.y);
dCdw += tmp * a_cur.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw += tmp * a_cur.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#else
dCdw += a_cur.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw += a_cur.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#endif
}
}
if (1) {
{
Point lr = s_cur.left - s_cur.right;
Point d = nxt - cur;
float dnorm2 = d.x * d.x + d.y * d.y;
float dnorm = sqrt(dnorm2);
float dxdynorm = d.x * d.y / dnorm;
#ifdef EXP_COST
float tmp = exp(a_nxt.x * a_nxt.x + a_nxt.y * a_nxt.y);
dCdw -= tmp * a_nxt.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw -= tmp * a_nxt.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#else
dCdw -= a_nxt.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw -= a_nxt.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#endif
}
}
if (1) {
{
Point lr = s_cur.left - s_cur.right;
Point d = cur - prv;
float dnorm2 = d.x * d.x + d.y * d.y;
float dnorm = sqrt(dnorm2);
float dxdynorm = d.x * d.y / dnorm;
#ifdef EXP_COST
float tmp = exp(a_prv.x * a_prv.x + a_prv.y * a_prv.y);
dCdw -= tmp * a_prv.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw -= tmp * a_prv.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#else
dCdw -= a_prv.x * lr.x * (dnorm + d.x / dnorm + dxdynorm);
dCdw -= a_prv.y * lr.y * (dnorm + d.y / dnorm + dxdynorm);
#endif
}
}
float K = 10.0;
float penalty = 0.0; //K*(0.5f - w[i])*(exp(fabs(0.5-w[i]))-1);
if (1) {
float b = 0.1f;
if (w[i] < b) {
penalty += K * (b - w[i]);
} else if (w[i] > 1.0 - b) {
penalty += K * ((1.0 - b) - w[i]);
}
}
P += K * penalty*penalty;
dCdw += K*penalty;
dw2[i] = lambda * dw2[i] + (1.0 - lambda) * dCdw*dCdw;
direction += dCdw * dw[i];
float delta = dCdw / (dw2[i] + 1.0);
dw[i] = delta;
w[i] += alpha * delta;
if (1) {
float b = 0.0;
if (w[i] < b) {
w[i] = b;
} else if (w[i] > 1.0 - b) {
w[i] = 1.0 - b;
}
}
dCdw2 += dCdw*dCdw;
EdCdw += delta / (float) N;
} // indices
if (direction < 0) {
alpha *= beta;
#ifdef DBG_OPTIMISE
fprintf(stderr, "# Reducing alpha to %f\n", alpha);
#endif
}
Z = (dCdw2);
if (Z < 0.01) {
Z = 0.01f;
}
bool early_exit = false;
delta_C = 0.9 * delta_C + 0.1 * fabs(EdCdw - prev_dCdw2);
prev_dCdw2 = EdCdw;
if (delta_C < 0.001f) {
early_exit = true;
}
if (iter % 100 == 0) {
clock_t current_time = clock();
float elapsed_time = (float) (current_time - start_time) / (float) CLOCKS_PER_SEC;
if (elapsed_time > time_limit) {
early_exit = true;
}
#ifdef DBG_OPTIMISE
fprintf(stderr, "%d %f %f %f %f %f %f\n",
iter,
C / (float) N,
P / (float) N, dCdw2, EdCdw, delta_C, elapsed_time);
#endif
}
if (iter > min_iter && early_exit) {
#ifdef DBG_OPTIMISE
fprintf(stderr, "# Time to break\n");
fflush(stderr);
#endif
break;
}
//prevC = C;
}
}
#if 0
// example
int main(int argc, char** argv) {
if (argc != 3) {
fprintf(stderr, "usage: optimise_road iterations learning_rate\n");
exit(-1);
}
int iter = atoi(argv[1]);
float alpha = atof(argv[2]);
TrackData track_data;
SegmentList track;
track_data.setStep(10.0f);
float width_l = 9.0f;
float width_r = 9.0f;
track_data.setWidth(19.0f);
track_data.AddStraight(track, 50.0, width_l, width_r); //1
track_data.AddCurve(track, 90.0, 100, width_l, width_r); //2
Optimise(track, iter, alpha);
return 0;
}
#endif