Skip to content

Commit 0570dea

Browse files
authored
Merge pull request borglab#2073 from borglab/hybrid-relinearize
Hybrid Fixes
2 parents 967c3ed + 2869a70 commit 0570dea

File tree

6 files changed

+57
-13
lines changed

6 files changed

+57
-13
lines changed

examples/Hybrid_City10000.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,10 @@ class Experiment {
109109
std::cout << "Smoother update: " << newFactors_.size() << std::endl;
110110
gttic_(SmootherUpdate);
111111
clock_t beforeUpdate = clock();
112-
auto linearized = newFactors_.linearize(initial_);
113-
smoother_.update(*linearized, initial_);
112+
smoother_.update(newFactors_, initial_, maxNrHypotheses);
113+
clock_t afterUpdate = clock();
114114
allFactors_.push_back(newFactors_);
115115
newFactors_.resize(0);
116-
clock_t afterUpdate = clock();
117116
return afterUpdate - beforeUpdate;
118117
}
119118

@@ -262,10 +261,20 @@ class Experiment {
262261
std::string timeFileName = "Hybrid_City10000_time.txt";
263262
outfileTime.open(timeFileName);
264263
for (auto accTime : timeList) {
265-
outfileTime << accTime << std::endl;
264+
outfileTime << accTime / CLOCKS_PER_SEC << std::endl;
266265
}
267266
outfileTime.close();
268267
std::cout << "Output " << timeFileName << " file." << std::endl;
268+
269+
std::ofstream timingFile;
270+
std::string timingFileName = "Hybrid_City10000_timing.txt";
271+
timingFile.open(timingFileName);
272+
for (size_t i = 0; i < smootherUpdateTimes.size(); i++) {
273+
auto p = smootherUpdateTimes.at(i);
274+
timingFile << p.first << ", " << p.second / CLOCKS_PER_SEC << std::endl;
275+
}
276+
timingFile.close();
277+
std::cout << "Wrote timing information to " << timingFileName << std::endl;
269278
}
270279
};
271280

examples/ISAM2_City10000.cpp

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ class Experiment {
7474
// Initialize local variables
7575
size_t index = 0;
7676

77+
std::vector<std::pair<size_t, double>> smootherUpdateTimes;
78+
7779
std::list<double> timeList;
7880

7981
// Set up initial prior
@@ -82,10 +84,15 @@ class Experiment {
8284
graph_.addPrior<Pose2>(X(0), priorPose, kPriorNoiseModel);
8385

8486
// Initial update
87+
clock_t beforeUpdate = clock();
8588
isam2_.update(graph_, initial_);
89+
results = isam2_.calculateBestEstimate();
90+
clock_t afterUpdate = clock();
91+
smootherUpdateTimes.push_back(
92+
std::make_pair(index, afterUpdate - beforeUpdate));
8693
graph_.resize(0);
8794
initial_.clear();
88-
results = isam2_.calculateBestEstimate();
95+
index += 1;
8996

9097
// Start main loop
9198
size_t keyS = 0;
@@ -127,10 +134,15 @@ class Experiment {
127134
index++;
128135
}
129136

137+
clock_t beforeUpdate = clock();
130138
isam2_.update(graph_, initial_);
139+
results = isam2_.calculateBestEstimate();
140+
clock_t afterUpdate = clock();
141+
smootherUpdateTimes.push_back(
142+
std::make_pair(index, afterUpdate - beforeUpdate));
131143
graph_.resize(0);
132144
initial_.clear();
133-
results = isam2_.calculateBestEstimate();
145+
index += 1;
134146

135147
// Print loop index and time taken in processor clock ticks
136148
if (index % 50 == 0 && keyS != keyT - 1) {
@@ -175,6 +187,16 @@ class Experiment {
175187
outfileTime.close();
176188
std::cout << "Written cumulative time to: " << timeFileName << " file."
177189
<< std::endl;
190+
191+
std::ofstream timingFile;
192+
std::string timingFileName = "ISAM2_City10000_timing.txt";
193+
timingFile.open(timingFileName);
194+
for (size_t i = 0; i < smootherUpdateTimes.size(); i++) {
195+
auto p = smootherUpdateTimes.at(i);
196+
timingFile << p.first << ", " << p.second / CLOCKS_PER_SEC << std::endl;
197+
}
198+
timingFile.close();
199+
std::cout << "Wrote timing information to " << timingFileName << std::endl;
178200
}
179201
};
180202

gtsam/discrete/DecisionTreeFactor.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -547,7 +547,9 @@ namespace gtsam {
547547
/* ************************************************************************ */
548548
DiscreteFactor::shared_ptr DecisionTreeFactor::restrict(
549549
const DiscreteValues& assignment) const {
550-
throw std::runtime_error("DecisionTreeFactor::restrict not implemented");
550+
ADT restricted_tree = ADT::restrict(assignment);
551+
return std::make_shared<DecisionTreeFactor>(this->discreteKeys(),
552+
restricted_tree);
551553
}
552554

553555
/* ************************************************************************ */

gtsam/hybrid/HybridSmoother.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -292,13 +292,19 @@ HybridValues HybridSmoother::optimize() const {
292292
}
293293

294294
/* ************************************************************************* */
295-
void HybridSmoother::relinearize() {
295+
void HybridSmoother::relinearize(const std::optional<Ordering> givenOrdering) {
296296
allFactors_ = allFactors_.restrict(fixedValues_);
297297
HybridGaussianFactorGraph::shared_ptr linearized =
298298
allFactors_.linearize(linearizationPoint_);
299-
HybridBayesNet::shared_ptr bayesNet = linearized->eliminateSequential();
299+
300+
// Compute ordering if not given
301+
Ordering ordering = this->maybeComputeOrdering(*linearized, givenOrdering);
302+
303+
HybridBayesNet::shared_ptr bayesNet =
304+
linearized->eliminateSequential(ordering);
300305
HybridValues delta = bayesNet->optimize();
301306
linearizationPoint_ = linearizationPoint_.retract(delta.continuous());
307+
302308
reInitialize(*bayesNet);
303309
}
304310

gtsam/hybrid/HybridSmoother.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -126,9 +126,13 @@ class GTSAM_EXPORT HybridSmoother {
126126
/// Optimize the hybrid Bayes Net, taking into accound fixed values.
127127
HybridValues optimize() const;
128128

129-
/// Relinearize the nonlinear factor graph
130-
/// with the latest linearization point.
131-
void relinearize();
129+
/**
130+
* @brief Relinearize the nonlinear factor graph with
131+
* the latest stored linearization point.
132+
*
133+
* @param givenOrdering An optional elimination ordering.
134+
*/
135+
void relinearize(const std::optional<Ordering> givenOrdering = {});
132136

133137
/// Return the current linearization point.
134138
Values linearizationPoint() const;

gtsam/hybrid/hybrid.i

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,8 @@ class HybridSmoother {
288288
std::optional<size_t> maxNrLeaves = std::nullopt,
289289
const std::optional<gtsam::Ordering> given_ordering = std::nullopt);
290290

291-
void relinearize();
291+
void relinearize(
292+
const std::optional<gtsam::Ordering> givenOrdering = std::nullopt);
292293

293294
gtsam::Values linearizationPoint() const;
294295
gtsam::HybridNonlinearFactorGraph allFactors() const;

0 commit comments

Comments
 (0)