2013-06-06 23:36:43 +08:00
|
|
|
/* ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
|
|
* 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
|
|
|
|
|
*/
|
2013-06-06 23:36:43 +08:00
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
2013-07-30 07:55:40 +08:00
|
|
|
#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>
|
2022-01-27 01:03:25 +08:00
|
|
|
#include <string>
|
2013-06-06 23:36:43 +08:00
|
|
|
|
|
|
|
|
namespace gtsam {
|
|
|
|
|
|
2021-04-30 07:43:27 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
template <class CONDITIONAL>
|
2022-01-27 07:46:06 +08:00
|
|
|
void BayesNet<CONDITIONAL>::print(const std::string& s,
|
|
|
|
|
const KeyFormatter& formatter) const {
|
2021-04-30 07:43:27 +08:00
|
|
|
Base::print(s, formatter);
|
|
|
|
|
}
|
2013-06-06 23:36:43 +08:00
|
|
|
|
2021-04-30 07:43:27 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
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";
|
2021-04-30 07:43:27 +08:00
|
|
|
|
2022-01-27 01:03:25 +08:00
|
|
|
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";
|
2013-06-06 23:36:43 +08:00
|
|
|
}
|
|
|
|
|
|
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);
|
2021-04-30 07:43:27 +08:00
|
|
|
of.close();
|
2013-06-06 23:36:43 +08:00
|
|
|
}
|
2021-04-30 07:43:27 +08:00
|
|
|
|
2021-12-19 12:48:23 +08:00
|
|
|
/* ************************************************************************* */
|
|
|
|
|
|
2021-04-30 07:43:27 +08:00
|
|
|
} // namespace gtsam
|