Loading...
Searching...
No Matches
BayesNet-inst.h
/* ----------------------------------------------------------------------------
* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)
* See LICENSE for the license information
* -------------------------------------------------------------------------- */
#pragma once
#include <gtsam/inference/BayesNet.h>
#include <gtsam/inference/FactorGraph-inst.h>
#include <boost/range/adaptor/reversed.hpp>
#include <fstream>
#include <string>
namespace gtsam {
/* ************************************************************************* */
template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::print(const std::string& s,
const KeyFormatter& formatter) const {
std::cout << (s.empty() ? "" : s + " ") << std::endl;
std::cout << "size: " << this->size() << std::endl;
for (size_t i = 0; i < this->size(); i++) {
const auto& conditional = this->at(i);
std::stringstream ss;
ss << "conditional " << i << ": ";
if (conditional) conditional->print(ss.str(), formatter);
}
}
/* ************************************************************************* */
template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::dot(std::ostream& os,
const KeyFormatter& keyFormatter,
const DotWriter& writer) const {
writer.digraphPreamble(&os);
// Create nodes for each variable in the graph
for (Key key : this->keys()) {
auto position = writer.variablePos(key);
writer.drawVariable(key, keyFormatter, position, &os);
}
os << "\n";
// Reverse order as typically Bayes nets stored in reverse topological sort.
for (auto conditional : boost::adaptors::reverse(*this)) {
auto frontals = conditional->frontals();
const Key me = frontals.front();
auto parents = conditional->parents();
for (const Key& p : parents) {
os << " var" << p << "->var" << me << "\n";
}
}
os << "}";
std::flush(os);
}
/* ************************************************************************* */
template <class CONDITIONAL>
std::string BayesNet<CONDITIONAL>::dot(const KeyFormatter& keyFormatter,
const DotWriter& writer) const {
std::stringstream ss;
dot(ss, keyFormatter, writer);
return ss.str();
}
/* ************************************************************************* */
template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::saveGraph(const std::string& filename,
const KeyFormatter& keyFormatter,
const DotWriter& writer) const {
std::ofstream of(filename.c_str());
dot(of, keyFormatter, writer);
of.close();
}
/* ************************************************************************* */
template <class CONDITIONAL>
double BayesNet<CONDITIONAL>::logProbability(const HybridValues& x) const {
double sum = 0.;
for (const auto& gc : *this) {
if (gc) sum += gc->logProbability(x);
}
return sum;
}
/* ************************************************************************* */
template <class CONDITIONAL>
double BayesNet<CONDITIONAL>::evaluate(const HybridValues& x) const {
return exp(-logProbability(x));
}
/* ************************************************************************* */
} // namespace gtsam
BayesNet.h
Bayes network.
FactorGraph-inst.h
Factor Graph Base Class.
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
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)
Dot product.
Definition Vector.h:195
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition types.h:100
gtsam::HybridValues
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:38
gtsam::BayesNet::print
void print(const std::string &s="BayesNet", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print out graph
Definition BayesNet-inst.h:32
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:46
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:83
gtsam::FactorGraph< CONDITIONAL >::keys
KeySet keys() const
gtsam::FactorGraph< CONDITIONAL >::size
size_t size() const
Definition FactorGraph.h:326
gtsam::FactorGraph< CONDITIONAL >::at
const sharedFactor at(size_t i) const
Definition FactorGraph.h:335
gtsam::DotWriter
DotWriter is a helper class for writing graphviz .dot files.
Definition DotWriter.h:35
gtsam::DotWriter::drawVariable
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
gtsam::DotWriter::digraphPreamble
void digraphPreamble(std::ostream *os) const
Write out preamble for digraph, including size.
Definition DotWriter.cpp:36
gtsam::DotWriter::variablePos
boost::optional< Vector2 > variablePos(Key key) const
Return variable position or none.
Definition DotWriter.cpp:79