-
Notifications
You must be signed in to change notification settings - Fork 6
/
body3dcedemstab.cc
300 lines (232 loc) · 7.11 KB
/
body3dcedemstab.cc
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
#include <iostream>
#include "viewer.h"
#include "body3dview.h"
#include "body3ddemcontroller.h"
#include "utils.h"
#include "disk.h"
#include "systemce.h"
#include "params.h"
#include "lqcost.h"
#include "controllertparam.h"
#include "demview.h"
#include "pqpdem.h"
#include "systemceview.h"
#include <fstream>
using namespace std;
using namespace Eigen;
using namespace gcop;
//typedef Ddp<Body3dState, 4, 2> Body3dDdp;
//typedef Disk<Body3dState, 4, 2> Body3dDisk;
//typedef ConstraintCost<Body3dState, 4, 2, Dynamic, 1> DiskCost;
typedef SystemCe<Body3dState, 12, 6, Dynamic, 5> Body3dCe;
typedef SystemCeView<Body3dState, 12, 6, Dynamic, 5> Body3dCeView;
typedef PqpDem<Body3dState, 12, 6> Body3dPqpDem;
Params params;
class Body3dSampler : public Creator<Body3dState> {
public:
Body3dSampler(int N, const Body3dState &xf, Constraint<Body3dState, 12, 6, Dynamic, 1> *con = 0) {
Matrix<double, 1, 1> g;
x = xf;
for (int i = 0; i < N; ++i) {
do {
x.p(1) = 2*xf.p(1)*RND;
if (con)
(*con)(g, 0, x);
} while (con && g[0] > -1); // 1 meter away from obstacles
samples.push_back(x);
}
}
Body3dState& operator()() {
x = samples[i%samples.size()];
i++;
return x;
}
int i;
Body3dState x;
vector<Body3dState> samples;
};
void solver_process(Viewer* viewer)
{
viewer->SetCamera(-7.25, 61, -2.8, -2.55, -10);
Body3d<6> sys;
sys.U.bnd = true;
sys.U.lb.setConstant(-50);
sys.U.ub.setConstant(50);
int N = 256;
double tf = 5;
int iters = 100;
params.GetInt("N", N);
params.GetDouble("tf", tf);
params.GetInt("iters", iters);
// cost
Body3dState x0;
x0.Clear();
x0.p << 46, 82, 5;
Body3dState xf;
xf.Clear();
xf.p << 160, 125, 5;
VectorXd qv0(12);
params.GetVectorXd("x0", qv0);
SO3::Instance().q2g(x0.R, qv0.head(3));
x0.p = qv0.segment<3>(3); x0.w = qv0.segment<3>(6); x0.v = qv0.tail<3>();
vector<Body3dState> xs(N+1);
xs[0] = x0;
VectorXd qvf(12);
params.GetVectorXd("xf", qvf);
SO3::Instance().q2g(xf.R, qvf.head(3));
xf.p = qvf.segment<3>(3); xf.w = qvf.segment<3>(6); xf.v = qvf.tail<3>();
float camParams[5];
if (viewer) {
params.GetFloatArray("camParams", 5, camParams);
viewer->SetCamera(camParams);
}
string mapName("maps/pic2_dilated.ppm");
double mapCellSize = .25;
double mapHeightScale = 30;
params.GetString("mapName", mapName);
params.GetDouble("mapCellSize", mapCellSize);
params.GetDouble("mapHeightScale", mapHeightScale);
Dem dem(mapName.c_str(), mapCellSize, mapHeightScale);
// dem.Convolve(1);
double cr = 0;
params.GetDouble("cr", cr);
Body3dPqpDem pqp(dem, cr);
Body3dDemController<> ctrl(sys, x0, &xf, pqp, 20);
ctrl.localCtrl.avoidCtrl->k = 5; // avoidance gain
ctrl.localCtrl.avoidCtrl->sr = 20; // avoidance gain
params.GetDouble("kb", ctrl.localCtrl.avoidCtrl->kb);
params.GetDouble("odExp", ctrl.localCtrl.avoidCtrl->odExp);
params.GetDouble("oc", ctrl.localCtrl.avoidCtrl->oc);
params.GetDouble("sr", ctrl.localCtrl.avoidCtrl->sr);
params.GetDouble("vd", ctrl.vd);
params.GetDouble("wpRadius", ctrl.wpRadius);
DemView dv(dem);
if (viewer)
viewer->Add(dv);
// goal view
vector<Body3dState> xfs;
xfs.push_back(xf);
Body3dView<6> goalView(sys, &xfs);
goalView.rgba[0] = 1;
if (viewer)
viewer->Add(goalView);
LqCost<Body3dState, 12, 6> cost(sys, tf, xf);
VectorXd Q(12);
VectorXd R(6);
VectorXd Qf(12);
params.GetVectorXd("Q", Q);
params.GetVectorXd("R", R);
params.GetVectorXd("Qf", Qf);
cost.Q = Q.asDiagonal();
cost.R = R.asDiagonal();
cost.Qf = Qf.asDiagonal();
// times
double h = tf/N;
vector<double> ts(N+1);
for (int k = 0; k <=N; ++k)
ts[k] = k*h;
// initial controls
vector<Vector6d> us(N);
ControllerTparam<Body3dState, 12, 6, Dynamic, 5, Body3dState> ctp(sys, ctrl, 5, &pqp);
Vector5d mu0;
params.GetVector5d("mu0", mu0);
Vector5d P0d;
params.GetVector5d("P0", P0d);
Matrix5d P0 = P0d.asDiagonal();
Vector5d Sd;
params.GetVector5d("S", Sd);
Matrix5d S = Sd.asDiagonal();
// process noise terms
params.GetDouble("sw", sys.sw);
params.GetDouble("sv", sys.sv);
int Ns;
params.GetInt("Ns", Ns);
Body3dSampler sampler(Ns, xf, &pqp);
ctp.stoch = true;
Body3dCe ce(sys, cost, ctp, &sampler, ts, xs, us, 0, mu0, P0, S);
ce.ce.gmm.ns[0].bounded = true;
ce.ce.gmm.ns[0].lb.setZero();
ce.ce.gmm.ns[0].ub.setConstant(1000);
ce.Ns = Ns;
ce.Jub = 100000;
ce.enforceUpperBound = true;
params.GetDouble("Jub", ce.Jub);
params.GetBool("enforceUpperBound", ce.enforceUpperBound);
params.GetDouble("enforceUpperBoundFactor", ce.enforceUpperBoundFactor);
params.GetBool("mras", ce.ce.mras);
params.GetBool("bAuto", ce.ce.bAuto);
params.GetDouble("b", ce.ce.b);
Body3dView<6> dslView(sys, &ctrl.xds);
dslView.lineWidth=5;
dslView.rgba[0] = 0; dslView.rgba[1] = 1; dslView.rgba[2] = 0;
if (viewer)
viewer->Add(dslView);
getchar();
Body3dView<> view(sys, &xs);
view.renderSystem = false;
viewer->Add(view);
view.rgba[0] = 0; view.rgba[1] = 0; view.rgba[2] = 1;
Body3dCeView ceview(ce, view);
viewer->Add(ceview);
struct timeval timer;
fstream fstr;
fstr.open("logs/body3dcedemstab.txt", std::ios::out | std::ios::trunc);
fstr.precision(20);
if (viewer)
viewer->Remove(dslView);
for (int i = 0; i < iters; ++i) {
timer_start(timer);
// rseed(0);
srand(19);
// save current distro
Normal<5> ns0 = ce.ce.gmm.ns[0];
ceview.Lock();
ce.Iterate();
ceview.Unlock();
long te = timer_us(timer);
cout << "Iteration #" << i << " took: " << te << " us." << endl;
cout << "Min Cost=" << ce.J << "\tAve Cost=" << ce.ce.Jave << endl;
// double Pc = std::count(ce.bs.begin(), ce.bs.end(), false).size()/ce.bs.size();
// cout << "Pc=" << Pc << endl;
VectorXd P = VectorXd::Map(ce.ce.gmm.ns[0].P.data(), ce.ce.gmm.ns[0].P.rows()*ce.ce.gmm.ns[0].P.cols());
VectorXd P0 = VectorXd::Map(ns0.P.data(), ns0.P.rows()*ns0.P.cols());
fstr << ns0.mu.transpose() << " " << P0.transpose() << " " << ce.ce.gmm.ns[0].mu.transpose() << " " << P.transpose() << " ";
for (int j = 0; j < ce.Ns; ++j) // samples
fstr << ce.ce.zps[j].first.transpose() << " ";
for (int j = 0; j < ce.Ns; ++j) // weights
fstr << ce.ce.zps[j].second << " ";
for (int j = 0; j < ce.Ns; ++j) // feasibilities
fstr << ce.bs[j] << " ";
for (int j = 0; j < ce.Ns; ++j) // costs
fstr << ce.ce.cs[j] << " ";
fstr << endl;
if (viewer)
viewer->saveSnapshot = true;
getchar();
}
fstr.close();
while(1)
usleep(10);
}
#define DISP
int main(int argc, char** argv)
{
if (argc > 1)
params.Load(argv[1]);
else
params.Load("../../bin/cecar.cfg");
#ifdef DISP
Viewer *viewer = new Viewer;
viewer->Init(&argc, argv);
viewer->frameName = "logs/body3d/frames/body3d";
viewer->displayName = "logs/body3d/display/body3d";
pthread_t dummy;
pthread_create( &dummy, NULL, (void *(*) (void *)) solver_process, viewer);
#else
solver_process(0);
#endif
#ifdef DISP
viewer->Start();
#endif
return 0;
}