Go to the documentation of this file.
37 using std::stringstream;
42 template class GTSAM_EXPORT
46 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
48 :
BaseFactor((
f /
f.sum(nrFrontals))->toDecisionTreeFactor()),
54 const ADT& potentials)
69 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
74 :
BaseFactor(signature.discreteKeys(), signature.cpt()),
88 std::set<Key> newFrontals;
90 for (
auto&&
key :
other.frontals()) newFrontals.insert(
key);
94 throw std::invalid_argument(
95 "DiscreteConditional::operator* called with overlapping frontal keys.");
124 throw std::invalid_argument(
125 "DiscreteConditional::marginal: single argument version only valid for "
126 "fully specified joint distributions (i.e., no parents).");
180 }
catch (std::out_of_range&) {
182 given.
print(
"parentsValues: ");
184 "DiscreteConditional::choose: parent value missing");
203 if (given.count(
j) == 0) {
207 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
219 value = frontalValues.at(
j);
221 }
catch (exception&) {
222 frontalValues.
print(
"frontalValues: ");
223 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
232 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
237 size_t frontal)
const {
239 throw std::invalid_argument(
240 "Single value likelihood can only be invoked on single-variable "
260 double pValueS = (*this)(
values);
262 if (pValueS > maxP) {
274 throw std::invalid_argument(
275 "DiscreteConditional::sampleInPlace can only be called on single "
276 "variable conditionals");
281 throw std::invalid_argument(
282 "DiscreteConditional::sampleInPlace: values already contains j");
285 (*values)[
j] = sampled;
290 static mt19937
rng(2);
297 throw std::invalid_argument(
298 "DiscreteConditional::sample can only be called on single variable "
303 vector<double>
p(nj);
312 std::discrete_distribution<size_t> distribution(
p.begin(),
p.end());
313 return distribution(
rng);
319 throw std::invalid_argument(
320 "Single value sample() can only be invoked on single-parent "
330 throw std::invalid_argument(
331 "sample() can only be invoked on no-parent prior");
338 vector<pair<Key, size_t>> pairs;
340 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
346 vector<pair<Key, size_t>> pairs;
349 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
361 if (!first) *
ss <<
",";
362 *
ss << keyFormatter(
key);
369 if (!first) *
ss <<
",";
370 *
ss << keyFormatter(parent);
383 ss <<
"*\n" << std::endl;
393 ss <<
"*" << keyFormatter(parent) <<
"*|";
399 size_t index =
a.at(*it);
418 size_t index =
a.at(*it);
423 count = (count + 1) %
n;
424 if (count == 0)
ss <<
"\n";
433 ss <<
"<div>\n<p> <i>";
443 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
448 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
454 size_t index =
a.at(*it);
462 ss <<
" </thead>\n <tbody>\n";
470 size_t index =
a.at(*it);
475 count = (count + 1) %
n;
476 if (count == 0)
ss <<
"</tr>\n";
480 ss <<
" </tbody>\n</table>\n</div>";
513 if (given.count(
key) == 0) {
514 frontals.emplace_back(key, this->cardinalities_.at(key));
521 if (given.count(
key) == 0) {
522 parents.emplace_back(key, this->cardinalities_.at(key));
530 this->
keys_ = allDkeys.indices();
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,...
DecisionTreeFactor prune(size_t maxNrAssignments) const
Prune the decision tree of discrete variables.
bool equals(const DiscreteFactor &other, double tol=1e-9) const override
GTSAM-style equals.
const KeyFormatter & formatter
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
const_iterator begin() const
std::shared_ptr< DecisionTreeFactor > shared_ptr
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as html table.
static double mul(const double &a, const double &b)
double operator()(const HybridValues &x) const
Evaluate probability density, sugar.
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.
NodePtr root_
A DecisionTree just contains the root. TODO(dellaert): make protected.
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.
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
double evaluate(const HybridValues &x) 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
const_iterator end() 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.
void sampleInPlace(DiscreteValues *parentsValues) const
sample in place, stores result in partial solution
double max() const override
Find the maximum value in the factor.
KeyVector keys_
The keys involved in this factor.
std::shared_ptr< DiscreteFactor > shared_ptr
shared_ptr to this class
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 KeyVector & keys() const
Access the factor's involved variable keys.
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.
void removeDiscreteModes(const DiscreteValues &given)
Remove the discrete modes whose assignments are given to us. Only applies to discrete conditionals.
Frontals frontals() const
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const Names &names={}) const override
Render as markdown table.
virtual void prune(size_t maxNrAssignments)
Prune the conditional.
gtsam
Author(s):
autogenerated on Wed Mar 19 2025 03:01:36