Used a map for config

release/4.3a0
Manohar Paluri 2009-12-07 00:49:13 +00:00
parent b9e15ee789
commit 28eb550781
2 changed files with 33 additions and 31 deletions

View File

@ -57,6 +57,15 @@ namespace gtsam {
cpt_ = cpt; cpt_ = cpt;
} }
double probability( std::map<std::string,bool> config) {
int index = 0, count = 0;
BOOST_FOREACH( std::string parent, parents_)
index += pow(2,count++)*(int)(config[parent]);
if( config.find(key_)->second )
index += pow(2,count);
return cpt_[index];
}
/** print */ /** print */
void print(const std::string& s = "BinaryConditional") const { void print(const std::string& s = "BinaryConditional") const {
std::cout << s << " P(" << key_; std::cout << s << " P(" << key_;

View File

@ -7,6 +7,7 @@
// STL/C++ // STL/C++
#include <iostream> #include <iostream>
#include <sstream> #include <sstream>
#include <map>
#include <CppUnitLite/TestHarness.h> #include <CppUnitLite/TestHarness.h>
#include <boost/tuple/tuple.hpp> #include <boost/tuple/tuple.hpp>
#include <boost/foreach.hpp> #include <boost/foreach.hpp>
@ -30,44 +31,36 @@ using namespace gtsam;
/** A Bayes net made from binary conditional probability tables */ /** A Bayes net made from binary conditional probability tables */
typedef BayesNet<BinaryConditional> BinaryBayesNet; typedef BayesNet<BinaryConditional> BinaryBayesNet;
struct BinaryConfig {
bool px_;
bool py_;
BinaryConfig( bool px, bool py ):px_(px), py_(py){}
};
double probability(const BinaryBayesNet& bayesNet, const BinaryConfig& config) {
double result = 1.0;
/* TODO: using config multiply the probabilities */
return result;
}
/* ************************************************************************* */ /* ************************************************************************* */
TEST( BinaryBayesNet, constructor ) TEST( BinaryBayesNet, constructor )
{ {
// small Bayes Net x <- y // small Bayes Net x <- y
// p(y) = 0.2 // p(y) = 0.2
// p(x|y=0) = 0.3 // p(x|y=0) = 0.3
// p(x|y=1) = 0.5 // p(x|y=1) = 0.6
// unary conditional for y map<string,bool> config;
boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2)); config["y"] = false;
py->print("py"); config["x"] = false;
// unary conditional for y
boost::shared_ptr<BinaryConditional> py(new BinaryConditional("y",0.2));
py->print("py");
DOUBLES_EQUAL(0.8,py->probability(config),0.01);
// single parent conditional for x // single parent conditional for x
vector<double> cpt; vector<double> cpt;
cpt += 0.7, 0.5, 0.3, 0.5 ; // array index corresponds to binary parent configuration cpt += 0.7, 0.4, 0.3, 0.6 ; // array index corresponds to binary parent configuration
boost::shared_ptr<BinaryConditional> px_y(new BinaryConditional("x","y",cpt)); boost::shared_ptr<BinaryConditional> px_y(new BinaryConditional("x","y",cpt));
px_y->print("px_y"); px_y->print("px_y");
DOUBLES_EQUAL(0.7,px_y->probability(config),0.01);
// push back conditionals in topological sort order (parents last) // push back conditionals in topological sort order (parents last)
BinaryBayesNet bbn; BinaryBayesNet bbn;
bbn.push_back(py); bbn.push_back(py);
bbn.push_back(px_y); bbn.push_back(px_y);
// Test probability of 00,01,10,11 // Test probability of 00,01,10,11
DOUBLES_EQUAL(0.56,probability(bbn,BinaryConfig(false,false)),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56; //DOUBLES_EQUAL(0.56,bbn.probability(config),0.01); // P(y=0)P(x=0|y=0) = 0.8 * 0.7 = 0.56;
} }
/* ************************************************************************* */ /* ************************************************************************* */