gtsam/gtsam/inference/BayesNet-inst.h

85 lines
2.6 KiB
C
Raw Normal View History

/* ----------------------------------------------------------------------------
* 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
* -------------------------------------------------------------------------- */
/**
2022-01-27 07:46:06 +08:00
* @file BayesNet.h
* @brief Bayes network
* @author Frank Dellaert
* @author Richard Roberts
*/
#pragma once
#include <gtsam/inference/BayesNet.h>
2022-01-27 07:46:06 +08:00
#include <gtsam/inference/FactorGraph-inst.h>
2013-06-06 23:36:51 +08:00
2016-05-23 05:11:42 +08:00
#include <boost/range/adaptor/reversed.hpp>
2013-06-06 23:36:51 +08:00
#include <fstream>
#include <string>
namespace gtsam {
/* ************************************************************************* */
template <class CONDITIONAL>
2022-01-27 07:46:06 +08:00
void BayesNet<CONDITIONAL>::print(const std::string& s,
const KeyFormatter& formatter) const {
Base::print(s, formatter);
}
/* ************************************************************************* */
template <class CONDITIONAL>
2021-12-19 12:48:23 +08:00
void BayesNet<CONDITIONAL>::dot(std::ostream& os,
2022-01-27 07:46:06 +08:00
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";
for (auto conditional : boost::adaptors::reverse(*this)) {
2021-12-19 23:20:05 +08:00
auto frontals = conditional->frontals();
2021-12-19 12:48:23 +08:00
const Key me = frontals.front();
auto parents = conditional->parents();
for (const Key& p : parents)
2022-01-27 07:46:06 +08:00
os << " var" << keyFormatter(p) << "->var" << keyFormatter(me) << "\n";
}
2021-12-19 12:48:23 +08:00
os << "}";
std::flush(os);
}
/* ************************************************************************* */
template <class CONDITIONAL>
2022-01-27 07:46:06 +08:00
std::string BayesNet<CONDITIONAL>::dot(const KeyFormatter& keyFormatter,
const DotWriter& writer) const {
2021-12-19 12:48:23 +08:00
std::stringstream ss;
2022-01-27 07:46:06 +08:00
dot(ss, keyFormatter, writer);
2021-12-19 12:48:23 +08:00
return ss.str();
}
/* ************************************************************************* */
template <class CONDITIONAL>
void BayesNet<CONDITIONAL>::saveGraph(const std::string& filename,
2022-01-27 07:46:06 +08:00
const KeyFormatter& keyFormatter,
const DotWriter& writer) const {
2021-12-19 12:48:23 +08:00
std::ofstream of(filename.c_str());
2022-01-27 07:46:06 +08:00
dot(of, keyFormatter, writer);
of.close();
}
2021-12-19 12:48:23 +08:00
/* ************************************************************************* */
} // namespace gtsam