40 using namespace gtsam;
53 size_t maxLoopCount = 8000;
59 size_t updateFrequency = 3;
61 size_t maxNrHypotheses = 10;
63 size_t reLinearizationFrequency = 10;
65 double marginalThreshold = 0.9999;
77 size_t loopCounter,
size_t keyS,
size_t keyT,
81 auto f0 = std::make_shared<BetweenFactor<Pose2>>(
83 auto f1 = std::make_shared<BetweenFactor<Pose2>>(
94 size_t numMeasurements,
size_t keyS,
size_t keyT,
const DiscreteKey&
m,
95 const std::vector<Pose2>& poseArray)
const {
96 auto f0 = std::make_shared<BetweenFactor<Pose2>>(
98 auto f1 = std::make_shared<BetweenFactor<Pose2>>(
104 return mixtureFactor;
109 std::cout <<
"Smoother update: " << newFactors_.
size() << std::endl;
111 clock_t beforeUpdate = clock();
112 auto linearized = newFactors_.
linearize(initial_);
113 smoother_.
update(*linearized, maxNrHypotheses);
116 clock_t afterUpdate = clock();
117 return afterUpdate - beforeUpdate;
122 std::cout <<
"================= Re-Initialize: " << allFactors_.
size()
124 clock_t beforeUpdate = clock();
126 auto linearized = allFactors_.
linearize(initial_);
127 auto bayesNet = linearized->eliminateSequential();
131 clock_t afterUpdate = clock();
132 std::cout <<
"Took " << (afterUpdate - beforeUpdate) / CLOCKS_PER_SEC
133 <<
" seconds." << std::endl;
134 return afterUpdate - beforeUpdate;
140 : dataset_(
filename), smoother_(marginalThreshold) {}
145 size_t discreteCount = 0, index = 0, loopCount = 0, updateCount = 0;
147 std::list<double> timeList;
150 Pose2 priorPose(0, 0, 0);
151 initial_.
insert(
X(0), priorPose);
156 auto time = smootherUpdate(maxNrHypotheses);
157 std::vector<std::pair<size_t, double>> smootherUpdateTimes;
158 smootherUpdateTimes.push_back({index,
time});
161 size_t numberOfHybridFactors = 0;
165 size_t keyS = 0, keyT = 0;
166 clock_t startTime = clock();
168 std::vector<Pose2> poseArray;
169 std::pair<size_t, size_t>
keys;
171 while (dataset_.
next(&poseArray, &
keys) && index < maxLoopCount) {
174 size_t numMeasurements = poseArray.size();
177 Pose2 odomPose = poseArray[0];
178 if (keyS == keyT - 1) {
180 if (numMeasurements > 1) {
184 hybridOdometryFactor(numMeasurements, keyS, keyT,
m, poseArray);
187 numberOfHybridFactors += 1;
188 std::cout <<
"mixtureFactor: " << keyS <<
" " << keyT << std::endl;
198 hybridLoopClosureFactor(loopCount, keyS, keyT, odomPose);
200 std::cout <<
"Loop closure: " << keyS <<
" " << keyT << std::endl;
201 newFactors_.
add(loopFactor);
202 numberOfHybridFactors += 1;
206 if (numberOfHybridFactors >= updateFrequency) {
207 auto time = smootherUpdate(maxNrHypotheses);
208 smootherUpdateTimes.push_back({index,
time});
209 numberOfHybridFactors = 0;
212 if (updateCount % reLinearizationFrequency == 0) {
218 if (keyS == keyT - 1) {
219 clock_t curTime = clock();
220 timeList.push_back(curTime - startTime);
224 if (index % 100 == 0) {
225 std::cout <<
"Index: " << index << std::endl;
226 if (!timeList.empty()) {
227 std::cout <<
"Acc_time: " << timeList.back() / CLOCKS_PER_SEC
228 <<
" seconds" << std::endl;
239 time = smootherUpdate(maxNrHypotheses);
240 smootherUpdateTimes.push_back({index,
time});
243 gttic_(HybridSmootherOptimize);
245 gttoc_(HybridSmootherOptimize);
252 clock_t endTime = clock();
253 clock_t totalTime = endTime - startTime;
254 std::cout <<
"Total time: " << totalTime / CLOCKS_PER_SEC <<
" seconds"
261 std::ofstream outfileTime;
262 std::string timeFileName =
"Hybrid_City10000_time.txt";
263 outfileTime.open(timeFileName);
264 for (
auto accTime : timeList) {
265 outfileTime << accTime << std::endl;
268 std::cout <<
"Output " << timeFileName <<
" file." << std::endl;
275 size_t& updateFrequency,
size_t& maxNrHypotheses) {
276 for (
int i = 1;
i < argc; ++
i) {
277 std::string
arg = argv[
i];
278 if (
arg ==
"--max-loop-count" &&
i + 1 < argc) {
279 maxLoopCount = std::stoul(argv[++
i]);
280 }
else if (
arg ==
"--update-frequency" &&
i + 1 < argc) {
281 updateFrequency = std::stoul(argv[++
i]);
282 }
else if (
arg ==
"--max-nr-hypotheses" &&
i + 1 < argc) {
283 maxNrHypotheses = std::stoul(argv[++
i]);
284 }
else if (
arg ==
"--help") {
285 std::cout <<
"Usage: " << argv[0] <<
" [options]\n"
287 <<
" --max-loop-count <value> Set the maximum loop "
288 "count (default: 3000)\n"
289 <<
" --update-frequency <value> Set the update frequency "
291 <<
" --max-nr-hypotheses <value> Set the maximum number of "
292 "hypotheses (default: 10)\n"
293 <<
" --help Show this help message\n";
301 int main(
int argc,
char* argv[]) {