Skip to content

Commit 4dafcc5

Browse files
authored
Merge pull request #1013 from borglab/feature/remove_potentials
Remove Potentials
2 parents 0683dfa + 79cb4d0 commit 4dafcc5

File tree

10 files changed

+57
-219
lines changed

10 files changed

+57
-219
lines changed

gtsam/discrete/AlgebraicDecisionTree.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,5 +179,6 @@ namespace gtsam {
179179
};
180180
// AlgebraicDecisionTree
181181

182+
template<typename T> struct traits<AlgebraicDecisionTree<T>> : public Testable<AlgebraicDecisionTree<T>> {};
182183
}
183184
// namespace gtsam

gtsam/discrete/DecisionTreeFactor.cpp

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ namespace gtsam {
3434
/* ******************************************************************************** */
3535
DecisionTreeFactor::DecisionTreeFactor(const DiscreteKeys& keys,
3636
const ADT& potentials) :
37-
DiscreteFactor(keys.indices()), Potentials(keys, potentials) {
37+
DiscreteFactor(keys.indices()), ADT(potentials),
38+
cardinalities_(keys.cardinalities()) {
3839
}
3940

4041
/* *************************************************************************/
4142
DecisionTreeFactor::DecisionTreeFactor(const DiscreteConditional& c) :
42-
DiscreteFactor(c.keys()), Potentials(c) {
43+
DiscreteFactor(c.keys()), AlgebraicDecisionTree<Key>(c), cardinalities_(c.cardinalities_) {
4344
}
4445

4546
/* ************************************************************************* */
@@ -48,16 +49,24 @@ namespace gtsam {
4849
return false;
4950
}
5051
else {
51-
const DecisionTreeFactor& f(static_cast<const DecisionTreeFactor&>(other));
52-
return Potentials::equals(f, tol);
52+
const auto& f(static_cast<const DecisionTreeFactor&>(other));
53+
return ADT::equals(f, tol);
5354
}
5455
}
5556

57+
/* ************************************************************************* */
58+
double DecisionTreeFactor::safe_div(const double &a, const double &b) {
59+
// The use for safe_div is when we divide the product factor by the sum
60+
// factor. If the product or sum is zero, we accord zero probability to the
61+
// event.
62+
return (a == 0 || b == 0) ? 0 : (a / b);
63+
}
64+
5665
/* ************************************************************************* */
5766
void DecisionTreeFactor::print(const string& s,
5867
const KeyFormatter& formatter) const {
5968
cout << s;
60-
Potentials::print("Potentials:",formatter);
69+
ADT::print("Potentials:",formatter);
6170
}
6271

6372
/* ************************************************************************* */
@@ -162,20 +171,20 @@ namespace gtsam {
162171
void DecisionTreeFactor::dot(std::ostream& os,
163172
const KeyFormatter& keyFormatter,
164173
bool showZero) const {
165-
Potentials::dot(os, keyFormatter, valueFormatter, showZero);
174+
ADT::dot(os, keyFormatter, valueFormatter, showZero);
166175
}
167176

168177
/** output to graphviz format, open a file */
169178
void DecisionTreeFactor::dot(const std::string& name,
170179
const KeyFormatter& keyFormatter,
171180
bool showZero) const {
172-
Potentials::dot(name, keyFormatter, valueFormatter, showZero);
181+
ADT::dot(name, keyFormatter, valueFormatter, showZero);
173182
}
174183

175184
/** output to graphviz format string */
176185
std::string DecisionTreeFactor::dot(const KeyFormatter& keyFormatter,
177186
bool showZero) const {
178-
return Potentials::dot(keyFormatter, valueFormatter, showZero);
187+
return ADT::dot(keyFormatter, valueFormatter, showZero);
179188
}
180189

181190
/* ************************************************************************* */
@@ -209,5 +218,15 @@ namespace gtsam {
209218
return ss.str();
210219
}
211220

221+
DecisionTreeFactor::DecisionTreeFactor(const DiscreteKeys &keys, const vector<double> &table) :
222+
DiscreteFactor(keys.indices()), AlgebraicDecisionTree<Key>(keys, table),
223+
cardinalities_(keys.cardinalities()) {
224+
}
225+
226+
DecisionTreeFactor::DecisionTreeFactor(const DiscreteKeys &keys, const string &table) :
227+
DiscreteFactor(keys.indices()), AlgebraicDecisionTree<Key>(keys, table),
228+
cardinalities_(keys.cardinalities()) {
229+
}
230+
212231
/* ************************************************************************* */
213232
} // namespace gtsam

gtsam/discrete/DecisionTreeFactor.h

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
#pragma once
2020

2121
#include <gtsam/discrete/DiscreteFactor.h>
22-
#include <gtsam/discrete/Potentials.h>
22+
#include <gtsam/discrete/DiscreteKey.h>
23+
#include <gtsam/discrete/AlgebraicDecisionTree.h>
2324
#include <gtsam/inference/Ordering.h>
2425

2526
#include <boost/shared_ptr.hpp>
@@ -35,14 +36,18 @@ namespace gtsam {
3536
/**
3637
* A discrete probabilistic factor
3738
*/
38-
class GTSAM_EXPORT DecisionTreeFactor: public DiscreteFactor, public Potentials {
39+
class GTSAM_EXPORT DecisionTreeFactor: public DiscreteFactor, public AlgebraicDecisionTree<Key> {
3940

4041
public:
4142

4243
// typedefs needed to play nice with gtsam
4344
typedef DecisionTreeFactor This;
4445
typedef DiscreteFactor Base; ///< Typedef to base class
4546
typedef boost::shared_ptr<DecisionTreeFactor> shared_ptr;
47+
typedef AlgebraicDecisionTree<Key> ADT;
48+
49+
protected:
50+
std::map<Key,size_t> cardinalities_;
4651

4752
public:
4853

@@ -55,11 +60,11 @@ namespace gtsam {
5560
/** Constructor from Indices, Ordering, and AlgebraicDecisionDiagram */
5661
DecisionTreeFactor(const DiscreteKeys& keys, const ADT& potentials);
5762

58-
/** Constructor from Indices and (string or doubles) */
59-
template<class SOURCE>
60-
DecisionTreeFactor(const DiscreteKeys& keys, SOURCE table) :
61-
DiscreteFactor(keys.indices()), Potentials(keys, table) {
62-
}
63+
/** Constructor from doubles */
64+
DecisionTreeFactor(const DiscreteKeys& keys, const std::vector<double>& table);
65+
66+
/** Constructor from string */
67+
DecisionTreeFactor(const DiscreteKeys& keys, const std::string& table);
6368

6469
/// Single-key specialization
6570
template <class SOURCE>
@@ -71,7 +76,7 @@ namespace gtsam {
7176
: DecisionTreeFactor(DiscreteKeys{key}, row) {}
7277

7378
/** Construct from a DiscreteConditional type */
74-
DecisionTreeFactor(const DiscreteConditional& c);
79+
explicit DecisionTreeFactor(const DiscreteConditional& c);
7580

7681
/// @}
7782
/// @name Testable
@@ -90,14 +95,18 @@ namespace gtsam {
9095

9196
/// Value is just look up in AlgebraicDecisonTree
9297
double operator()(const DiscreteValues& values) const override {
93-
return Potentials::operator()(values);
98+
return ADT::operator()(values);
9499
}
95100

96101
/// multiply two factors
97102
DecisionTreeFactor operator*(const DecisionTreeFactor& f) const override {
98103
return apply(f, ADT::Ring::mul);
99104
}
100105

106+
static double safe_div(const double& a, const double& b);
107+
108+
size_t cardinality(Key j) const { return cardinalities_.at(j);}
109+
101110
/// divide by factor f (safely)
102111
DecisionTreeFactor operator/(const DecisionTreeFactor& f) const {
103112
return apply(f, safe_div);

gtsam/discrete/DiscreteConditional.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ void DiscreteConditional::print(const string& s,
8080
}
8181
}
8282
cout << ")";
83-
Potentials::print("");
83+
ADT::print("");
8484
cout << endl;
8585
}
8686

gtsam/discrete/DiscreteConditional.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
128128

129129
/// Evaluate, just look up in AlgebraicDecisonTree
130130
double operator()(const DiscreteValues& values) const override {
131-
return Potentials::operator()(values);
131+
return ADT::operator()(values);
132132
}
133133

134134
/** Convert to a factor */

gtsam/discrete/Potentials.cpp

Lines changed: 0 additions & 96 deletions
This file was deleted.

gtsam/discrete/Potentials.h

Lines changed: 0 additions & 97 deletions
This file was deleted.

0 commit comments

Comments
 (0)