example_sprinkler_em.cpp
This example shows how to use the EMAlg class.
#include <dai/alldai.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
using namespace dai;
int main() {
FactorGraph SprinklerNetwork;
SprinklerNetwork.ReadFromFile( "sprinkler.fg" );
PropertySet infprops;
infprops.set( "verbose", (size_t)1 );
infprops.set( "updates", string("HUGIN") );
InfAlg* inf = newInfAlg( "JTREE", SprinklerNetwork, infprops );
inf->init();
Evidence e;
ifstream estream( "sprinkler.tab" );
e.addEvidenceTabFile( estream, SprinklerNetwork );
cout << "Number of samples: " << e.nrSamples() << endl;
ifstream emstream( "sprinkler.em" );
EMAlg em(e, *inf, emstream);
while( !em.hasSatisfiedTermConditions() ) {
Real l = em.iterate();
cout << "Iteration " << em.Iterations() << " likelihood: " << l <<endl;
}
cout << endl << "True factor graph:" << endl << "##################" << endl;
cout.precision(12);
cout << SprinklerNetwork;
cout << endl << "Learned factor graph:" << endl << "#####################" << endl;
cout.precision(12);
cout << inf->fg();
delete inf;
return 0;
}