TinyHybridExample.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010-2023, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
12 /*
13  * @file TinyHybridExample.h
14  * @date December, 2022
15  * @author Frank Dellaert
16  */
17 
20 #include <gtsam/inference/Symbol.h>
21 #pragma once
22 
23 namespace gtsam {
24 namespace tiny {
25 
29 
30 // Create mode key: 0 is low-noise, 1 is high-noise.
31 const DiscreteKey mode{M(0), 2};
32 
39 inline HybridBayesNet createHybridBayesNet(size_t num_measurements = 1,
40  bool manyModes = false) {
41  HybridBayesNet bayesNet;
42 
43  // Create Gaussian mixture z_i = x0 + noise for each measurement.
44  for (size_t i = 0; i < num_measurements; i++) {
45  const auto mode_i = manyModes ? DiscreteKey{M(i), 2} : mode;
46  bayesNet.emplace_back(
47  new GaussianMixture({Z(i)}, {X(0)}, {mode_i},
49  Z(i), I_1x1, X(0), Z_1x1, 0.5),
51  Z(i), I_1x1, X(0), Z_1x1, 3)}));
52  }
53 
54  // Create prior on X(0).
55  bayesNet.push_back(
57 
58  // Add prior on mode.
59  const size_t nrModes = manyModes ? num_measurements : 1;
60  for (size_t i = 0; i < nrModes; i++) {
61  bayesNet.emplace_back(new DiscreteConditional({M(i), 2}, "4/6"));
62  }
63  return bayesNet;
64 }
65 
73  size_t num_measurements = 1,
74  std::optional<VectorValues> measurements = {},
75  bool manyModes = false) {
76  auto bayesNet = createHybridBayesNet(num_measurements, manyModes);
77  if (measurements) {
78  // Use the measurements to create a hybrid factor graph.
79  return bayesNet.toFactorGraph(*measurements);
80  } else {
81  // Sample from the generative model to create a hybrid factor graph.
82  return bayesNet.toFactorGraph(bayesNet.sample().continuous());
83  }
84 }
85 
86 } // namespace tiny
87 } // namespace gtsam
Key M(std::uint64_t j)
Matrix< RealScalar, Dynamic, Dynamic > M
Definition: bench_gemm.cpp:51
const DiscreteKey mode
Key X(std::uint64_t j)
A Bayes net of Gaussian Conditionals indexed by discrete keys.
Key Z(std::uint64_t j)
#define Z
Definition: icosphere.cpp:21
A conditional of gaussian mixtures indexed by discrete variables, as part of a Bayes Network...
void push_back(std::shared_ptr< HybridConditional > conditional)
Add a hybrid conditional using a shared_ptr.
Linearized Hybrid factor graph that uses type erasure.
static shared_ptr sharedMeanAndStddev(Args &&... args)
Create shared pointer by forwarding arguments to fromMeanAndStddev.
HybridGaussianFactorGraph createHybridGaussianFactorGraph(size_t num_measurements=1, std::optional< VectorValues > measurements={}, bool manyModes=false)
void emplace_back(Conditional *conditional)
Eigen::Matrix< double, 1, 1 > Vector1
Definition: Vector.h:41
traits
Definition: chartTesting.h:28
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
HybridBayesNet createHybridBayesNet(size_t num_measurements=1, bool manyModes=false)
#define X
Definition: icosphere.cpp:20


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:40:20