gtsam/gtsam/inference/BayesNet-inst.h

94 lines
3.0 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 {
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>
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);
2022-01-28 01:53:27 +08:00
writer.drawVariable(key, keyFormatter, position, &os);
2022-01-27 07:46:06 +08:00
}
os << "\n";
2022-01-28 03:34:38 +08:00
// Reverse order as typically Bayes nets stored in reverse topological sort.
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) {
os << " var" << p << "->var" << 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