Ejemplo n.º 1
0
void mexFunction( int nlhs, mxArray *plhs[], int nrhs, const mxArray*prhs[] ) {
    // Check for proper number of arguments
    if( ((nrhs < NR_IN) || (nrhs > NR_IN + NR_IN_OPT)) || ((nlhs < NR_OUT) || (nlhs > NR_OUT + NR_OUT_OPT)) ) {
        mexErrMsgTxt("Usage: [logZ,q,qv,qf,qmap,margs] = dai_jtree(psi,varsets,opts)\n\n"
        "\n"
        "INPUT:  psi        = linear cell array containing the factors\n"
        "                     (psi{i} should be a structure with a Member field\n"
        "                     and a P field).\n"
        "        varsets    = linear cell array containing varsets for which marginals\n"
        "                     are requested.\n"
        "        opts       = string of options.\n"
        "\n"
        "OUTPUT: logZ       = logarithm of the partition sum.\n"
        "        q          = linear cell array containing all calculated marginals.\n"
        "        qv         = linear cell array containing all variable marginals.\n"
        "        qf         = linear cell array containing all factor marginals.\n"
        "        qmap       = linear array containing the MAP state.\n"
        "        margs      = linear cell array containing all requested marginals.\n");
    }

    // Get psi and construct factorgraph
    vector<Factor> factors = mx2Factors(PSI_IN, 0);
    FactorGraph fg(factors);

    // Get varsets
    vector<Permute> perms;
    vector<VarSet> varsets = mx2VarSets(VARSETS_IN,fg,0,perms);

    // Get options string
    char *opts;
    size_t buflen = mxGetN( OPTS_IN ) + 1;
    opts = (char *)mxCalloc( buflen, sizeof(char) );
    mxGetString( OPTS_IN, opts, buflen );
    // Convert to options object props
    stringstream ss;
    ss << opts;
    PropertySet props;
    ss >> props;

    // Construct InfAlg object, init and run
    JTree jt = JTree( fg, props );
    jt.init();
    jt.run();

    // Save logZ
	double logZ = NAN;
    logZ = jt.logZ();

    // Hand over results to MATLAB
    LOGZ_OUT = mxCreateDoubleMatrix(1,1,mxREAL);
    *(mxGetPr(LOGZ_OUT)) = logZ;

    Q_OUT = Factors2mx(jt.beliefs());

    if( nlhs >= 3 ) {
        vector<Factor> qv;
        qv.reserve( fg.nrVars() );
        for( size_t i = 0; i < fg.nrVars(); i++ )
            qv.push_back( jt.belief( fg.var(i) ) );
        QV_OUT = Factors2mx( qv );
    }

    if( nlhs >= 4 ) {
        vector<Factor> qf;
        qf.reserve( fg.nrFactors() );
        for( size_t I = 0; I < fg.nrFactors(); I++ )
            qf.push_back( jt.belief( fg.factor(I).vars() ) );
        QF_OUT = Factors2mx( qf );
    }

    if( nlhs >= 5 ) {
        std::vector<size_t> map_state;
        bool supported = true;
        try {
            map_state = jt.findMaximum();
        } catch( Exception &e ) {
            if( e.getCode() == Exception::NOT_IMPLEMENTED )
                supported = false;
            else
                throw;
        }
        if( supported ) {
            QMAP_OUT = mxCreateNumericMatrix(map_state.size(), 1, mxUINT32_CLASS, mxREAL);
            uint32_T* qmap_p = reinterpret_cast<uint32_T *>(mxGetPr(QMAP_OUT));
            for (size_t n = 0; n < map_state.size(); ++n)
                qmap_p[n] = map_state[n];
        } else {
            mexErrMsgTxt("Calculating a MAP state is not supported by this inference algorithm.");
        }
    }

    if( nlhs >= 6 ) {
        vector<Factor> margs;
        margs.reserve( varsets.size() );
        for( size_t s = 0; s < varsets.size(); s++ ) {
            Factor marg;
            jt.init();
            jt.run();
            marg = jt.calcMarginal( varsets[s] );

            // permute entries of marg
            Factor margperm = marg;
            for( size_t li = 0; li < marg.nrStates(); li++ )
                margperm.set( li, marg[perms[s].convertLinearIndex(li)] );
            margs.push_back( margperm );
        }
        MARGS_OUT = Factors2mx( margs );
    }

    return;
}