35 template class FactorGraph<DiscreteFactor>;
36 template class EliminateableFactorGraph<DiscreteFactorGraph>;
48 if (factor) keys.insert(factor->begin(), factor->end());
56 for (
auto&& factor : *
this) {
57 if (
auto p = std::dynamic_pointer_cast<DecisionTreeFactor>(factor)) {
59 result.insert(result.end(), factor_keys.begin(), factor_keys.end());
70 if (factor) result = (*factor) *
result;
79 product *= (*factor)(
values);
86 std::cout << s << std::endl;
87 std::cout <<
"size: " <<
size() << std::endl;
90 ss <<
"factor " <<
i <<
": ";
115 std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr>
121 for (
auto&& factor : factors) product = (*factor) *
product;
125 auto normalization = product.max(product.size());
128 product = product / (*normalization);
137 for (
auto&&
key : frontalKeys)
138 orderedKeys.emplace_back(
key, product.cardinality(
key));
139 for (
auto&&
key : max->keys())
140 orderedKeys.emplace_back(
key, product.cardinality(
key));
144 size_t nrFrontals = frontalKeys.size();
145 auto lookup = std::make_shared<DiscreteLookupTable>(nrFrontals,
156 gttic(DiscreteFactorGraph_sumProduct);
163 gttic(DiscreteFactorGraph_sumProduct);
176 gttic(DiscreteFactorGraph_maxProduct);
183 gttic(DiscreteFactorGraph_maxProduct);
191 gttic(DiscreteFactorGraph_optimize);
198 gttic(DiscreteFactorGraph_optimize);
204 std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr>
210 for (
auto&& factor : factors) product = (*factor) *
product;
214 auto normalization = product.max(product.size());
217 product = product / (*normalization);
226 orderedKeys.insert(orderedKeys.end(), frontalKeys.begin(),
228 orderedKeys.insert(orderedKeys.end(), sum->keys().begin(),
234 std::make_shared<DiscreteConditional>(
product, *sum, orderedKeys);
237 return {conditional, sum};
245 std::stringstream
ss;
246 ss <<
"`DiscreteFactorGraph` of size " <<
size() << endl << endl;
248 ss <<
"factor " <<
i <<
":\n";
249 ss <<
factors_[
i]->markdown(keyFormatter, names) << endl;
258 std::stringstream
ss;
259 ss <<
"<div><p><tt>DiscreteFactorGraph</tt> of size " <<
size() <<
"</p>";
261 ss <<
"<p>factor " <<
i <<
":</p>";
262 ss <<
factors_[
i]->html(keyFormatter, names) << endl;
const gtsam::Symbol key('X', 0)
std::string markdown(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as markdown tables.
bool equals(const This &fg, double tol=1e-9) const
Check equality up to tolerance.
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateDiscrete(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
Main elimination function for DiscreteFactorGraph.
bool equals(const This &fg, double tol=1e-9) const
DecisionTreeFactor product() const
const GaussianFactorGraph factors
DiscreteValues optimize(OptionalOrderingType orderingType={}) const
Find the maximum probable explanation (MPE) by doing max-product.
static enum @1107 ordering
const KeyFormatter & formatter
std::string html(const KeyFormatter &keyFormatter=DefaultKeyFormatter, const DiscreteFactor::Names &names={}) const
Render as html tables.
double operator()(const DiscreteValues &values) const
DiscreteKeys discreteKeys() const
Return the DiscreteKeys in this factor graph.
std::shared_ptr< BayesNetType > eliminateSequential(OptionalOrderingType orderingType={}, const Eliminate &function=EliminationTraitsType::DefaultEliminate, OptionalVariableIndex variableIndex={}) const
Discrete Bayes Tree, the result of eliminating a DiscreteJunctionTree.
std::function< std::string(Key)> KeyFormatter
Typedef for a function to format a key, i.e. to convert it to a string.
DiscreteBayesNet sumProduct(OptionalOrderingType orderingType={}) const
Implement the sum-product algorithm.
static std::stringstream ss
DiscreteValues::Names Names
Translation table from values to strings.
std::shared_ptr< DecisionTreeFactor > shared_ptr
static DiscreteLookupDAG FromBayesNet(const DiscreteBayesNet &bayesNet)
Create from BayesNet with LookupTables.
DiscreteLookupDAG maxProduct(OptionalOrderingType orderingType={}) const
Implement the max-product algorithm.
void print(const std::string &s="DiscreteFactorGraph", const KeyFormatter &formatter=DefaultKeyFormatter) const override
print
std::shared_ptr< DiscreteFactor > sharedFactor
Shared pointer to a factor.
std::pair< DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr > EliminateForMPE(const DiscreteFactorGraph &factors, const Ordering &frontalKeys)
std::optional< Ordering::OrderingType > OptionalOrderingType
Typedef for an optional ordering type.
DiscreteValues argmax(DiscreteValues given=DiscreteValues()) const
argmax by back-substitution, optionally given certain variables.
FastVector< sharedFactor > factors_
DiscreteKeys is a set of keys that can be assembled using the & operator.