gtsam
Loading...
Searching...
No Matches
DiscreteBayesNet.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
18
19#pragma once
20
25
26#include <memory>
27#include <map>
28#include <string>
29#include <utility>
30#include <vector>
31
32namespace gtsam {
33
38class GTSAM_EXPORT DiscreteBayesNet: public BayesNet<DiscreteConditional> {
39 public:
41 typedef DiscreteBayesNet This;
42 typedef DiscreteConditional ConditionalType;
43 typedef std::shared_ptr<This> shared_ptr;
44 typedef std::shared_ptr<ConditionalType> sharedConditional;
45
48
51
53 template <typename ITERATOR>
54 DiscreteBayesNet(ITERATOR firstConditional, ITERATOR lastConditional)
55 : Base(firstConditional, lastConditional) {}
56
58 template <class CONTAINER>
59 explicit DiscreteBayesNet(const CONTAINER& conditionals)
60 : Base(conditionals) {}
61
64 template <class DERIVEDCONDITIONAL>
66 : Base(graph) {}
67
69
72
74 bool equals(const This& bn, double tol = 1e-9) const;
75
77
80
81 // Add inherited versions of add.
82 using Base::add;
83
85 void add(const DiscreteKey& key, const std::string& spec) {
87 }
88
90 template <typename... Args>
91 void add(Args&&... args) {
92 emplace_shared<DiscreteConditional>(std::forward<Args>(args)...);
93 }
94
95 //** evaluate for given DiscreteValues */
96 double evaluate(const DiscreteValues & values) const;
97
98 //** (Preferred) sugar for the above for given DiscreteValues */
99 double operator()(const DiscreteValues & values) const {
100 return evaluate(values);
101 }
102
103 //** log(evaluate(values)) for given DiscreteValues */
104 double logProbability(const DiscreteValues & values) const;
105
115 DiscreteValues sample(std::mt19937_64* rng = nullptr) const;
116
125 DiscreteValues sample(DiscreteValues given,
126 std::mt19937_64* rng = nullptr) const;
127
136 DiscreteBayesNet prune(size_t maxNrLeaves,
137 const std::optional<double>& marginalThreshold = {},
138 DiscreteValues* fixedValues = nullptr) const;
139
148 DiscreteConditional joint() const;
149
153
155 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
156 const DiscreteFactor::Names& names = {}) const;
157
159 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
160 const DiscreteFactor::Names& names = {}) const;
161
165
166 using Base::error; // Expose error(const HybridValues&) method..
167 using Base::evaluate; // Expose evaluate(const HybridValues&) method..
168 using Base::logProbability; // Expose logProbability(const HybridValues&)
169
171
172 private:
173#if GTSAM_ENABLE_BOOST_SERIALIZATION
175 friend class boost::serialization::access;
176 template<class ARCHIVE>
177 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
178 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
179 }
180#endif
181 };
182
183// traits
184template<> struct traits<DiscreteBayesNet> : public Testable<DiscreteBayesNet> {};
185
186} // \ namespace gtsam
187
Factor Graph Base Class.
Bayes network.
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:38
Global functions in a separate testing namespace.
Definition chartTesting.h:28
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition DiscreteValues.cpp:160
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:155
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition Group.h:37
Template to create a binary predicate.
Definition Testable.h:112
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:152
A Bayes net made from discrete conditional distributions.
Definition DiscreteBayesNet.h:38
DiscreteBayesNet(ITERATOR firstConditional, ITERATOR lastConditional)
Construct from iterator over conditionals.
Definition DiscreteBayesNet.h:54
DiscreteBayesNet(const FactorGraph< DERIVEDCONDITIONAL > &graph)
Implicit copy/downcast constructor to override explicit template container constructor.
Definition DiscreteBayesNet.h:65
DiscreteBayesNet(const CONTAINER &conditionals)
Construct from container of factors (shared_ptr or plain objects).
Definition DiscreteBayesNet.h:59
DiscreteBayesNet()
Construct empty Bayes net.
Definition DiscreteBayesNet.h:50
void add(Args &&... args)
Add a DiscreteCondtional.
Definition DiscreteBayesNet.h:91
void add(const DiscreteKey &key, const std::string &spec)
Add a DiscreteDistribution using a table or a string.
Definition DiscreteBayesNet.h:85
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
A map from keys to values.
Definition DiscreteValues.h:34
BayesNet()
Definition BayesNet.h:48
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition FactorGraph.h:58
IsDerived< DERIVEDFACTOR > emplace_shared(Args &&... args)
Emplace a shared pointer to factor of given type.
Definition FactorGraph.h:153
The Factor::error simply extracts the.