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


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