SharedParameters::SharedParameters( std::istream &is, const FactorGraph &fg ) : _varsets(), _perms(), _varorders(), _estimation(NULL), _ownEstimation(true) { // Read the desired parameter estimation method from the stream std::string est_method; PropertySet props; is >> est_method; is >> props; // Construct a corresponding object _estimation = ParameterEstimation::construct( est_method, props ); // Read in the factors that are to be estimated size_t num_factors; is >> num_factors; for( size_t sp_i = 0; sp_i < num_factors; ++sp_i ) { std::string line; while( line.size() == 0 && getline(is, line) ) ; std::vector<std::string> fields; tokenizeString(line, fields, " \t"); // Lookup the factor in the factorgraph if( fields.size() < 1 ) DAI_THROWE(INVALID_EMALG_FILE,"Empty line unexpected"); std::istringstream iss; iss.str( fields[0] ); size_t factor; iss >> factor; const VarSet &vs = fg.factor(factor).vars(); if( fields.size() != vs.size() + 1 ) DAI_THROWE(INVALID_EMALG_FILE,"Number of fields does not match factor size"); // Construct the vector of Vars std::vector<Var> var_order; var_order.reserve( vs.size() ); for( size_t fi = 1; fi < fields.size(); ++fi ) { // Lookup a single variable by label size_t label; std::istringstream labelparse( fields[fi] ); labelparse >> label; VarSet::const_iterator vsi = vs.begin(); for( ; vsi != vs.end(); ++vsi ) if( vsi->label() == label ) break; if( vsi == vs.end() ) DAI_THROWE(INVALID_EMALG_FILE,"Specified variables do not match the factor variables"); var_order.push_back( *vsi ); } _varorders[factor] = var_order; } // Calculate the necessary permutations setPermsAndVarSetsFromVarOrders(); }
ParameterEstimation* ParameterEstimation::construct( const std::string &method, const PropertySet &p ) { if( _registry == NULL ) loadDefaultRegistry(); std::map<std::string, ParamEstFactory>::iterator i = _registry->find(method); if( i == _registry->end() ) DAI_THROWE(UNKNOWN_PARAMETER_ESTIMATION_METHOD, "Unknown parameter estimation method: " + method); ParamEstFactory factory = i->second; return factory(p); }
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; }