Ejemplo n.º 1
0
/// Writes a FactorGraph to an output stream
ostream& operator << (ostream& os, const FactorGraph& fg) {
    os << fg.nrFactors() << endl;

    for( size_t I = 0; I < fg.nrFactors(); I++ ) {
        os << endl;
        os << fg.factor(I).vars().size() << endl;
        for( VarSet::const_iterator i = fg.factor(I).vars().begin(); i != fg.factor(I).vars().end(); i++ )
            os << i->label() << " ";
        os << endl;
        for( VarSet::const_iterator i = fg.factor(I).vars().begin(); i != fg.factor(I).vars().end(); i++ )
            os << i->states() << " ";
        os << endl;
        size_t nr_nonzeros = 0;
        for( size_t k = 0; k < fg.factor(I).states(); k++ )
            if( fg.factor(I)[k] != 0.0 )
                nr_nonzeros++;
        os << nr_nonzeros << endl;
        for( size_t k = 0; k < fg.factor(I).states(); k++ )
            if( fg.factor(I)[k] != 0.0 ) {
                char buf[20];
                sprintf(buf,"%18.14g", fg.factor(I)[k]);
                os << k << " " << buf << endl;
            }
    }

    return(os);
}
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();
}
Ejemplo n.º 3
0
/// Writes a FactorGraph to an output stream
std::ostream& operator<< ( std::ostream &os, const FactorGraph &fg ) {
    os << fg.nrFactors() << endl;

    for( size_t I = 0; I < fg.nrFactors(); I++ ) {
        os << endl;
        os << fg.factor(I).vars().size() << endl;
        for( VarSet::const_iterator i = fg.factor(I).vars().begin(); i != fg.factor(I).vars().end(); i++ )
            os << i->label() << " ";
        os << endl;
        for( VarSet::const_iterator i = fg.factor(I).vars().begin(); i != fg.factor(I).vars().end(); i++ )
            os << i->states() << " ";
        os << endl;
        size_t nr_nonzeros = 0;
        for( size_t k = 0; k < fg.factor(I).nrStates(); k++ )
            if( fg.factor(I)[k] != (Real)0 )
                nr_nonzeros++;
        os << nr_nonzeros << endl;
        for( size_t k = 0; k < fg.factor(I).nrStates(); k++ )
            if( fg.factor(I)[k] != (Real)0 )
                os << k << " " << setw(os.precision()+4) << fg.factor(I)[k] << endl;
    }

    return(os);
}