Revert "turns out we can merge DiscreteConditional and DiscreteLookupTable"
This reverts commit f6449c0ad8
.
release/4.3a0
parent
83eff969c5
commit
016f6f28d1
|
@ -236,10 +236,6 @@ DecisionTreeFactor::shared_ptr DiscreteConditional::likelihood(
|
||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
size_t DiscreteConditional::argmax(const DiscreteValues& parentsValues) const {
|
size_t DiscreteConditional::argmax(const DiscreteValues& parentsValues) const {
|
||||||
ADT pFS = choose(parentsValues, true); // P(F|S=parentsValues)
|
|
||||||
|
|
||||||
// Then, find the max over all remaining
|
|
||||||
// TODO(Duy): only works for one key now, seems horribly slow this way
|
|
||||||
size_t maxValue = 0;
|
size_t maxValue = 0;
|
||||||
double maxP = 0;
|
double maxP = 0;
|
||||||
DiscreteValues values = parentsValues;
|
DiscreteValues values = parentsValues;
|
||||||
|
@ -258,33 +254,6 @@ size_t DiscreteConditional::argmax(const DiscreteValues& parentsValues) const {
|
||||||
return maxValue;
|
return maxValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* ************************************************************************** */
|
|
||||||
void DiscreteConditional::argmaxInPlace(DiscreteValues* values) const {
|
|
||||||
ADT pFS = choose(*values, true); // P(F|S=parentsValues)
|
|
||||||
|
|
||||||
// Initialize
|
|
||||||
DiscreteValues mpe;
|
|
||||||
double maxP = 0;
|
|
||||||
|
|
||||||
// Get all Possible Configurations
|
|
||||||
const auto allPosbValues = frontalAssignments();
|
|
||||||
|
|
||||||
// Find the maximum
|
|
||||||
for (const auto& frontalVals : allPosbValues) {
|
|
||||||
double pValueS = pFS(frontalVals); // P(F=value|S=parentsValues)
|
|
||||||
// Update maximum solution if better
|
|
||||||
if (pValueS > maxP) {
|
|
||||||
maxP = pValueS;
|
|
||||||
mpe = frontalVals;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set values (inPlace) to maximum
|
|
||||||
for (Key j : frontals()) {
|
|
||||||
(*values)[j] = mpe[j];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
void DiscreteConditional::sampleInPlace(DiscreteValues* values) const {
|
void DiscreteConditional::sampleInPlace(DiscreteValues* values) const {
|
||||||
assert(nrFrontals() == 1);
|
assert(nrFrontals() == 1);
|
||||||
|
|
|
@ -18,9 +18,9 @@
|
||||||
|
|
||||||
#pragma once
|
#pragma once
|
||||||
|
|
||||||
|
#include <gtsam/inference/Conditional-inst.h>
|
||||||
#include <gtsam/discrete/DecisionTreeFactor.h>
|
#include <gtsam/discrete/DecisionTreeFactor.h>
|
||||||
#include <gtsam/discrete/Signature.h>
|
#include <gtsam/discrete/Signature.h>
|
||||||
#include <gtsam/inference/Conditional-inst.h>
|
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
@ -159,7 +159,9 @@ class GTSAM_EXPORT DiscreteConditional
|
||||||
/// @{
|
/// @{
|
||||||
|
|
||||||
/// Log-probability is just -error(x).
|
/// Log-probability is just -error(x).
|
||||||
double logProbability(const DiscreteValues& x) const { return -error(x); }
|
double logProbability(const DiscreteValues& x) const {
|
||||||
|
return -error(x);
|
||||||
|
}
|
||||||
|
|
||||||
/// print index signature only
|
/// print index signature only
|
||||||
void printSignature(
|
void printSignature(
|
||||||
|
@ -212,18 +214,11 @@ class GTSAM_EXPORT DiscreteConditional
|
||||||
size_t sample() const;
|
size_t sample() const;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Return assignment for single frontal variable that maximizes value.
|
* @brief Return assignment that maximizes distribution.
|
||||||
* @param parentsValues Known assignments for the parents.
|
* @return Optimal assignment (1 frontal variable).
|
||||||
* @return maximizing assignment for the frontal variable.
|
|
||||||
*/
|
*/
|
||||||
size_t argmax(const DiscreteValues& parentsValues = DiscreteValues()) const;
|
size_t argmax(const DiscreteValues& parentsValues = DiscreteValues()) const;
|
||||||
|
|
||||||
/**
|
|
||||||
* @brief Calculate assignment for frontal variables that maximizes value.
|
|
||||||
* @param (in/out) parentsValues Known assignments for the parents.
|
|
||||||
*/
|
|
||||||
void argmaxInPlace(DiscreteValues* parentsValues) const;
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
/// @name Advanced Interface
|
/// @name Advanced Interface
|
||||||
/// @{
|
/// @{
|
||||||
|
@ -249,6 +244,7 @@ class GTSAM_EXPORT DiscreteConditional
|
||||||
std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
|
||||||
const Names& names = {}) const override;
|
const Names& names = {}) const override;
|
||||||
|
|
||||||
|
|
||||||
/// @}
|
/// @}
|
||||||
/// @name HybridValues methods.
|
/// @name HybridValues methods.
|
||||||
/// @{
|
/// @{
|
||||||
|
|
|
@ -29,20 +29,97 @@ using std::vector;
|
||||||
|
|
||||||
namespace gtsam {
|
namespace gtsam {
|
||||||
|
|
||||||
|
/* ************************************************************************** */
|
||||||
|
// TODO(dellaert): copy/paste from DiscreteConditional.cpp :-(
|
||||||
|
void DiscreteLookupTable::print(const std::string& s,
|
||||||
|
const KeyFormatter& formatter) const {
|
||||||
|
using std::cout;
|
||||||
|
using std::endl;
|
||||||
|
|
||||||
|
cout << s << " g( ";
|
||||||
|
for (const_iterator it = beginFrontals(); it != endFrontals(); ++it) {
|
||||||
|
cout << formatter(*it) << " ";
|
||||||
|
}
|
||||||
|
if (nrParents()) {
|
||||||
|
cout << "; ";
|
||||||
|
for (const_iterator it = beginParents(); it != endParents(); ++it) {
|
||||||
|
cout << formatter(*it) << " ";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
cout << "):\n";
|
||||||
|
ADT::print("", formatter);
|
||||||
|
cout << endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************** */
|
||||||
|
void DiscreteLookupTable::argmaxInPlace(DiscreteValues* values) const {
|
||||||
|
ADT pFS = choose(*values, true); // P(F|S=parentsValues)
|
||||||
|
|
||||||
|
// Initialize
|
||||||
|
DiscreteValues mpe;
|
||||||
|
double maxP = 0;
|
||||||
|
|
||||||
|
// Get all Possible Configurations
|
||||||
|
const auto allPosbValues = frontalAssignments();
|
||||||
|
|
||||||
|
// Find the maximum
|
||||||
|
for (const auto& frontalVals : allPosbValues) {
|
||||||
|
double pValueS = pFS(frontalVals); // P(F=value|S=parentsValues)
|
||||||
|
// Update maximum solution if better
|
||||||
|
if (pValueS > maxP) {
|
||||||
|
maxP = pValueS;
|
||||||
|
mpe = frontalVals;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// set values (inPlace) to maximum
|
||||||
|
for (Key j : frontals()) {
|
||||||
|
(*values)[j] = mpe[j];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/* ************************************************************************** */
|
||||||
|
size_t DiscreteLookupTable::argmax(const DiscreteValues& parentsValues) const {
|
||||||
|
ADT pFS = choose(parentsValues, true); // P(F|S=parentsValues)
|
||||||
|
|
||||||
|
// Then, find the max over all remaining
|
||||||
|
// TODO(Duy): only works for one key now, seems horribly slow this way
|
||||||
|
size_t mpe = 0;
|
||||||
|
double maxP = 0;
|
||||||
|
DiscreteValues frontals;
|
||||||
|
assert(nrFrontals() == 1);
|
||||||
|
Key j = (firstFrontalKey());
|
||||||
|
for (size_t value = 0; value < cardinality(j); value++) {
|
||||||
|
frontals[j] = value;
|
||||||
|
double pValueS = pFS(frontals); // P(F=value|S=parentsValues)
|
||||||
|
// Update MPE solution if better
|
||||||
|
if (pValueS > maxP) {
|
||||||
|
maxP = pValueS;
|
||||||
|
mpe = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return mpe;
|
||||||
|
}
|
||||||
|
|
||||||
/* ************************************************************************** */
|
/* ************************************************************************** */
|
||||||
DiscreteLookupDAG DiscreteLookupDAG::FromBayesNet(
|
DiscreteLookupDAG DiscreteLookupDAG::FromBayesNet(
|
||||||
const DiscreteBayesNet& bayesNet) {
|
const DiscreteBayesNet& bayesNet) {
|
||||||
DiscreteLookupDAG dag;
|
DiscreteLookupDAG dag;
|
||||||
for (auto&& conditional : bayesNet) {
|
for (auto&& conditional : bayesNet) {
|
||||||
dag.push_back(conditional);
|
if (auto lookupTable =
|
||||||
|
std::dynamic_pointer_cast<DiscreteLookupTable>(conditional)) {
|
||||||
|
dag.push_back(lookupTable);
|
||||||
|
} else {
|
||||||
|
throw std::runtime_error(
|
||||||
|
"DiscreteFactorGraph::maxProduct: Expected look up table.");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return dag;
|
return dag;
|
||||||
}
|
}
|
||||||
|
|
||||||
DiscreteValues DiscreteLookupDAG::argmax(DiscreteValues result) const {
|
DiscreteValues DiscreteLookupDAG::argmax(DiscreteValues result) const {
|
||||||
// Argmax each node in turn in topological sort order (parents first).
|
// Argmax each node in turn in topological sort order (parents first).
|
||||||
for (auto it = std::make_reverse_iterator(end());
|
for (auto it = std::make_reverse_iterator(end()); it != std::make_reverse_iterator(begin()); ++it) {
|
||||||
it != std::make_reverse_iterator(begin()); ++it) {
|
|
||||||
// dereference to get the sharedFactor to the lookup table
|
// dereference to get the sharedFactor to the lookup table
|
||||||
(*it)->argmaxInPlace(&result);
|
(*it)->argmaxInPlace(&result);
|
||||||
}
|
}
|
||||||
|
|
|
@ -37,9 +37,41 @@ class DiscreteBayesNet;
|
||||||
* Inherits from discrete conditional for convenience, but is not normalized.
|
* Inherits from discrete conditional for convenience, but is not normalized.
|
||||||
* Is used in the max-product algorithm.
|
* Is used in the max-product algorithm.
|
||||||
*/
|
*/
|
||||||
// Typedef for backwards compatibility
|
class GTSAM_EXPORT DiscreteLookupTable : public DiscreteConditional {
|
||||||
// TODO(Varun): Remove
|
public:
|
||||||
using DiscreteLookupTable = DiscreteConditional;
|
using This = DiscreteLookupTable;
|
||||||
|
using shared_ptr = std::shared_ptr<This>;
|
||||||
|
using BaseConditional = Conditional<DecisionTreeFactor, This>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Construct a new Discrete Lookup Table object
|
||||||
|
*
|
||||||
|
* @param nFrontals number of frontal variables
|
||||||
|
* @param keys a sorted list of gtsam::Keys
|
||||||
|
* @param potentials the algebraic decision tree with lookup values
|
||||||
|
*/
|
||||||
|
DiscreteLookupTable(size_t nFrontals, const DiscreteKeys& keys,
|
||||||
|
const ADT& potentials)
|
||||||
|
: DiscreteConditional(nFrontals, keys, potentials) {}
|
||||||
|
|
||||||
|
/// GTSAM-style print
|
||||||
|
void print(
|
||||||
|
const std::string& s = "Discrete Lookup Table: ",
|
||||||
|
const KeyFormatter& formatter = DefaultKeyFormatter) const override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief return assignment for single frontal variable that maximizes value.
|
||||||
|
* @param parentsValues Known assignments for the parents.
|
||||||
|
* @return maximizing assignment for the frontal variable.
|
||||||
|
*/
|
||||||
|
size_t argmax(const DiscreteValues& parentsValues) const;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Calculate assignment for frontal variables that maximizes value.
|
||||||
|
* @param (in/out) parentsValues Known assignments for the parents.
|
||||||
|
*/
|
||||||
|
void argmaxInPlace(DiscreteValues* parentsValues) const;
|
||||||
|
};
|
||||||
|
|
||||||
/** A DAG made from lookup tables, as defined above. */
|
/** A DAG made from lookup tables, as defined above. */
|
||||||
class GTSAM_EXPORT DiscreteLookupDAG : public BayesNet<DiscreteLookupTable> {
|
class GTSAM_EXPORT DiscreteLookupDAG : public BayesNet<DiscreteLookupTable> {
|
||||||
|
|
Loading…
Reference in New Issue