gtsam 4.2.0
gtsam
Loading...
Searching...
No Matches
BayesNet-inst.h
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
23
24#include <boost/range/adaptor/reversed.hpp>
25#include <fstream>
26#include <string>
27
28namespace gtsam {
29
30/* ************************************************************************* */
31template <class CONDITIONAL>
32void BayesNet<CONDITIONAL>::print(const std::string& s,
33 const KeyFormatter& formatter) const {
34 std::cout << (s.empty() ? "" : s + " ") << std::endl;
35 std::cout << "size: " << this->size() << std::endl;
36 for (size_t i = 0; i < this->size(); i++) {
37 const auto& conditional = this->at(i);
38 std::stringstream ss;
39 ss << "conditional " << i << ": ";
40 if (conditional) conditional->print(ss.str(), formatter);
41 }
42}
43
44/* ************************************************************************* */
45template <class CONDITIONAL>
46void BayesNet<CONDITIONAL>::dot(std::ostream& os,
47 const KeyFormatter& keyFormatter,
48 const DotWriter& writer) const {
49 writer.digraphPreamble(&os);
50
51 // Create nodes for each variable in the graph
52 for (Key key : this->keys()) {
53 auto position = writer.variablePos(key);
54 writer.drawVariable(key, keyFormatter, position, &os);
55 }
56 os << "\n";
57
58 // Reverse order as typically Bayes nets stored in reverse topological sort.
59 for (auto conditional : boost::adaptors::reverse(*this)) {
60 auto frontals = conditional->frontals();
61 const Key me = frontals.front();
62 auto parents = conditional->parents();
63 for (const Key& p : parents) {
64 os << " var" << p << "->var" << me << "\n";
65 }
66 }
67
68 os << "}";
69 std::flush(os);
71
72/* ************************************************************************* */
73template <class CONDITIONAL>
74std::string BayesNet<CONDITIONAL>::dot(const KeyFormatter& keyFormatter,
75 const DotWriter& writer) const {
76 std::stringstream ss;
77 dot(ss, keyFormatter, writer);
78 return ss.str();
80
81/* ************************************************************************* */
82template <class CONDITIONAL>
83void BayesNet<CONDITIONAL>::saveGraph(const std::string& filename,
84 const KeyFormatter& keyFormatter,
85 const DotWriter& writer) const {
86 std::ofstream of(filename.c_str());
87 dot(of, keyFormatter, writer);
88 of.close();
89}
90
91/* ************************************************************************* */
92template <class CONDITIONAL>
94 double sum = 0.;
95 for (const auto& gc : *this) {
96 if (gc) sum += gc->logProbability(x);
97 }
98 return sum;
99}
100
101/* ************************************************************************* */
102template <class CONDITIONAL>
103double BayesNet<CONDITIONAL>::evaluate(const HybridValues& x) const {
104 return exp(-logProbability(x));
105}
106
107/* ************************************************************************* */
108
109} // namespace gtsam
Bayes network.
Factor Graph Base Class.
Global functions in a separate testing namespace.
Definition chartTesting.h:28
double dot(const V1 &a, const V2 &b)
Dot product.
Definition Vector.h:195
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
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
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:38
A BayesNet is a tree of conditionals, stored in elimination order.
Definition BayesNet.h:35
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print out graph
Definition BayesNet-inst.h:32
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format, stream version.
Definition BayesNet-inst.h:46
void saveGraph(const std::string &filename, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
output to file with graphviz format.
Definition BayesNet-inst.h:83
DotWriter is a helper class for writing graphviz .dot files.
Definition DotWriter.h:35
void drawVariable(Key key, const KeyFormatter &keyFormatter, const boost::optional< Vector2 > &position, std::ostream *os) const
Create a variable dot fragment.
Definition DotWriter.cpp:42
void digraphPreamble(std::ostream *os) const
Write out preamble for digraph, including size.
Definition DotWriter.cpp:36
boost::optional< Vector2 > variablePos(Key key) const
Return variable position or none.
Definition DotWriter.cpp:79
the error.