HybridConditional.cpp
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, 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 
23 #include <gtsam/inference/Key.h>
24 
25 namespace gtsam {
26 
27 /* ************************************************************************ */
29  const DiscreteKeys &discreteFrontals,
30  const KeyVector &continuousParents,
31  const DiscreteKeys &discreteParents)
32  : HybridConditional(CollectKeys(continuousFrontals, continuousParents),
33  CollectDiscreteKeys(discreteFrontals, discreteParents),
34  continuousFrontals.size() + discreteFrontals.size()) {}
35 
36 /* ************************************************************************ */
38  const std::shared_ptr<GaussianConditional> &continuousConditional)
39  : HybridConditional(continuousConditional->keys(), {},
40  continuousConditional->nrFrontals()) {
41  inner_ = continuousConditional;
42 }
43 
44 /* ************************************************************************ */
46  const std::shared_ptr<DiscreteConditional> &discreteConditional)
47  : HybridConditional({}, discreteConditional->discreteKeys(),
48  discreteConditional->nrFrontals()) {
49  inner_ = discreteConditional;
50 }
51 
52 /* ************************************************************************ */
54  const std::shared_ptr<HybridGaussianConditional> &hybridGaussianCond)
55  : BaseFactor(hybridGaussianCond->continuousKeys(),
56  hybridGaussianCond->discreteKeys()),
57  BaseConditional(hybridGaussianCond->nrFrontals()) {
58  inner_ = hybridGaussianCond;
59 }
60 
61 /* ************************************************************************ */
62 void HybridConditional::print(const std::string &s,
63  const KeyFormatter &formatter) const {
64  std::cout << s;
65 
66  if (inner_) {
67  inner_->print("", formatter);
68  } else {
69  if (isContinuous()) std::cout << "Continuous ";
70  if (isDiscrete()) std::cout << "Discrete ";
71  if (isHybrid()) std::cout << "Hybrid ";
73 
74  std::cout << "P(";
75  size_t index = 0;
76  const size_t N = keys().size();
77  const size_t contN = N - discreteKeys_.size();
78  while (index < N) {
79  if (index > 0) {
80  if (index == nrFrontals_)
81  std::cout << " | ";
82  else
83  std::cout << ", ";
84  }
85  if (index < contN) {
86  std::cout << formatter(keys()[index]);
87  } else {
88  auto &dk = discreteKeys_[index - contN];
89  std::cout << "(" << formatter(dk.first) << ", " << dk.second << ")";
90  }
91  index++;
92  }
93  }
94 }
95 
96 /* ************************************************************************ */
97 bool HybridConditional::equals(const HybridFactor &other, double tol) const {
98  const This *e = dynamic_cast<const This *>(&other);
99  if (e == nullptr) return false;
100  if (auto gm = asHybrid()) {
101  auto other = e->asHybrid();
102  return other != nullptr && gm->equals(*other, tol);
103  } else if (auto gc = asGaussian()) {
104  auto other = e->asGaussian();
105  return other != nullptr && gc->equals(*other, tol);
106  } else if (auto dc = asDiscrete()) {
107  auto other = e->asDiscrete();
108  return other != nullptr && dc->equals(*other, tol);
109  } else
110  return inner_ ? (e->inner_ ? inner_->equals(*(e->inner_), tol) : false)
111  : !(e->inner_);
112 }
113 
114 /* ************************************************************************ */
116  if (auto gc = asGaussian()) {
117  return gc->error(values.continuous());
118  } else if (auto gm = asHybrid()) {
119  return gm->error(values);
120  } else if (auto dc = asDiscrete()) {
121  return dc->error(values.discrete());
122  } else
123  throw std::runtime_error(
124  "HybridConditional::error: conditional type not handled");
125 }
126 
127 /* ************************************************************************ */
129  const VectorValues &values) const {
130  if (auto gc = asGaussian()) {
131  return {gc->error(values)}; // NOTE: a "constant" tree
132  } else if (auto gm = asHybrid()) {
133  return gm->errorTree(values);
134  } else if (auto dc = asDiscrete()) {
135  return dc->errorTree();
136  } else
137  throw std::runtime_error(
138  "HybridConditional::error: conditional type not handled");
139 }
140 
141 /* ************************************************************************ */
143  if (auto gc = asGaussian()) {
144  return gc->logProbability(values.continuous());
145  } else if (auto gm = asHybrid()) {
146  return gm->logProbability(values);
147  } else if (auto dc = asDiscrete()) {
148  return dc->logProbability(values.discrete());
149  } else
150  throw std::runtime_error(
151  "HybridConditional::logProbability: conditional type not handled");
152 }
153 
154 /* ************************************************************************ */
156  if (auto gc = asGaussian()) {
157  return gc->negLogConstant();
158  } else if (auto gm = asHybrid()) {
159  return gm->negLogConstant();
160  } else if (auto dc = asDiscrete()) {
161  return dc->negLogConstant(); // 0.0!
162  } else
163  throw std::runtime_error(
164  "HybridConditional::negLogConstant: conditional type not handled");
165 }
166 
167 /* ************************************************************************ */
169  return std::exp(logProbability(values));
170 }
171 
172 } // namespace gtsam
gtsam::Conditional::print
void print(const std::string &s="Conditional", const KeyFormatter &formatter=DefaultKeyFormatter) const
Definition: Conditional-inst.h:30
gtsam::HybridConditional::asHybrid
HybridGaussianConditional::shared_ptr asHybrid() const
Return HybridConditional as a HybridGaussianConditional.
Definition: HybridConditional.h:155
gtsam::HybridValues
Definition: HybridValues.h:37
gtsam::HybridConditional
Definition: HybridConditional.h:60
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::HybridConditional::HybridConditional
HybridConditional()=default
Default constructor needed for serialization.
gtsam::HybridFactor::isDiscrete
bool isDiscrete() const
True if this is a factor of discrete variables only.
Definition: HybridFactor.h:119
gtsam::HybridConditional::errorTree
AlgebraicDecisionTree< Key > errorTree(const VectorValues &values) const override
Compute error of the HybridConditional as a tree.
Definition: HybridConditional.cpp:128
gtsam::HybridFactor
Definition: HybridFactor.h:51
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
gtsam::HybridFactor::isHybrid
bool isHybrid() const
True is this is a Discrete-Continuous factor.
Definition: HybridFactor.h:125
gtsam::KeyVector
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
Definition: Key.h:92
gtsam::AlgebraicDecisionTree< Key >
gtsam::HybridConditional::evaluate
double evaluate(const HybridValues &values) const override
Return the probability (or density) of the underlying conditional.
Definition: HybridConditional.cpp:168
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
Key.h
HybridFactor.h
gtsam::HybridConditional::print
void print(const std::string &s="Hybrid Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
Definition: HybridConditional.cpp:62
gtsam::VectorValues
Definition: VectorValues.h:74
gtsam::KeyFormatter
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
Definition: Key.h:35
gtsam::Conditional< HybridFactor, HybridConditional >::nrFrontals_
size_t nrFrontals_
Definition: Conditional.h:67
gtsam::CollectKeys
KeyVector CollectKeys(const KeyVector &continuousKeys, const DiscreteKeys &discreteKeys)
Definition: HybridFactor.cpp:23
gtsam::HybridConditional::error
double error(const HybridValues &values) const override
Return the error of the underlying conditional.
Definition: HybridConditional.cpp:115
gtsam::HybridFactor::discreteKeys_
DiscreteKeys discreteKeys_
Definition: HybridFactor.h:62
gtsam::HybridFactor::isContinuous
bool isContinuous() const
True if this is a factor of continuous variables only.
Definition: HybridFactor.h:122
gtsam::Conditional
Definition: Conditional.h:63
gtsam::HybridConditional::inner_
std::shared_ptr< Factor > inner_
Type-erased pointer to the inner type.
Definition: HybridConditional.h:73
different_sigmas::gc
const auto gc
Definition: testHybridBayesNet.cpp:231
gtsam
traits
Definition: SFMdata.h:40
gtsam::Factor::keys
const KeyVector & keys() const
Access the factor's involved variable keys.
Definition: Factor.h:143
gtsam::HybridConditional::asDiscrete
DiscreteConditional::shared_ptr asDiscrete() const
Return conditional as a DiscreteConditional.
Definition: HybridConditional.h:173
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::HybridConditional::negLogConstant
double negLogConstant() const override
Definition: HybridConditional.cpp:155
N
#define N
Definition: igam.h:9
gtsam::HybridConditional::asGaussian
GaussianConditional::shared_ptr asGaussian() const
Return HybridConditional as a GaussianConditional.
Definition: HybridConditional.h:164
HybridValues.h
gtsam::HybridConditional::equals
bool equals(const HybridFactor &other, double tol=1e-9) const override
GTSAM-style equals.
Definition: HybridConditional.cpp:97
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Conditional-inst.h
gtsam::HybridConditional::logProbability
double logProbability(const HybridValues &values) const override
Return the log-probability (or density) of the underlying conditional.
Definition: HybridConditional.cpp:142
HybridConditional.h
gtsam::CollectDiscreteKeys
DiscreteKeys CollectDiscreteKeys(const DiscreteKeys &key1, const DiscreteKeys &key2)
Definition: HybridFactor.cpp:43


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:26