-
Notifications
You must be signed in to change notification settings - Fork 768
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Hybrid Factor Graph Implementation #1203
Changes from 41 commits
ea4ebb6
0567303
efa37ff
3aac52c
53551e0
095f6ad
2bae286
ee4f9d1
5ea614a
0aeb596
fe5dde7
f237438
36ee4ce
7ad2031
d42fc21
1e8aae3
b4f8eea
4f8dfeb
293ef61
a36c86a
8b4283b
5f03e0f
8d88860
d2dc620
7f2fa61
0f69b4c
a9c1d43
1fe7e74
2de3169
1bf9952
4e481ea
d012bcd
d5fd279
2c4990b
04593cc
2ae2cb6
b8299d7
74af969
b215d3a
e36583e
e325cd1
eb074e7
3f239c2
c3a92a4
b3cab1b
865d10d
573448f
6d26818
7bfa011
d60c4ac
adcbb65
5d3ffb7
e2c7753
30c8e1d
4ee4b37
9d26a3d
3bde044
f443cf3
6cd20fb
c3a59cf
852a9b9
841e6b0
1a8fa23
7c7b5dd
31ab1a3
d2029f3
c8bf9d3
2cc0611
709bbb0
932e65c
2afa678
3e10514
dd87747
92176db
b47cd9d
eeecb27
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,6 +10,7 @@ set (gtsam_subdirs | |
inference | ||
symbolic | ||
discrete | ||
hybrid | ||
linear | ||
nonlinear | ||
sam | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
# Install headers | ||
set(subdir hybrid) | ||
file(GLOB hybrid_headers "*.h") | ||
# FIXME: exclude headers | ||
install(FILES ${hybrid_headers} DESTINATION include/gtsam/hybrid) | ||
|
||
# Add all tests | ||
add_subdirectory(tests) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file GaussianMixtureConditional.cpp | ||
* @brief A hybrid conditional in the Conditional Linear Gaussian scheme | ||
* @author Fan Jiang | ||
* @author Varun Agrawal | ||
* @author Frank Dellaert | ||
* @date Mar 12, 2022 | ||
*/ | ||
|
||
#include <gtsam/base/utilities.h> | ||
#include <gtsam/discrete/DecisionTree-inl.h> | ||
#include <gtsam/hybrid/GaussianMixtureConditional.h> | ||
#include <gtsam/inference/Conditional-inst.h> | ||
#include <gtsam/linear/GaussianFactorGraph.h> | ||
|
||
namespace gtsam { | ||
|
||
GaussianMixtureConditional::GaussianMixtureConditional( | ||
const KeyVector &continuousFrontals, const KeyVector &continuousParents, | ||
const DiscreteKeys &discreteParents, | ||
const GaussianMixtureConditional::Conditionals &conditionals) | ||
: BaseFactor(CollectKeys(continuousFrontals, continuousParents), | ||
discreteParents), | ||
BaseConditional(continuousFrontals.size()), | ||
conditionals_(conditionals) {} | ||
|
||
/* *******************************************************************************/ | ||
const GaussianMixtureConditional::Conditionals &GaussianMixtureConditional::conditionals() { | ||
return conditionals_; | ||
} | ||
|
||
/* *******************************************************************************/ | ||
GaussianMixtureConditional GaussianMixtureConditional::FromConditionalList( | ||
const KeyVector &continuousFrontals, const KeyVector &continuousParents, | ||
const DiscreteKeys &discreteParents, | ||
const std::vector<GaussianConditional::shared_ptr> &conditionalsList) { | ||
Conditionals dt(discreteParents, conditionalsList); | ||
|
||
return GaussianMixtureConditional(continuousFrontals, continuousParents, discreteParents, | ||
dt); | ||
} | ||
|
||
/* *******************************************************************************/ | ||
GaussianMixtureConditional::Sum GaussianMixtureConditional::add( | ||
const GaussianMixtureConditional::Sum &sum) const { | ||
using Y = GaussianFactorGraph; | ||
auto add = [](const Y &graph1, const Y &graph2) { | ||
auto result = graph1; | ||
result.push_back(graph2); | ||
return result; | ||
}; | ||
const Sum wrapped = asGraph(); | ||
return sum.empty() ? wrapped : sum.apply(wrapped, add); | ||
} | ||
|
||
/* *******************************************************************************/ | ||
GaussianMixtureConditional::Sum GaussianMixtureConditional::asGraph() const { | ||
auto lambda = [](const GaussianFactor::shared_ptr &factor) { | ||
GaussianFactorGraph result; | ||
result.push_back(factor); | ||
return result; | ||
}; | ||
return {conditionals_, lambda}; | ||
} | ||
|
||
/* TODO(fan): this (for Testable) is not implemented! */ | ||
bool GaussianMixtureConditional::equals(const HybridFactor &lf, double tol) const { | ||
return false; | ||
} | ||
|
||
/* *******************************************************************************/ | ||
void GaussianMixtureConditional::print(const std::string &s, | ||
const KeyFormatter &formatter) const { | ||
std::cout << s << ": "; | ||
if (isContinuous_) std::cout << "Cont. "; | ||
if (isDiscrete_) std::cout << "Disc. "; | ||
if (isHybrid_) std::cout << "Hybr. "; | ||
BaseConditional::print("", formatter); | ||
std::cout << "Discrete Keys = "; | ||
for (auto &dk : discreteKeys_) { | ||
std::cout << "(" << formatter(dk.first) << ", " << dk.second << "), "; | ||
} | ||
std::cout << "\n"; | ||
conditionals_.print( | ||
"", [&](Key k) { return formatter(k); }, | ||
[&](const GaussianConditional::shared_ptr &gf) -> std::string { | ||
RedirectCout rd; | ||
if (!gf->empty()) | ||
gf->print("", formatter); | ||
else | ||
return {"nullptr"}; | ||
return rd.str(); | ||
}); | ||
} | ||
} // namespace gtsam |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file GaussianMixtureConditional.h | ||
* @brief A hybrid conditional in the Conditional Linear Gaussian scheme | ||
* @author Fan Jiang | ||
* @author Varun Agrawal | ||
* @date Mar 12, 2022 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <gtsam/discrete/DecisionTree.h> | ||
#include <gtsam/hybrid/HybridFactor.h> | ||
#include <gtsam/inference/Conditional.h> | ||
#include <gtsam/linear/GaussianConditional.h> | ||
|
||
namespace gtsam { | ||
class GaussianMixtureConditional : public HybridFactor, | ||
public Conditional<HybridFactor, GaussianMixtureConditional> { | ||
public: | ||
using This = GaussianMixtureConditional; | ||
using shared_ptr = boost::shared_ptr<GaussianMixtureConditional>; | ||
using BaseFactor = HybridFactor; | ||
using BaseConditional = Conditional<HybridFactor, GaussianMixtureConditional>; | ||
|
||
using Conditionals = DecisionTree<Key, GaussianConditional::shared_ptr>; | ||
|
||
Conditionals conditionals_; | ||
|
||
public: | ||
/** | ||
* @brief Construct a new Gaussian Mixture object | ||
* | ||
* @param continuousFrontals the continuous frontals. | ||
* @param continuousParents the continuous parents. | ||
* @param discreteParents the discrete parents. Will be placed last. | ||
* @param conditionals a decision tree of GaussianConditionals. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment on the requirement of those conditionals. Do you check for it? Is an exception thrown when not correct? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not resolved |
||
*/ | ||
GaussianMixtureConditional(const KeyVector &continuousFrontals, | ||
const KeyVector &continuousParents, | ||
const DiscreteKeys &discreteParents, | ||
const Conditionals &conditionals); | ||
|
||
using Sum = DecisionTree<Key, GaussianFactorGraph>; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. One-line comment? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not a fan of the typedef |
||
|
||
const Conditionals &conditionals(); | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** | ||
* @brief Combine Decision Trees | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
Sum add(const Sum &sum) const; | ||
|
||
/** | ||
* @brief Convert a DecisionTree of factors into a DT of Gaussian FGs. | ||
*/ | ||
Sum asGraph() const; | ||
|
||
/** | ||
* @brief Make a Gaussian Mixture from a list of Gaussian conditionals | ||
* | ||
* @param continuousFrontals The continuous frontal variables | ||
* @param continuousParents The continuous parent variables | ||
* @param discreteParents Discrete parents variables | ||
* @param conditionals List of conditionals | ||
*/ | ||
static This FromConditionalList( | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const KeyVector &continuousFrontals, const KeyVector &continuousParents, | ||
const DiscreteKeys &discreteParents, | ||
const std::vector<GaussianConditional::shared_ptr> &conditionals); | ||
|
||
/* TODO: this is only a stub */ | ||
bool equals(const HybridFactor &lf, double tol = 1e-9) const override; | ||
|
||
/* print utility */ | ||
void print( | ||
const std::string &s = "GaussianMixtureConditional\n", | ||
const KeyFormatter &formatter = DefaultKeyFormatter) const override; | ||
}; | ||
} // namespace gtsam |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file GaussianMixtureFactor.cpp | ||
* @brief A set of Gaussian factors indexed by a set of discrete keys. | ||
* @author Fan Jiang | ||
* @author Varun Agrawal | ||
* @author Frank Dellaert | ||
* @date Mar 12, 2022 | ||
*/ | ||
|
||
#include <gtsam/base/utilities.h> | ||
#include <gtsam/discrete/DecisionTree-inl.h> | ||
#include <gtsam/discrete/DecisionTree.h> | ||
#include <gtsam/hybrid/GaussianMixtureFactor.h> | ||
#include <gtsam/linear/GaussianFactorGraph.h> | ||
|
||
namespace gtsam { | ||
|
||
GaussianMixtureFactor::GaussianMixtureFactor(const KeyVector &continuousKeys, | ||
const DiscreteKeys &discreteKeys, | ||
const Factors &factors) | ||
: Base(continuousKeys, discreteKeys), factors_(factors) {} | ||
bool GaussianMixtureFactor::equals(const HybridFactor &lf, double tol) const { | ||
return false; | ||
} | ||
|
||
/* *******************************************************************************/ | ||
GaussianMixtureFactor GaussianMixtureFactor::FromFactorList( | ||
const KeyVector &continuousKeys, const DiscreteKeys &discreteKeys, | ||
const std::vector<GaussianFactor::shared_ptr> &factorsList) { | ||
Factors dt(discreteKeys, factorsList); | ||
|
||
return GaussianMixtureFactor(continuousKeys, discreteKeys, dt); | ||
} | ||
|
||
|
||
/* *******************************************************************************/ | ||
void GaussianMixtureFactor::print(const std::string &s, | ||
const KeyFormatter &formatter) const { | ||
HybridFactor::print(s, formatter); | ||
factors_.print( | ||
"mixture = ", [&](Key k) { return formatter(k); }, | ||
[&](const GaussianFactor::shared_ptr &gf) -> std::string { | ||
RedirectCout rd; | ||
if (!gf->empty()) | ||
gf->print("", formatter); | ||
else | ||
return {"nullptr"}; | ||
return rd.str(); | ||
}); | ||
} | ||
|
||
/* *******************************************************************************/ | ||
const GaussianMixtureFactor::Factors &GaussianMixtureFactor::factors() { | ||
return factors_; | ||
} | ||
|
||
/* *******************************************************************************/ | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
GaussianMixtureFactor::Sum GaussianMixtureFactor::add( | ||
const GaussianMixtureFactor::Sum &sum) const { | ||
using Y = GaussianFactorGraph; | ||
auto add = [](const Y &graph1, const Y &graph2) { | ||
auto result = graph1; | ||
result.push_back(graph2); | ||
return result; | ||
}; | ||
const Sum wrapped = wrappedFactors(); | ||
return sum.empty() ? wrapped : sum.apply(wrapped, add); | ||
} | ||
|
||
/* *******************************************************************************/ | ||
GaussianMixtureFactor::Sum GaussianMixtureFactor::wrappedFactors() const { | ||
auto wrap = [](const GaussianFactor::shared_ptr &factor) { | ||
GaussianFactorGraph result; | ||
result.push_back(factor); | ||
return result; | ||
}; | ||
return {factors_, wrap}; | ||
} | ||
} // namespace gtsam |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* ---------------------------------------------------------------------------- | ||
|
||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
|
||
* See LICENSE for the license information | ||
|
||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file GaussianMixtureFactor.h | ||
* @brief A set of Gaussian factors indexed by a set of discrete keys. | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
* @author Fan Jiang | ||
* @author Varun Agrawal | ||
* @author Frank Dellaert | ||
* @date Mar 12, 2022 | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <gtsam/discrete/DecisionTree.h> | ||
#include <gtsam/discrete/DiscreteKey.h> | ||
#include <gtsam/hybrid/HybridFactor.h> | ||
#include <gtsam/linear/GaussianFactor.h> | ||
|
||
namespace gtsam { | ||
|
||
class GaussianFactorGraph; | ||
|
||
typedef std::vector<gtsam::GaussianFactor::shared_ptr> GaussianFactorVector; | ||
|
||
class GaussianMixtureFactor : public HybridFactor { | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
public: | ||
using Base = HybridFactor; | ||
using This = GaussianMixtureFactor; | ||
using shared_ptr = boost::shared_ptr<This>; | ||
|
||
using Factors = DecisionTree<Key, GaussianFactor::shared_ptr>; | ||
|
||
Factors factors_; | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
GaussianMixtureFactor() = default; | ||
|
||
GaussianMixtureFactor(const KeyVector &continuousKeys, | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
const DiscreteKeys &discreteKeys, | ||
const Factors &factors); | ||
|
||
using Sum = DecisionTree<Key, GaussianFactorGraph>; | ||
|
||
const Factors &factors(); | ||
|
||
static This FromFactorList( | ||
const KeyVector &continuousKeys, const DiscreteKeys &discreteKeys, | ||
const std::vector<GaussianFactor::shared_ptr> &factors); | ||
|
||
Sum add(const Sum &sum) const; | ||
|
||
Sum wrappedFactors() const; | ||
|
||
bool equals(const HybridFactor &lf, double tol = 1e-9) const override; | ||
varunagrawal marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
void print( | ||
const std::string &s = "HybridFactor\n", | ||
const KeyFormatter &formatter = DefaultKeyFormatter) const override; | ||
}; | ||
|
||
} // namespace gtsam |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
/* ---------------------------------------------------------------------------- | ||
* GTSAM Copyright 2010, Georgia Tech Research Corporation, | ||
* Atlanta, Georgia 30332-0415 | ||
* All Rights Reserved | ||
* Authors: Frank Dellaert, et al. (see THANKS for the full author list) | ||
* See LICENSE for the license information | ||
* -------------------------------------------------------------------------- */ | ||
|
||
/** | ||
* @file HybridBayesNet.cpp | ||
* @brief A bayes net of Gaussian Conditionals indexed by discrete keys. | ||
* @author Fan Jiang | ||
* @date January 2022 | ||
*/ | ||
|
||
#include <gtsam/hybrid/HybridBayesNet.h> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we need this file? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We don't but could use it in the future? Seems fine to have it. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Kill until we need it. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this should be called
GaussianMixture
. I thought it was. Was it renamed?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In one of my reviews I asked it to be renamed to
GaussianMixtureConditional
so that it is obvious it is a conditional and contrasts withGaussianMixtureFactor
. I can undo that if you'd like.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would like you to undo it, yes.