Loading...
Searching...
No Matches
DiscreteConditional.h
Go to the documentation of this file.
/* ----------------------------------------------------------------------------
* 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/Conditional-inst.h>
#include <gtsam/discrete/DecisionTreeFactor.h>
#include <gtsam/discrete/Signature.h>
#include <boost/make_shared.hpp>
#include <boost/shared_ptr.hpp>
#include <string>
#include <vector>
namespace gtsam {
class GTSAM_EXPORT DiscreteConditional
: public DecisionTreeFactor,
public Conditional<DecisionTreeFactor, DiscreteConditional> {
public:
// typedefs needed to play nice with gtsam
typedef DiscreteConditional This;
typedef boost::shared_ptr<This> shared_ptr;
typedef DecisionTreeFactor BaseFactor;
typedef Conditional<BaseFactor, This>
BaseConditional;
using Values = DiscreteValues;
DiscreteConditional() {}
DiscreteConditional(size_t nFrontals, const DecisionTreeFactor& f);
DiscreteConditional(size_t nFrontals, const DiscreteKeys& keys,
const ADT& potentials);
explicit DiscreteConditional(const Signature& signature);
DiscreteConditional(const DiscreteKey& key, const DiscreteKeys& parents,
const Signature::Table& table)
: DiscreteConditional(Signature(key, parents, table)) {}
DiscreteConditional(const DiscreteKey& key, const DiscreteKeys& parents,
const std::string& spec)
: DiscreteConditional(Signature(key, parents, spec)) {}
DiscreteConditional(const DiscreteKey& key, const std::string& spec)
: DiscreteConditional(Signature(key, {}, spec)) {}
DiscreteConditional(const DecisionTreeFactor& joint,
const DecisionTreeFactor& marginal);
DiscreteConditional(const DecisionTreeFactor& joint,
const DecisionTreeFactor& marginal,
const Ordering& orderedKeys);
DiscreteConditional operator*(const DiscreteConditional& other) const;
DiscreteConditional marginal(Key key) const;
void print(
const std::string& s = "Discrete Conditional: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const override;
bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
double logProbability(const DiscreteValues& x) const {
return -error(x);
}
void printSignature(
const std::string& s = "Discrete Conditional: ",
const KeyFormatter& formatter = DefaultKeyFormatter) const {
static_cast<const BaseConditional*>(this)->print(s, formatter);
}
double evaluate(const DiscreteValues& values) const {
return ADT::operator()(values);
}
using DecisionTreeFactor::error;
using DecisionTreeFactor::operator();
shared_ptr choose(const DiscreteValues& given) const;
DecisionTreeFactor::shared_ptr likelihood(
const DiscreteValues& frontalValues) const;
DecisionTreeFactor::shared_ptr likelihood(size_t frontal) const;
size_t sample(const DiscreteValues& parentsValues) const;
size_t sample(size_t parent_value) const;
size_t sample() const;
size_t argmax() const;
void sampleInPlace(DiscreteValues* parentsValues) const;
std::vector<DiscreteValues> frontalAssignments() const;
std::vector<DiscreteValues> allAssignments() const;
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;
std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;
double evaluate(const HybridValues& x) const override;
using BaseConditional::operator();
double logProbability(const HybridValues& x) const override {
return -error(x);
}
double logNormalizationConstant() const override { return 0.0; }
#ifdef GTSAM_ALLOW_DEPRECATED_SINCE_V42
size_t GTSAM_DEPRECATED solve(const DiscreteValues& parentsValues) const;
void GTSAM_DEPRECATED solveInPlace(DiscreteValues* parentsValues) const;
#endif
protected:
DiscreteConditional::ADT choose(const DiscreteValues& given,
bool forceComplete) const;
private:
friend class boost::serialization::access;
template <class Archive>
void serialize(Archive& ar, const unsigned int /*version*/) {
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseFactor);
ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseConditional);
}
};
// DiscreteConditional
// traits
template <>
struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
} // namespace gtsam
DecisionTreeFactor.h
Signature.h
signatures for conditional densities
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Key type for discrete variables.
Definition DiscreteKey.h:36
gtsam
Global functions in a separate testing namespace.
Definition chartTesting.h:28
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition DiscreteValues.cpp:129
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::traits
A manifold defines a space in which there is a notion of a linear tangent space that can be centered ...
Definition concepts.h:30
gtsam::Testable
A helper that implements the traits interface for GTSAM types.
Definition Testable.h:151
gtsam::DecisionTree::operator()
const Y & operator()(const Assignment< L > &x) const
evaluate
Definition DecisionTree-inl.h:884
gtsam::DecisionTreeFactor
A discrete probabilistic factor.
Definition DecisionTreeFactor.h:45
gtsam::DecisionTreeFactor::error
double error(const DiscreteValues &values) const
Calculate error for DiscreteValues x, is -log(probability).
Definition DecisionTreeFactor.cpp:61
gtsam::DecisionTreeFactor::DecisionTreeFactor
DecisionTreeFactor()
Default constructor for I/O.
Definition DecisionTreeFactor.cpp:34
gtsam::DiscreteConditional
Discrete Conditional Density Derives from DecisionTreeFactor.
Definition DiscreteConditional.h:40
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const std::string &spec)
No-parent specialization; can also use DiscreteDistribution.
Definition DiscreteConditional.h:95
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const std::string &spec)
Construct from key, parents, and a string specifying the conditional probability table (CPT) in 00 01...
Definition DiscreteConditional.h:90
gtsam::DiscreteConditional::evaluate
double evaluate(const DiscreteValues &values) const
Evaluate, just look up in AlgebraicDecisonTree.
Definition DiscreteConditional.h:163
gtsam::DiscreteConditional::shared_ptr
boost::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition DiscreteConditional.h:44
gtsam::DiscreteConditional::Values
DiscreteValues Values
backwards compatibility
Definition DiscreteConditional.h:49
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const Signature::Table &table)
Construct from key, parents, and a Signature::Table specifying the conditional probability table (CPT...
Definition DiscreteConditional.h:77
gtsam::DiscreteConditional::print
void print(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
Definition DiscreteConditional.cpp:132
gtsam::DiscreteConditional::BaseConditional
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition DiscreteConditional.h:47
gtsam::DiscreteConditional::logProbability
double logProbability(const HybridValues &x) const override
< HybridValues version
Definition DiscreteConditional.h:253
gtsam::DiscreteConditional::This
DiscreteConditional This
Typedef to this class.
Definition DiscreteConditional.h:43
gtsam::DiscreteConditional::BaseFactor
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition DiscreteConditional.h:45
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional()
Default constructor needed for serialization.
Definition DiscreteConditional.h:55
gtsam::DiscreteConditional::error
double error(const DiscreteValues &values) const
Calculate error for DiscreteValues x, is -log(probability).
Definition DecisionTreeFactor.cpp:61
gtsam::DiscreteConditional::logProbability
double logProbability(const DiscreteValues &x) const
Log-probability is just -error(x).
Definition DiscreteConditional.h:151
gtsam::DiscreteConditional::printSignature
void printSignature(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
print index signature only
Definition DiscreteConditional.h:156
gtsam::DiscreteConditional::logNormalizationConstant
double logNormalizationConstant() const override
logNormalizationConstant K is just zero, such that logProbability(x) = log(evaluate(x)) = - error(x) ...
Definition DiscreteConditional.h:262
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition DiscreteKey.h:39
gtsam::DiscreteValues
A map from keys to values.
Definition DiscreteValues.h:34
gtsam::Signature
Signature for a discrete conditional density, used to construct conditionals.
Definition Signature.h:54
gtsam::HybridValues
HybridValues represents a collection of DiscreteValues and VectorValues.
Definition HybridValues.h:38
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::parents
Parents parents() const
Definition Conditional.h:127
gtsam::Conditional< DecisionTreeFactor, DiscreteConditional >::Conditional
Conditional()
Definition Conditional.h:85