TreeEP::TreeEP( const FactorGraph &fg, const PropertySet &opts ) : JTree(fg, opts("updates",string("HUGIN")), false), _maxdiff(0.0), _iters(0), props(), _Q() { setProperties( opts ); if( opts.hasKey("tree") ) { construct( fg, opts.getAs<RootedTree>("tree") ); } else { if( props.type == Properties::TypeType::ORG || props.type == Properties::TypeType::ALT ) { // ORG: construct weighted graph with as weights a crude estimate of the // mutual information between the nodes // ALT: construct weighted graph with as weights an upper bound on the // effective interaction strength between pairs of nodes WeightedGraph<Real> wg; // in order to get a connected weighted graph, we start // by connecting every variable to the zero'th variable with weight 0 for( size_t i = 1; i < fg.nrVars(); i++ ) wg[UEdge(i,0)] = 0.0; for( size_t i = 0; i < fg.nrVars(); i++ ) { SmallSet<size_t> delta_i = fg.bipGraph().delta1( i, false ); const Var& v_i = fg.var(i); foreach( size_t j, delta_i ) if( i < j ) { const Var& v_j = fg.var(j); VarSet v_ij( v_i, v_j ); SmallSet<size_t> nb_ij = fg.bipGraph().nb1Set( i ) | fg.bipGraph().nb1Set( j ); Factor piet; foreach( size_t I, nb_ij ) { const VarSet& Ivars = fg.factor(I).vars(); if( props.type == Properties::TypeType::ORG ) { if( (Ivars == v_i) || (Ivars == v_j) ) piet *= fg.factor(I); else if( Ivars >> v_ij ) piet *= fg.factor(I).marginal( v_ij ); } else { if( Ivars >> v_ij ) piet *= fg.factor(I); } } if( props.type == Properties::TypeType::ORG ) { if( piet.vars() >> v_ij ) { piet = piet.marginal( v_ij ); Factor pietf = piet.marginal(v_i) * piet.marginal(v_j); wg[UEdge(i,j)] = dist( piet, pietf, DISTKL ); } else { // this should never happen... DAI_ASSERT( 0 == 1 ); wg[UEdge(i,j)] = 0; } } else wg[UEdge(i,j)] = piet.strength(v_i, v_j); }
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(); }
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) { char *filename; // Check for proper number of arguments if ((nrhs != NR_IN) || (nlhs != NR_OUT)) { mexErrMsgTxt("Usage: [psi] = dai_readfg(filename);\n\n" "\n" "INPUT: filename = filename of a .fg file\n" "\n" "OUTPUT: psi = linear cell array containing the factors\n" " (psi{i} is a structure with a Member field\n" " and a P field, like a CPTAB).\n"); } // Get input parameters size_t buflen; buflen = mxGetN( FILENAME_IN ) + 1; filename = (char *)mxCalloc( buflen, sizeof(char) ); mxGetString( FILENAME_IN, filename, buflen ); // Read factorgraph FactorGraph fg; try { fg.ReadFromFile( filename ); } catch( std::exception &e ) { mexErrMsgTxt( e.what() ); } // Save factors vector<Factor> psi; for( size_t I = 0; I < fg.nrFactors(); I++ ) psi.push_back(fg.factor(I)); // Hand over results to MATLAB PSI_OUT = Factors2mx(psi); return; }