forked from kektobiologist/motion-simulation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
controlpoint-optimization.cpp
263 lines (230 loc) · 8.64 KB
/
controlpoint-optimization.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
#include "controlpoint-optimization.hpp"
#include "splines.hpp"
#include <vector>
using namespace std;
#include "drawable.h"
#include "collision-checking.h"
extern RenderArea *gRenderArea;
//double Optimization::f_cubic2CP(const gsl_vector *x, void *params_)
//{
// OptParams *params = static_cast<OptParams*>(params_);
// Pose cp1(gsl_vector_get(x,0)*fieldXConvert, gsl_vector_get(x,1)*fieldXConvert, 0);
// Pose cp2(gsl_vector_get(x,2)*fieldXConvert, gsl_vector_get(x,3)*fieldXConvert, 0);
// vector<Pose> midPoints;
// midPoints.push_back(cp1);
// midPoints.push_back(cp2);
// CubicSpline *p = new CubicSpline(params->start, params->end, midPoints);
// SplineTrajectory *st = new SplineTrajectory(p, params->vls, params->vrs, params->vle, params->vre);
// double time = st->totalTime();
// // use maxk also as a cost.
// // try using ONLY maxk as cost function?
// //double maxk = p->maxk();
//// delete st;
// //vector<pair<double,float> > mp = p->lmaxk();
// //p->lmaxk();
// return time;
// //return maxk;
//}
//Trajectory *Optimization::cubicSpline2CPOptimization(Pose start, Pose end, double vls, double vrs, double vle, double vre)
//{
// OptParams params(start, end, vls, vrs, vle, vre);
// const gsl_multimin_fminimizer_type *T =
// gsl_multimin_fminimizer_nmsimplex2;
// gsl_multimin_fminimizer *s = NULL;
// gsl_vector *ss, *x;
// gsl_multimin_function minex_func;
// size_t iter = 0;
// int status;
// double size;
// int n = 4; // number of optimization params. here they are 2 CPs (x,y) in cm
// /* Starting point */
// // set some good starting values for the 2 CPs
//// Pose cp1((start.x()*2+end.x())*1/3., (start.y()*2+end.y())*1/3., 0);
//// Pose cp2((start.x()+2*end.x())*1/3., (start.y()+2*end.y())*1/3., 0);
// Pose cp1(1000,1000,0);
// Pose cp2(-1000,-1000,0);
// x = gsl_vector_alloc (n);
// gsl_vector_set(x, 0, cp1.x()/fieldXConvert);
// gsl_vector_set(x, 1, cp1.y()/fieldXConvert);
// gsl_vector_set(x, 2, cp2.x()/fieldXConvert);
// gsl_vector_set(x, 3, cp2.y()/fieldXConvert);
// /* Set initial step sizes to 100 */
// ss = gsl_vector_alloc (n);
// gsl_vector_set_all (ss, 100.0);
// /* Initialize method and iterate */
// minex_func.n = n;
// minex_func.f = f_cubic2CP;
// minex_func.params = ¶ms;
// s = gsl_multimin_fminimizer_alloc (T, n);
// gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
// do
// {
// iter++;
// status = gsl_multimin_fminimizer_iterate(s);
// if (status)
// break;
// size = gsl_multimin_fminimizer_size (s);
// status = gsl_multimin_test_size (size, 1e-2);
// if (status == GSL_SUCCESS)
// {
// printf ("converged to minimum at\n");
// }
// printf ("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n",
// iter,
// gsl_vector_get (s->x, 0),
// gsl_vector_get (s->x, 1),
// s->fval, size);
// }
// while (status == GSL_CONTINUE && iter < 100);
// // make the trajectory now
// SplineTrajectory *st;
// {
// Pose cp1(gsl_vector_get(s->x,0)*fieldXConvert, gsl_vector_get(s->x,1)*fieldXConvert, 0);
// Pose cp2(gsl_vector_get(s->x,2)*fieldXConvert, gsl_vector_get(s->x,3)*fieldXConvert, 0);
// static PointDrawable *pt1 = NULL, *pt2 = NULL;
// if (pt1)
// delete pt1;
// if (pt2)
// delete pt2;
// pt1 = new PointDrawable(QPointF(cp1.x(), cp1.y()), gRenderArea);
// pt2 = new PointDrawable(QPointF(cp2.x(), cp2.y()), gRenderArea);
// vector<Pose> midPoints;
// midPoints.push_back(cp1);
// midPoints.push_back(cp2);
// CubicSpline *p = new CubicSpline(start, end, midPoints);
// st = new SplineTrajectory(p, vls, vrs, vle, vre);
//// double maxk_u, maxk;
//// maxk = p->maxk(&maxk_u);
//// qDebug() << "maxk = " << maxk << ", maxk_u = " << maxk_u;
// }
// gsl_vector_free(x);
// gsl_vector_free(ss);
// gsl_multimin_fminimizer_free (s);
// return st;
//}
// make n cp optimizer
double Optimization::f_cubicnCP(const gsl_vector *x, void *params_)
{
OptParams *params = static_cast<OptParams*>(params_);
int n = params->n;
std::vector<Pose> cps;
for (int i = 0; i < n; i++) {
cps.push_back(Pose(gsl_vector_get(x, 2*i)*fieldXConvert, gsl_vector_get(x, 2*i+1)*fieldXConvert, 0));
}
CubicSpline *p = new CubicSpline(params->start, params->end, cps);
// check if collides with wall. if so, make the score = 1.5 x time score
using CollisionChecking::LineSegment;
vector<LineSegment> ls;
double collix = HALF_FIELD_MAXX/fieldXConvert;
double botr = BOT_RADIUS/(fieldXConvert);
double colliy = HALF_FIELD_MAXY/fieldXConvert;
ls.push_back(LineSegment(-collix, -colliy+botr, collix, -colliy+botr));
ls.push_back(LineSegment(-collix, colliy-botr, collix, colliy-botr));
ls.push_back(LineSegment(-collix+botr, -colliy, -collix+botr, +colliy));
ls.push_back(LineSegment(collix-botr, -colliy, collix-botr, +colliy));
bool collides_flag = false;
for (int i = 0; i < ls.size(); i++) {
vector<Pose> collisions = CollisionChecking::cubicSpline_LineSegmentIntersection(*p, ls[i]);
if (collisions.size()) {
collides_flag = true;
break;
}
}
SplineTrajectory *st = new SplineTrajectory(p, params->vls, params->vrs, params->vle, params->vre);
double time = st->totalTime();
// use maxk also as a cost.
// try using ONLY maxk as cost function?
//double maxk = p->maxk();
// delete st;
//vector<pair<double,float> > mp = p->lmaxk();
//p->lmaxk();
if (collides_flag)
time *= 10;
return time;
//return maxk;
}
Trajectory *Optimization::cubicSplinenCPOptimization(Pose start, Pose end, double vls, double vrs, double vle, double vre, int n)
{
assert(n >= 0 && n <= 5);
OptParams params(start, end, vls, vrs, vle, vre, n);
const gsl_multimin_fminimizer_type *T =
gsl_multimin_fminimizer_nmsimplex2;
gsl_multimin_fminimizer *s = NULL;
gsl_vector *ss, *x;
gsl_multimin_function minex_func;
size_t iter = 0;
int status;
double size;
/* Starting point */
// set some values for the control points
std::vector<Pose> cps;
for (int i = 0; i < n; i++) {
// initialize all cps to 0,0?
// double x = 0;
// double y = 0;
double x = rand()/(double)RAND_MAX*1000.*((rand()%2)*2-1);
double y = rand()/(double)RAND_MAX*1000.*((rand()%2)*2-1);
cps.push_back(Pose(x, y, 0));
}
x = gsl_vector_alloc (2*n);
for (int i = 0; i < n; i++) {
gsl_vector_set(x, i*2, cps[i].x()/fieldXConvert);
gsl_vector_set(x, i*2+1, cps[i].y()/fieldXConvert);
}
/* Set initial step sizes to 100 */
ss = gsl_vector_alloc (2*n);
gsl_vector_set_all (ss, 100.0);
/* Initialize method and iterate */
minex_func.n = 2*n;
minex_func.f = f_cubicnCP;
minex_func.params = ¶ms;
s = gsl_multimin_fminimizer_alloc (T, 2*n);
gsl_multimin_fminimizer_set (s, &minex_func, x, ss);
do
{
iter++;
status = gsl_multimin_fminimizer_iterate(s);
if (status)
break;
size = gsl_multimin_fminimizer_size (s);
status = gsl_multimin_test_size (size, 1e-2);
if (status == GSL_SUCCESS)
{
printf ("converged to minimum at\n");
}
printf ("%5d %10.3e %10.3e f() = %7.3f size = %.3f\n",
iter,
gsl_vector_get (s->x, 0),
gsl_vector_get (s->x, 1),
s->fval, size);
}
while (status == GSL_CONTINUE && iter < 100);
// make the trajectory now
SplineTrajectory *st;
{
vector<Pose> cps;
for (int i = 0; i < n; i++) {
cps.push_back(Pose(gsl_vector_get(s->x, 2*i)*fieldXConvert, gsl_vector_get(s->x, 2*i+1)*fieldXConvert, 0));
}
static vector<PointDrawable*> pts;
for (int i = 0; i < pts.size(); i++) {
if (pts[i])
delete pts[i];
}
pts.clear();
for (int i = 0; i < n; i++) {
PointDrawable *pt = new PointDrawable(QPointF(cps[i].x(), cps[i].y()), gRenderArea);
pts.push_back(pt);
}
CubicSpline *p = new CubicSpline(start, end, cps);
st = new SplineTrajectory(p, vls, vrs, vle, vre);
// double maxk_u, maxk;
// maxk = p->maxk(&maxk_u);
// qDebug() << "maxk = " << maxk << ", maxk_u = " << maxk_u;
qDebug() << "Spline Time : " << st->totalTime();
}
gsl_vector_free(x);
gsl_vector_free(ss);
gsl_multimin_fminimizer_free (s);
return st;
}