gtsam 4.2.0
gtsam
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
19#pragma once
20
25
26#include <boost/shared_ptr.hpp>
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;
43 typedef boost::shared_ptr<This> shared_ptr;
44 typedef boost::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 virtual ~DiscreteBayesNet() {}
70
72
75
77 bool equals(const This& bn, double tol = 1e-9) const;
78
80
83
84 // Add inherited versions of add.
85 using Base::add;
86
88 void add(const DiscreteKey& key, const std::string& spec) {
89 emplace_shared<DiscreteDistribution>(key, spec);
90 }
91
93 template <typename... Args>
94 void add(Args&&... args) {
95 emplace_shared<DiscreteConditional>(std::forward<Args>(args)...);
96 }
97
98 //** evaluate for given DiscreteValues */
99 double evaluate(const DiscreteValues & values) const;
100
101 //** (Preferred) sugar for the above for given DiscreteValues */
102 double operator()(const DiscreteValues & values) const {
103 return evaluate(values);
104 }
105
106 //** log(evaluate(values)) for given DiscreteValues */
107 double logProbability(const DiscreteValues & values) const;
108
118 DiscreteValues sample() const;
119
128 DiscreteValues sample(DiscreteValues given) const;
129
133
135 std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
136 const DiscreteFactor::Names& names = {}) const;
137
139 std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
140 const DiscreteFactor::Names& names = {}) const;
141
145
146 using Base::error; // Expose error(const HybridValues&) method..
147 using Base::evaluate; // Expose evaluate(const HybridValues&) method..
148 using Base::logProbability; // Expose logProbability(const HybridValues&)
149
151
152#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42
155
156 DiscreteValues GTSAM_DEPRECATED optimize() const;
157 DiscreteValues GTSAM_DEPRECATED optimize(DiscreteValues given) const;
159#endif
160
161 private:
163 friend class boost::serialization::access;
164 template<class ARCHIVE>
165 void serialize(ARCHIVE & ar, const unsigned int /*version*/) {
166 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base);
167 }
168 };
169
170// traits
171template<> struct traits<DiscreteBayesNet> : public Testable<DiscreteBayesNet> {};
172
173} // \ namespace gtsam
174
Factor Graph Base Class.
Bayes network.
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition: DiscreteKey.h:36
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:134
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:129
std::string serialize(const T &input)
serializes to a string
Definition: serialization.h:113
Point3 optimize(const NonlinearFactorGraph &graph, const Values &values, Key landmarkKey)
Optimize for triangulation.
Definition: triangulation.cpp:155
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
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition: concepts.h:30
Template to create a binary predicate.
Definition: Testable.h:111
A helper that implements the traits interface for GTSAM types.
Definition: Testable.h:151
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
virtual ~DiscreteBayesNet()
Destructor.
Definition: DiscreteBayesNet.h:69
void add(Args &&... args)
Add a DiscreteCondtional.
Definition: DiscreteBayesNet.h:94
void add(const DiscreteKey &key, const std::string &spec)
Add a DiscreteDistribution using a table or a string.
Definition: DiscreteBayesNet.h:88
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition: DiscreteConditional.h:40
DiscreteValues::Names Names
Translation table from values to strings.
Definition: DiscreteFactor.h:106
A map from keys to values.
Definition: DiscreteValues.h:34
A BayesNet is a tree of conditionals, stored in elimination order.
Definition: BayesNet.h:35
A factor graph is a bipartite graph with factor nodes connected to variable nodes.
Definition: FactorGraph.h:97
The Factor::error simply extracts the.