Skip to content

Commit 2897acc

Browse files
authored
Merge pull request #1834 from borglab/hybrid-cleanup
Remove HybridNonlinearFactor normalization code
2 parents 2c140df + 1496017 commit 2897acc

File tree

2 files changed

+8
-60
lines changed

2 files changed

+8
-60
lines changed

gtsam/hybrid/HybridNonlinearFactor.h

+6-53
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ class HybridNonlinearFactor : public HybridFactor {
6363
private:
6464
/// Decision tree of Gaussian factors indexed by discrete keys.
6565
Factors factors_;
66-
bool normalized_;
6766

6867
public:
6968
HybridNonlinearFactor() = default;
@@ -74,12 +73,10 @@ class HybridNonlinearFactor : public HybridFactor {
7473
* @param keys Vector of keys for continuous factors.
7574
* @param discreteKeys Vector of discrete keys.
7675
* @param factors Decision tree with of shared factors.
77-
* @param normalized Flag indicating if the factor error is already
78-
* normalized.
7976
*/
8077
HybridNonlinearFactor(const KeyVector& keys, const DiscreteKeys& discreteKeys,
81-
const Factors& factors, bool normalized = false)
82-
: Base(keys, discreteKeys), factors_(factors), normalized_(normalized) {}
78+
const Factors& factors)
79+
: Base(keys, discreteKeys), factors_(factors) {}
8380

8481
/**
8582
* @brief Convenience constructor that generates the underlying factor
@@ -95,15 +92,12 @@ class HybridNonlinearFactor : public HybridFactor {
9592
* @param discreteKey The discrete key indexing each component factor.
9693
* @param factors Vector of nonlinear factor and scalar pairs.
9794
* Same size as the cardinality of discreteKey.
98-
* @param normalized Flag indicating if the factor error is already
99-
* normalized.
10095
*/
10196
template <typename FACTOR>
10297
HybridNonlinearFactor(
10398
const KeyVector& keys, const DiscreteKey& discreteKey,
104-
const std::vector<std::pair<std::shared_ptr<FACTOR>, double>>& factors,
105-
bool normalized = false)
106-
: Base(keys, {discreteKey}), normalized_(normalized) {
99+
const std::vector<std::pair<std::shared_ptr<FACTOR>, double>>& factors)
100+
: Base(keys, {discreteKey}) {
107101
std::vector<NonlinearFactorValuePair> nonlinear_factors;
108102
KeySet continuous_keys_set(keys.begin(), keys.end());
109103
KeySet factor_keys_set;
@@ -225,11 +219,10 @@ class HybridNonlinearFactor : public HybridFactor {
225219
};
226220
if (!factors_.equals(f.factors_, compare)) return false;
227221

228-
// If everything above passes, and the keys_, discreteKeys_ and normalized_
222+
// If everything above passes, and the keys_ and discreteKeys_
229223
// member variables are identical, return true.
230224
return (std::equal(keys_.begin(), keys_.end(), f.keys().begin()) &&
231-
(discreteKeys_ == f.discreteKeys_) &&
232-
(normalized_ == f.normalized_));
225+
(discreteKeys_ == f.discreteKeys_));
233226
}
234227

235228
/// @}
@@ -260,46 +253,6 @@ class HybridNonlinearFactor : public HybridFactor {
260253
return std::make_shared<HybridGaussianFactor>(
261254
continuousKeys_, discreteKeys_, linearized_factors);
262255
}
263-
264-
/**
265-
* If the component factors are not already normalized, we want to compute
266-
* their normalizing constants so that the resulting joint distribution is
267-
* appropriately computed. Remember, this is the _negative_ normalizing
268-
* constant for the measurement likelihood (since we are minimizing the
269-
* _negative_ log-likelihood).
270-
*/
271-
double nonlinearFactorLogNormalizingConstant(const sharedFactor& factor,
272-
const Values& values) const {
273-
// Information matrix (inverse covariance matrix) for the factor.
274-
Matrix infoMat;
275-
276-
// If this is a NoiseModelFactor, we'll use its noiseModel to
277-
// otherwise noiseModelFactor will be nullptr
278-
if (auto noiseModelFactor =
279-
std::dynamic_pointer_cast<NoiseModelFactor>(factor)) {
280-
// If dynamic cast to NoiseModelFactor succeeded, see if the noise model
281-
// is Gaussian
282-
auto noiseModel = noiseModelFactor->noiseModel();
283-
284-
auto gaussianNoiseModel =
285-
std::dynamic_pointer_cast<noiseModel::Gaussian>(noiseModel);
286-
if (gaussianNoiseModel) {
287-
// If the noise model is Gaussian, retrieve the information matrix
288-
infoMat = gaussianNoiseModel->information();
289-
} else {
290-
// If the factor is not a Gaussian factor, we'll linearize it to get
291-
// something with a normalized noise model
292-
// TODO(kevin): does this make sense to do? I think maybe not in
293-
// general? Should we just yell at the user?
294-
auto gaussianFactor = factor->linearize(values);
295-
infoMat = gaussianFactor->information();
296-
}
297-
}
298-
299-
// Compute the (negative) log of the normalizing constant
300-
return -(factor->dim() * log(2.0 * M_PI) / 2.0) -
301-
(log(infoMat.determinant()) / 2.0);
302-
}
303256
};
304257

305258
} // namespace gtsam

gtsam/hybrid/hybrid.i

+2-7
Original file line numberDiff line numberDiff line change
@@ -248,20 +248,15 @@ class HybridNonlinearFactor : gtsam::HybridFactor {
248248
HybridNonlinearFactor(
249249
const gtsam::KeyVector& keys, const gtsam::DiscreteKeys& discreteKeys,
250250
const gtsam::DecisionTree<
251-
gtsam::Key, std::pair<gtsam::NonlinearFactor*, double>>& factors,
252-
bool normalized = false);
251+
gtsam::Key, std::pair<gtsam::NonlinearFactor*, double>>& factors);
253252

254253
HybridNonlinearFactor(
255254
const gtsam::KeyVector& keys, const gtsam::DiscreteKey& discreteKey,
256-
const std::vector<std::pair<gtsam::NonlinearFactor*, double>>& factors,
257-
bool normalized = false);
255+
const std::vector<std::pair<gtsam::NonlinearFactor*, double>>& factors);
258256

259257
double error(const gtsam::Values& continuousValues,
260258
const gtsam::DiscreteValues& discreteValues) const;
261259

262-
double nonlinearFactorLogNormalizingConstant(
263-
const gtsam::NonlinearFactor* factor, const gtsam::Values& values) const;
264-
265260
HybridGaussianFactor* linearize(const gtsam::Values& continuousValues) const;
266261

267262
void print(string s = "HybridNonlinearFactor\n",

0 commit comments

Comments
 (0)