DiscreteConditional.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2 
3  * GTSAM Copyright 2010, Georgia Tech Research Corporation,
4  * Atlanta, Georgia 30332-0415
5  * All Rights Reserved
6  * Authors: Frank Dellaert, et al. (see THANKS for the full author list)
7 
8  * See LICENSE for the license information
9 
10  * -------------------------------------------------------------------------- */
11 
19 #pragma once
20 
24 
25 #include <memory>
26 #include <string>
27 #include <vector>
28 
29 namespace gtsam {
30 
37 class GTSAM_EXPORT DiscreteConditional
38  : public DecisionTreeFactor,
39  public Conditional<DecisionTreeFactor, DiscreteConditional> {
40  public:
41  // typedefs needed to play nice with gtsam
43  typedef std::shared_ptr<This> shared_ptr;
47 
49 
52 
55 
57  DiscreteConditional(size_t nFrontals, const DecisionTreeFactor& f);
58 
63  DiscreteConditional(size_t nFrontals, const DiscreteKeys& keys,
64  const ADT& potentials);
65 
67  explicit DiscreteConditional(const Signature& signature);
68 
77  const Signature::Table& table)
78  : DiscreteConditional(Signature(key, parents, table)) {}
79 
88  const std::vector<double>& table)
89  : DiscreteConditional(1, DiscreteKeys{key} & parents,
90  ADT(DiscreteKeys{key} & parents, table)) {}
91 
102  const std::string& spec)
103  : DiscreteConditional(Signature(key, parents, spec)) {}
104 
106  DiscreteConditional(const DiscreteKey& key, const std::string& spec)
107  : DiscreteConditional(Signature(key, {}, spec)) {}
108 
113  DiscreteConditional(const DecisionTreeFactor& joint,
114  const DecisionTreeFactor& marginal);
115 
121  DiscreteConditional(const DecisionTreeFactor& joint,
122  const DecisionTreeFactor& marginal,
123  const Ordering& orderedKeys);
124 
140  DiscreteConditional operator*(const DiscreteConditional& other) const;
141 
143  DiscreteConditional marginal(Key key) const;
144 
148 
150  void print(
151  const std::string& s = "Discrete Conditional: ",
152  const KeyFormatter& formatter = DefaultKeyFormatter) const override;
153 
155  bool equals(const DiscreteFactor& other, double tol = 1e-9) const override;
156 
160 
162  double logProbability(const DiscreteValues& x) const { return -error(x); }
163 
166  const std::string& s = "Discrete Conditional: ",
167  const KeyFormatter& formatter = DefaultKeyFormatter) const {
168  static_cast<const BaseConditional*>(this)->print(s, formatter);
169  }
170 
172  double evaluate(const DiscreteValues& values) const {
173  return ADT::operator()(values);
174  }
175 
177  using DecisionTreeFactor::operator();
178 
192  shared_ptr choose(const DiscreteValues& given) const;
193 
196  const DiscreteValues& frontalValues) const;
197 
199  DecisionTreeFactor::shared_ptr likelihood(size_t frontal) const;
200 
206  size_t sample(const DiscreteValues& parentsValues) const;
207 
209  size_t sample(size_t parent_value) const;
210 
212  size_t sample() const;
213 
219  size_t argmax(const DiscreteValues& parentsValues = DiscreteValues()) const;
220 
224 
226  void sampleInPlace(DiscreteValues* parentsValues) const;
227 
229  std::vector<DiscreteValues> frontalAssignments() const;
230 
232  std::vector<DiscreteValues> allAssignments() const;
233 
237 
239  std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
240  const Names& names = {}) const override;
241 
243  std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
244  const Names& names = {}) const override;
245 
249 
254  double evaluate(const HybridValues& x) const override;
255 
256  using BaseConditional::operator();
257 
262  double logProbability(const HybridValues& x) const override {
263  return -error(x);
264  }
265 
272  double negLogConstant() const override;
273 
275 
276  protected:
279  bool forceComplete) const;
280 
281  private:
282 #ifdef GTSAM_ENABLE_BOOST_SERIALIZATION
283 
284  friend class boost::serialization::access;
285  template <class Archive>
286  void serialize(Archive& ar, const unsigned int /*version*/) {
287  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseFactor);
288  ar& BOOST_SERIALIZATION_BASE_OBJECT_NVP(BaseConditional);
289  }
290 #endif
291 };
292 // DiscreteConditional
293 
294 // traits
295 template <>
296 struct traits<DiscreteConditional> : public Testable<DiscreteConditional> {};
297 
298 } // namespace gtsam
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const std::vector< double > &table)
Definition: DiscreteConditional.h:87
gtsam::DiscreteConditional::logProbability
double logProbability(const DiscreteValues &x) const
Log-probability is just -error(x).
Definition: DiscreteConditional.h:162
gtsam::Signature::Table
std::vector< Row > Table
Definition: Signature.h:60
gtsam::markdown
string markdown(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of markdown.
Definition: DiscreteValues.cpp:130
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
gtsam::HybridValues
Definition: HybridValues.h:37
s
RealScalar s
Definition: level1_cplx_impl.h:126
e
Array< double, 1, 3 > e(1./3., 0.5, 2.)
keys
const KeyVector keys
Definition: testRegularImplicitSchurFactor.cpp:40
x
set noclip points set clip one set noclip two set bar set border lt lw set xdata set ydata set zdata set x2data set y2data set boxwidth set dummy x
Definition: gnuplot_common_settings.hh:12
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const Signature::Table &table)
Definition: DiscreteConditional.h:76
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::DecisionTreeFactor::error
double error(const DiscreteValues &values) const override
Calculate error for DiscreteValues x, is -log(probability).
Definition: DecisionTreeFactor.cpp:56
different_sigmas::values
HybridValues values
Definition: testHybridBayesNet.cpp:245
gtsam::DiscreteKeys
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41
HybridValues
gtsam::DecisionTreeFactor::shared_ptr
std::shared_ptr< DecisionTreeFactor > shared_ptr
Definition: DecisionTreeFactor.h:50
DiscreteFactor
Discrete values for.
gtsam::DefaultKeyFormatter
KeyFormatter DefaultKeyFormatter
Assign default key formatter.
Definition: Key.cpp:30
gtsam::AlgebraicDecisionTree< Key >
gtsam::operator*
Point2 operator*(double s, const Point2 &p)
multiply with scalar
Definition: Point2.h:52
gtsam::DiscreteConditional::BaseConditional
Conditional< BaseFactor, This > BaseConditional
Typedef to our conditional base class.
Definition: DiscreteConditional.h:46
gtsam::print
void print(const Matrix &A, const string &s, ostream &stream)
Definition: Matrix.cpp:155
ADT
AlgebraicDecisionTree< Key > ADT
Definition: testAlgebraicDecisionTree.cpp:32
table
ArrayXXf table(10, 4)
operator()
internal::enable_if< internal::valid_indexed_view_overload< RowIndices, ColIndices >::value &&internal::traits< typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::ReturnAsIndexedView, typename EIGEN_INDEXED_VIEW_METHOD_TYPE< RowIndices, ColIndices >::type >::type operator()(const RowIndices &rowIndices, const ColIndices &colIndices) EIGEN_INDEXED_VIEW_METHOD_CONST
Definition: IndexedViewMethods.h:73
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
Signature.h
signatures for conditional densities
gtsam::DiscreteConditional::This
DiscreteConditional This
Typedef to this class.
Definition: DiscreteConditional.h:42
gtsam::Conditional
Definition: Conditional.h:63
key
const gtsam::Symbol key('X', 0)
gtsam::DiscreteConditional::shared_ptr
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Definition: DiscreteConditional.h:43
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
process_shonan_timing_results.names
dictionary names
Definition: process_shonan_timing_results.py:175
gtsam::DiscreteConditional
Definition: DiscreteConditional.h:37
gtsam::DiscreteConditional::evaluate
double evaluate(const DiscreteValues &values) const
Evaluate, just look up in AlgebraicDecisionTree.
Definition: DiscreteConditional.h:172
gtsam::DiscreteConditional::logProbability
double logProbability(const HybridValues &x) const override
< HybridValues version
Definition: DiscreteConditional.h:262
gtsam
traits
Definition: SFMdata.h:40
gtsam::Testable
Definition: Testable.h:152
gtsam::DiscreteConditional::printSignature
void printSignature(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const
print index signature only
Definition: DiscreteConditional.h:165
error
static double error
Definition: testRot3.cpp:37
gtsam::traits
Definition: Group.h:36
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::DiscreteKey
std::pair< Key, size_t > DiscreteKey
Definition: DiscreteKey.h:38
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const std::string &spec)
No-parent specialization; can also use DiscreteDistribution.
Definition: DiscreteConditional.h:106
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional()
Default constructor needed for serialization.
Definition: DiscreteConditional.h:54
gtsam::tol
const G double tol
Definition: Group.h:79
gtsam::html
string html(const DiscreteValues &values, const KeyFormatter &keyFormatter, const DiscreteValues::Names &names)
Free version of html.
Definition: DiscreteValues.cpp:135
gtsam::DiscreteConditional::BaseFactor
DecisionTreeFactor BaseFactor
Typedef to our factor base class.
Definition: DiscreteConditional.h:44
gtsam::Key
std::uint64_t Key
Integer nonlinear key type.
Definition: types.h:97
gtsam::DiscreteConditional::DiscreteConditional
DiscreteConditional(const DiscreteKey &key, const DiscreteKeys &parents, const std::string &spec)
Definition: DiscreteConditional.h:101
DecisionTreeFactor.h
choose
static const T & choose(int layout, const T &col, const T &row)
Definition: cxx11_tensor_block_access.cpp:27
gtsam::Signature
Definition: Signature.h:54
pybind_wrapper_test_script.other
other
Definition: pybind_wrapper_test_script.py:42
Conditional-inst.h


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:32:26