-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtoro3d.cpp
329 lines (294 loc) · 10.8 KB
/
toro3d.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
318
319
320
321
322
323
324
325
326
327
328
329
/**********************************************************************
*
* This source code is part of the Tree-based Network Optimizer (TORO)
*
* TORO Copyright (c) 2007 Giorgio Grisetti, Cyrill Stachniss,
* Slawomir Grzonka and Wolfram Burgard
*
* You are free:
* - to Share - to copy, distribute and transmit the work
* - to Remix - to adapt the work
*
* Under the following conditions:
*
* - Attribution. You must attribute the work in the manner specified
* by the author or licensor (but not in any way that suggests that
* they endorse you or your use of the work).
*
* - Noncommercial. You may not use this work for commercial purposes.
*
* - Share Alike. If you alter, transform, or build upon this work,
* you may distribute the resulting work only under the same or
* similar license to this one.
*
* Any of the above conditions can be waived if you get permission
* from the copyright holder. Nothing in this license impairs or
* restricts the author's moral rights.
*
* TORO is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
* PURPOSE.
**********************************************************************/
#include <sys/time.h>
#include <iostream>
#include <fstream>
#include "treeoptimizer3.hh"
using namespace std;
using namespace AISNavigation;
const char* message[] =
{
"*******************************************************************",
"* TORO v 0.1 *",
"* (c) Giorgio Grisetti, Cyrill Stachniss, *",
"* Slawomir Grzonka, and Wolfram Burgard *",
"*******************************************************************",
"",
" Usage toro [options] <graph file>",
" options ",
" -vl <int> set verbosity level",
" -dr disables node reduction",
" -mst use the minimum spanning tree",
" -ic enable index renaming (saves memory)",
" -nib disable initialization according to observations",
" -i <int> perform <int> iterations",
" -df dump gnuplot files of the intermediate results",
" -nde disable dump error on screen (saves time)",
" -ar adaptive restart",
" -oc overrides covariances",
" -ip ignores preconditioner",
" -2d load a 2d file",
" -sl sort the constraints according to the length of the path",
"Enjoy!",
0
};
std::string stripExtension(const std::string s){
int i;
for (i=s.length()-1; i>0; i--){
string k=s.substr(i,1);
if (k==".")
break;
}
return s.substr(0,i);
}
std::string getExtension(const std::string s){
int i;
for (i=s.length()-1; i>0; i--){
string k=s.substr(i,1);
if (k==".")
break;
}
return s.substr(i,s.length()-i);
}
TreeOptimizer3 pg;
bool compressIndices=false;
bool reduceNodes=true;
bool initializeOnTree=true;
int treeType=0;
int iterations=100;
bool dumpIterations=false;
bool dumpError=true;
bool adaptiveRestart=false;
int verboseLevel=0;
bool ignorePreconditioner=false;
bool overrideCovariances=false;
bool twoDimensions = false;
TreeOptimizer3::EdgeCompareMode compareMode=EVComparator<TreeOptimizer3::Edge*>::CompareLevel;
std::string filename;
void printBannerAndStatus(ostream& os){
os << "#******************************************************************" << endl;
os << "# TORO3D v 0.1 *" << endl;
os << "# (c) G.Grisetti, C.Stachniss, S.Grzonka, and W. Burgard *" << endl;
os << "#******************************************************************" << endl;
os << "#Verbosity Level = " << verboseLevel << endl;
os << "#Node Reduction = " << ((reduceNodes)?"enabled":"disabled") << endl;
os << "#Tree Construction = " << ((treeType)?"MST":"Simple") << endl;
os << "#IndexCompression = " << ((compressIndices)?"enabled":"disabled") << endl;
os << "#InitialBoost = " << ((initializeOnTree)?"enabled":"disabled") << endl;
os << "#Dumping Iterations = " << ((dumpIterations)?"enabled":"disabled") << endl;
os << "#Iterations = " << iterations << endl;
os << "#Dumping Intermediate Files = " << ((dumpIterations)?"enabled":"disabled") << endl;
os << "#Dumping Error = " << ((dumpError)?"enabled":"disabled") << endl;
os << "#AdaptiveRestart = " << ((adaptiveRestart)?"enabled":"disabled") << endl;
os << "#IgnorePreconditioner = " << ((ignorePreconditioner)?"enabled":"disabled") << endl;
os << "#OverrideCovariances = " << ((overrideCovariances)?"enabled":"disabled") << endl;
os << "#2D File = " << ((twoDimensions)?"enabled":"disabled") << endl;
os << "#Edge Sorting = " << ((compareMode==EVComparator<TreeOptimizer3::Edge*>::CompareLevel)?"level":"length") << endl;
os << "#***********************************" << endl;
os << endl;
}
int main (int argc, const char** argv){
if (argc==1){
int i=0;
while (message[i]!=0){
cerr << message[i++] << endl;
}
return 0;
}
int c=1;
while (c<argc){
if (string(argv[c])=="-vl"){
verboseLevel=atoi(argv[++c]);
} else if (string(argv[c])=="-dr"){
reduceNodes=false;
} else if (string(argv[c])=="-mst"){
treeType=1;
} else if (string(argv[c])=="-st"){
treeType=0;
} else if (string(argv[c])=="-ic"){
compressIndices=true;
} else if (string(argv[c])=="-nib"){
initializeOnTree=false;
} else if (string(argv[c])=="-i"){
iterations=atoi(argv[++c]);
} else if (string(argv[c])=="-df"){
dumpIterations=true;
} else if (string(argv[c])=="-nde"){
dumpError=false;
} else if (string(argv[c])=="-ar"){
adaptiveRestart=true;
} else if (string(argv[c])=="-ip"){
ignorePreconditioner=true;
} else if (string(argv[c])=="-oc"){
overrideCovariances=true;
} else if (string(argv[c])=="-2d"){
twoDimensions=true;
} else if (string(argv[c])=="-sl"){
compareMode=EVComparator<TreeOptimizer3::Edge*>::CompareLevel;
} else {
filename=argv[c];
break;
}
c++;
}
if (filename == "") {
cerr << "FATAL ERROR: Specify a graph file to load. Aborting." << endl;
return 0;
}
printBannerAndStatus(cerr);
pg.verboseLevel=verboseLevel;
cerr << "Loading graph file \"" << filename << "\"... ";
if (!pg.load( filename.c_str(), overrideCovariances, twoDimensions)) {
cerr << "FATAL ERROR: Could not read file. Abrting." << endl;
return 0;
}
cerr << "Done" << endl;
cerr << " #nodes:" << pg.vertices.size() << " #edges:" << pg.edges.size() << endl;
if (reduceNodes){
cerr << "Loading equivalence constraints and collapsing nodes... ";
pg.loadEquivalences(filename.c_str());
cerr << "Done" << endl;
cerr << " #nodes:" << pg.vertices.size() << " #edges:" << pg.edges.size() << endl;
}
pg.restartOnDivergence=adaptiveRestart;
if (compressIndices){
cerr << "Compressing indices... ";
pg.compressIndices();
cerr << "Done" << endl;
}
switch (treeType){
case 0:
cerr << "Incremental tree construction... ";
pg.buildSimpleTree();
cerr << "Done" << endl;
break;
case 1:
cerr << "MST construction... ";
pg.buildMST(pg.vertices.begin()->first);
cerr << "Done" << endl;
break;
default:
cerr << " FATAL ERROR: Invalid tree type. Aborting!";
return -1;
}
if (initializeOnTree){
cerr << "Computing initial guess from observations... ";
pg.initializeOnTree();
cerr << "Done" << endl;
}
cerr << "Initializing the optimizer... ";
pg.initializeTreeParameters();
pg.initializeOptimization(compareMode);
double l=pg.totalPathLength();
int nEdges=pg.edges.size();
double apl=l/(double)(nEdges);
cerr << "Done" << endl;
cerr << " Average path length=" << apl << endl;
cerr << " Complexity of an iteration=" << l << endl;
string strippedFilename=stripExtension(filename);
string extension=getExtension(filename);
cerr << "Saving starting graph... ";
string output=strippedFilename+"-treeopt-initial.graph";
pg.save(output.c_str());
cerr << "Done" << endl << endl;
output=strippedFilename+"-treeopt-initial.dat";
pg.saveGnuplot(output.c_str());
cerr << "Done" << endl << endl;
string errorOutput=strippedFilename+"-treeopt-error.dat";
ofstream errorStream;
if (dumpError){
errorStream.open(errorOutput.c_str());
printBannerAndStatus(errorStream);
errorStream << "# InputFile : " << filename << endl;
errorStream << "# Nodes : " << pg.vertices.size() << " #edges: " << pg.edges.size() << endl;
errorStream << "# Average path length: " << apl << endl;
errorStream << "# Complexity of an iteration: " << l << endl;
errorStream << "# Line Format:" << endl
<< "# 1:iteration" << endl
<< "# 2:RotationalGain" << endl
<< "# 3:error" << endl
<< "# 4:error/constraint" << endl
<< "# 5:max rotational error" << endl
<< "# 6:average rotational error" << endl
<< "# 7:max translational error" << endl
<< "# 8:average translational error" << endl;
}
bool corrupted=false;
cerr << "**** Starting optimization ****" << endl;
struct timeval ts, te;
gettimeofday(&ts,0);
for (int i=0; i<iterations; i++){
pg.iterate(0,ignorePreconditioner);
if (dumpIterations){
char b[10];
sprintf(b,"%04d",i);
string output=strippedFilename+"-treeopt-" + b + ".dat";
pg.saveGnuplot(output.c_str());
}
if (dumpError){
// compute the error and dump it
double mte, mre, are, ate;
double error=pg.error(&mre, &mte, &are, &ate);
cerr << "iteration " << i << " RotGain=" << pg.getRotGain() << endl
<< " global error = " << error << " error/constraint = " << error/nEdges << endl;
cerr << " mte=" << mte << " mre=" << mre << " are=" << are << " ate=" << ate << endl;
errorStream << i << " "
<< pg.getRotGain() << " "
<< error << " "
<< error/nEdges << " "
<< mre << " "
<< are << " "
<< mte << " "
<< ate << endl;
cout << error << endl;
if (mre>(M_PI/2)*(M_PI/2))
corrupted=true;
else
corrupted=false;
}
}
gettimeofday(&te,0);
cerr << "**** Optimization Done ****" << endl;
double dts=(te.tv_sec-ts.tv_sec)+1e-6*(te.tv_usec-ts.tv_usec);
cerr << "TOTAL TIME= " << dts << " s." << endl;
if (corrupted)
strippedFilename=strippedFilename+"-corrupted";
cerr << "Saving files...(graph file)" << endl;
output=strippedFilename+"-treeopt-final.graph";
pg.save(output.c_str());
cerr << "...(gnuplot file)..." << endl;
output=strippedFilename+"-treeopt-final.dat";
pg.saveGnuplot(output.c_str());
errorStream.close();
cerr << "Done" << endl;
}