Go to the documentation of this file.
55 template class EliminateableFactorGraph<HybridGaussianFactorGraph>;
57 using std::dynamic_pointer_cast;
69 return conditional ==
other.conditional && negLogK ==
other.negLogK &&
80 const std::shared_ptr<Factor> &
f) {
82 throw std::runtime_error(
s +
" not implemented for factor type " +
91 index,
KeyVector(discrete_keys.begin(), discrete_keys.end()),
true);
95 static void printFactor(
const std::shared_ptr<Factor> &factor,
98 if (
auto hgf = dynamic_pointer_cast<HybridGaussianFactor>(factor)) {
99 if (assignment.empty()) {
100 hgf->print(
"HybridGaussianFactor:", keyFormatter);
102 hgf->operator()(assignment)
103 .first->print(
"HybridGaussianFactor, component:", keyFormatter);
105 }
else if (
auto gf = dynamic_pointer_cast<GaussianFactor>(factor)) {
106 factor->print(
"GaussianFactor:\n", keyFormatter);
108 }
else if (
auto df = dynamic_pointer_cast<DiscreteFactor>(factor)) {
109 factor->print(
"DiscreteFactor:\n", keyFormatter);
110 }
else if (
auto hc = dynamic_pointer_cast<HybridConditional>(factor)) {
111 if (
hc->isContinuous()) {
112 factor->print(
"GaussianConditional:\n", keyFormatter);
113 }
else if (
hc->isDiscrete()) {
114 factor->print(
"DiscreteConditional:\n", keyFormatter);
116 if (assignment.empty()) {
117 hc->print(
"HybridConditional:", keyFormatter);
121 ->print(
"HybridConditional, component:\n", keyFormatter);
125 factor->print(
"Unknown factor type\n", keyFormatter);
132 std::cout << (
s.empty() ?
"" :
s +
" ") << std::endl;
133 std::cout <<
"size: " <<
size() << std::endl;
137 if (factor ==
nullptr) {
138 std::cout <<
"Factor " <<
i <<
": nullptr\n";
142 std::cout <<
"Factor " <<
i <<
"\n";
153 const std::function<
bool(
const Factor * ,
155 &printCondition)
const {
156 std::cout <<
str <<
"size: " <<
size() << std::endl << std::endl;
160 if (factor ==
nullptr) {
161 std::cout <<
"Factor " <<
i <<
": nullptr\n";
164 const double errorValue = factor->error(
values);
165 if (!printCondition(factor.get(), errorValue,
i))
169 std::cout <<
"Factor " <<
i <<
", error = " << errorValue <<
"\n";
183 if (
auto orphan = dynamic_pointer_cast<OrphanWrapper>(
f)) {
185 }
else if (
auto gf = dynamic_pointer_cast<GaussianFactor>(
f)) {
187 }
else if (
auto gc = dynamic_pointer_cast<GaussianConditional>(
f)) {
189 }
else if (
auto gmf = dynamic_pointer_cast<HybridGaussianFactor>(
f)) {
191 }
else if (
auto gm = dynamic_pointer_cast<HybridGaussianConditional>(
f)) {
193 }
else if (
auto hc = dynamic_pointer_cast<HybridConditional>(
f)) {
194 if (
auto gm =
hc->asHybrid()) {
196 }
else if (
auto g =
hc->asGaussian()) {
203 }
else if (dynamic_pointer_cast<DiscreteFactor>(
f)) {
218 static std::pair<HybridConditional::shared_ptr, std::shared_ptr<Factor>>
223 if (
auto gf = dynamic_pointer_cast<GaussianFactor>(
f)) {
225 }
else if (
auto orphan = dynamic_pointer_cast<OrphanWrapper>(
f)) {
228 }
else if (
auto hc = dynamic_pointer_cast<HybridConditional>(
f)) {
229 auto gc =
hc->asGaussian();
238 return {std::make_shared<HybridConditional>(
result.first),
result.second};
252 double min_log = errors.
min();
254 errors, [&min_log](
const double x) {
return exp(-(
x - min_log)); });
255 return std::make_shared<DecisionTreeFactor>(discreteKeys, potentials);
259 static std::pair<HybridConditional::shared_ptr, std::shared_ptr<Factor>>
265 if (
auto df = dynamic_pointer_cast<DiscreteFactor>(
f)) {
267 }
else if (
auto gmf = dynamic_pointer_cast<HybridGaussianFactor>(
f)) {
270 auto calculateError = [&](
const auto &pair) ->
double {
271 auto [factor,
scalar] = pair;
273 if (!factor)
return std::numeric_limits<double>::infinity();
279 }
else if (
auto orphan = dynamic_pointer_cast<OrphanWrapper>(
f)) {
282 }
else if (
auto hc = dynamic_pointer_cast<HybridConditional>(
f)) {
283 auto dc =
hc->asDiscrete();
294 return {std::make_shared<HybridConditional>(
result.first),
result.second};
307 auto calculateError = [&](
const Result &
result) ->
double {
316 return std::numeric_limits<double>::infinity();
318 throw std::runtime_error(
"createDiscreteFactor has mixed NULLs");
337 return {
nullptr, std::numeric_limits<double>::infinity()};
339 throw std::runtime_error(
"createHybridGaussianFactors has mixed NULLs");
345 return std::make_shared<HybridGaussianFactor>(discreteSeparator, newFactors);
352 const std::set<DiscreteKey> discreteKeySet = hfg.discreteKeys();
353 return {discreteKeySet.begin(), discreteKeySet.end()};
357 std::pair<HybridConditional::shared_ptr, std::shared_ptr<Factor>>
374 bool someContinuousLeft =
false;
376 [&](
const std::pair<GaussianFactorGraph, double> &pair) ->
Result {
382 return {
nullptr, 0.0,
nullptr, 0.0};
386 auto [conditional, factor] =
390 someContinuousLeft |= !factor->empty();
393 return {conditional, conditional->negLogConstant(), factor,
scalar};
412 auto hybridGaussian =
413 std::make_shared<HybridGaussianConditional>(discreteSeparator, pairs);
415 return {std::make_shared<HybridConditional>(hybridGaussian), newFactor};
432 std::pair<HybridConditional::shared_ptr, std::shared_ptr<Factor>>
484 bool only_discrete =
true, only_continuous =
true;
485 for (
auto &&factor :
factors) {
486 if (
auto hybrid_factor = std::dynamic_pointer_cast<HybridFactor>(factor)) {
487 if (hybrid_factor->isDiscrete()) {
488 only_continuous =
false;
489 }
else if (hybrid_factor->isContinuous()) {
490 only_discrete =
false;
491 }
else if (hybrid_factor->isHybrid()) {
492 only_continuous =
false;
493 only_discrete =
false;
496 }
else if (
auto cont_factor =
497 std::dynamic_pointer_cast<GaussianFactor>(factor)) {
498 only_discrete =
false;
499 }
else if (
auto discrete_factor =
500 std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
501 only_continuous =
false;
510 }
else if (only_continuous) {
525 if (
auto hf = std::dynamic_pointer_cast<HybridFactor>(factor)) {
528 }
else if (
auto df = std::dynamic_pointer_cast<DiscreteFactor>(factor)) {
531 }
else if (
auto gf = dynamic_pointer_cast<GaussianFactor>(factor)) {
563 for (
auto &&
f : *
this) {
564 if (
auto gf = std::dynamic_pointer_cast<GaussianFactor>(
f)) {
566 }
else if (
auto gc = std::dynamic_pointer_cast<GaussianConditional>(
f)) {
568 }
else if (
auto hgf = std::dynamic_pointer_cast<HybridGaussianFactor>(
f)) {
570 }
else if (
auto hgc = dynamic_pointer_cast<HybridGaussianConditional>(
f)) {
572 }
else if (
auto hc = dynamic_pointer_cast<HybridConditional>(
f)) {
573 if (
auto gc =
hc->asGaussian())
575 else if (
auto hgc =
hc->asHybrid())
588 if (
auto discreteFactor = std::dynamic_pointer_cast<DiscreteFactor>(
f)) {
static Ordering ColamdConstrainedLast(const FACTOR_GRAPH &graph, const KeyVector &constrainLast, bool forceOrder=false)
Linear Factor Graph where all factors are Gaussians.
Conditional Gaussian Base class.
FastVector< sharedFactor > factors_
GaussianConditional::shared_ptr conditional
A hybrid conditional in the Conditional Linear Gaussian scheme.
double error(const HybridValues &values) const
const GaussianFactorGraph factors
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
static auto GetDiscreteKeys
Get the discrete keys from the HybridGaussianFactorGraph as DiscreteKeys.
Alias for DecisionTree of GaussianFactorGraphs and their scalar sums.
Contains the HessianFactor class, a general quadratic factor.
DiscreteKeys is a set of keys that can be assembled using the & operator.
negation< all_of< negation< Ts >... > > any_of
const EIGEN_DEVICE_FUNC ExpReturnType exp() const
DiscreteFactorGraph discreteFactors() const
Helper method to get all the discrete factors as a DiscreteFactorGraph.
std::shared_ptr< DecisionTreeFactor > shared_ptr
FastVector< Key > KeyVector
Define collection type once and for all - also used in wrappers.
A set of GaussianFactors, indexed by a set of discrete keys.
HybridGaussianProductFactor collectProductFactor() const
Create a decision tree of factor graphs out of this hybrid factor graph.
std::pair< std::shared_ptr< GaussianConditional >, std::shared_ptr< GaussianFactor > > EliminatePreferCholesky(const GaussianFactorGraph &factors, const Ordering &keys)
std::pair< GaussianFactor::shared_ptr, double > GaussianFactorValuePair
Alias for pair of GaussianFactor::shared_pointer and a double value.
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
static void throwRuntimeError(const std::string &s, const std::shared_ptr< Factor > &f)
static std::shared_ptr< Factor > createDiscreteFactor(const ResultTree &eliminationResults, const DiscreteKeys &discreteSeparator)
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
mxArray * scalar(mxClassID classid)
std::shared_ptr< This > shared_ptr
shared_ptr to this class
static std::pair< HybridConditional::shared_ptr, std::shared_ptr< Factor > > continuousElimination(const HybridGaussianFactorGraph &factors, const Ordering &frontalKeys)
An assignment from labels to a discrete value index (size_t)
static std::pair< HybridConditional::shared_ptr, std::shared_ptr< Factor > > discreteElimination(const HybridGaussianFactorGraph &factors, const Ordering &frontalKeys)
std::pair< std::shared_ptr< HybridConditional >, std::shared_ptr< Factor > > eliminate(const Ordering &keys) const
Eliminate the given continuous keys.
static const VectorValues kEmpty
std::pair< HybridConditional::shared_ptr, std::shared_ptr< Factor > > EliminateHybrid(const HybridGaussianFactorGraph &factors, const Ordering &keys)
Main elimination function for HybridGaussianFactorGraph.
static void printFactor(const std::shared_ptr< Factor > &factor, const DiscreteValues &assignment, const KeyFormatter &keyFormatter)
DecisionTree apply(const Unary &op) const
void g(const string &key, int i)
specialized key for discrete variables
bool operator==(const Result &other) const
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
a decision tree is a function from assignments to values.
Linearized Hybrid factor graph that uses type erasure.
GaussianFactorGraph choose(const DiscreteValues &assignment) const
Get the GaussianFactorGraph at a given discrete assignment. Note this corresponds to the Gaussian pos...
void printErrors(const HybridValues &values, const std::string &str="HybridGaussianFactorGraph: ", const KeyFormatter &keyFormatter=DefaultKeyFormatter, const std::function< bool(const Factor *, double, size_t)> &printCondition=[](const Factor *, double, size_t) { return true;}) const
Print the errors of each factor in the hybrid factor graph.
double probPrime(const HybridValues &values) const
Compute the unnormalized posterior probability for a continuous vector values given a specific assign...
IsDerived< DERIVEDFACTOR > push_back(std::shared_ptr< DERIVEDFACTOR > factor)
Add a factor directly using a shared_ptr.
static std::shared_ptr< Factor > createHybridGaussianFactor(const ResultTree &eliminationResults, const DiscreteKeys &discreteSeparator)
std::string demangle(const char *name)
Pretty print Value type name.
GaussianFactor::shared_ptr factor
std::shared_ptr< This > shared_ptr
shared_ptr to this class
AlgebraicDecisionTree< Key > errorTree(const VectorValues &continuousValues) const
Compute error for each discrete assignment, and return as a tree.
void print(const std::string &s="HybridGaussianFactorGraph", const KeyFormatter &keyFormatter=DefaultKeyFormatter) const override
Print out graph to std::cout, with optional key formatter.
const Ordering HybridOrdering(const HybridGaussianFactorGraph &graph)
Return a Colamd constrained ordering where the discrete keys are eliminated after the continuous keys...
double min() const
Find the minimum values amongst all leaves.
static DecisionTreeFactor::shared_ptr DiscreteFactorFromErrors(const DiscreteKeys &discreteKeys, const AlgebraicDecisionTree< Key > &errors)
Take negative log-values, shift them so that the minimum value is 0, and then exponentiate to create ...
AlgebraicDecisionTree< Key > discretePosterior(const VectorValues &continuousValues) const
Computer posterior P(M|X=x) when all continuous values X are given. This is efficient as this simply ...
NonlinearFactorGraph graph
gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:02:26