Ejemplo n.º 1
0
void
CMVarNode::SetValid
	(
	const JBoolean valid
	)
{
	if (valid != itsValidFlag)
		{
		itsValidFlag = valid;

		JTree* tree;
		if (GetTree(&tree))
			{
			tree->BroadcastChange(this);
			}
		}

	// pointer values update their own contents

	if (!valid || !itsIsPointerFlag)
		{
		const JSize count = GetChildCount();
		for (JIndex i=1; i<=count; i++)
			{
			(GetVarChild(i))->SetValid(valid);
			}
		}
}
Ejemplo n.º 2
0
void TestHSMM::test_loglik(const char* filename, size_t ID, string type){

	HSMMparam param(filename);

	vector<Real> likelihood_test;
	FactorGraph *graph;
	JTree *jt;

	// Set some constants
	size_t maxiter = 10000;
	Real   tol = 1e-9;
	size_t verb = 0;

	// Store the constants in a PropertySet object
	PropertySet opts;
	opts.set("maxiter",maxiter);  // Maximum number of iterations
	opts.set("tol",tol);          // Tolerance for convergence
	opts.set("verbose",verb);     // Verbosity (amount of output generated)


	cout << "Now we do testing...\n";

	for(size_t i=0; i<test_data.size(); i++) {

		//initialize HSMM of size equal the number of observations
		graph = new FactorGraph();
		graph->createHSMMFactorGraph(param.init, param.dist, test_data[i].size());

		jt = new JTree(*graph, opts("updates",string("HUGIN"))("heuristic",string("MINWEIGHT")) );

		//clamp the observation variables to their observed values
		for(size_t j = 0; j < test_data[i].size(); j++ ){
			//cout << "clamping var" << test_data[i][j].first << " to value " << test_data[i][j].second << "\n";
			jt->clamp(test_data[i][j].first, test_data[i][j].second);
		}

		jt->init();
		jt->run();

		//compute normalized loglikelyhood
		likelihood_test.push_back(jt->logZ()/test_data[i].size());

		delete jt;
		delete graph;

		cout << "Tested point " << i << " out of " << test_data.size() <<"\n";
	}

	cout << "done.\n";

	ofstream os;
	stringstream result;
	result << string("data/HSMMlikelihood_") << type << string("_") << ID << string(".txt");
	os.open(result.str().c_str(), ios::trunc);

	for(size_t i=0; i<likelihood_test.size(); i++){
		os << likelihood_test.at(i)<<"\n";
	}
}
void
JNamedTreeNode::SetName
	(
	const JCharacter* name
	)
{
	if (name != itsName)
		{
		itsName = name;

		NameChanged();

		JTree* tree;
		if (GetTree(&tree))
			{
			tree->BroadcastChange(this);
			}
		}
}
Ejemplo n.º 4
0
void
CMVarNode::SetValue
	(
	const JString& value
	)
{
	itsNewValueFlag = JI2B(!itsValue.IsEmpty() && value != itsValue);
	itsValue        = value;	// set *after* checking value

	itsValue.TrimWhitespace();
	if (itsOrigValue != NULL)
		{
		itsOrigValue->Clear();
		}
	ConvertToBase();

	JTree* tree;
	if (!itsCanConvertBaseFlag && GetTree(&tree))
		{
		tree->BroadcastChange(this);
		}
}
Ejemplo n.º 5
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;
}
Ejemplo n.º 6
0
void
CMVarNode::ConvertToBase()
{
	itsCanConvertBaseFlag = kJFalse;
	if (itsOrigValue != NULL && !itsOrigValue->IsEmpty())
		{
		JTree* tree;
		if (itsValue != *itsOrigValue && GetTree(&tree))
			{
			tree->BroadcastChange(this);
			}
		itsValue = *itsOrigValue;
		}

	if (itsBase == 0 || itsIsPointerFlag)
		{
		return;		// avoid constructing matchList
		}

	JArray<JIndexRange> matchList;
	if (valuePattern.Match(itsValue, &matchList))
		{
		JString vStr = itsValue.GetSubstring(matchList.GetElement(2));

		JUInt v;
		itsCanConvertBaseFlag = JI2B(
			vStr.ConvertToUInt(&v, vStr.GetFirstCharacter() == '0' ? 8 : 10) &&
			(itsBase != 1 || (0 <= v && v <= 255)));
		if (itsCanConvertBaseFlag)
			{
			// save value for when base reset to "default"

			itsOrigValue = new JString(itsValue);
			assert( itsOrigValue != NULL );

			// replace only the value, preserving whatever else is there

			if (itsBase == 1)
				{
				assert( 0 <= v && v <= 255 );

				vStr  = JString(v, JString::kBase16, kJTrue);
				vStr += " '";

				JBoolean found = kJFalse;
				for (JIndex i=0; i<kSpecialCharCount; i++)
					{
					if (JCharacter(v) == kSpecialCharInfo[i].c)
						{
						vStr += kSpecialCharInfo[i].s;
						found = kJTrue;
						}
					}
				if (!found)
					{
					vStr.AppendCharacter(v);
					}

				vStr.AppendCharacter('\'');
				}
			else
				{
				vStr = JString(v, (JString::Base) itsBase, kJTrue);
				if (itsBase == 8)
					{
					vStr.PrependCharacter('0');
					}
				}

			JIndexRange r;
			itsValue.ReplaceSubstring(matchList.GetElement(2), vStr, &r);

			JTree* tree;
			if (GetTree(&tree))
				{
				tree->BroadcastChange(this);
				}
			}
		}
}
Ejemplo n.º 7
0
void TestHSMM::test_loglik(const char* filename, size_t ID, string type, int dummy, int ID2){

	//"type" specifies the suffix of the output file "test" or "true"

	//read in HSMM parameters from textfile
	HSMMparam param(filename, 0);

	vector<Real> likelihood_test;
	FactorGraph *graph;
	JTree *jt;

	VarSet X;

	// Set some constants
	size_t maxiter = 10000;
	Real   tol = 1e-9;
	size_t verb = 0;

	// Store the constants in a PropertySet object
	PropertySet opts;
	opts.set("maxiter",maxiter);  // Maximum number of iterations
	opts.set("tol",tol);          // Tolerance for convergence
	opts.set("verbose",verb);     // Verbosity (amount of output generated)


	cout << "Now we do testing...\n";

	for(size_t i=0; i<test_data.size(); i++) {

		//initialize HSMM of size equal the number of observations
		graph = new FactorGraph();
		graph->createHSMMFactorGraph(param.init, param.dist, test_data[i].size(), 0);

		jt = new JTree(*graph, opts("updates",string("HUGIN"))("heuristic",string("MINWEIGHT")) );


		//clamp the observation variables to their observed values
		for(size_t j = 0; j < test_data[i].size(); j++ ){
			//cout << "clamping var" << test_data[i][j].first << " to value " << test_data[i][j].second << "\n";
			jt->clamp(test_data[i][j].first, test_data[i][j].second);
		}

		jt->init();
		jt->run();

		//compute normalized loglikelihood
		//likelihood_test.push_back(jt->logZ()/test_data[i].size());
		likelihood_test.push_back(jt->logZ());

		delete jt;
		delete graph;

		cout << "Tested point " << i << " out of " << test_data.size() <<"\n";

		//cout << "jt->logZ() = " << likelihood_test.at(0) << "\n";
		//exit(1);
	}

	cout << "done.\n";

	ofstream os;
	stringstream result;

	//ID2 is used to wirte test results with fixed number of training iterations
	if(ID2 >= 0){
		result << string("data/HSMMlikelihood_") << type << string("_") << ID << string("-") << ID2 << string(".txt");
	}
	else{
		result << string("data/HSMMlikelihood_") << type << string("_") << ID << string(".txt");
	}
	os.open(result.str().c_str(), ios::trunc);


	os.unsetf ( std::ios::floatfield );
	os.precision(18);
	for(size_t i=0; i<likelihood_test.size(); i++){
		os << likelihood_test.at(i)<<"\n";
	}
}
Ejemplo n.º 8
0
void TestHSMM::test_marginal_cut(const char* filename, size_t ID){

	HSMMparam param(filename);

	FactorGraph *graph;
	JTree *jt;

	// Set some constants
	size_t maxiter = 10000;
	Real   tol = 1e-9;
	size_t verb = 0;

	//window size
    size_t W = 20;
    size_t start = 0;

	// Store the constants in a PropertySet object
	PropertySet opts;
	opts.set("maxiter",maxiter);  // Maximum number of iterations
	opts.set("tol",tol);          // Tolerance for convergence
	opts.set("verbose",verb);     // Verbosity (amount of output generated)

	Factor O_last;
	vector< vector<Real> > all_marginal;
	vector<Real> sequence_marginal;
	all_marginal.reserve(test_data.size());


	cout << "Now we do testing...\n";

	for(size_t i=0; i<test_data.size(); i++) {

		//allocate memory
		sequence_marginal.reserve(test_data[i].size());

		//initialize HSMM of ever increasing size up to test_data[i].size()
		graph = new FactorGraph();
		graph->createHSMMFactorGraph(param.init, param.dist, test_data[i].size());

		jt = new JTree(*graph, opts("updates",string("HUGIN"))("heuristic",string("MINWEIGHT")) );

		jt->init();
		jt->run();

		O_last = jt->calcMarginal(graph->var(4));
		sequence_marginal.push_back( log(O_last.p().get(test_data[i][0].second)) );

		delete jt;

		for(size_t k=1; k < test_data[i].size(); k++){

			jt = new JTree(*graph, opts("updates",string("HUGIN"))("heuristic",string("MINWEIGHT")) );

			//clamp a window of observable variables to their values, except last variable which is not clamped
			start = k-W;
			if(start < 0) start = 0;

			for(size_t j = start; j <= k-1; j++){
				jt->clamp(test_data[i][j].first, test_data[i][j].second);
			}

			jt->init();
			jt->run();

			//compute p(o_last=c | o_1...o_{last-1})
			//this will give us a distribution: {o_last=1, o_last=2, ... o_last=M}
			O_last = jt->calcMarginal(graph->var(3*k+4));

			//since we have a specific observation at last time step: o_last=c, get its probability:
			sequence_marginal.push_back( log(O_last.p().get(test_data[i][k].second)) );

			delete jt;
		}

		cout << "Tested point " << i << " out of " << test_data.size() <<"\n";

		all_marginal.push_back(sequence_marginal);
		sequence_marginal.clear();

		delete graph;
	}

	cout << "Testing done.\n";

	ofstream os;
	stringstream result;
	result << string("data/HSMMmarginal_test_") << ID << string(".txt");
	os.open(result.str().c_str(), ios::trunc);

	for(size_t i=0; i<all_marginal.size(); i++){
		for(size_t j=0; j<all_marginal[i].size(); j++){
			os << all_marginal[i][j]<<" ";
		}
		os << "\n";
	}
}