/// Set all factors interacting with var(i) to 1
virtual void makeCavity( size_t i ) = 0;
- /// Get index of variable n
- virtual size_t findVar( const Var & n ) const = 0;
-
- /// Get index of first factor involving ns
- virtual size_t findFactor( const VarSet &ns ) const = 0;
-
- /// Get number of variables
- virtual size_t nrVars() const = 0;
-
- /// Get number of factors
- virtual size_t nrFactors() const = 0;
-
- /// Get const reference to variable i
- virtual const Var & var(size_t i) const = 0;
-
- /// Get reference to variable i
- virtual Var & var(size_t i) = 0;
-
- /// Get const reference to factor I
- virtual const Factor & factor( size_t I ) const = 0;
-
- /// Get reference to factor I
- virtual Factor & factor( size_t I ) = 0;
+ /// Get reference to underlying FactorGraph
+ virtual FactorGraph &fg() = 0;
- /// Factor I has been updated
- virtual void updatedFactor( size_t I ) = 0;
+ /// Get const reference to underlying FactorGraph
+ virtual const FactorGraph &fg() const = 0;
- /// Checks whether all necessary properties have been set
- /// and casts string-valued properties to other values if necessary
- virtual bool checkProperties() = 0;
+ /// Return maximum difference between beliefs in the last pass
+ virtual double maxDiff() const = 0;
};
#include <dai/daialg.h>
#include <dai/enum.h>
#include <dai/factorgraph.h>
+#include <dai/properties.h>
+ #include <dai/exceptions.h>
namespace dai {
std::string identify() const;
Factor belief (const Var &n) const { return( _beliefs[findVar(n)] ); }
- Factor belief (const VarSet &/*ns*/) const { assert( 0 == 1 ); }
+ Factor belief (const VarSet &/*ns*/) const {
+ DAI_THROW(NOT_IMPLEMENTED);
+ return Factor();
+ }
std::vector<Factor> beliefs() const { return _beliefs; }
- Complex logZ() const { return NAN; }
+ Real logZ() const {
+ DAI_THROW(NOT_IMPLEMENTED);
+ return 0.0;
+ }
void CalcBelief (size_t i);
- const Factor & belief (size_t i) const { return _beliefs[i]; };
- const Factor & pancake (size_t i) const { return _pancakes[i]; };
- const Factor & cavitydist (size_t i) const { return _cavitydists[i]; };
+ const Factor &belief (size_t i) const { return _beliefs[i]; };
+ const Factor &pancake (size_t i) const { return _pancakes[i]; };
+ const Factor &cavitydist (size_t i) const { return _cavitydists[i]; };
- void clamp( const Var &/*n*/, size_t /*i*/ ) { assert( 0 == 1 ); }
- void undoProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); }
- void saveProbs( const VarSet &/*ns*/ ) { assert( 0 == 1 ); }
- virtual void makeCavity(const Var & /*n*/) { assert( 0 == 1 ); }
-
+ void clamp( const Var &/*n*/, size_t /*i*/ ) {
+ DAI_THROW(NOT_IMPLEMENTED);
+ }
+ void undoProbs( const VarSet &/*ns*/ ) {
+ DAI_THROW(NOT_IMPLEMENTED);
+ }
+ void saveProbs( const VarSet &/*ns*/ ) {
+ DAI_THROW(NOT_IMPLEMENTED);
+ }
+ virtual void makeCavity(const Var & /*n*/) {
+ DAI_THROW(NOT_IMPLEMENTED);
+ }
- bool checkProperties();
+ void setProperties( const PropertySet &opts );
+ PropertySet getProperties() const;
+ double maxDiff() const { return maxdiff; }
};
#include <dai/factorgraph.h>
#include <dai/daialg.h>
#include <dai/enum.h>
+#include <dai/properties.h>
+ #include <dai/exceptions.h>
namespace dai {
void solveM();
double run();
Factor belief( const Var &n ) const;
- Factor belief( const VarSet &/*ns*/ ) const { assert( 0 == 1 ); }
+ Factor belief( const VarSet &/*ns*/ ) const {
+ DAI_THROW(NOT_IMPLEMENTED);
+ return Factor();
+ }
std::vector<Factor> beliefs() const;
- Complex logZ() const { return NAN; }
+ Real logZ() const {
+ DAI_THROW(NOT_IMPLEMENTED);
+ return 0.0;
+ }
- void init() { assert( checkProperties() ); }
+ void init() {}
static const char *Name;
std::string identify() const;
double _tJ(size_t i, sub_nb A);
void sum_subs(size_t j, sub_nb A, double *sum_even, double *sum_odd);
double sign(double a) { return (a >= 0) ? 1.0 : -1.0; }
- MR* clone() const { assert( 0 == 1 ); }
+ MR* clone() const { return new MR(*this); }
- bool checkProperties();
-
+ void setProperties( const PropertySet &opts );
+ PropertySet getProperties() const;
+ double maxDiff() const { return maxdiff; }
};
#include <string>
#include <dai/alldai.h>
+#include <dai/properties.h>
+ #include <dai/exceptions.h>
namespace dai {
return new JTree (fg, opts);
#endif
#ifdef WITH_MR
- else if( name == MR::Name )
+ if( name == MR::Name )
return new MR (fg, opts);
#endif
- throw "Unknown inference algorithm";
- else
- DAI_THROW(UNKNOWN_DAI_ALGORITHM);
++ DAI_THROW(UNKNOWN_DAI_ALGORITHM);
}
const ind_t ind = index(i,_I);
for( size_t r = 0; r < prod.size(); ++r )
marg[ind[r]] += prod[r];
- marg.normalize( _normtype );
+ marg.normalize( Prob::NORMPROB );
// Store result
- if( logDomain )
+ if( props.logdomain )
newMessage(i,_I) = marg.log();
else
newMessage(i,_I) = marg;
InfAlg *cav = newInfAlg( name, *this, opts );
cav->makeCavity( i );
- if( Cavity() == CavityType::FULL )
- Bi = calcMarginal( *cav, cav->fg().delta(i), reInit() );
- else if( Cavity() == CavityType::PAIR )
- Bi = calcMarginal2ndO( *cav, cav->fg().delta(i), reInit() );
- else if( Cavity() == CavityType::PAIR2 ) {
- vector<Factor> pairbeliefs = calcPairBeliefsNew( *cav, cav->fg().delta(i), reInit() );
+ if( props.cavity == Properties::CavityType::FULL )
- Bi = calcMarginal( *cav, cav->delta(i), props.reinit );
++ Bi = calcMarginal( *cav, cav->fg().delta(i), props.reinit );
+ else if( props.cavity == Properties::CavityType::PAIR )
- Bi = calcMarginal2ndO( *cav, cav->delta(i), props.reinit );
++ Bi = calcMarginal2ndO( *cav, cav->fg().delta(i), props.reinit );
+ else if( props.cavity == Properties::CavityType::PAIR2 ) {
- vector<Factor> pairbeliefs = calcPairBeliefsNew( *cav, cav->delta(i), props.reinit );
++ vector<Factor> pairbeliefs = calcPairBeliefsNew( *cav, cav->fg().delta(i), props.reinit );
for( size_t ij = 0; ij < pairbeliefs.size(); ij++ )
Bi *= pairbeliefs[ij];
- } else if( Cavity() == CavityType::PAIRINT ) {
- Bi = calcMarginal( *cav, cav->fg().delta(i), reInit() );
+ } else if( props.cavity == Properties::CavityType::PAIRINT ) {
- Bi = calcMarginal( *cav, cav->delta(i), props.reinit );
++ Bi = calcMarginal( *cav, cav->fg().delta(i), props.reinit );
// Set interactions of order > 2 to zero
size_t N = delta(i).size();
x2x::w2logp (N, p);
// x2x::logpnorm (N, p);
x2x::logp2p (N, p);
- } else if( Cavity() == CavityType::PAIRCUM ) {
- Bi = calcMarginal( *cav, cav->fg().delta(i), reInit() );
+ } else if( props.cavity == Properties::CavityType::PAIRCUM ) {
- Bi = calcMarginal( *cav, cav->delta(i), props.reinit );
++ Bi = calcMarginal( *cav, cav->fg().delta(i), props.reinit );
// Set cumulants of order > 2 to zero
size_t N = delta(i).size();
x2x::c2m (N, p, N);
x2x::m2p (N, p);
}
- maxdiff = cav->MaxDiff();
+ maxdiff = cav->maxDiff();
delete cav;
}
- Bi.normalize( _normtype );
+ Bi.normalize( Prob::NORMPROB );
_cavitydists[i] = Bi;
return maxdiff;
os << boost::any_cast<double>(p.second);
else if( p.second.type() == typeid(bool) )
os << boost::any_cast<bool>(p.second);
- else if( p.second.type() == typeid(Properties) )
- os << boost::any_cast<Properties>(p.second);
+ else if( p.second.type() == typeid(PropertySet) )
+ os << boost::any_cast<PropertySet>(p.second);
#ifdef WITH_BP
- else if( p.second.type() == typeid(BP::UpdateType) )
- os << boost::any_cast<BP::UpdateType>(p.second);
+ else if( p.second.type() == typeid(BP::Properties::UpdateType) )
+ os << boost::any_cast<BP::Properties::UpdateType>(p.second);
#endif
#ifdef WITH_HAK
- else if( p.second.type() == typeid(HAK::ClustersType) )
- os << boost::any_cast<HAK::ClustersType>(p.second);
+ else if( p.second.type() == typeid(HAK::Properties::ClustersType) )
+ os << boost::any_cast<HAK::Properties::ClustersType>(p.second);
#endif
#ifdef WITH_JTREE
- else if( p.second.type() == typeid(JTree::UpdateType) )
- os << boost::any_cast<JTree::UpdateType>(p.second);
+ else if( p.second.type() == typeid(JTree::Properties::UpdateType) )
+ os << boost::any_cast<JTree::Properties::UpdateType>(p.second);
#endif
#ifdef WITH_MR
- else if( p.second.type() == typeid(MR::UpdateType) )
- os << boost::any_cast<MR::UpdateType>(p.second);
- else if( p.second.type() == typeid(MR::InitType) )
- os << boost::any_cast<MR::InitType>(p.second);
+ else if( p.second.type() == typeid(MR::Properties::UpdateType) )
+ os << boost::any_cast<MR::Properties::UpdateType>(p.second);
+ else if( p.second.type() == typeid(MR::Properties::InitType) )
+ os << boost::any_cast<MR::Properties::InitType>(p.second);
#endif
#ifdef WITH_TREEEP
- else if( p.second.type() == typeid(TreeEP::TypeType) )
- os << boost::any_cast<TreeEP::TypeType>(p.second);
+ else if( p.second.type() == typeid(TreeEP::Properties::TypeType) )
+ os << boost::any_cast<TreeEP::Properties::TypeType>(p.second);
#endif
#ifdef WITH_LC
- else if( p.second.type() == typeid(LC::CavityType) )
- os << boost::any_cast<LC::CavityType>(p.second);
- else if( p.second.type() == typeid(LC::UpdateType) )
- os << boost::any_cast<LC::UpdateType>(p.second);
+ else if( p.second.type() == typeid(LC::Properties::CavityType) )
+ os << boost::any_cast<LC::Properties::CavityType>(p.second);
+ else if( p.second.type() == typeid(LC::Properties::UpdateType) )
+ os << boost::any_cast<LC::Properties::UpdateType>(p.second);
#endif
else
- throw "Unknown property type";
+ DAI_THROW(UNKNOWN_PROPERTY_TYPE);
return( os );
}
double logZ;
double maxdiff;
double time;
+ bool has_logZ;
- TestAI( const FactorGraph &fg, const string &_name, const PropertySet &opts ) : obj(NULL), name(_name), err(), q(), logZ(0.0), maxdiff(0.0), time(0) {
- TestAI( const FactorGraph &fg, const string &_name, const Properties &opts ) : obj(NULL), name(_name), err(), q(), logZ(0.0), maxdiff(0.0), time(0), has_logZ(true) {
++ TestAI( const FactorGraph &fg, const string &_name, const PropertySet &opts ) : obj(NULL), name(_name), err(), q(), logZ(0.0), maxdiff(0.0), time(0), has_logZ(true) {
double tic = toc();
obj = newInfAlg( name, fg, opts );
time += toc() - tic;
obj->init();
obj->run();
time += toc() - tic;
- logZ = real(obj->logZ());
+ try {
+ logZ = obj->logZ();
+ has_logZ = true;
+ } catch( Exception &e ) {
+ has_logZ = false;
+ }
- maxdiff = obj->MaxDiff();
+ maxdiff = obj->maxDiff();
q = allBeliefs();
};
}