Go to the documentation of this file.
37 using std::stringstream;
42 template class GTSAM_EXPORT
46 DiscreteConditional::DiscreteConditional(
const size_t nrFrontals,
53 const ADT& potentials)
68 keys_.insert(
keys_.end(), orderedKeys.begin(), orderedKeys.end());
73 :
BaseFactor(signature.discreteKeys(), signature.cpt()),
80 std::set<Key> newFrontals;
82 for (
auto&&
key :
other.frontals()) newFrontals.insert(
key);
86 throw std::invalid_argument(
87 "DiscreteConditional::operator* called with overlapping frontal keys.");
116 throw std::invalid_argument(
117 "DiscreteConditional::marginal: single argument version only valid for "
118 "fully specified joint distributions (i.e., no parents).");
172 }
catch (std::out_of_range&) {
174 given.
print(
"parentsValues: ");
176 "DiscreteConditional::choose: parent value missing");
195 if (given.count(
j) == 0) {
199 return std::make_shared<DiscreteConditional>(
nrFrontals(), dKeys, adt);
211 value = frontalValues.at(
j);
213 }
catch (exception&) {
214 frontalValues.
print(
"frontalValues: ");
215 throw runtime_error(
"DiscreteConditional::choose: frontal value missing");
224 return std::make_shared<DecisionTreeFactor>(
discreteKeys, adt);
229 size_t frontal)
const {
231 throw std::invalid_argument(
232 "Single value likelihood can only be invoked on single-variable "
252 double pValueS = (*this)(
values);
254 if (pValueS > maxP) {
266 throw std::invalid_argument(
267 "DiscreteConditional::sampleInPlace can only be called on single "
268 "variable conditionals");
273 throw std::invalid_argument(
274 "DiscreteConditional::sampleInPlace: values already contains j");
277 (*values)[
j] = sampled;
282 static mt19937
rng(2);
289 throw std::invalid_argument(
290 "DiscreteConditional::sample can only be called on single variable "
295 vector<double>
p(nj);
304 std::discrete_distribution<size_t> distribution(
p.begin(),
p.end());
305 return distribution(
rng);
311 throw std::invalid_argument(
312 "Single value sample() can only be invoked on single-parent "
322 throw std::invalid_argument(
323 "sample() can only be invoked on no-parent prior");
330 vector<pair<Key, size_t>> pairs;
332 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
338 vector<pair<Key, size_t>> pairs;
341 vector<pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
353 if (!first) *
ss <<
",";
354 *
ss << keyFormatter(
key);
361 if (!first) *
ss <<
",";
362 *
ss << keyFormatter(parent);
375 ss <<
"*\n" << std::endl;
385 ss <<
"*" << keyFormatter(parent) <<
"*|";
391 size_t index =
a.at(*it);
410 size_t index =
a.at(*it);
415 count = (count + 1) %
n;
416 if (count == 0)
ss <<
"\n";
425 ss <<
"<div>\n<p> <i>";
435 ss <<
"<table class='DiscreteConditional'>\n <thead>\n";
440 ss <<
"<th><i>" << keyFormatter(parent) <<
"</i></th>";
446 size_t index =
a.at(*it);
454 ss <<
" </thead>\n <tbody>\n";
462 size_t index =
a.at(*it);
467 count = (count + 1) %
n;
468 if (count == 0)
ss <<
"</tr>\n";
472 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
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 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.
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
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
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 Sun Dec 22 2024 04:11:28