gtsam/gtsam/discrete/DecisionTree.h

351 lines
11 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
* -------------------------------------------------------------------------- */
/**
* @file DecisionTree.h
* @brief Decision Tree for use in DiscreteFactors
* @author Frank Dellaert
* @author Can Erdogan
* @date Jan 30, 2012
2012-04-16 06:35:28 +08:00
*/
#pragma once
2021-12-21 10:13:57 +08:00
#include <gtsam/base/types.h>
2012-04-16 07:12:17 +08:00
#include <gtsam/discrete/Assignment.h>
2012-04-16 06:35:28 +08:00
#include <boost/function.hpp>
#include <functional>
2012-04-16 06:35:28 +08:00
#include <iostream>
#include <map>
#include <sstream>
#include <vector>
2022-01-04 11:43:45 +08:00
#include <set>
2012-04-16 06:35:28 +08:00
namespace gtsam {
/**
2013-10-25 13:58:32 +08:00
* Decision Tree
* L = label for variables
* Y = function range (any algebra), e.g., bool, int, double
*/
template<typename L, typename Y>
2022-01-23 00:07:09 +08:00
class DecisionTree {
2012-04-16 06:35:28 +08:00
protected:
/// Default method for comparison of two objects of type Y.
static bool DefaultCompare(const Y& a, const Y& b) {
return a == b;
}
public:
2012-04-16 06:35:28 +08:00
2022-01-03 02:57:12 +08:00
using LabelFormatter = std::function<std::string(L)>;
using ValueFormatter = std::function<std::string(Y)>;
using CompareFunc = std::function<bool(const Y&, const Y&)>;
/** Handy typedefs for unary and binary function types */
using Unary = std::function<Y(const Y&)>;
using Binary = std::function<Y(const Y&, const Y&)>;
2012-04-16 06:35:28 +08:00
/** A label annotated with cardinality */
using LabelC = std::pair<L,size_t>;
2012-04-16 06:35:28 +08:00
2013-10-25 13:58:32 +08:00
/** DTs consist of Leaf and Choice nodes, both subclasses of Node */
class Leaf;
class Choice;
2012-04-16 06:35:28 +08:00
/** ------------------------ Node base class --------------------------- */
class Node {
public:
using Ptr = boost::shared_ptr<const Node>;
2012-04-16 06:35:28 +08:00
#ifdef DT_DEBUG_MEMORY
static int nrNodes;
2012-04-16 06:35:28 +08:00
#endif
// Constructor
Node() {
2012-04-16 06:35:28 +08:00
#ifdef DT_DEBUG_MEMORY
2013-10-25 13:58:32 +08:00
std::cout << ++nrNodes << " constructed " << id() << std::endl; std::cout.flush();
2012-04-16 06:35:28 +08:00
#endif
}
2012-04-16 06:35:28 +08:00
// Destructor
virtual ~Node() {
2012-04-16 06:35:28 +08:00
#ifdef DT_DEBUG_MEMORY
2013-10-25 13:58:32 +08:00
std::cout << --nrNodes << " destructed " << id() << std::endl; std::cout.flush();
2012-04-16 06:35:28 +08:00
#endif
}
2012-04-16 06:35:28 +08:00
// Unique ID for dot files
const void* id() const { return this; }
2012-04-16 06:35:28 +08:00
// everything else is virtual, no documentation here as internal
2022-01-03 22:50:46 +08:00
virtual void print(const std::string& s,
const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter) const = 0;
virtual void dot(std::ostream& os, const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter,
bool showZero) const = 0;
virtual bool sameLeaf(const Leaf& q) const = 0;
virtual bool sameLeaf(const Node& q) const = 0;
2022-01-03 02:57:12 +08:00
virtual bool equals(const Node& other, const CompareFunc& compare =
&DefaultCompare) const = 0;
virtual const Y& operator()(const Assignment<L>& x) const = 0;
virtual Ptr apply(const Unary& op) const = 0;
virtual Ptr apply_f_op_g(const Node&, const Binary&) const = 0;
virtual Ptr apply_g_op_fL(const Leaf&, const Binary&) const = 0;
virtual Ptr apply_g_op_fC(const Choice&, const Binary&) const = 0;
virtual Ptr choose(const L& label, size_t index) const = 0;
virtual bool isLeaf() const = 0;
};
/** ------------------------ Node base class --------------------------- */
2012-04-16 06:35:28 +08:00
public:
2012-04-16 06:35:28 +08:00
2013-10-25 13:58:32 +08:00
/** A function is a shared pointer to the root of a DT */
using NodePtr = typename Node::Ptr;
2012-04-16 06:35:28 +08:00
/// A DecisionTree just contains the root. TODO(dellaert): make protected.
NodePtr root_;
2012-04-16 06:35:28 +08:00
protected:
/** Internal recursive function to create from keys, cardinalities, and Y values */
template<typename It, typename ValueIt>
NodePtr create(It begin, It end, ValueIt beginY, ValueIt endY) const;
2012-04-16 06:35:28 +08:00
/**
* @brief Convert from a DecisionTree<M, X> to DecisionTree<L, Y>.
*
* @tparam M The previous label type.
2022-01-04 00:07:28 +08:00
* @tparam X The previous value type.
* @param f The node pointer to the root of the previous DecisionTree.
* @param L_of_M Functor to convert from label type M to type L.
2022-01-04 00:07:28 +08:00
* @param Y_of_X Functor to convert from value type X to type Y.
* @return NodePtr
*/
2022-01-02 11:10:54 +08:00
template <typename M, typename X>
NodePtr convertFrom(const typename DecisionTree<M, X>::NodePtr& f,
std::function<L(const M&)> L_of_M,
std::function<Y(const X&)> Y_of_X) const;
public:
2012-04-16 06:35:28 +08:00
/// @name Standard Constructors
/// @{
2012-04-16 06:35:28 +08:00
/** Default constructor (for serialization) */
DecisionTree();
/** Create a constant */
DecisionTree(const Y& y);
2012-04-16 06:35:28 +08:00
/** Create a new leaf function splitting on a variable */
DecisionTree(const L& label, const Y& y1, const Y& y2);
2012-04-16 06:35:28 +08:00
/** Allow Label+Cardinality for convenience */
DecisionTree(const LabelC& label, const Y& y1, const Y& y2);
2012-04-16 06:35:28 +08:00
2013-10-25 13:58:32 +08:00
/** Create from keys and a corresponding vector of values */
DecisionTree(const std::vector<LabelC>& labelCs, const std::vector<Y>& ys);
2012-04-16 06:35:28 +08:00
/** Create from keys and string table */
DecisionTree(const std::vector<LabelC>& labelCs, const std::string& table);
2012-04-16 06:35:28 +08:00
/** Create DecisionTree from others */
template<typename Iterator>
DecisionTree(Iterator begin, Iterator end, const L& label);
2012-04-16 06:35:28 +08:00
2013-10-25 13:58:32 +08:00
/** Create DecisionTree from two others */
DecisionTree(const L& label, //
const DecisionTree& f0, const DecisionTree& f1);
2012-04-16 06:35:28 +08:00
/**
2022-01-04 00:07:28 +08:00
* @brief Convert from a different value type.
*
2022-01-04 00:07:28 +08:00
* @tparam X The previous value type.
* @param other The DecisionTree to convert from.
2022-01-04 00:07:28 +08:00
* @param Y_of_X Functor to convert from value type X to type Y.
*/
template <typename X, typename Func>
DecisionTree(const DecisionTree<L, X>& other, Func Y_of_X);
/**
2022-01-04 00:07:28 +08:00
* @brief Convert from a different value type X to value type Y, also transate
* labels via map from type M to L.
*
* @tparam M Previous label type.
2022-01-04 00:07:28 +08:00
* @tparam X Previous value type.
* @param other The decision tree to convert.
* @param L_of_M Map from label type M to type L.
* @param Y_of_X Functor to convert from type X to type Y.
*/
template <typename M, typename X, typename Func>
DecisionTree(const DecisionTree<M, X>& other, const std::map<M, L>& map,
Func Y_of_X);
2022-01-02 11:10:54 +08:00
/// @}
/// @name Testable
/// @{
2012-04-16 06:35:28 +08:00
/**
* @brief GTSAM-style print
*
* @param s Prefix string.
* @param labelFormatter Functor to format the node label.
* @param valueFormatter Functor to format the node value.
*/
2022-01-03 22:50:46 +08:00
void print(const std::string& s, const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter) const;
2012-04-16 06:35:28 +08:00
// Testable
2022-01-03 02:57:12 +08:00
bool equals(const DecisionTree& other,
const CompareFunc& compare = &DefaultCompare) const;
2012-04-16 06:35:28 +08:00
/// @}
/// @name Standard Interface
/// @{
2012-04-16 06:35:28 +08:00
/** Make virtual */
virtual ~DecisionTree() {
}
2012-04-16 06:35:28 +08:00
/// Check if tree is empty.
bool empty() const { return !root_; }
/** equality */
bool operator==(const DecisionTree& q) const;
2012-04-16 06:35:28 +08:00
/** evaluate */
const Y& operator()(const Assignment<L>& x) const;
2012-04-16 06:35:28 +08:00
2022-01-04 11:43:45 +08:00
/**
* @brief Visit all leaves in depth-first fashion.
*
* @param f side-effect taking a value.
*
* Example:
* int sum = 0;
* auto visitor = [&](int y) { sum += y; };
* tree.visitWith(visitor);
*/
template <typename Func>
void visit(Func f) const;
/**
* @brief Visit all leaves in depth-first fashion.
*
* @param f side-effect taking an assignment and a value.
*
* Example:
* int sum = 0;
* auto visitor = [&](const Assignment<L>& choices, int y) { sum += y; };
* tree.visitWith(visitor);
*/
template <typename Func>
void visitWith(Func f) const;
/**
* @brief Fold a binary function over the tree, returning accumulator.
*
* @tparam X type for accumulator.
* @param f binary function: Y * X -> X returning an updated accumulator.
* @param x0 initial value for accumulator.
* @return X final value for accumulator.
*
* @note X is always passed by value.
2022-01-04 11:43:45 +08:00
*
* Example:
* auto add = [](const double& y, double x) { return y + x; };
* double sum = tree.fold(add, 0.0);
*/
template <typename Func, typename X>
X fold(Func f, X x0) const;
2022-01-04 11:43:45 +08:00
/** Retrieve all unique labels as a set. */
std::set<L> labels() const;
/** apply Unary operation "op" to f */
DecisionTree apply(const Unary& op) const;
2012-04-16 06:35:28 +08:00
/** apply binary operation "op" to f and g */
DecisionTree apply(const DecisionTree& g, const Binary& op) const;
2012-04-16 06:35:28 +08:00
/** create a new function where value(label)==index
* It's like "restrict" in Darwiche09book pg329, 330? */
DecisionTree choose(const L& label, size_t index) const {
NodePtr newRoot = root_->choose(label, index);
return DecisionTree(newRoot);
}
2012-04-16 06:35:28 +08:00
/** combine subtrees on key with binary operation "op" */
DecisionTree combine(const L& label, size_t cardinality, const Binary& op) const;
2012-04-16 06:35:28 +08:00
/** combine with LabelC for convenience */
DecisionTree combine(const LabelC& labelC, const Binary& op) const {
return combine(labelC.first, labelC.second, op);
}
2012-04-16 06:35:28 +08:00
/** output to graphviz format, stream version */
2022-01-03 22:50:46 +08:00
void dot(std::ostream& os, const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter, bool showZero = true) const;
2012-04-16 06:35:28 +08:00
/** output to graphviz format, open a file */
2022-01-03 22:50:46 +08:00
void dot(const std::string& name, const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter, bool showZero = true) const;
2012-04-16 06:35:28 +08:00
2021-12-18 06:12:44 +08:00
/** output to graphviz format string */
2022-01-03 22:50:46 +08:00
std::string dot(const LabelFormatter& labelFormatter,
const ValueFormatter& valueFormatter,
2022-01-03 02:57:12 +08:00
bool showZero = true) const;
2021-12-18 06:12:44 +08:00
/// @name Advanced Interface
/// @{
2012-04-16 06:35:28 +08:00
// internal use only
DecisionTree(const NodePtr& root);
2012-04-16 06:35:28 +08:00
// internal use only
template<typename Iterator> NodePtr
compose(Iterator begin, Iterator end, const L& label) const;
2012-04-16 06:35:28 +08:00
/// @}
2012-04-16 06:35:28 +08:00
}; // DecisionTree
2012-04-16 06:35:28 +08:00
/** free versions of apply */
2012-04-16 06:35:28 +08:00
/// Apply unary operator `op` to DecisionTree `f`.
template<typename L, typename Y>
DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
const typename DecisionTree<L, Y>::Unary& op) {
return f.apply(op);
}
2012-04-16 06:35:28 +08:00
/// Apply binary operator `op` to DecisionTree `f`.
template<typename L, typename Y>
DecisionTree<L, Y> apply(const DecisionTree<L, Y>& f,
const DecisionTree<L, Y>& g,
const typename DecisionTree<L, Y>::Binary& op) {
return f.apply(g, op);
}
2012-04-16 06:35:28 +08:00
2022-01-23 00:07:09 +08:00
/// unzip a DecisionTree if its leaves are `std::pair`
template<typename L, typename T1, typename T2>
std::pair<DecisionTree<L, T1>, DecisionTree<L, T2> > unzip(const DecisionTree<L, std::pair<T1, T2> > &input) {
return std::make_pair(DecisionTree<L, T1>(input, [](std::pair<T1, T2> i) { return i.first; }),
DecisionTree<L, T2>(input, [](std::pair<T1, T2> i) { return i.second; }));
}
2012-04-16 06:35:28 +08:00
} // namespace gtsam