Go to the documentation of this file.
35 using std::stringstream;
40 template class GTSAM_EXPORT
44 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
51 const ADT& potentials)
66 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
71 :
BaseFactor(signature.discreteKeys(), signature.cpt()),
78 std::set<Key> newFrontals;
80 for (
auto&&
key :
other.frontals()) newFrontals.insert(
key);
84 throw std::invalid_argument(
85 "DiscreteConditional::operator* called with overlapping frontal keys.");
114 throw std::invalid_argument(
115 "DiscreteConditional::marginal: single argument version only valid for "
116 "fully specified joint distributions (i.e., no parents).");
170 }
catch (std::out_of_range&) {
172 given.
print(
"parentsValues: ");
174 "DiscreteConditional::choose: parent value missing");
193 if (given.count(
j) == 0) {
197 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
209 value = frontalValues.at(
j);
211 }
catch (exception&) {
212 frontalValues.
print(
"frontalValues: ");
213 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
222 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
227 size_t frontal)
const {
229 throw std::invalid_argument(
230 "Single value likelihood can only be invoked on single-variable "
250 double pValueS = (*this)(
values);
252 if (pValueS > maxP) {
264 throw std::invalid_argument(
265 "DiscreteConditional::sampleInPlace can only be called on single "
266 "variable conditionals");
271 throw std::invalid_argument(
272 "DiscreteConditional::sampleInPlace: values already contains j");
275 (*values)[
j] = sampled;
280 static mt19937
rng(2);
287 throw std::invalid_argument(
288 "DiscreteConditional::sample can only be called on single variable "
293 vector<double>
p(nj);
302 std::discrete_distribution<size_t> distribution(
p.begin(),
p.end());
303 return distribution(
rng);
309 throw std::invalid_argument(
310 "Single value sample() can only be invoked on single-parent "
320 throw std::invalid_argument(
321 "sample() can only be invoked on no-parent prior");
328 vector<pair<Key, size_t>> pairs;
330 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
336 vector<pair<Key, size_t>> pairs;
339 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
351 if (!first) *
ss <<
",";
352 *
ss << keyFormatter(
key);
359 if (!first) *
ss <<
",";
360 *
ss << keyFormatter(parent);
373 ss <<
"*\n" << std::endl;
383 ss <<
"*" << keyFormatter(parent) <<
"*|";
389 size_t index =
a.at(*it);
408 size_t index =
a.at(*it);
413 count = (count + 1) %
n;
414 if (count == 0)
ss <<
"\n";
423 ss <<
"<div>\n<p> <i>";
433 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
438 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
444 size_t index =
a.at(*it);
452 ss <<
" </thead>\n <tbody>\n";
460 size_t index =
a.at(*it);
465 count = (count + 1) %
n;
466 if (count == 0)
ss <<
"</tr>\n";
470 ss <<
" </tbody>\n</table>\n</div>";
size_t sample() const
Zero parent version.
void print(const std::string &s="", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const
print required by Testable.
Concept check for values that can be used in unit tests.
std::map< Key, size_t > cardinalities_
Map of Keys and their cardinalities.
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
DiscreteConditional operator*(const DiscreteConditional &other) const
Combine two conditionals, yielding a new conditional with the union of the frontal keys,...
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
GTSAM-style equals.
const KeyFormatter & formatter
double mul(const double &a, const double &b)
DiscreteKeys is a set of keys that can be assembled using the & operator.
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
DecisionTree choose(const L &label, size_t index) const
std::shared_ptr< DecisionTreeFactor > shared_ptr
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
static std::stringstream ss
DecisionTreeFactor ::const_iterator endParents() const
DiscreteConditional marginal(Key key) const
std::vector< DiscreteValues > allAssignments() const
Return all assignments for frontal and parent variables.
void print(const std::string &s="Discrete Conditional: ", const KeyFormatter &formatter=DefaultKeyFormatter) const override
GTSAM-style print.
static std::vector< DiscreteValues > CartesianProduct(const DiscreteKeys &keys)
Return a vector of DiscreteValues, one for each possible combination of values.
DecisionTreeFactor ::const_iterator endFrontals() const
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
double operator()(const DiscreteValues &values) const override
Evaluate probability distribution, sugar.
signatures for conditional densities
std::vector< DiscreteValues > frontalAssignments() const
Return all assignments for frontal variables.
static void streamSignature(const DiscreteConditional &conditional, const KeyFormatter &keyFormatter, stringstream *ss)
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
equality
double negLogConstant() const override
DecisionTreeFactor ::const_iterator beginFrontals() const
size_t argmax(const DiscreteValues &parentsValues=DiscreteValues()) const
Return assignment for single frontal variable that maximizes value.
DecisionTree apply(const Unary &op) const
DecisionTreeFactor::shared_ptr likelihood(const DiscreteValues &frontalValues) const
size_t nrFrontals() const
double sum() const
Compute sum of all values.
const gtsam::Symbol key('X', 0)
std::shared_ptr< This > shared_ptr
shared_ptr to this class
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
DecisionTreeFactor ::const_iterator beginParents() const
size_t cardinality(Key j) const
Key firstFrontalKey() const
KeyVector::const_iterator const_iterator
Const iterator over keys.
double evaluate(const DiscreteValues &values) const
Evaluate, just look up in AlgebraicDecisionTree.
void sampleInPlace(DiscreteValues *parentsValues) const
sample in place, stores result in partial solution
KeyVector keys_
The keys involved in this factor.
DiscreteValues::Names Names
Translation table from values to strings.
void print(const std::string &s="", const typename Base::LabelFormatter &labelFormatter=&DefaultFormatter) const
print method customized to value type double.
const DiscreteValues & discrete() const
Return the discrete values.
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
void product(const MatrixType &m)
DiscreteConditional()
Default constructor needed for serialization.
shared_ptr choose(const DiscreteValues &given) const
< DiscreteValues version
DiscreteKeys discreteKeys() const
Return all the discrete keys associated with this factor.
static std::string Translate(const Names &names, Key key, size_t index)
Translate an integer index value for given key to a string.
std::uint64_t Key
Integer nonlinear key type.
Frontals frontals() const
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:13