Added chooseAsFactor method for wrapper

release/4.3a0
Frank Dellaert 2021-12-15 21:55:02 -05:00
parent 4e5530b6d5
commit 8f4b15b780
2 changed files with 27 additions and 7 deletions

View File

@ -97,25 +97,41 @@ bool DiscreteConditional::equals(const DiscreteFactor& other,
}
/* ******************************************************************************** */
Potentials::ADT DiscreteConditional::choose(const DiscreteValues& parentsValues) const {
Potentials::ADT DiscreteConditional::choose(
const DiscreteValues& parentsValues) const {
// Get the big decision tree with all the levels, and then go down the
// branches based on the value of the parent variables.
ADT pFS(*this);
Key j; size_t value;
for(Key key: parents()) {
size_t value;
for (Key j : parents()) {
try {
j = (key);
value = parentsValues.at(j);
pFS = pFS.choose(j, value);
pFS = pFS.choose(j, value); // ADT keeps getting smaller.
} catch (exception&) {
cout << "Key: " << j << " Value: " << value << endl;
parentsValues.print("parentsValues: ");
// pFS.print("pFS: ");
throw runtime_error("DiscreteConditional::choose: parent value missing");
};
}
return pFS;
}
/* ******************************************************************************** */
DecisionTreeFactor::shared_ptr DiscreteConditional::chooseAsFactor(
const DiscreteValues& parentsValues) const {
ADT pFS = choose(parentsValues);
// Convert ADT to factor.
if (nrFrontals() != 1) {
throw std::runtime_error("Expected only one frontal variable in choose.");
}
DiscreteKeys keys;
const Key frontalKey = keys_[0];
size_t frontalCardinality = this->cardinality(frontalKey);
keys.push_back(DiscreteKey(frontalKey, frontalCardinality));
return boost::make_shared<DecisionTreeFactor>(keys, pFS);
}
/* ******************************************************************************** */
void DiscreteConditional::solveInPlace(DiscreteValues* values) const {
// TODO: Abhijit asks: is this really the fastest way? He thinks it is.

View File

@ -134,6 +134,10 @@ public:
/** Restrict to given parent values, returns AlgebraicDecisionDiagram */
ADT choose(const DiscreteValues& parentsValues) const;
/** Restrict to given parent values, returns DecisionTreeFactor */
DecisionTreeFactor::shared_ptr chooseAsFactor(
const DiscreteValues& parentsValues) const;
/**
* solve a conditional
* @param parentsValues Known values of the parents