void CFactorGraph::writeToFile( const char *filename ) const { ofstream outfile; outfile.open( filename ); if( outfile.is_open() ) { outfile << *this; outfile.close(); } else DAI_THROW(CANNOT_WRITE_FILE); }
Factor LC::belief (const VarSet &ns) const { if( ns.size() == 0 ) return Factor(); else if( ns.size() == 1 ) return beliefV( findVar( *(ns.begin()) ) ); else { DAI_THROW(BELIEF_NOT_AVAILABLE); return Factor(); } }
Factor ExactInf::belief( const VarSet &ns ) const { if( ns.size() == 0 ) return Factor(); else if( ns.size() == 1 ) { return beliefV( findVar( *(ns.begin()) ) ); } else { size_t I; for( I = 0; I < nrFactors(); I++ ) if( factor(I).vars() >> ns ) break; if( I == nrFactors() ) DAI_THROW(BELIEF_NOT_AVAILABLE); return beliefF(I).marginal(ns); } }
Real BBPCostFunction::evaluate( const InfAlg &ia, const vector<size_t> *stateP ) const { Real cf = 0.0; const FactorGraph &fg = ia.fg(); switch( (size_t)(*this) ) { case CFN_BETHE_ENT: // ignores state cf = -ia.logZ(); break; case CFN_VAR_ENT: // ignores state for( size_t i = 0; i < fg.nrVars(); i++ ) cf += -ia.beliefV(i).entropy(); break; case CFN_FACTOR_ENT: // ignores state for( size_t I = 0; I < fg.nrFactors(); I++ ) cf += -ia.beliefF(I).entropy(); break; case CFN_GIBBS_B: case CFN_GIBBS_B2: case CFN_GIBBS_EXP: { DAI_ASSERT( stateP != NULL ); vector<size_t> state = *stateP; DAI_ASSERT( state.size() == fg.nrVars() ); for( size_t i = 0; i < fg.nrVars(); i++ ) { Real b = ia.beliefV(i)[state[i]]; switch( (size_t)(*this) ) { case CFN_GIBBS_B: cf += b; break; case CFN_GIBBS_B2: cf += b * b / 2.0; break; case CFN_GIBBS_EXP: cf += exp( b ); break; default: DAI_THROW(UNKNOWN_ENUM_VALUE); } } break; } case CFN_GIBBS_B_FACTOR: case CFN_GIBBS_B2_FACTOR: case CFN_GIBBS_EXP_FACTOR: { DAI_ASSERT( stateP != NULL ); vector<size_t> state = *stateP; DAI_ASSERT( state.size() == fg.nrVars() ); for( size_t I = 0; I < fg.nrFactors(); I++ ) { size_t x_I = getFactorEntryForState( fg, I, state ); Real b = ia.beliefF(I)[x_I]; switch( (size_t)(*this) ) { case CFN_GIBBS_B_FACTOR: cf += b; break; case CFN_GIBBS_B2_FACTOR: cf += b * b / 2.0; break; case CFN_GIBBS_EXP_FACTOR: cf += exp( b ); break; default: DAI_THROW(UNKNOWN_ENUM_VALUE); } } break; } default: DAI_THROWE(UNKNOWN_ENUM_VALUE, "Unknown cost function " + std::string(*this)); } return cf; }