Go to the documentation of this file.
46 size_t maxNrLeaves,
const std::optional<double> &marginalThreshold,
48 #if GTSAM_HYBRID_TIMING
56 auto prunedBN = marginal.
prune(maxNrLeaves, marginalThreshold, &fixed);
60 for (
auto &&conditional : prunedBN) pruned = pruned * (*conditional);
63 if (marginalThreshold && fixedValues) {
72 for (std::shared_ptr<HybridConditional> conditional : *
this) {
73 if (conditional->isDiscrete())
continue;
76 if (marginalThreshold) {
77 conditional = std::static_pointer_cast<HybridConditional>(
78 conditional->restrict(fixed));
82 if (
auto hgc = conditional->asHybrid()) {
84 auto prunedHybridGaussianConditional =
hgc->prune(pruned);
85 if (!prunedHybridGaussianConditional) {
86 throw std::runtime_error(
87 "A HybridGaussianConditional had all its conditionals pruned");
90 result.push_back(prunedHybridGaussianConditional);
91 }
else if (conditional->isContinuous()) {
93 result.push_back(conditional);
95 throw std::runtime_error(
96 "HybrdiBayesNet::prune: Unknown HybridConditional type.");
99 #if GTSAM_HYBRID_TIMING
105 result.push_back(discrete);
113 for (
auto &&conditional : *
this) {
114 if (
auto dc = conditional->asDiscrete()) {
125 for (
auto &&conditional : *
this) {
126 if (
auto gm = conditional->asHybrid()) {
128 gbn.push_back(gm->choose(assignment));
129 }
else if (
auto gc = conditional->asGaussian()) {
132 }
else if (
auto dc = conditional->asDiscrete()) {
146 for (
auto &&conditional : *
this) {
147 if (conditional->isDiscrete()) {
152 dtc->toDecisionTreeFactor()));
154 discrete_fg.
push_back(conditional->asDiscrete());
177 if (std::find(
gbn.begin(),
gbn.end(),
nullptr) !=
gbn.end()) {
180 return gbn.optimize();
185 std::mt19937_64 *
rng)
const {
187 for (
auto &&conditional : *
this) {
188 if (conditional->isDiscrete()) {
190 dbn.
push_back(conditional->asDiscrete());
199 return {
sample, assignment};
224 for (
auto &&conditional : *
this) {
225 result =
result + conditional->errorTree(continuousValues);
233 const std::optional<DiscreteValues> &discrete)
const {
234 double negLogNormConst = 0.0;
236 for (
auto &&conditional : *
this) {
237 if (discrete.has_value()) {
238 if (
auto gm = conditional->asHybrid()) {
239 negLogNormConst += gm->choose(*discrete)->negLogConstant();
240 }
else if (
auto gc = conditional->asGaussian()) {
241 negLogNormConst +=
gc->negLogConstant();
242 }
else if (
auto dc = conditional->asDiscrete()) {
243 negLogNormConst += dc->choose(*discrete)->negLogConstant();
245 throw std::runtime_error(
246 "Unknown conditional type when computing negLogConstant");
249 negLogNormConst += conditional->negLogConstant();
252 return negLogNormConst;
276 for (
auto &&conditional : *
this) {
278 if (
auto gc = conditional->asGaussian()) {
280 }
else if (
auto gm = conditional->asHybrid()) {
283 throw std::runtime_error(
"Unknown conditional type");
bool equals(const This &fg, double tol=1e-9) const
GTSAM-style equals.
HybridValues sample() const
Sample using ancestral sampling, use default rng.
DiscreteValues sample() const
do ancestral sampling
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
double error(const HybridValues &values) const
double evaluate(const HybridValues &values) const
Evaluate hybrid probability density for given HybridValues.
const KeyFormatter & formatter
A Bayes net of Gaussian Conditionals indexed by discrete keys.
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
const VectorValues & continuous() const
Return the multi-dimensional vector values.
HybridValues optimize() const
Solve the HybridBayesNet by first computing the MPE of all the discrete variables and then optimizing...
static std::mt19937 kRandomNumberGenerator(42)
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
AlgebraicDecisionTree< Key > errorTree(const VectorValues &continuousValues) const
Compute the negative log posterior log P'(M|x) of all assignments up to a constant,...
DiscreteBayesNet prune(size_t maxNrLeaves, const std::optional< double > &marginalThreshold={}, DiscreteValues *fixedValues=nullptr) const
Prune the Bayes net.
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
void print(const std::string &s="", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style printing.
DiscreteValues mpe() const
Compute the Most Probable Explanation (MPE) of the discrete variables.
DiscreteBayesNet discreteMarginal() const
Get the discrete Bayes Net P(M). As the hybrid Bayes net defines P(X,M) = P(X|M) P(M),...
DecisionTree apply(const Unary &op) const
static const GaussianBayesNet gbn
std::shared_ptr< This > shared_ptr
shared_ptr to this class
std::vector< double > measurements
AlgebraicDecisionTree< Key > discretePosterior(const VectorValues &continuousValues) const
Compute normalized posterior P(M|X=x) and return as a tree.
double logProbability(const HybridValues &x) const
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
const DiscreteValues & discrete() const
Return the discrete values.
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
HybridBayesNet prune(size_t maxNrLeaves, const std::optional< double > &marginalThreshold={}, DiscreteValues *fixedValues=nullptr) const
Prune the Bayes Net such that we have at most maxNrLeaves leaves.
HybridGaussianFactorGraph toFactorGraph(const VectorValues &measurements) const
GaussianBayesNet choose(const DiscreteValues &assignment) const
Get the Gaussian Bayes net P(X|M=m) corresponding to a specific assignment m for the discrete variabl...
static std::mt19937_64 kRandomNumberGenerator(42)
double negLogConstant(const std::optional< DiscreteValues > &discrete) const
Get the negative log of the normalization constant corresponding to the joint density represented by ...
gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:48