DiscreteConditional.h
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 
19 #pragma once
20 
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 namespace gtsam {
30 
37 class GTSAM_EXPORT DiscreteConditional
38  : public DecisionTreeFactor,
39  public Conditional<DecisionTreeFactor, DiscreteConditional> {
40  public:
41  // typedefs needed to play nice with gtsam
43  typedef std::shared_ptr<This> shared_ptr;
47 
49 
52 
55 
57  DiscreteConditional(size_t nFrontals, const DecisionTreeFactor& f);
58 
63  DiscreteConditional(size_t nFrontals, const DiscreteKeys& keys,
64  const ADT& potentials);
65 
67  explicit DiscreteConditional(const Signature& signature);
68 
77  const Signature::Table& table)
78  : DiscreteConditional(Signature(key, parents, table)) {}
79 
88  const std::vector<double>& table)
89  : DiscreteConditional(1, DiscreteKeys{key} & parents,
90  ADT(DiscreteKeys{key} & parents, table)) {}
91 
102  const std::string& spec)
103  : DiscreteConditional(Signature(key, parents, spec)) {}
104 
106  DiscreteConditional(const DiscreteKey& key, const std::string& spec)
107  : DiscreteConditional(Signature(key, {}, spec)) {}
108 
113  DiscreteConditional(const DecisionTreeFactor& joint,
114  const DecisionTreeFactor& marginal);
115 
121  DiscreteConditional(const DecisionTreeFactor& joint,
122  const DecisionTreeFactor& marginal,
123  const Ordering& orderedKeys);
124 
140  DiscreteConditional operator*(const DiscreteConditional& other) const;
141 
143  DiscreteConditional marginal(Key key) const;
144 
148 
150  void print(
151  const std::string& s = "Discrete Conditional: ",
152  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
153 
155  bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
156 
160 
162  double logProbability(const DiscreteValues& x) const { return -error(x); }
163 
166  const std::string& s = "Discrete Conditional: ",
167  const KeyFormatter& formatter = DefaultKeyFormatter) const {
168  static_cast<const BaseConditional*>(this)->print(s, formatter);
169  }
170 
171  using BaseFactor::error;
172  using BaseFactor::evaluate;
173  using BaseFactor::operator();
174 
188  shared_ptr choose(const DiscreteValues& given) const;
189 
192  const DiscreteValues& frontalValues) const;
193 
195  DecisionTreeFactor::shared_ptr likelihood(size_t frontal) const;
196 
202  size_t sample(const DiscreteValues& parentsValues) const;
203 
205  size_t sample(size_t parent_value) const;
206 
208  size_t sample() const;
209 
215  size_t argmax(const DiscreteValues& parentsValues = DiscreteValues()) const;
216 
220 
222  void sampleInPlace(DiscreteValues* parentsValues) const;
223 
225  std::vector<DiscreteValues> frontalAssignments() const;
226 
228  std::vector<DiscreteValues> allAssignments() const;
229 
233 
235  std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
236  const Names& names = {}) const override;
237 
239  std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
240  const Names& names = {}) const override;
241 
245 
250  double evaluate(const HybridValues& x) const override;
251 
252  using BaseConditional::operator();
253 
258  double logProbability(const HybridValues& x) const override {
259  return -error(x);
260  }
261 
268  double negLogConstant() const override;
269 
271 
272  protected:
275  bool forceComplete) const;
276 
277  private:
278 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
279 
280  friend class boost::serialization::access;
281  template <class Archive>
282  void serialize(Archive& ar, const unsigned int /*version*/) {
283  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseFactor);
284  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseConditional);
285  }
286 #endif
287 };
288 // DiscreteConditional
289 
290 // traits
291 template <>
292 struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
293 
294 } // namespace gtsam
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const std::vector< double > &table)
Definition: DiscreteConditional.h:87
gtsam::DiscreteConditional::logProbability
double logProbability(const DiscreteValues &x) const
Log-probability is just -error(x).
Definition: DiscreteConditional.h:162
gtsam::Signature::Table
std::vector< Row > Table
Definition: Signature.h:60
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:130
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:45
gtsam::HybridValues
Definition: HybridValues.h:37
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
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const Signature::Table &table)
Definition: DiscreteConditional.h:76
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
HybridValues
gtsam::DecisionTreeFactor::shared_ptr
std::shared_ptr< DecisionTreeFactor > shared_ptr
Definition: DecisionTreeFactor.h:51
DiscreteFactor
Discrete values for.
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree< Key >
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::DiscreteConditional::BaseConditional
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:46
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:156
ADT
AlgebraicDecisionTree< Key > ADT
Definition: testAlgebraicDecisionTree.cpp:32
table
ArrayXXf table(10, 4)
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
Signature.h
signatures for conditional densities
gtsam::DiscreteConditional::This
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:42
gtsam::Conditional
Definition: Conditional.h:63
key
const gtsam::Symbol key('X', 0)
gtsam::DiscreteConditional::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:43
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
gtsam::DiscreteConditional::logProbability
double logProbability(const HybridValues &x) const override
< HybridValues version
Definition: DiscreteConditional.h:258
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
gtsam::DiscreteConditional::printSignature
void printSignature(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
print index signature only
Definition: DiscreteConditional.h:165
error
static double error
Definition: testRot3.cpp:37
gtsam::traits
Definition: Group.h:36
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const std::string &spec)
No-parent specialization; can also use DiscreteDistribution.
Definition: DiscreteConditional.h:106
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional()
Default constructor needed for serialization.
Definition: DiscreteConditional.h:54
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::html
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition: DiscreteValues.cpp:135
gtsam::DiscreteConditional::BaseFactor
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:44
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const std::string &spec)
Definition: DiscreteConditional.h:101
DecisionTreeFactor.h
choose
static const T & choose(int layout, const T &col, const T &row)
Definition: cxx11_tensor_block_access.cpp:27
gtsam::Signature
Definition: Signature.h:54
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Conditional-inst.h


gtsam
Author(s):
autogenerated on Thu Dec 19 2024 04:01:03