BayesNet-inst.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 
23 
24 #include <fstream>
25 #include <string>
26 
27 namespace gtsam {
28 
29 /* ************************************************************************* */
30 template <class CONDITIONAL>
31 void BayesNet<CONDITIONAL>::print(const std::string& s,
32  const KeyFormatter& formatter) const {
33  std::cout << (s.empty() ? "" : s + " ") << std::endl;
34  std::cout << "size: " << this->size() << std::endl;
35  for (size_t i = 0; i < this->size(); i++) {
36  const auto& conditional = this->at(i);
37  std::stringstream ss;
38  ss << "conditional " << i << ": ";
39  if (conditional) conditional->print(ss.str(), formatter);
40  }
41 }
42 
43 /* ************************************************************************* */
44 template <class CONDITIONAL>
45 void BayesNet<CONDITIONAL>::dot(std::ostream& os,
46  const KeyFormatter& keyFormatter,
47  const DotWriter& writer) const {
48  writer.digraphPreamble(&os);
49 
50  // Create nodes for each variable in the graph
51  for (Key key : this->keys()) {
52  auto position = writer.variablePos(key);
53  writer.drawVariable(key, keyFormatter, position, &os);
54  }
55  os << "\n";
56 
57  // Reverse order as typically Bayes nets stored in reverse topological sort.
58  for (auto it = std::make_reverse_iterator(this->end());
59  it != std::make_reverse_iterator(this->begin()); ++it) {
60  const auto& conditional = *it;
61  auto frontals = conditional->frontals();
62  const Key me = frontals.front();
63  auto parents = conditional->parents();
64  for (const Key& p : parents) {
65  os << " var" << p << "->var" << me << "\n";
66  }
67  }
68 
69  os << "}";
70  std::flush(os);
71 }
72 
73 /* ************************************************************************* */
74 template <class CONDITIONAL>
75 std::string BayesNet<CONDITIONAL>::dot(const KeyFormatter& keyFormatter,
76  const DotWriter& writer) const {
77  std::stringstream ss;
78  dot(ss, keyFormatter, writer);
79  return ss.str();
80 }
81 
82 /* ************************************************************************* */
83 template <class CONDITIONAL>
85  const KeyFormatter& keyFormatter,
86  const DotWriter& writer) const {
87  std::ofstream of(filename.c_str());
88  dot(of, keyFormatter, writer);
89  of.close();
90 }
91 
92 /* ************************************************************************* */
93 template <class CONDITIONAL>
95  double sum = 0.;
96  for (const auto& gc : *this) {
97  if (gc) sum += gc->logProbability(x);
98  }
99  return sum;
100 }
101 
102 /* ************************************************************************* */
103 template <class CONDITIONAL>
105  return exp(-logProbability(x));
106 }
107 
108 /* ************************************************************************* */
109 
110 } // namespace gtsam
gtsam::HybridValues
Definition: HybridValues.h:37
gtsam::DotWriter::drawVariable
void drawVariable(Key key, const KeyFormatter &keyFormatter, const std::optional< Vector2 > &position, std::ostream *os) const
Create a variable dot fragment.
Definition: DotWriter.cpp:42
s
RealScalar s
Definition: level1_cplx_impl.h:126
gtsam::BayesNet::print
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
Definition: BayesNet-inst.h:31
gtsam::BayesNet::evaluate
double evaluate(const HybridValues &c) const
Definition: BayesNet-inst.h:104
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
gtsam::position
Point3_ position(const NavState_ &X)
Definition: navigation/expressions.h:37
gtsam::DotWriter::digraphPreamble
void digraphPreamble(std::ostream *os) const
Write out preamble for digraph, including size.
Definition: DotWriter.cpp:36
gtsam::BayesNet::dot
void dot(std::ostream &os, const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DotWriter &writer=DotWriter()) const
Output to graphviz format, stream version.
Definition: BayesNet-inst.h:45
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
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
exp
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
Definition: ArrayCwiseUnaryOps.h:97
os
ofstream os("timeSchurFactors.csv")
FactorGraph-inst.h
Factor Graph Base Class.
gtsam::DotWriter::variablePos
std::optional< Vector2 > variablePos(Key key) const
Return variable position or none.
Definition: DotWriter.cpp:79
size
Scalar Scalar int size
Definition: benchVecAdd.cpp:17
ss
static std::stringstream ss
Definition: testBTree.cpp:31
relicense.filename
filename
Definition: relicense.py:57
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
gtsam::dot
double dot(const V1 &a, const V2 &b)
Definition: Vector.h:196
BayesNet.h
Bayes network.
key
const gtsam::Symbol key('X', 0)
gtsam::BayesNet::saveGraph
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:84
different_sigmas::gc
const auto gc
Definition: testHybridBayesNet.cpp:231
gtsam
traits
Definition: SFMdata.h:40
gtsam::BayesNet::logProbability
double logProbability(const HybridValues &x) const
Definition: BayesNet-inst.h:94
make_reverse_iterator
std::reverse_iterator< Iterator > make_reverse_iterator(Iterator i)
Definition: stl_iterators.cpp:16
p
float * p
Definition: Tutorial_Map_using.cpp:9
Eigen::placeholders::end
static const EIGEN_DEPRECATED end_t end
Definition: IndexedViewHelper.h:181
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
i
int i
Definition: BiCGSTAB_step_by_step.cpp:9
gtsam::DotWriter
DotWriter is a helper class for writing graphviz .dot files.
Definition: DotWriter.h:36


gtsam
Author(s):
autogenerated on Sun Dec 22 2024 04:11:11