void
CPersistBNet::TraverseSubobject(CPNLBase *pObj, CContext *pContext)
{
    CBNet *pModel = dynamic_cast<CBNet*>(pObj);

    pContext->Put(pModel->GetGraph(), "Graph");
    TraverseSubobjectOfGrModel(pModel, pContext);
}
Exemplo n.º 2
0
CBNet* CreateTwoNodeExDiscrete(void)
{

    const int numOfNds = 2;
    int numOfNbrs[numOfNds] = { 1, 1 };

    int nbrs0[] = { 1 };
    int nbrs1[] = { 0 };

    ENeighborType nbrsTypes0[] = { ntChild };
    ENeighborType nbrsTypes1[] = { ntParent };

    int *nbrs[] = { nbrs0, nbrs1 };
    ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1 };

    CGraph* pGraph = CGraph::Create( numOfNds, numOfNbrs, nbrs, nbrsTypes );

    // 2) Creation of the Model Domain.
    CModelDomain* pMD;

    nodeTypeVector variableTypes;

    int nVariableTypes = 1;
    variableTypes.resize( nVariableTypes );
    variableTypes[0].SetType( 1, 2 ); // discrete, 2 states

    intVector variableAssociation;
    int nnodes = pGraph->GetNumberOfNodes();
    variableAssociation.assign(nnodes, 1);
    variableAssociation[0] = 0;
    variableAssociation[1] = 0;

    pMD = CModelDomain::Create( variableTypes, variableAssociation );

    // 2) Creation base for BNet using Graph, and Model Domain

    CBNet *pBNet = CBNet::Create(pGraph, pMD);

    // 3)Allocation space for all factors of the model
    pBNet->AllocFactors();

    int nnodes0 = 1;
    int domain0[] = { 0 };
    float table0[] = { 0.3f, 0.7f};
    CTabularCPD *pCPD0 = CTabularCPD::Create( domain0, nnodes0, pMD, table0 );
    pCPD0->AllocMatrix(table0, matTable);
    pBNet->AttachParameter(pCPD0);

    int nnodes1 = 2;
    int domain1[] = { 0, 1 };
    float table1[] = { 0.3f, 0.7f, 0.3f, 0.7f};
    CTabularCPD *pCPD1 = CTabularCPD::Create( domain1, nnodes1, pMD, table1 );
    pCPD1->AllocMatrix(table1, matTable);
    pBNet->AttachParameter(pCPD1);

    return pBNet;
}
Exemplo n.º 3
0
int GibbsForSparseBNet( float eps)
{

    CBNet *pDenseBnet;
    CBNet *pSparseBnet;
    
    CGibbsSamplingInfEngine *pGibbsInfDense;
    CGibbsSamplingInfEngine *pGibbsInfSparse;
    
    pEvidencesVector evidences;
    const CPotential *pQueryPot1, *pQueryPot2;
    int ret;

    pDenseBnet = tCreateIncineratorBNet();
    pSparseBnet = pDenseBnet->ConvertToSparse();

    evidences.clear();
    pDenseBnet->GenerateSamples( &evidences, 1 );


    const int ndsToToggle1[] = { 0, 1, 3 };
    evidences[0]->ToggleNodeState( 3, ndsToToggle1 );
   
    const int querySz1 = 2;
    const int query1[] = { 0, 1 };
    
    pGibbsInfDense = CGibbsSamplingInfEngine::Create( pDenseBnet );
    pGibbsInfSparse = CGibbsSamplingInfEngine::Create( pSparseBnet );
    
    intVecVector queries(1);
    queries[0].clear();
    queries[0].push_back( 0 );
    queries[0].push_back( 1 );
    
    pGibbsInfSparse->SetQueries( queries );
    pGibbsInfSparse->EnterEvidence( evidences[0] );
    pGibbsInfSparse->MarginalNodes( query1, querySz1 );

    pGibbsInfDense->SetQueries( queries );
    pGibbsInfDense->EnterEvidence( evidences[0] );
    pGibbsInfDense->MarginalNodes( query1, querySz1 );


    pQueryPot1 = pGibbsInfDense->GetQueryJPD();
    pQueryPot2 = pGibbsInfSparse->GetQueryJPD();
    

    ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    delete pGibbsInfSparse;
    delete pGibbsInfDense;
    delete pDenseBnet;
    delete pSparseBnet;

    return ret;
}
Exemplo n.º 4
0
CBNet* Create_BNet_CompleteGraph(int num_nodes, int max_num_states, 
    long &num_edges)
{
    CGraph* pGraph = CreateCompleteGraph(num_nodes);
    CBNet* pBNet = CreateRandomBayessian(pGraph, max_num_states);
    num_edges = pBNet->GetGraph()->GetNumberOfEdges();

    return pBNet;
}
Exemplo n.º 5
0
CBNet* Create_BNet_RegularGrid(int& num_nodes, int width, int height, 
    int max_num_states, long& num_edges, int num_layers)
{
    CGraph* pGraph = CreateGraphWithRegularGridSpecific(num_nodes, 
        width, height, num_layers);
    CBNet* pBNet = CreateRandomBayessian(pGraph, max_num_states);
    num_edges = pBNet->GetGraph()->GetNumberOfEdges();

    return pBNet;
}
Exemplo n.º 6
0
CBNet* Create_BNet_toyQMR(int num_nodes, int max_num_states, 
    int num_indep_nodes, int max_size_family, long& num_edges)
{
    CGraph* pGraph = CreateRandomGraphWithToyQMRSpecific(
        num_nodes, num_indep_nodes, max_size_family);
    CBNet* pBNet = CreateRandomBayessian(pGraph, max_num_states);
    num_edges = pBNet->GetGraph()->GetNumberOfEdges();

    return pBNet;
}
Exemplo n.º 7
0
CBNet* Create_BNet_Pyramid(int &num_nodes, int max_num_states, 
    int num_indep_nodes, int num_layers, long& num_edges)
{
    CGraph* pGraph = CreateGraphWithPyramidSpecific(
        num_nodes, num_indep_nodes, num_layers);
    CBNet* pBNet = CreateRandomBayessian(pGraph, max_num_states);
    num_edges = pBNet->GetGraph()->GetNumberOfEdges();

    return pBNet;
}
Exemplo n.º 8
0
int GibbsMPEforScalarGaussianBNet( float eps)
{
    std::cout<<std::endl<<"Gibbs MPE for scalar gaussian BNet"<<std::endl;

    int ret =1;
    CBNet *pBnet = pnlExCreateScalarGaussianBNet();
    std::cout<<"BNet has been created \n";
    
    CGibbsSamplingInfEngine *pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    pGibbsInf->SetBurnIn( 100);
    pGibbsInf->SetMaxTime( 10000 );
    std::cout<<"burnIN and MaxTime have been defined \n";
    
    pEvidencesVector evidences;
    pBnet->GenerateSamples(&evidences, 1 );
    std::cout<<"evidence has been generated \n";
    
    const int ndsToToggle[] = { 0, 3 };
    evidences[0]->ToggleNodeState( 2, ndsToToggle );
    
    
    intVecVector queryes(1);
    queryes[0].push_back(0);
    pGibbsInf->SetQueries( queryes);
    std::cout<<"set queries"<<std::endl;
    
    pGibbsInf->EnterEvidence( evidences[0], 1 );
    std::cout<<"enter evidence"<<std::endl;
    
    
    intVector query(1,0);
    pGibbsInf->MarginalNodes( &query.front(),query.size() );
    std::cout<<"marginal nodes"<<std::endl;
    
    const CEvidence *pEvGibbs = pGibbsInf->GetMPE();

    CJtreeInfEngine *pJTreeInf = CJtreeInfEngine::Create(pBnet);
    pJTreeInf->EnterEvidence(evidences[0], 1);
    pJTreeInf->MarginalNodes(&query.front(), query.size());
    const CEvidence* pEvJTree = pJTreeInf->GetMPE();

    
   
    
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pEvGibbs->Dump();
    pEvJTree->Dump();
    
    delete evidences[0];
   
    delete pGibbsInf;
    delete pJTreeInf;
    delete pBnet;
    return ret;
}
Exemplo n.º 9
0
int GibbsForSingleGaussian(float eps)
{
    std::cout<<std::endl<<"Using Gibbs for testing samples from gaussian"<<std::endl;

    int nnodes = 1;
    int numnt = 1;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,2);
   
    intVector nodeAssociation = intVector(nnodes,0);
   
   
    CGraph *graph;
    graph = CGraph::Create(nnodes, 0, NULL, NULL);

    CBNet *pBnet = CBNet::Create( nnodes, numnt, nodeTypes,
	&nodeAssociation.front(),graph );
    pBnet->AllocFactors();
	pBnet->AllocFactor(0);


    float mean[2] = {0.0f, 0.0f};
    intVector ranges(2,1);
    ranges[0] = 2;

    ///////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *mean0 =	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), mean);

    ranges[1] = 2;
    float cov[4] = {1.0f, 0.3f, 0.3f, 1.0f};
    CNumericDenseMatrix<float> *cov0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), cov);

    pBnet->GetFactor(0)->AttachMatrix( mean0, matMean );
    pBnet->GetFactor(0)->AttachMatrix( cov0, matCovariance );
    /////////////////////////////////////////////////////////////////////
    CGibbsSamplingInfEngine *pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    pGibbsInf->SetBurnIn( 100 );
    pGibbsInf->SetMaxTime( 5000 );

    pEvidencesVector evidences;
    pBnet->GenerateSamples(&evidences, 1 );
    
    const int ndsToToggle[] = { 0 };
    evidences[0]->ToggleNodeState( 1, ndsToToggle );
    
    intVector query(1,0);
    
    
    intVecVector queryes(1);
    queryes[0].push_back(0);
    pGibbsInf->SetQueries( queryes);
    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( &query.front(),query.size() );

    const CPotential *pQueryPot1 = pGibbsInf->GetQueryJPD();
  
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    
    delete evidences[0];
   
    delete pGibbsInf;
    delete pBnet;
    delete []nodeTypes;

    return 1;

}
Exemplo n.º 10
0
void CBICLearningEngine::Learn()
{
    CEMLearningEngine *pLearn = NULL;

    float resultBIC = -FLT_MAX;
    CBNet *pResultBNet = NULL;
    intVector resultOrder;
    
    
    pEvidencesVector pEv(m_Vector_pEvidences.size(), NULL );
    
    CModelDomain *pMD = m_pGrModel->GetModelDomain();
    
    int nnodes = m_pGrModel->GetNumberOfNodes();
    
    nodeTypeVector varTypes;
    pMD->GetVariableTypes(&varTypes);

    intVector varAss( pMD->GetVariableAssociations(), pMD->GetVariableAssociations() + nnodes );
       
    intVector currentAssociation(nnodes);
    intVector currentObsNodes(nnodes);
    int i;
    for( i = 0; i < nnodes; i++ )
    {
	currentObsNodes[i] = i;
    }

    CGraph *pGraph = CGraph::Create(nnodes, NULL, NULL, NULL);
    CBNet *pBNet;
    int lineSz = int( nnodes * ( nnodes - 1 ) / 2 );
    intVecVector connect;
    intVector indexes(lineSz, 0);
    int startNode, endNode;
    int ind;
    for( ind = 0; ind < lineSz ; )
    {
	if( indexes[ind] == 1 )
	{
	    FindNodesByNumber(&startNode, &endNode, nnodes, ind);
	    pGraph->RemoveEdge(startNode, endNode );
	    indexes[ind] = 0;
	    ind++;
	}
	else
	{
	    FindNodesByNumber(&startNode, &endNode, nnodes, ind);
	    pGraph->AddEdge(startNode, endNode, 1 );
	    indexes[ind] = 1;
	    ind = 0;
	    connect.clear();
	    pGraph->GetConnectivityComponents(&connect);
	    if( connect.size() == 1 )
	    {
		
		do
		{
		    CGraph *pCopyGraph = CGraph::Copy(pGraph);
		    int j;
		    for( j = 0; j < nnodes; j++ )
		    {
			currentAssociation[j] = varAss[currentObsNodes[j]];
		    }
		    
		    pBNet = CBNet::Create(nnodes, varTypes, currentAssociation, pCopyGraph);
		    pBNet->AllocFactors();
		    for( j = 0; j < nnodes; j++ )
		    {
			pBNet->AllocFactor( j );
			pBNet->GetFactor(j)->CreateAllNecessaryMatrices();
		    }

		    int dimOfModel = DimOfModel(pBNet);
		    int k;
		    for( k = 0; k < pEv.size(); k++ )
		    {
			valueVector vls; 
			m_Vector_pEvidences[k]->GetRawData(&vls);
			pEv[k] = CEvidence::Create( pBNet->GetModelDomain(),currentObsNodes, vls );
		    }
		    
		    
		    pLearn = CEMLearningEngine::Create(pBNet);
		    pLearn->SetData(pEv.size(), &pEv.front());
		    pLearn->Learn();
		    int nsteps;
		    const float *score;
		    pLearn->GetCriterionValue(&nsteps, &score);
		    float log_lik = score[nsteps-1];
		    float BIC = log_lik - 0.5f*float( dimOfModel*log(float(pEv.size())) );
		    
		    if( BIC >= resultBIC )
		    {
			delete pResultBNet;
			resultBIC = BIC;
			m_critValue.push_back(BIC);
			pResultBNet = pBNet;
			resultOrder.assign( currentObsNodes.begin(), currentObsNodes.end() );
		    }
		    else
		    {
			delete pBNet;
		    }
		    for( k = 0; k < pEv.size(); k++ )
		    {
			delete pEv[k];
		    }

		    delete pLearn;
		}while(std::next_permutation(currentObsNodes.begin(), currentObsNodes.end()));
		
	    }
	    
	}
    }
    
    delete pGraph;
    m_pResultGrModel = pResultBNet;
    m_resultRenaming.assign(resultOrder.begin(), resultOrder.end());
    
}
CBNet* CreateBNet()
{
	// Creation Water-Sprinkler Bayesian network

	const int numOfNds = 4;//*<-

	// 1 STEP:
	// need to specify the graph structure of the model;
	// there are two way to do it

	CGraph *pGraph;

		// Graph creation using neighbors list

		int numOfNbrs[numOfNds] = { 2, 2, 2, 2 };//*<-
		int nbrs0[] = { 1, 2 };//*<-
		int nbrs1[] = { 0, 3 };//*<-
		int nbrs2[] = { 0, 3 };//*<-
		int nbrs3[] = { 1, 2 };//*<-

		// number of neighbors for every node
		int *nbrs[] = { nbrs0, nbrs1, nbrs2, nbrs3 };//*<-

		// neighbors can be of either one of the three following types:
		// a parent, a child (for directed arcs) or just a neighbor (for undirected graphs).
		// Accordingly, the types are ntParent, ntChild or ntNeighbor.

		ENeighborType nbrsTypes0[] = { ntChild, ntChild };//*<-
		ENeighborType nbrsTypes1[] = { ntParent, ntChild };//*<-
		ENeighborType nbrsTypes2[] = { ntParent, ntChild };//*<-
		ENeighborType nbrsTypes3[] = { ntParent, ntParent };//*<-

		ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1,nbrsTypes2, nbrsTypes3 };//*<-

		// this is creation of a directed graph for the BNet model using neighbors list
		pGraph = CGraph::Create( numOfNds, numOfNbrs, nbrs, nbrsTypes );

	// 2 STEP:
	// Creation NodeType objects and specify node types for all nodes of the model.

	nodeTypeVector  nodeTypes;

	// number of node types is 1, because all nodes are of the same type
	// all four are discrete and binary
	CNodeType nt(1,2);//*<-
	nodeTypes.push_back(nt);

	intVector nodeAssociation;
	// reflects association between node numbers and node types
	// nodeAssociation[k] is a number of node type object in the
	// node types array for the k-th node
	nodeAssociation.assign(numOfNds, 0);

	// 2 STEP:
	// Creation base for BNet using Graph, types of nodes and nodes association

	CBNet* pBNet = CBNet::Create( numOfNds, nodeTypes, nodeAssociation, pGraph );

	// 3 STEP:
	// Allocation space for all factors of the model
	pBNet->AllocFactors();

	// 4 STEP:
	// Creation factors and attach their to model

	//create raw data tables for CPDs
	float table0[] = { 0.5f, 0.5f };//*<-
	float table1[] = { 0.5f, 0.5f, 0.9f, 0.1f };
	float table2[] = { 0.8f, 0.2f, 0.2f, 0.8f };
	float table3[] = { 1.0f, 0.0f, 0.1f, 0.9f, 0.1f, 0.9f, 0.01f, 0.99f };

	float* table[] = { table0, table1, table2, table3 };//*<-

	int i;
	for( i = 0; i < numOfNds; ++i )
	{
		pBNet->AllocFactor(i);

		CFactor* pFactor = pBNet->GetFactor(i);

		pFactor->AllocMatrix( table[i], matTable );
	}

	return pBNet;
}
Exemplo n.º 12
0
int testRandomFactors()
{
    int ret = TRS_OK;
    
    int nnodes = 0;
    int i;
    while(nnodes <= 0)
    {
        trsiRead( &nnodes, "5", "Number of nodes in Model" );
    }
    //create node types
    int seed1 = pnlTestRandSeed();
    //create string to display the value
    char *value = new char[20];
#if 0
    value = _itoa(seed1, value, 10);
#else
    sprintf( value, "%d", seed1 );
#endif
    trsiRead(&seed1, value, "Seed for srand to define NodeTypes etc.");
    delete []value;
    trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "seed for rand = %d\n", seed1);
    //create 2 node types and model domain for them
    nodeTypeVector modelNodeType;
    modelNodeType.resize(2);
    modelNodeType[0] = CNodeType( 1, 4 );
    modelNodeType[1] = CNodeType( 1, 3 );
    intVector NodeAssociat;
    NodeAssociat.assign(nnodes, 0);
    for( i = 0; i < nnodes; i++ )
    {
        float rand = pnlRand( 0.0f, 1.0f );
        if( rand < 0.5f )
        {
            NodeAssociat[i] = 1;
        }
    }
    CModelDomain* pMDDiscr = CModelDomain::Create( modelNodeType,
        NodeAssociat );
    //create random graph - number of nodes for every node is rand too
    int lowBorder = nnodes - 1;
    int upperBorder = int((nnodes * (nnodes - 1))/2);
    int numEdges = pnlRand( lowBorder, upperBorder );
mark: 
    CGraph* pGraph = tCreateRandomDAG( nnodes, numEdges, 1 );
    if ( pGraph->NumberOfConnectivityComponents() != 1 )
    {
        delete pGraph;
        goto mark;
    }
    CBNet* pDiscrBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDDiscr );
    //start jtree inference just for checking 
    //the model is valid for inference and all operations can be made
    CEvidence* pDiscrEmptyEvid = CEvidence::Create( pMDDiscr, 0, NULL, valueVector() );
    CJtreeInfEngine* pDiscrInf = CJtreeInfEngine::Create( pDiscrBNet );
    pDiscrInf->EnterEvidence( pDiscrEmptyEvid );
    const CPotential* pot = NULL;
    for( i = 0; i < nnodes; i++ )
    {
        intVector domain;
        pDiscrBNet->GetFactor(i)->GetDomain( &domain );
        pDiscrInf->MarginalNodes( &domain.front(), domain.size() );
        pot = pDiscrInf->GetQueryJPD();
    }
    //make copy of Graph for using with other models
    pGraph = CGraph::Copy( pDiscrBNet->GetGraph() );
    delete pDiscrInf;
    delete pDiscrBNet;
    delete pDiscrEmptyEvid;
    delete pMDDiscr;  

    //create gaussian model domain
    modelNodeType[0] = CNodeType( 0, 4 );
    modelNodeType[1] = CNodeType( 0, 2 );
    CModelDomain* pMDCont = CModelDomain::Create( modelNodeType,
        NodeAssociat );
    CBNet* pContBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDCont );
    CEvidence* pContEmptyEvid = CEvidence::Create( pMDCont, 0, NULL, valueVector() );
    CNaiveInfEngine* pContInf = CNaiveInfEngine::Create( pContBNet );
    pContInf->EnterEvidence( pContEmptyEvid );
    for( i = 0; i < nnodes; i++ )
    {
        intVector domain;
        pContBNet->GetFactor(i)->GetDomain( &domain );
        pContInf->MarginalNodes( &domain.front(), domain.size() );
        pot = pContInf->GetQueryJPD();
    }

    pGraph = CGraph::Copy(pContBNet->GetGraph());
    delete pContInf;
    delete pContBNet;
    delete pContEmptyEvid;
    delete pMDCont;
    //find the node that haven't any parents 
    //and change its node type for it to create Conditional Gaussian CPD
    int numOfNodeWithoutParents = -1;
    intVector parents;
    parents.reserve(nnodes);
    for( i = 0; i < nnodes; i++ )
    {
        pGraph->GetParents( i, &parents );
        if( parents.size() == 0 )
        {
            numOfNodeWithoutParents = i;
            break;
        }
    }
    //change node type of this node, make it discrete
    CNodeType ntTab = CNodeType( 1,4 );
    modelNodeType.push_back( ntTab );
    NodeAssociat[numOfNodeWithoutParents] = 2;
    //need to change this model domain
    CModelDomain* pMDCondGau = CModelDomain::Create( modelNodeType, NodeAssociat );
    CBNet* pCondGauBNet = CBNet::CreateWithRandomMatrices( pGraph, pMDCondGau );
    //need to create evidence for all gaussian nodes
    intVector obsNodes;
    obsNodes.reserve(nnodes);
    int numGauVals = 0;
    for( i = 0; i < numOfNodeWithoutParents; i++ )
    {
        int GauSize = pMDCondGau->GetVariableType(i)->GetNodeSize();
        numGauVals += GauSize;
        obsNodes.push_back( i );
    }
    for( i = numOfNodeWithoutParents + 1; i < nnodes; i++ )
    {
        int GauSize = pMDCondGau->GetVariableType(i)->GetNodeSize();
        numGauVals += GauSize;
        obsNodes.push_back( i );
    }
    valueVector obsGauVals;
    obsGauVals.resize( numGauVals );
    floatVector obsGauValsFl;
    obsGauValsFl.resize( numGauVals);
    pnlRand( numGauVals, &obsGauValsFl.front(), -3.0f, 3.0f);
    //fill the valueVector
    for( i = 0; i < numGauVals; i++ )
    {
        obsGauVals[i].SetFlt(obsGauValsFl[i]);
    }
    CEvidence* pCondGauEvid = CEvidence::Create( pMDCondGau, obsNodes, obsGauVals );
    CJtreeInfEngine* pCondGauInf = CJtreeInfEngine::Create( pCondGauBNet );
    pCondGauInf->EnterEvidence( pCondGauEvid );
    pCondGauInf->MarginalNodes( &numOfNodeWithoutParents, 1 );
    pot = pCondGauInf->GetQueryJPD();
    pot->Dump();

    delete pCondGauInf;
    delete pCondGauBNet;
    delete pCondGauEvid;
    delete pMDCondGau;

    return trsResult( ret, ret == TRS_OK ? "No errors" : 
    "Bad test on RandomFactors");
}
Exemplo n.º 13
0
int testBayesLearningEngine()
{
    
    int ret = TRS_OK;
    int i, j;
    const int nnodes = 4;//Number of nodes
    int numNt = 1;//number of Node Types

    float eps = -1.0f;
    while( eps <= 0)
    {
	trssRead( &eps, "0.01f", "accuracy in test");
    }
    CNodeType *nodeTypes = new CNodeType [numNt];
    for( i=0; i < numNt; i++ )
    {
	nodeTypes[i] = CNodeType(1,2);//all nodes are discrete and binary
    }

    int nodeAssociation[] = {0, 0, 0, 0};
    int obs_nodes[] = { 0, 1, 2, 3 };
    int numOfNeigh[] = { 2, 2, 2, 2};

    int neigh0[] = { 1, 2 };
    int neigh1[] = { 0, 3 };
    int neigh2[] = { 0, 3 };
    int neigh3[] = { 1, 2 };

    ENeighborType orient0[] = { ntChild, ntChild };
    ENeighborType orient1[] = { ntParent, ntChild };
    ENeighborType orient2[] = { ntParent, ntChild };
    ENeighborType orient3[] = { ntParent, ntParent };

    int *neigh[] = { neigh0, neigh1, neigh2, neigh3 };
    ENeighborType *orient[] = { orient0, orient1, orient2, orient3 };

    float prior0[] = { 1.f, 1.f };
    float prior1[] = { 1.f, 1.f, 1.f, 1.f };
    float prior2[] = { 1.f, 1.f, 1.f, 1.f };
    float prior3[] = { 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f, 1.f };

    float* priors[] = { prior0,prior1,prior2,prior3 };

    float zero_array[] = { 1,1,1,1, 1,1,1,1 };

    float test_data0[] = { 0.636364f, 0.363636f };
    float test_data1[] = { 0.6f, 0.4f, 0.777778f, 0.222222f };
    float test_data2[] = { 0.866667f, 0.133333f, 0.111111f, 0.888889f };
    float test_data3[] = { 0.888889f, 0.111111f, 0.111111f, 0.888889f,
	0.142857f, 0.857143f, 0.333333f, 0.666667f };
    float* test_data_first[] = { test_data0, test_data1, test_data2, test_data3 };

    float test_data4[] = { 0.519231f, 0.480769f };
    float test_data5[] = { 0.571429f, 0.428571f, 0.884615f, 0.115385f };
    float test_data6[] = { 0.857143f, 0.142857f, 0.0769231f, 0.923077f };
    float test_data7[] = { 0.937500f, 0.0625000f, 0.12f, 0.88f,
	0.166667f, 0.833333f, 0.2f, 0.8f };
    float* test_data_second[] = { test_data4, test_data5, test_data6, test_data7 };

    CGraph* Graph = CGraph::Create( nnodes, numOfNeigh, neigh,
	orient);

    CBNet *myBNet = CBNet::Create(nnodes, numNt, nodeTypes,
	nodeAssociation, Graph );

    myBNet->AllocFactors();
    for ( int node = 0; node < nnodes; node++ )
    {
	myBNet->AllocFactor( node );
	//allocate empty CPT matrix, it needs to be allocated even if
	//we are going to learn train it.
	(myBNet->GetFactor( node ))->AllocMatrix( zero_array, matTable );
	//allocate prior matrices
	(myBNet->GetFactor( node ))->AllocMatrix( priors[node], matDirichlet );
	static_cast<CCPD *>(myBNet->GetFactor( node ))->NormalizeCPD();
    }

    ///////////////////////////////////////////////////////////////
    //Reading cases from disk

    FILE *fp;
    const int nEv = 50;
    CEvidence **m_pEv;
    m_pEv = new CEvidence *[nEv];
    std::vector<valueVector> Evidence(nEv);
    for( int ev = 0; ev < nEv; ev++)
    {
	Evidence[ev].resize(nnodes);
    }

    int simbol;
    char *argv = "../c_pgmtk/tests/testdata/cases1";
    fp = fopen(argv, "r");
    if(!fp)
    {
        argv = "../testdata/cases1";
        if ((fp = fopen(argv, "r")) == NULL)
	{
            printf( "can't open file %s\n", argv );
	    ret = TRS_FAIL;
	    return trsResult( ret, ret == TRS_OK ? "No errors" :
	        "Bad test: no file with cases");
	}
    }
    if (fp)
    {
	i = 0;
	j = 0;
	while( i < nEv)
	{
	    simbol = getc( fp );
	    if( isdigit( simbol ) )
	    {
		(Evidence[i])[j].SetInt(simbol - '0');
		j++;
		if( ( j - nnodes ) == 0 )
		{
		    i++;
		    j = 0;
		}
	    }
	}
    }

    for( i = 0; i < nEv; i++ )
    {
	m_pEv[i] = CEvidence::Create(myBNet->GetModelDomain(),
	    nnodes, obs_nodes, Evidence[i]);
    }

    //create bayesin learning engine
    CBayesLearningEngine *pLearn = CBayesLearningEngine::Create(myBNet);

    //Learn only portion of evidences
    pLearn->SetData(20, m_pEv);
    pLearn ->Learn();

    ///////////////////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *pMatrix;
    int length = 0;
    const float *output;

    for ( i = 0; i < nnodes; i++)
    {
	pMatrix = static_cast<CNumericDenseMatrix<float>*>(myBNet->
	    GetFactor(i)->GetMatrix(matTable));
	pMatrix->GetRawData(&length, &output);
	for (j = 0; j < length; j++)
	{
	    if( fabs(output[j] - test_data_first[i][j] ) > eps )
	    {
		ret = TRS_FAIL;
		return trsResult( ret, ret == TRS_OK ? "No errors" :
		"Bad test on BayesLearningEngine");
		break;
	    }

	}
    }

    //Learn second portion of evidences
    pLearn->AppendData(20, m_pEv+20);
    pLearn->AppendData(10, m_pEv+40);
    pLearn ->Learn();

    //check second portion
    for ( i = 0; i < nnodes; i++)
    {
	pMatrix = static_cast<CNumericDenseMatrix<float>*>(myBNet->
	    GetFactor(i)->GetMatrix(matTable));
	pMatrix->GetRawData(&length, &output);
	for (j = 0; j < length; j++)
	{
	    if( fabs(output[j] - test_data_second[i][j] ) > eps )
	    {
		ret = TRS_FAIL;
		return trsResult( ret, ret == TRS_OK ? "No errors" :
		"Bad test on BayesLearningEngine");
		break;
	    }

	}
    }

    for( i = 0; i < nEv; i++ )
    {
	delete (m_pEv[i]);

    }
    delete []m_pEv;
    delete (pLearn);
    delete (myBNet);
    delete [](nodeTypes);
    fclose(fp);
    return trsResult( ret, ret == TRS_OK ? "No errors"
	: "Bad test on BayesLearningEngine");
}
Exemplo n.º 14
0
int GibbsForSimplestGaussianBNet( float eps)
{
    std::cout<<std::endl<<"Gibbs for simplest gaussian BNet (3 nodes) "<<std::endl;

    int nnodes = 3;
    int numnt = 2;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,1);
    nodeTypes[1] = CNodeType(0,2);
    intVector nodeAssociation = intVector(nnodes,1);
    nodeAssociation[0] = 0;
    int nbs0[] = { 1 };
    int nbs1[] = { 0, 2 };
    int nbs2[] = { 1 };
    ENeighborType ori0[] = { ntChild };
    ENeighborType ori1[] = { ntParent, ntChild  };
    ENeighborType ori2[] = { ntParent };
    int *nbrs[] = { nbs0, nbs1, nbs2 };
    ENeighborType *orient[] = { ori0, ori1, ori2 };

    intVector numNeighb = intVector(3);
    numNeighb[0] = 1;
    numNeighb[1] = 2;
    numNeighb[2] = 1;

    CGraph *graph;
    graph = CGraph::Create(nnodes, &numNeighb.front(), nbrs, orient);

    CBNet *pBnet = CBNet::Create( nnodes, numnt, nodeTypes,
	&nodeAssociation.front(),graph );
    pBnet->AllocFactors();

    for(int i = 0; i < nnodes; i++ )
    {
	pBnet->AllocFactor(i);

    }

    floatVector data(1,0.0f);
    intVector ranges(2,1);

    ///////////////////////////////////////////////////////////////////
    CNumericDenseMatrix<float> *mean0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    data[0] = 0.3f;
    CNumericDenseMatrix<float> *cov0 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    pBnet->GetFactor(0)->AttachMatrix( mean0, matMean );
    pBnet->GetFactor(0)->AttachMatrix( cov0, matCovariance );
    /////////////////////////////////////////////////////////////////////

    ranges[0] = 2;
    data.resize(2);
    data[0] = -1.0f;
    data[1] = 0.0f;
    CNumericDenseMatrix<float> *mean1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] = 2;
    data.resize(4);
    data[0] = 1.0f;
    data[1] = 0.1f;
    data[3] = 3.0f;
    data[2] = 0.1f;
    CNumericDenseMatrix<float> *cov1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] =1;
    data.resize(2);
    data[0] = 1.0f;
    data[1] = 0.5f;
    CNumericDenseMatrix<float> *weight1 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    pBnet->GetFactor(1)->AttachMatrix( mean1, matMean );
    pBnet->GetFactor(1)->AttachMatrix( cov1, matCovariance );
    pBnet->GetFactor(1)->AttachMatrix( weight1, matWeights,0 );
    ///////////////////////////////////////////////////////////////////////////


    ranges[0] = 2;
    data.resize(2);
    data[0] = 1.0f;
    data[1] = 20.5f;
    CNumericDenseMatrix<float> *mean2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());

    ranges[1] = 2;
    data.resize(4);
    data[0] = 1.0f;
    data[1] = 0.0f;
    data[3] = 9.0f;
    data[2] = 0.0f;
    CNumericDenseMatrix<float> *cov2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    data.resize(2);
    data[0] = 1.0f;
    data[1] = 3.5f;
    data[2] = 1.0f;
    data[3] = 0.5f;
    CNumericDenseMatrix<float> *weight2 =
	CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &data.front());


    pBnet->GetFactor(2)->AttachMatrix( mean2, matMean );
    pBnet->GetFactor(2)->AttachMatrix( cov2, matCovariance );
    pBnet->GetFactor(2)->AttachMatrix( weight2, matWeights,0 );
    ///////////////////////////////////////////////////////////////////////////

    pEvidencesVector evidences;

    pBnet->GenerateSamples( &evidences, 1 );

    const int ndsToToggle[] = { 0, 1 };
    evidences[0]->ToggleNodeState( 2, ndsToToggle );

    intVector query(1,1);



    CNaiveInfEngine *pNaiveInf = CNaiveInfEngine::Create(pBnet);
    pNaiveInf->EnterEvidence( evidences[0] );
    pNaiveInf->MarginalNodes( &query.front(),query.size() );

    CGibbsSamplingInfEngine *pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    intVecVector queryes(1);
    queryes[0].push_back(1);
    pGibbsInf->SetQueries( queryes);
    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( &query.front(),query.size() );

    const CPotential *pQueryPot1 = pGibbsInf->GetQueryJPD();
    const CPotential *pQueryPot2 = pNaiveInf->GetQueryJPD();
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    std::cout<<"result of naive"<<std::endl;
    pQueryPot2->Dump();

    int ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    delete pNaiveInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;

}
Exemplo n.º 15
0
int timeJTreeInfEngine()
{
    int ret = TRS_OK;

    char filename[120];

    trstRead(filename, sizeof(filename), DSL_NETWORK_NAME, "Model name");

    trsTimerStart(0);

    CBNet* pBNet = ConvertFromDSLNet(filename);

    trsTimerStop(0);

    double timeOfDSL2PNLConversion = trsTimerSec(0);

    if( pBNet == NULL )
    {
        ret = TRS_FAIL;

        return trsResult( ret, ret == TRS_OK ? "No errors" : "JTree timing FAILED");
    }

    const CModelDomain* pModelDomain = pBNet->GetModelDomain();

    const int numOfNds               = pBNet->GetNumberOfNodes();

    const int numOfObsNds            = NUM_OF_OBS_NDS;

    assert( numOfObsNds <= numOfNds );


    intVector   obsNds(numOfObsNds);

    valueVector obsNdsVals(numOfObsNds);

    SetRndObsNdsAndVals( pModelDomain, &obsNds, &obsNdsVals );

    CEvidence* pEvidence = CEvidence::Create( pModelDomain, obsNds,
        obsNdsVals );

    trsTimerStart(1);

    CJtreeInfEngine* pJTreeInfEngine = CJtreeInfEngine::Create(pBNet);

    trsTimerStop(1);

    double timeOfInfCreation = trsTimerSec(1);


    const int numOfEnterEvidenceLoops = TIMES_TO_RUN_ENTER_EVIDENCE;

    assert( numOfEnterEvidenceLoops > 0 );

    trsTimerStart(2);

    int i = 0;

    for( ; i < numOfEnterEvidenceLoops; ++i )
    {
        pJTreeInfEngine->EnterEvidence(pEvidence);
    }

    trsTimerStop(2);

    double timeOfEnterEvidence = trsTimerSec(2);

    double averageTimeOfEnterEvidence = timeOfEnterEvidence
        /numOfEnterEvidenceLoops;

    double freqCPU = trsClocksPerSec();

    trsCSVString8( "d", func_name,
        trsDouble(timeOfInfCreation),
        trsDouble(averageTimeOfEnterEvidence),
        trsDouble(freqCPU),
        "\n JTree inference creation ",
        "\n average time for entering evidence ",
        "\n CPU frequency " );

    trsWrite( TW_RUN | TW_CON,
        " %s performance measurement:\n\n", func_name );

    trsWrite( TW_RUN | TW_CON,
        " Conversion from DSL to PNL network took    %g seconds\n"
        " JTree inference engine creation took       %g seconds\n"
        " Average entering evidence time is          %g seconds\n",
        timeOfDSL2PNLConversion,
        timeOfInfCreation,
        averageTimeOfEnterEvidence );

    delete pEvidence;

    //CJtreeInfEngine::Release(&pJTreeInfEngine);
    delete pJTreeInfEngine;

    delete pBNet;

    return trsResult( ret, ret == TRS_OK ? "No errors" : "JTree timing FAILED");
}
Exemplo n.º 16
0
// ----------------------------------------------------------------------------
CBNet* CreateSevenNodeExDiscrete(void)
{

    // 0  1   2
    // |\ \  /
    // | \ \ /
    // |    3
    // |   / \ 
    // |  4   5
    // |/
    // 6
    // 0, 1, 5 -  continuous
    // 3, 6 - softmax
    // 2, 4 - discrete

    const int numOfNds = 7;
    int numOfNbrs[numOfNds] = { 2, 1, 1, 5, 2, 1, 2 };

    int nbrs0[] = { 3, 6 };
    int nbrs1[] = { 3 };
    int nbrs2[] = { 3 };
    int nbrs3[] = { 0, 1, 2, 4, 5 };
    int nbrs4[] = { 3, 6 };
    int nbrs5[] = { 3 };
    int nbrs6[] = { 0, 4 };

    ENeighborType nbrsTypes0[] = { ntChild, ntChild };
    ENeighborType nbrsTypes1[] = { ntChild };
    ENeighborType nbrsTypes2[] = { ntChild };
    ENeighborType nbrsTypes3[] = { ntParent, ntParent, ntParent, ntChild, ntChild };
    ENeighborType nbrsTypes4[] = { ntParent, ntChild };
    ENeighborType nbrsTypes5[] = { ntParent };
    ENeighborType nbrsTypes6[] = { ntParent, ntParent };

    int *nbrs[] = { nbrs0, nbrs1, nbrs2, nbrs3, nbrs4, nbrs5, nbrs6 };
    ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1, nbrsTypes2, nbrsTypes3, nbrsTypes4,
                                   nbrsTypes5, nbrsTypes6
                                 };

    CGraph* pGraph = CGraph::Create( numOfNds, numOfNbrs, nbrs, nbrsTypes );

    // 2) Creation of the Model Domain.
    CModelDomain* pMD;

    nodeTypeVector variableTypes;

    int nVariableTypes = 2;
    variableTypes.resize( nVariableTypes );

    variableTypes[0].SetType( 0, 1 ); // continuous
    variableTypes[1].SetType( 1, 2 ); // discrete, 2 states

    intVector variableAssociation;
    int nnodes = pGraph->GetNumberOfNodes();
    variableAssociation.assign(nnodes, 1);
    variableAssociation[0] = 1;
    variableAssociation[1] = 1;
    variableAssociation[2] = 1;
    variableAssociation[3] = 1;
    variableAssociation[4] = 1;
    variableAssociation[5] = 1;
    variableAssociation[6] = 1;

    pMD = CModelDomain::Create( variableTypes, variableAssociation );

    // 2) Creation base for BNet using Graph, and Model Domain

    CBNet *pBNet = CBNet::Create(pGraph, pMD);

    // 3)Allocation space for all factors of the model
    pBNet->AllocFactors();

    int nnodes0 = 1;
    int domain0[] = { 0 };
    float table0[] = { 0.3f, 0.7f};
    CTabularCPD *pCPD0 = CTabularCPD::Create( domain0, nnodes0, pMD, table0 );
    pCPD0->AllocMatrix(table0, matTable);
    pBNet->AttachParameter(pCPD0);

    int nnodes1 = 1;
    int domain1[] = { 1 };
    float table1[] = { 0.3f, 0.7f};
    CTabularCPD *pCPD1 = CTabularCPD::Create( domain1, nnodes1, pMD, table1 );
    pCPD1->AllocMatrix(table1, matTable);
    pBNet->AttachParameter(pCPD1);

    int nnodes2 = 1;
    int domain2[] = { 2 };
    float table2[] = { 0.3f, 0.7f};
    CTabularCPD *pCPD2 = CTabularCPD::Create( domain2, nnodes2, pMD, table2 );
    pCPD2->AllocMatrix(table2, matTable);
    pBNet->AttachParameter(pCPD2);

    int nnodes3 = 4;
    int domain3[] = { 0, 1, 2, 3 };
    float table3[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f};
    CTabularCPD *pCPD3 = CTabularCPD::Create( domain3, nnodes3, pMD, table3 );
    pCPD3->AllocMatrix(table3, matTable);
    pBNet->AttachParameter(pCPD3);

    int nnodes4 = 2;
    int domain4[] = { 3, 4 };
    float table4[] = { 0.3f, 0.7f, 0.5, 0.5 };
    CTabularCPD *pCPD4 = CTabularCPD::Create( domain4, nnodes4, pMD, table4 );
    pCPD4->AllocMatrix(table4, matTable);
    pBNet->AttachParameter(pCPD4);

    int nnodes5 = 2;
    int domain5[] = { 3, 5 };
    float table5[] = { 0.3f, 0.7f, 0.5f, 0.5f };
    CTabularCPD *pCPD5 = CTabularCPD::Create( domain5, nnodes5, pMD, table5 );
    pCPD5->AllocMatrix(table5, matTable);
    pBNet->AttachParameter(pCPD5);

    int nnodes6 = 3;
    int domain6[] = { 0, 4, 6 };
    float table6[] = { 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 1.0f };
    CTabularCPD *pCPD6 = CTabularCPD::Create( domain6, nnodes6, pMD, table6 );
    pCPD6->AllocMatrix(table6, matTable);
    pBNet->AttachParameter(pCPD6);

    return pBNet;
}
Exemplo n.º 17
0
CBNet *CreateTestTabularNetWithDecTreeNodeBNet()
{
	//   Baysam network     
	//        0 1...8 9       0,1, 9- discrete nodes distribution type is tabular, size is 2
	//        \ \  / /      10 - is discrete desigion tree node
	//            10        
	//          
	//      
	//
	// Desigion tree on the node 10 is
	//
	//     0
	//    / \	
	//   1	 2
	//  / \ / \
	// 3  4 5  6
	//  	   
    const int nnodes = 11; //Number of nodes
    const int numNt =  1; //number of Node types (all nodes are discrete)
    CNodeType* nodeTypes = new CNodeType [numNt];
	int size = 2;
    int i;
	nodeTypes[0] = CNodeType( 1,size );
    int *nodeAssociation = new int[nnodes];
    for( i = 0; i < nnodes; i++ )
	{
		nodeAssociation[i] = 0;
	};
    int *numOfNeigh;
    numOfNeigh = new int[nnodes];

	for( i = 0; i < nnodes - 1; i++ )
	{
		numOfNeigh[i] = 1;
	};
	numOfNeigh[nnodes - 1] = nnodes - 1;

    int **neigh;
	neigh = new int*[nnodes];
    for( i = 0; i < nnodes - 1; i++ )
	{
		neigh[i] = new int;
		neigh[i][0] = nnodes - 1;

	};
	neigh[nnodes - 1] = new int[nnodes - 1];

    for( i = 0; i < nnodes - 1; i++ )
	{
		neigh[nnodes - 1][i] = i;
	};

	ENeighborType **orient;
    orient = new ENeighborType*[nnodes];

	for( i = 0; i < nnodes - 1; i++ )
	{
		orient[i] = new ENeighborType;
		orient[i][0] = ntChild;

	};
    
	orient[nnodes - 1] = new ENeighborType[nnodes - 1];

	for( i = 0; i < nnodes - 1; i++ )
	{
		orient[nnodes - 1][i] = ntParent;
	};

    CGraph* pGraph = CGraph::Create( nnodes, numOfNeigh, neigh, orient);

    //Create static BNet
    CBNet* pBNet = CBNet::Create( nnodes, numNt, nodeTypes, nodeAssociation, pGraph );
    pBNet->AllocFactors();


    int nnodesInDom = 1;
    int *domains;
	domains = new int[nnodes -1];
	for( i = 0; i < nnodes - 1; i++ )
	{
		domains[i] = i;
	};
    float table[] = { 0.3f, 0.7f};

    CTabularCPD **pCPDPar;
	pCPDPar = new CTabularCPD*[nnodes - 1];
	for( i = 0; i < nnodes - 1; i++ )
	{
		pCPDPar[i] = CTabularCPD::Create( &domains[i], nnodesInDom, pBNet->GetModelDomain(), table );
		pBNet->AttachFactor(pCPDPar[i]);
	};	
		

    int nnodesInChilddom = nnodes;
	int *domainChild; 
	domainChild = new int[ nnodes ];

		for( i = 0; i < nnodes; i++ )
		{
			domainChild[i] = i;
		};

    CTreeCPD *pCPDChild = CTreeCPD::Create( domainChild, nnodesInChilddom, pBNet->GetModelDomain());
    // creating tree on node 11
	// 1) start of graph creation
     const int nnodesT = 7; 
    int numOfNeighT[] = {2, 3, 3, 1, 1, 1, 1 };
    int neigh0T[] = {1, 2};
    int neigh1T[] = {0, 3, 4};
    int neigh2T[] = {0, 5, 6};
    int neigh3T[] = {1};
    int neigh4T[] = {1};
    int neigh5T[] = {2};
    int neigh6T[] = {2};
    

    ENeighborType orient0T[] = { ntChild, ntChild };
    ENeighborType orient1T[] = { ntParent, ntChild, ntChild };
    ENeighborType orient2T[] = { ntParent, ntChild, ntChild  };
    ENeighborType orient3T[] = { ntParent };
    ENeighborType orient4T[] = { ntParent };
    ENeighborType orient5T[] = { ntParent };
    ENeighborType orient6T[] = { ntParent };
   

    int *neighT[] = { neigh0T, neigh1T, neigh2T, neigh3T, neigh4T, neigh5T, neigh6T };
    ENeighborType *orientT[] = { orient0T, orient1T, orient2T, orient3T, orient4T, orient5T, orient6T };

    CGraph* pGraphT = CGraph::Create( nnodesT, numOfNeighT, neighT, orientT);
    // end of graph creation
	// 2) start of filling tree nodes
    TreeNodeFields fnode0; //This structures will contain properties of all     
    TreeNodeFields fnode1; //tree nodes 
    TreeNodeFields fnode2; //
    TreeNodeFields fnode3; //
    TreeNodeFields fnode4; //
    TreeNodeFields fnode5; //
    TreeNodeFields fnode6; //

	// start of filling information of node 0  
    fnode0.isTerminal = false; // means that this node is split
    fnode0.Question = 0; // question type on node 0. 
	                     // Value 0 means that that question type is "="
						 //	Value 1 means that that question type is ">"
    fnode0.questionValue = 0; //Asking value
    fnode0.node_index = 0;    // Index of asked desigion tree parent
	// end of filling information of node 0  

    // start of filling information of node 1  
    fnode1.isTerminal = false;
    fnode1.Question = 0;
    fnode1.questionValue = 0;
    fnode1.node_index = 1;
    // end of filling information of node 1  
    
    // start of filling information of node 2 
    fnode2.isTerminal = false;
    fnode2.Question = 0;
    fnode2.questionValue = 0;
    fnode2.node_index = 1;
	// end of filling information of node 2  

	// start of filling information of node 3	
    fnode3.isTerminal = true; //means that this node is terminal
    fnode3.probVect = new float[2];
    fnode3.probVect[0] = 0.3f;  //Terminal nodes probabilities.
    fnode3.probVect[1] = 0.7f;  //This properties doesn`t fill when desigion tree node
								// is continuous node.
	// end of filling information of node 3
    
	// start of filling information of node 4
    fnode4.isTerminal = true;
    fnode4.probVect = new float[2];
    fnode4.probVect[0] = 0.6f;
    fnode4.probVect[1] = 0.4f;
	// end of filling information of node 4

    // start of filling information of node 5
    fnode5.isTerminal = true;
    fnode5.probVect = new float[2];
    fnode5.probVect[0] = 0.9f;
    fnode5.probVect[1] = 0.1f;
	// end of filling information of node 5

	// start of filling information of node 6
    fnode6.isTerminal = true;
    fnode6.probVect = new float[2];
    fnode6.probVect[0] = 0.2f;
    fnode6.probVect[1] = 0.8f;
	// end of filling information of node 6
   
    TreeNodeFields fields[7];
    fields[0] = fnode0;
    fields[1] = fnode1;
    fields[2] = fnode2;
    fields[3] = fnode3;
    fields[4] = fnode4;
    fields[5] = fnode5;
    fields[6] = fnode6;
   
   
    pCPDChild->UpdateTree(pGraphT,fields);
    pBNet->AttachFactor(pCPDChild);
    return pBNet;
}
bool CStaticStructLearnSEM::LearnOneStep()
{
	intVecVector decompsition;
	CGraph* graph = m_pCurrBNet->GetGraph();
	graph->GetConnectivityComponents( &decompsition );
	CEMLearningEngine* pEMLearn;
	if(decompsition.size() > 1)
	{
		CExInfEngine< CJtreeInfEngine, CBNet, PNL_EXINFENGINEFLAVOUR_DISCONNECTED > *pInf = 
                	CExInfEngine< CJtreeInfEngine, CBNet, PNL_EXINFENGINEFLAVOUR_DISCONNECTED >::
       		Create( m_pCurrBNet  );
		pEMLearn = CEMLearningEngine::Create(m_pCurrBNet, pInf);
	}
	else
    {
        CJtreeInfEngine *pInf = CJtreeInfEngine::Create(m_pCurrBNet);
        pEMLearn = CEMLearningEngine::Create(m_pCurrBNet, pInf);
    }

	int i;
	for(i=0; i<decompsition.size(); i++)
		decompsition[i].clear();
	decompsition.clear();

	ConvertToCurrEvidences(m_pCurrBNet);
	pEMLearn->SetData(m_numberOfAllEvidences, &m_vCurrEvidences.front());
	
	pEMLearn->SetMaxIterEM(m_IterEM);

//	pEMLearn->ClearStatisticData();
	pCPDVector vNeighborCPDs;
	floatVector vNeighborLLs;
	EDGEOPVECTOR vValidMoves;
	intVector vRevCorrespDel;
	CreateNeighborCPDs(m_pCurrBNet, &vNeighborCPDs, &vValidMoves, &vRevCorrespDel);

	pEMLearn->LearnExtraCPDs(m_nMaxFanIn+1, &vNeighborCPDs, &vNeighborLLs);

//	m_pCurrBNet = static_cast<CBNet*>(pEMLearn->GetStaticModel());
	const float* familyLL = pEMLearn->GetFamilyLogLik();
	floatVector familyScores(m_nNodes,0);
	int j, freeparams;
	float logebase = (float)log(float(m_numberOfAllEvidences));
	float total_score = 0.0f;
	CFactor* pCPD;
	for(i=0; i<m_nNodes; i++)
	{
		pCPD = m_pCurrBNet->GetFactor(i);
		freeparams = pCPD->GetNumberOfFreeParameters();
		familyScores[i] = familyLL[i] - 0.5f * float(freeparams) * logebase;
		total_score += familyScores[i];
	}
	int nMoves = vValidMoves.size();
	floatVector neighborScores(nMoves, 0);
	for(i=0; i<nMoves; i++)
	{
		pCPD = static_cast<CFactor*>(vNeighborCPDs[i]);
		freeparams = pCPD->GetNumberOfFreeParameters();
		neighborScores[i] = vNeighborLLs[i] - 0.5f * float(freeparams) * logebase;
	}

	int start, end, max_position=0;
	float tmp_score, best_score = -1e37f; 
	EDGEOP move;
	for(i=0; i<nMoves; i++)
	{
		move = vValidMoves[i];
		switch (move.DAGChangeType)
		{
		case DAG_DEL : 
			end = move.originalEdge.endNode;
			tmp_score = neighborScores[i] - familyScores[end];
			if( best_score<tmp_score )
			{
				best_score = tmp_score;
				max_position = i;
			}
			break;

		case DAG_ADD :
			end = move.originalEdge.endNode;
			tmp_score = neighborScores[i] - familyScores[end];
			if( best_score<tmp_score )
			{
				best_score = tmp_score;
				max_position = i;
			}
			break;

		case DAG_REV :
			end = move.originalEdge.startNode;
			tmp_score = neighborScores[i] - familyScores[end];
			
			end = move.originalEdge.endNode;
			tmp_score += neighborScores[vRevCorrespDel[i]] - familyScores[end];
			if( best_score<tmp_score )
			{
				best_score = tmp_score;
				max_position = i;
			}
			break;
		}
	}

	move = vValidMoves[max_position];
	start = move.originalEdge.startNode;
	end = move.originalEdge.endNode;
	EDAGChangeType changeType = move.DAGChangeType;
	CCPD *addCPD=0, *delCPD=0;
	switch (changeType)
	{
	case DAG_DEL : 
		delCPD = static_cast<CCPD*>((vNeighborCPDs[max_position])->Clone());
		break;

	case DAG_ADD :
		addCPD = static_cast<CCPD*>((vNeighborCPDs[max_position])->Clone());
		break;

	case DAG_REV :
		addCPD = static_cast<CCPD*>((vNeighborCPDs[max_position])->Clone());
		delCPD = static_cast<CCPD*>((vNeighborCPDs[vRevCorrespDel[max_position]])->Clone());			
		break;
	}

	delete pEMLearn;
	for(i=0; i<vNeighborCPDs.size(); i++)
	{
		delete vNeighborCPDs[i];
	}
	vNeighborCPDs.clear();
	for(i=0; i<m_numberOfAllEvidences; i++)
	{
		delete m_vCurrEvidences[i];
	}
	m_vCurrEvidences.clear();
	vValidMoves.clear();
	float score_gate = (float)fabs(m_minProgress * total_score);
	if(best_score <= score_gate)
	{
		if(changeType == DAG_REV)
		{
			delete addCPD;
			delete delCPD;
		}
		if(changeType == DAG_ADD)delete addCPD;
		if(changeType == DAG_DEL)delete delCPD;
		return false;
	}

	total_score += best_score;
	CDAG* pDAG = CDAG::Create(*(m_pCurrBNet->GetGraph()));
	int node, node1, newnode;
	if(!(pDAG->DoMove(start, end, changeType)))
	{
		PNL_THROW(CInternalError, "There are some internal errors");
	}

	intVector vRenaming, Old2New;
	CDAG* iDAG;
	int TopologicSorted = pDAG->IsTopologicallySorted();
	if( TopologicSorted )
	{
		iDAG = pDAG->Clone();
		for(i=0; i<m_nNodes; i++) vRenaming.push_back(i);
	}
	else
		iDAG = pDAG->TopologicalCreateDAG(vRenaming);
	pDAG->Dump();
	intVector gRename;
	for(i=0; i<m_nNodes; i++)
	{
		node = vRenaming[i];
		node1 = m_vGlobalRenaming[node];
		gRename.push_back(node1);
	}
	m_vGlobalRenaming.assign(gRename.begin(), gRename.end());

	int pos;
	for(i=0; i<m_nNodes; i++)
	{
		pos = std::find(vRenaming.begin(), vRenaming.end(), i) - vRenaming.begin();
		Old2New.push_back(pos);
	}

	const int* oldNodeAsso = m_pCurrBNet->GetNodeAssociations();
	intVector newNodeAsso(m_nNodes,0);
	for(i=0; i<m_nNodes; i++)
	{
		newNodeAsso[i] = oldNodeAsso[vRenaming[i]];
	}
	nodeTypeVector vpnt;
	m_pCurrBNet->GetNodeTypes(&vpnt);
	CBNet* pBNet = CBNet::Create(m_nNodes, vpnt.size(), &vpnt.front(), 
		           &newNodeAsso.front(), static_cast<CGraph*>(iDAG));
	CModelDomain* pMDnew = pBNet->GetModelDomain();
	pBNet->AllocFactors();
	intVector domainNew, domainOld;
	const CFactor* factor=0;
	CFactor* curFactor;
	for(i=0; i<m_nNodes; i++)
	{
		domainNew.clear();
		newnode = Old2New[i];
		if( (i != start) && (i != end) )
		{
			factor = m_pCurrBNet->GetFactor(i);
		}
		else
		{
			if(changeType == DAG_REV)
			{
				if(i == start)
					factor = addCPD->Clone();
				if(i == end)
					factor = delCPD->Clone();
			}
			if(changeType == DAG_DEL)
			{
				if(i == start)
					factor = m_pCurrBNet->GetFactor(i);
				if(i == end)
					factor = delCPD->Clone();
			}
			if(changeType == DAG_ADD)
			{
				if(i == start)
					factor = m_pCurrBNet->GetFactor(i);
				if(i == end)
					factor = addCPD->Clone();
			}
		}
		factor->GetDomain(&domainOld);
		for(j=0; j<domainOld.size(); j++)
		{
			domainNew.push_back(Old2New[domainOld[j]]);
		}
		curFactor = CFactor::CopyWithNewDomain(factor, domainNew, pMDnew);
		pBNet->AttachFactor(curFactor);
	}

	if(changeType == DAG_REV)
	{
		delete addCPD;
		delete delCPD;
	}
	if(changeType == DAG_ADD)delete addCPD;
	if(changeType == DAG_DEL)delete delCPD;

	delete m_pCurrBNet;
	delete pDAG;
	m_pCurrBNet = pBNet;
	m_critValue.push_back(total_score);
	return true;
}
Exemplo n.º 19
0
CBNet* CreateRandomBayessian(CGraph* pGraph, int max_num_states)
{
    PNL_CHECK_LEFT_BORDER( max_num_states, 1 );
    PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph );

    if( !pGraph->IsDAG() )
    {
        PNL_THROW( CInconsistentType, " the graph should be a DAG " );
    }
    if( !pGraph->IsTopologicallySorted() )
    {
        PNL_THROW( CInconsistentType, 
            " the graph should be sorted topologically " );
    }
    if (pGraph->NumberOfConnectivityComponents() > 1)
    {
        PNL_THROW( CInconsistentType, " the graph should be linked " );
    }

    int i, j, k;

    int num_nodes = pGraph->GetNumberOfNodes();
    CNodeType *nodeTypes = new CNodeType [num_nodes];
    int num_states;
    
    for ( i = 0; i < num_nodes; i++ )
    {
        num_states = GetRandomNumberOfStates(max_num_states);
        nodeTypes[i].SetType(1, num_states, nsChance);
    }

    int *nodeAssociation = new int[num_nodes];
    for ( i = 0; i < num_nodes; i++ )
    {
        nodeAssociation[i] = i;
    }

    CBNet *pBNet = CBNet::Create( num_nodes, num_nodes, nodeTypes,
                                     nodeAssociation, pGraph );
    
    CModelDomain* pMD = pBNet->GetModelDomain();
    
    CFactor **myParams = new CFactor*[num_nodes];
    int *nodeNumbers = new int[num_nodes];
    int **domains = new int*[num_nodes];

    intVector parents(0);
    for ( i = 0; i < num_nodes; i++)
    {
        nodeNumbers[i] = pGraph->GetNumberOfParents(i) + 1;
        domains[i] = new int[nodeNumbers[i]];
        pGraph->GetParents(i, &parents);
        
        for ( j = 0; j < parents.size(); j++ )
            domains[i][j] = parents[j];
        domains[i][nodeNumbers[i]-1] = i;
    }

    pBNet->AllocFactors();

    for( i = 0; i < num_nodes; i++ )
    {
        myParams[i] = CTabularCPD::Create( domains[i], 
            nodeNumbers[i], pMD);
    }

    float **data = new float*[num_nodes];
    int size_data;
    int num_states_node;
    int num_blocks;
    intVector size_nodes(0);
    float belief, sum_beliefs;

    for ( i = 0; i < num_nodes; i++ )
    {
        size_data = 1;
        size_nodes.resize(0);
        for ( j = 0; j < nodeNumbers[i]; j++ )
        {
            size_nodes.push_back(pBNet->GetNodeType(
                domains[i][j])->GetNodeSize());
            size_data *= size_nodes[j];
        }
        num_states_node = size_nodes[size_nodes.size() - 1];
        num_blocks = size_data / num_states_node;
        
        data[i] = new float[size_data];

        for ( j = 0; j < num_blocks; j++ )
        {
            sum_beliefs = 0.0;
            for ( k = 0; k < num_states_node - 1; k++ )
            {
                belief = GetBelief(1.0 - sum_beliefs);
                data[i][j * num_states_node + k] = belief;
                sum_beliefs += belief;
            }
            belief = 1.0 - sum_beliefs;
            data[i][j * num_states_node + num_states_node - 1] = belief;
        }
    }

    for( i = 0; i < num_nodes; i++ )
    {
        myParams[i]->AllocMatrix(data[i], matTable);
        pBNet->AttachFactor(myParams[i]);
    }    

    delete [] nodeTypes;
    delete [] nodeAssociation;

    return pBNet;
}
Exemplo n.º 20
0
int main()
{
    PNL_USING
	//we create very small model to start inference on it
	// the model is from Kevin Murphy's BNT\examples\static\belprop_polytree_gaussain
	/*
	Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46
	Make the following polytree, where all arcs point down
	
	 0   1
	  \ /
	   2
	  / \
	 3   4


	*/
	int i;
	//create this model
	int nnodes = 5;
	int numnt = 2;
	CNodeType *nodeTypes = new CNodeType[numnt];
 	nodeTypes[0] = CNodeType(0,2);
	nodeTypes[1] = CNodeType(0,1);
	
	intVector nodeAssociation = intVector(nnodes,0);
	nodeAssociation[1] = 1;
	nodeAssociation[3] = 1;
	int nbs0[] = { 2 };
	int nbs1[] = { 2 };
	int nbs2[] = { 0, 1, 3, 4 };
	int nbs3[] = { 2 };
	int nbs4[] = { 2 };
	int *nbrs[] = { nbs0, nbs1, nbs2, nbs3, nbs4 };
	int numNeighb[] = {1, 1, 4, 1, 1};

	
	ENeighborType ori0[] = { ntChild };
	ENeighborType ori1[] = { ntChild };
	ENeighborType ori2[] = { ntParent, ntParent, ntChild, ntChild };
	ENeighborType ori3[] = { ntParent };
	ENeighborType ori4[] = { ntParent };
	ENeighborType *orient[] = { ori0, ori1, ori2, ori3, ori4 }; 
	
	
	CGraph *pGraph;
	pGraph = CGraph::Create(nnodes, numNeighb, nbrs, orient);
	
	CBNet *pBNet;
	
	pBNet = CBNet::Create( nnodes, numnt, nodeTypes, &nodeAssociation.front(), pGraph );
	//Allocation space for all factors of the model
	pBNet->AllocFactors();
	
	for( i = 0; i < nnodes; i++ )
	{
	    //Allocation space for all matrices of CPD
	    pBNet->AllocFactor(i);
	}
	
	//now we need to create data for CPDs - we'll create matrices
	CFactor *pCPD;
	floatVector smData = floatVector(2,0.0f);
	floatVector bigData = floatVector(4,1.0f);
	intVector ranges = intVector(2, 1);
	ranges[0] = 2;
	smData[0] = 1.0f;
	CNumericDenseMatrix<float> *mean0 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &smData.front());
	bigData[0] = 4.0f;
	bigData[3] = 4.0f;
	ranges[1] = 2;
	CNumericDenseMatrix<float> *cov0 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(0);
	pCPD->AttachMatrix(mean0, matMean);
	pCPD->AttachMatrix(cov0, matCovariance);
	ranges[0] = 1;
	ranges[1] = 1;
	float val = 1.0f;
	CNumericDenseMatrix<float> *mean1 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &val );
	CNumericDenseMatrix<float> *cov1 = CNumericDenseMatrix<float>::
        Create( 2, &ranges.front(), &val );
	pCPD = pBNet->GetFactor(1);
	pCPD->AttachMatrix(mean1, matMean);
	pCPD->AttachMatrix(cov1, matCovariance);
	smData[0] = 0.0f;
	smData[1] = 0.0f;
	ranges[0] = 2;
	CNumericDenseMatrix<float> *mean2 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	smData[0] = 2.0f;
	smData[1] = 1.0f;
	CNumericDenseMatrix<float> *w21 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	bigData[0] = 2.0f;
	bigData[1] = 1.0f;
	bigData[2] = 1.0f;
	bigData[3] = 1.0f;
	ranges[1] = 2;
	CNumericDenseMatrix<float> *cov2 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	bigData[0] = 1.0f;
	bigData[1] = 2.0f;
	bigData[2] = 1.0f;
	bigData[3] = 0.0f;
	CNumericDenseMatrix<float> *w20 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(2);
	pCPD->AttachMatrix( mean2, matMean );
	pCPD->AttachMatrix( cov2, matCovariance );
	pCPD->AttachMatrix( w20, matWeights,0 );
	pCPD->AttachMatrix( w21, matWeights,1 );
	
	val = 0.0f;
	ranges[0] = 1;
	ranges[1] = 1;
	CNumericDenseMatrix<float> *mean3 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &val);
	val = 1.0f;
	CNumericDenseMatrix<float> *cov3 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &val);
	ranges[1] = 2;
	smData[0] = 1.0f;
	smData[1] = 1.0f;
	CNumericDenseMatrix<float> *w30 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	pCPD = pBNet->GetFactor(3);
	pCPD->AttachMatrix( mean3, matMean );
	pCPD->AttachMatrix( cov3, matCovariance );
	pCPD->AttachMatrix( w30, matWeights,0 );

	ranges[0] = 2; 
	ranges[1] = 1;
	smData[0] = 0.0f;
	smData[1] = 0.0f;
	CNumericDenseMatrix<float> *mean4 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &smData.front());
	ranges[1] = 2;
	bigData[0] = 1.0f;
	bigData[1] = 0.0f;
	bigData[2] = 0.0f;
	bigData[3] = 1.0f;
	CNumericDenseMatrix<float> *cov4 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	bigData[2] = 1.0f;
	CNumericDenseMatrix<float> *w40 = CNumericDenseMatrix<float>::
        Create(2, &ranges.front(), &bigData.front());
	pCPD = pBNet->GetFactor(4);
	pCPD->AttachMatrix( mean4, matMean );
	pCPD->AttachMatrix( cov4, matCovariance );
	pCPD->AttachMatrix( w40, matWeights,0 );

	//Generate random evidences for the modes
	int nEv = 1000;
	pEvidencesVector evid;
	pBNet->GenerateSamples( &evid, nEv );
	/////////////////////////////////////////////////////////////////////
		
	//Create copy of initial model with random matrices 
	CGraph *pGraphCopy = CGraph::Copy(pGraph); 
	CBNet *pLearnBNet = CBNet::CreateWithRandomMatrices(pGraphCopy, pBNet->GetModelDomain() );
	
	// Creating learning process	
	CEMLearningEngine *pLearn = CEMLearningEngine::Create(pLearnBNet);

	pLearn->SetData(nEv, &evid.front());
	pLearn->Learn();
	CNumericDenseMatrix<float> *pMatrix;
	int length = 0;
	const float *output;
	
	///////////////////////////////////////////////////////////////////////
	std::cout<<" results of learning (number of evidences = "<<nEv<<std::endl;
	for (i = 0; i < nnodes; i++ )
	{
	    int j;
	    std::cout<<"\n matrix mean for node "<<i;
	    std::cout<<"\n initial BNet \n";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pBNet->GetFactor(i)->GetMatrix(matMean));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }
	    std::cout<<"\n BNet with random matrices after learning \n ";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pLearnBNet->GetFactor(i)->GetMatrix(matMean));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++)
	    {
		std::cout<<" "<<output[j];
	    }
	    
    	    std::cout<<"\n \n matrix covariance for node "<<i<<'\n';
	    std::cout<<"\n initial BNet \n";

	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pBNet->GetFactor(i)->GetMatrix(matCovariance));
	    pMatrix->GetRawData(&length, &output);
	    for (j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }
    	    std::cout<<"\n BNet with random matrices after learning \n ";
	    pMatrix = static_cast<CNumericDenseMatrix<float>*>
		(pLearnBNet->GetFactor(i)->GetMatrix(matCovariance));
	    pMatrix->GetRawData(&length, &output);
	    for ( j = 0; j < length; j++ )
	    {
		std::cout<<" "<<output[j];
	    }

	    std::cout<<"\n ___________________________\n";
	    
	}
	
	
	for( i = 0; i < nEv; i++)
	{
	    delete evid[i];
	}
	delete pLearn;
	delete pLearnBNet;
	delete pBNet;
	
	

return 1;
}
Exemplo n.º 21
0
CBNet* CreateTwoNodeEx(void)
{
    const int numOfNds = 2;
    int numOfNbrs[numOfNds] = { 1, 1 };

    int nbrs0[] = { 1 };
    int nbrs1[] = { 0 };

    ENeighborType nbrsTypes0[] = { ntChild };
    ENeighborType nbrsTypes1[] = { ntParent };

    int *nbrs[] = { nbrs0, nbrs1 };
    ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1 };

    CGraph* pGraph = CGraph::Create(numOfNds, numOfNbrs, nbrs, nbrsTypes);

    CModelDomain* pMD;

    nodeTypeVector variableTypes;

    int nVariableTypes = 2;
    variableTypes.resize(nVariableTypes);

    variableTypes[0].SetType(0, 1);
    variableTypes[1].SetType(1, 2);

    intVector variableAssociation;
    int nnodes = pGraph->GetNumberOfNodes();
    variableAssociation.assign(nnodes, 1);
    variableAssociation[0] = 0;
    variableAssociation[1] = 1;

    pMD = CModelDomain::Create(variableTypes, variableAssociation);

    CBNet *pBNet = CBNet::Create(pGraph, pMD);

    pBNet->AllocFactors();

    int nnodes0 = 1;
    int domain0[] = { 0 };
    float mean0 = 0.0f;
    float cov0 = 1.0f;
    CGaussianCPD *pCPD0 = CGaussianCPD::Create(domain0, nnodes0, pMD);
    pCPD0->AllocDistribution(&mean0, &cov0, 1.0f, NULL);
    pBNet->AttachFactor(pCPD0);

    int nnodes1 = 2;
    int domain1[] = { 0, 1 };

    CSoftMaxCPD *pCPD3 = CSoftMaxCPD::Create(domain1, nnodes1, pMD);

    int parInd0[] = { 0 };

//  float weight30[] = { -1.0f,-1.0f };
//  float offset30[] = { 1.0f, 1.0f };

    float weight30[] = { -0.3059f, -1.1777f };
    float offset30[] = { 0.0886f, 0.2034f };

    pCPD3->AllocDistribution(weight30, offset30, parInd0);

    pBNet->AttachFactor(pCPD3);

    return pBNet;
}
Exemplo n.º 22
0
PNL_USING
int main()
{
    int nnodes = 16;
    int nodeslice = 8;
    CBNet* pKjaerulf = pnlExCreateKjaerulfsBNet();
    CDBN*  pKj = CDBN::Create(pKjaerulf);

    int nSeries = 50;
    int	nslices = 101;
    int i;

    intVector nS(nSeries);
    for(i = 0; i < nSeries; i++)
    {
        nS[i] = nslices;
    }

    valueVector vValues;
    vValues.resize(nodeslice);
    intVector obsNodes(nodeslice);
    for( i=0; i<nodeslice; i++)obsNodes[i] = i;
    CEvidence ***pEv;
    pEv = new CEvidence **[nSeries];

    int series, slice, node, val;

    FILE * fp;
    fp = fopen("../Data/kjaerulff.dat", "r");

    if( !fp )
    {
        std::cout<<"can't open cases file"<<std::endl;
        exit(1);
    }

    for( series = 0; series < nSeries; series++ )
    {
        pEv[series] = new CEvidence*[ nslices ];
        for( slice = 0;  slice < nslices; slice++ )
        {
            for( node = 0; node < nodeslice; node++)
            {
                fscanf(fp, "%d,", &val);
		vValues[node].SetFlt(val);
            }
            (pEv[series])[slice] = CEvidence::Create(pKj->GetModelDomain(), obsNodes,  vValues );
        }

    }
    fclose(fp);

    CGraph *pGraph = CGraph::Create(nnodes, NULL, NULL, NULL);
    for(i=0; i<nnodes-1; i++)
    {
	pGraph->AddEdge(i, i+1,1);
    }
    CNodeType *nodeTypes = new CNodeType[1];

    nodeTypes[0].SetType(1, 2);
    int *nodeAssociation = new int[nnodes];

    for ( i = 0; i < nnodes; i++ )
    {
	nodeAssociation[i] = 0;
    }
    CBNet *pBnet = CBNet::Create( nnodes, 1, nodeTypes, nodeAssociation, pGraph );
    pBnet -> AllocFactors();
    floatVector data;
    data.assign(64, 0.0f);

    for ( node = 0; node < nnodes; node++ )
    {
	pBnet->AllocFactor( node );
	(pBnet->GetFactor( node )) ->AllocMatrix( &data.front(), matTable );
    }

    CDBN*  pDBN = CDBN::Create(pBnet);

    CMlDynamicStructLearn *pLearn = CMlDynamicStructLearn::Create(pDBN, itDBNStructLearnML,
	StructLearnHC, BIC, 4, 1, 30);

    pLearn -> SetData(nSeries, &nS.front(), pEv);
    //	pLearn->SetLearnPriorSlice(true);
    //	pLearn->SetMinProgress((float)1e-4);
    pLearn ->Learn();
    const CDAG* pDAG = pLearn->GetResultDag();
    pDAG->Dump();
    ////////////////////////////////////////////////////////////////////////////
    delete pLearn;
    delete pDBN;
    delete pKj;
    for(series = 0; series < nSeries; series++ )
    {
        for( slice = 0;  slice < nslices; slice++ )
        {
            delete (pEv[series])[slice];
        }
	delete[] pEv[series];
    }
    delete[] pEv;
    return 0;
}
Exemplo n.º 23
0
int GibbsForAsiaBNet( float eps )
{
    ///////////////////////////////////////////////////////////////////////////////

    std::cout<<std::endl<<" Asia BNet "<< std::endl;
    CBNet* pBnet = pnlExCreateAsiaBNet();
    int ret;
    pEvidencesVector evidences;

    pBnet->GenerateSamples( &evidences, 1 );

    const int ndsToToggle[] = { 1, 2, 5, 7 };

    evidences[0]->ToggleNodeState( 4, ndsToToggle );

    CGibbsSamplingInfEngine *pGibbsInf;

    pGibbsInf = CGibbsSamplingInfEngine::Create(pBnet);

    intVecVector queries(1);

    queries[0].push_back(0);
    queries[0].push_back(2);
    queries[0].push_back(7);

    pGibbsInf->SetQueries(queries);

    pGibbsInf->EnterEvidence( evidences[0] );

    CJtreeInfEngine *pJTreeInf = CJtreeInfEngine::Create(pBnet);

    pJTreeInf->EnterEvidence( evidences[0] );

    const int querySz = 2;
    const int query[] = {0, 2};

    pGibbsInf->MarginalNodes( query,querySz );

    pGibbsInf->MarginalNodes( query,querySz );

    pJTreeInf->MarginalNodes( query,querySz );

    const CPotential *pQueryPot1 = pGibbsInf->GetQueryJPD();
    const CPotential *pQueryPot2 = pJTreeInf->GetQueryJPD();

    ret = pQueryPot1-> IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    std::cout<<"Test on gibbs for asia bnet"<<std::endl;
    std::cout<<"result of gibbs"<<std::endl;
    pQueryPot1->Dump();
    std::cout<<std::endl<<"result of junction"<<std::endl;
    pQueryPot2->Dump();

    delete evidences[0];
    //CJtreeInfEngine::Release(&pJTreeInf);
    delete pJTreeInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;
    ///////////////////////////////////////////////////////////////////////////////
}
Exemplo n.º 24
0
// ----------------------------------------------------------------------------
CBNet* CreateSevenNodeEx(void)
{

    // 0  1   2
    // |\ \  /
    // | \ \ /
    // |    3
    // |   / \ 
    // |  4   5
    // |/
    // 6
    // 0, 1, 5 - continuous
    // 3, 6 - softmax
    // 2, 4 - discrete

    const int numOfNds = 7;
    int numOfNbrs[numOfNds] = { 2, 1, 1, 5, 2, 1, 2 };

    int nbrs0[] = { 3, 6 };
    int nbrs1[] = { 3 };
    int nbrs2[] = { 3 };
    int nbrs3[] = { 0, 1, 2, 4, 5 };
    int nbrs4[] = { 3, 6 };
    int nbrs5[] = { 3 };
    int nbrs6[] = { 0, 4 };

    ENeighborType nbrsTypes0[] = { ntChild, ntChild };
    ENeighborType nbrsTypes1[] = { ntChild };
    ENeighborType nbrsTypes2[] = { ntChild };
    ENeighborType nbrsTypes3[] = { ntParent, ntParent, ntParent, ntChild, ntChild };
    ENeighborType nbrsTypes4[] = { ntParent, ntChild };
    ENeighborType nbrsTypes5[] = { ntParent };
    ENeighborType nbrsTypes6[] = { ntParent, ntParent };

    int *nbrs[] = { nbrs0, nbrs1, nbrs2, nbrs3, nbrs4, nbrs5, nbrs6 };
    ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1, nbrsTypes2, nbrsTypes3, nbrsTypes4,
                                   nbrsTypes5, nbrsTypes6
                                 };

    CGraph* pGraph = CGraph::Create( numOfNds, numOfNbrs, nbrs, nbrsTypes );

    // 2) Creation of the Model Domain.
    CModelDomain* pMD;

    nodeTypeVector variableTypes;

    int nVariableTypes = 2;
    variableTypes.resize( nVariableTypes );

    variableTypes[0].SetType( 0, 1 ); // continuous
    variableTypes[1].SetType( 1, 2 ); // discrete, 2 states

    intVector variableAssociation;
    int nnodes = pGraph->GetNumberOfNodes();
    variableAssociation.assign(nnodes, 1);
    variableAssociation[0] = 0;
    variableAssociation[1] = 0;
    variableAssociation[2] = 1;
    variableAssociation[3] = 1;
    variableAssociation[4] = 1;
    variableAssociation[5] = 0;
    variableAssociation[6] = 1;

    pMD = CModelDomain::Create( variableTypes, variableAssociation );

    // 2) Creation base for BNet using Graph, and Model Domain

    CBNet *pBNet = CBNet::Create(pGraph, pMD);

    // 3)Allocation space for all factors of the model
    pBNet->AllocFactors();

    int nnodes0 = 1;
    int domain0[] = { 0 };
    float mean0 = 0.5f;
    float cov0 = 1.0f;
    CGaussianCPD *pCPD0 = CGaussianCPD::Create( domain0, nnodes0, pMD );
    pCPD0->AllocDistribution( &mean0, &cov0, 1.0f, NULL );
    pBNet->AttachFactor( pCPD0 );

    int nnodes1 = 1;
    int domain1[] = { 1 };
    float mean1 = 0.5f;
    float cov1 = 1.0f;
    CGaussianCPD *pCPD1 = CGaussianCPD::Create( domain1, nnodes1, pMD );
    pCPD1->AllocDistribution( &mean1, &cov1, 1.0f, NULL );
    pBNet->AttachFactor( pCPD1 );

    int nnodes2 = 1;
    int domain2[] = { 2 };
    float table2[] = { 0.3f, 0.7f};
    CTabularCPD *pCPD2 = CTabularCPD::Create( domain2, nnodes2, pMD, table2 );
    pCPD2->AllocMatrix(table2, matTable);
    pBNet->AttachParameter(pCPD2);

    int nnodes3 = 4;
    int domain3[] = { 0, 1, 2, 3 };
    CSoftMaxCPD *pCPD3 = CSoftMaxCPD::Create( domain3, nnodes3, pMD );

    int parInd30[] = { 0 };
//  float weight30[] = { 0.5f, 0.5f, 0.5f, 0.7f, 0.3f, 0.7f };
//  float offset30[] = { 0.3f, 0.5f, 1.2f };
    float weight30[] = { 0.5f, 0.4f, 0.5f, 0.7f };
    float offset30[] = { 0.3f, 0.5f };

    pCPD3->AllocDistribution( weight30, offset30, parInd30 );

    int parInd31[] = { 1 };
    float weight31[] = { 0.5f, 0.1f, 0.5f, 0.7f };
    float offset31[] = { 0.3f, 0.5f };
//  float weight31[] = { 0.5f, 0.5f, 0.5f, 0.7f, 0.3f, 0.7f };
//  float offset31[] = { 0.3f, 0.5f, 5.4f };

    pCPD3->AllocDistribution( weight31, offset31, parInd31 );
    pBNet->AttachFactor( pCPD3 );

    int nnodes4 = 2;
    int domain4[] = { 3, 4 };
    float table4[] = { 0.3f, 0.7f, 0.8f, 0.2f };
//  float table4[] = { 0.3f, 0.7f, 0.5, 0.5, 0.1, 0.9 };

    CTabularCPD *pCPD4 = CTabularCPD::Create( domain4, nnodes4, pMD, table4 );
    pCPD4->AllocMatrix(table4, matTable);
    pBNet->AttachParameter(pCPD4);

    int nnodes5 = 2;
    int domain5[] = { 3, 5 };
    CGaussianCPD *pCPD5 = CGaussianCPD::Create( domain5, nnodes5, pMD );

    float mean50 = 1.0f;
    float cov50 = 1.0f;
    int parInd50[] = { 0 };
    pCPD5->AllocDistribution( &mean50, &cov50, 1.0f, NULL, parInd50 );

    float mean51 = 0.5f;
    float cov51 = 0.5f;
    int parInd51[] = { 1 };
    pCPD5->AllocDistribution( &mean51, &cov51, 1.0f, NULL, parInd51 );

    /*  float mean52 = 0.0f;
      float cov52 = 1.f;
      int parInd52[] = { 2 };
      pCPD5->AllocDistribution( &mean52, &cov52, 1.0f, NULL, parInd52 );
    */
    pBNet->AttachFactor(pCPD5);

    int nnodes6 = 3;
    int domain6[] = { 0, 4, 6 };
    CSoftMaxCPD *pCPD6 = CSoftMaxCPD::Create( domain6, nnodes6, pMD );
    int parInd60[] = { 0 };

    float weight60[] = { 0.5f, 0.9f, 3.2f };
    float offset60[] = { 0.7f, 0.3f, 0.1f };

    pCPD6->AllocDistribution( weight60, offset60, parInd60 );

    int parInd61[] = { 1 };

//  float weight61[] = { 0.8f, 0.2f, 0.5f };
//  float offset61[] = { 0.1f, 0.9f, 1.9f };
    float weight61[] = { 0.8f, 0.2f };
    float offset61[] = { 0.1f, 0.9f };

    pCPD6->AllocDistribution( weight61, offset61, parInd61 );

    pBNet->AttachFactor( pCPD6 );

    return pBNet;
}
Exemplo n.º 25
0
int GibbsForInceneratorBNet(float eps)
{

    std::cout<<std::endl<<"Gibbs for Incenerator BNet"<< std::endl;

    CBNet *pBnet;
    pEvidencesVector evidences;
    CJtreeInfEngine *pJTreeInf;
    CGibbsSamplingInfEngine *pGibbsInf;
    const CPotential *pQueryPot1, *pQueryPot2;
    int i, ret;

    pBnet = tCreateIncineratorBNet();

    evidences.clear();
    pBnet->GenerateSamples( &evidences, 1 );


    const int ndsToToggle1[] = { 0, 1, 3 };
    evidences[0]->ToggleNodeState( 3, ndsToToggle1 );
    const int *flags = evidences[0]->GetObsNodesFlags();
    std::cout<<"observed nodes"<<std::endl;
    for( i = 0; i < pBnet->GetNumberOfNodes(); i++ )
    {
	if ( flags[i] )
	{
	    std::cout<<"node "<<i<<"; ";
	}
    }
    std::cout<<std::endl<<std::endl;

    const int querySz1 = 2;
    const int query1[] = { 0, 1 };

    pJTreeInf = CJtreeInfEngine::Create(pBnet);
    pJTreeInf->EnterEvidence( evidences[0] );
    pJTreeInf->MarginalNodes( query1,querySz1 );

    pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );

    intVecVector queries(1);
    queries[0].clear();
    queries[0].push_back( 0 );
    queries[0].push_back( 1 );
    pGibbsInf->SetQueries( queries );

    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( query1, querySz1 );

    pQueryPot1 = pGibbsInf->GetQueryJPD();
    pQueryPot2 = pJTreeInf->GetQueryJPD();
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    std::cout<<"result of junction"<<std::endl;
    pQueryPot2->Dump();

    ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    //CJtreeInfEngine::Release(&pJTreeInf);
    delete pJTreeInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;

    ///////////////////////////////////////////////////////////////////////////////

}
Exemplo n.º 26
0
int testSetStatistics()
{
    int ret = TRS_OK;
    float eps = 0.1f;
    
    int seed = pnlTestRandSeed();
    pnlSeed( seed );   
            
    CBNet *pBNet = pnlExCreateCondGaussArBNet();
    CModelDomain *pMD = pBNet->GetModelDomain();

    
    CGraph *pGraph = CGraph::Copy(pBNet->GetGraph());
    
    CBNet *pBNet1 = CBNet::CreateWithRandomMatrices( pGraph, pMD );

    pEvidencesVector evidences;
    int nEvidences = pnlRand( 3000, 4000);
    
    pBNet->GenerateSamples( &evidences, nEvidences );
   
    
    int i;
    for( i = 0; i < nEvidences; i++)
    {
	
	//evidences[i]->MakeNodeHiddenBySerialNum(0);
    }
    

    CEMLearningEngine *pLearn = CEMLearningEngine::Create(pBNet1);
    pLearn->SetData( nEvidences, &evidences.front() );
    pLearn->SetMaxIterEM();
    pLearn->Learn();

    for( i = 0; i < pBNet->GetNumberOfFactors(); i++ )
    {
	if( ! pBNet->GetFactor(i)->IsFactorsDistribFunEqual(pBNet1->GetFactor(i), eps))
	{
	    ret = TRS_FAIL;
	    pBNet->GetFactor(i)->GetDistribFun()->Dump();
	    pBNet1->GetFactor(i)->GetDistribFun()->Dump();

	}
    }
    
    CDistribFun *pDistr;
    const CMatrix<float>* pMat;
    CFactor *pCPD;
    
    pDistr = pBNet1->GetFactor(0)->GetDistribFun();
    pMat = pDistr->GetStatisticalMatrix(stMatTable);
    
    pCPD = pBNet->GetFactor(0);
    pCPD->SetStatistics(pMat, stMatTable);
    pCPD->ProcessingStatisticalData(nEvidences);
    if( ! pCPD->IsFactorsDistribFunEqual(pBNet1->GetFactor(0), 0.0001f) )
    {
	ret = TRS_FAIL;
    }
    

    pDistr = pBNet1->GetFactor(1)->GetDistribFun();
    
    int parentVal;
    pCPD = pBNet->GetFactor(1);
    
    parentVal = 0;

    pCPD->SetStatistics(pMat, stMatCoeff);

    pMat = pDistr->GetStatisticalMatrix(stMatMu, &parentVal);
    pCPD->SetStatistics(pMat, stMatMu, &parentVal);
    
    
    pMat = pDistr->GetStatisticalMatrix(stMatSigma, &parentVal);
    pCPD->SetStatistics(pMat, stMatSigma, &parentVal);
    
    parentVal = 1;
    
    pMat = pDistr->GetStatisticalMatrix(stMatMu, &parentVal);
    pCPD->SetStatistics(pMat, stMatMu, &parentVal);
    
    
    pMat = pDistr->GetStatisticalMatrix(stMatSigma, &parentVal);
    pCPD->SetStatistics(pMat, stMatSigma, &parentVal);

    pCPD->ProcessingStatisticalData(nEvidences);
    
    if( ! pCPD->IsFactorsDistribFunEqual(pBNet1->GetFactor(1), eps) )
    {
	ret = TRS_FAIL;
    }
    
    
    for( i = 0; i < nEvidences; i++)
    {
	delete evidences[i];
    }
    delete pLearn;
    delete pBNet1;
    delete pBNet;

    
    return trsResult( ret, ret == TRS_OK ? "No errors" : 
    "Bad test on SetStatistics");
    
    
}
Exemplo n.º 27
0
int GibbsForScalarGaussianBNet( float eps)
{
    std::cout<<std::endl<<" Scalar gaussian BNet (5 nodes)"<< std::endl;
    CBNet *pBnet;
    pEvidencesVector evidences;

    CGibbsSamplingInfEngine *pGibbsInf;
    const CPotential *pQueryPot1, *pQueryPot2;
    int i, ret;

    ////////////////////////////////////////////////////////////////////////
    //Do the example from Satnam Alag's PhD thesis, UCB ME dept 1996 p46
    //Make the following polytree, where all arcs point down
    //
    // 0   1
    //  \ /
    //   2
    //  / \
    // 3   4
    //
    //////////////////////////////////////////////////////////////////////

    int nnodes = 5;
    int numnt = 1;
    CNodeType *nodeTypes = new CNodeType[numnt];
    nodeTypes[0] = CNodeType(0,1);

    intVector nodeAssociation = intVector(nnodes,0);

    int nbs0[] = { 2 };
    int nbs1[] = { 2 };
    int nbs2[] = { 0, 1, 3, 4 };
    int nbs3[] = { 2 };
    int nbs4[] = { 2 };
    ENeighborType ori0[] = { ntChild };
    ENeighborType ori1[] = { ntChild };
    ENeighborType ori2[] = { ntParent, ntParent, ntChild, ntChild };
    ENeighborType ori3[] = { ntParent };
    ENeighborType ori4[] = { ntParent };
    int *nbrs[] = { nbs0, nbs1, nbs2, nbs3, nbs4 };
    ENeighborType *orient[] = { ori0, ori1, ori2, ori3, ori4 };
    intVector numNeighb = intVector(5,1);
    numNeighb[2] = 4;
    CGraph *graph;
    graph = CGraph::Create(nnodes, &numNeighb.front(), nbrs, orient);

    pBnet = CBNet::Create( nnodes, numnt, nodeTypes, &nodeAssociation.front(),graph );
    pBnet->AllocFactors();

    for( i = 0; i < nnodes; i++ )
    {
	pBnet->AllocFactor(i);
    }
    //now we need to create data for factors - we'll create matrices
    floatVector smData = floatVector(1,0.0f);
    floatVector bigData = floatVector(1,1.0f);
    intVector ranges = intVector(2, 1);
    ranges[0] = 1;
    smData[0] = 1.0f;
    CNumericDenseMatrix<float> *mean0 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &smData.front());
    bigData[0] = 4.0f;

    CNumericDenseMatrix<float> *cov0 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(0)->AttachMatrix(mean0, matMean);
    pBnet->GetFactor(0)->AttachMatrix(cov0, matCovariance);

    float val = 1.0f;

    CNumericDenseMatrix<float> *mean1 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &val );
    CNumericDenseMatrix<float> *cov1 = CNumericDenseMatrix<float>::Create( 2, &ranges.front(), &val );
    pBnet->GetFactor(1)->AttachMatrix(mean1, matMean);
    pBnet->GetFactor(1)->AttachMatrix(cov1, matCovariance);
    smData[0] = 0.0f;

    CNumericDenseMatrix<float> *mean2 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    smData[0] = 2.0f;

    CNumericDenseMatrix<float> *w21 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    bigData[0] = 2.0f;

    CNumericDenseMatrix<float> *cov2 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    bigData[0] = 1.0f;

    CNumericDenseMatrix<float> *w20 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(2)->AttachMatrix( mean2, matMean );
    pBnet->GetFactor(2)->AttachMatrix( cov2, matCovariance );
    pBnet->GetFactor(2)->AttachMatrix( w20, matWeights,0 );
    pBnet->GetFactor(2)->AttachMatrix( w21, matWeights,1 );

    val = 0.0f;

    CNumericDenseMatrix<float> *mean3 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &val);
    val = 4.0f;
    CNumericDenseMatrix<float> *cov3 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &val);

    smData[0] = 1.1f;

    CNumericDenseMatrix<float> *w30 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());
    pBnet->GetFactor(3)->AttachMatrix( mean3, matMean );
    pBnet->GetFactor(3)->AttachMatrix( cov3, matCovariance );
    pBnet->GetFactor(3)->AttachMatrix( w30, matWeights,0 );


    smData[0] = -0.8f;

    CNumericDenseMatrix<float> *mean4 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &smData.front());

    bigData[0] = 1.2f;

    CNumericDenseMatrix<float> *cov4 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    bigData[0] = 2.0f;

    CNumericDenseMatrix<float> *w40 = CNumericDenseMatrix<float>::Create(2, &ranges.front(), &bigData.front());
    pBnet->GetFactor(4)->AttachMatrix( mean4, matMean );
    pBnet->GetFactor(4)->AttachMatrix( cov4, matCovariance );
    pBnet->GetFactor(4)->AttachMatrix( w40, matWeights,0 );


    evidences.clear();
    pBnet->GenerateSamples( &evidences, 1 );

    const int ndsToToggle2[] = { 0, 1, 2 };
    evidences[0]->ToggleNodeState( 3, ndsToToggle2 );
    const int *flags1 = evidences[0]->GetObsNodesFlags();
    std::cout<<"observed nodes"<<std::endl;
    for( i = 0; i < pBnet->GetNumberOfNodes(); i++ )
    {
	if ( flags1[i] )
	{
	    std::cout<<"node "<<i<<"; ";
	}
    }
    std::cout<<std::endl<<std::endl;

    const int querySz2 = 1;
    const int query2[] = { 0 };

    CNaiveInfEngine *pNaiveInf = CNaiveInfEngine::Create(pBnet);
    pNaiveInf->EnterEvidence( evidences[0] );
    pNaiveInf->MarginalNodes( query2,querySz2 );

    pGibbsInf = CGibbsSamplingInfEngine::Create( pBnet );
    pGibbsInf->SetNumStreams( 1 );
    pGibbsInf->SetMaxTime( 10000 );
    pGibbsInf->SetBurnIn( 1000 );
    


    intVecVector queries(1);
    queries[0].clear();
    queries[0].push_back( 0 );
    //queries[0].push_back( 2 );
    pGibbsInf->SetQueries( queries );

    pGibbsInf->EnterEvidence( evidences[0] );
    pGibbsInf->MarginalNodes( query2, querySz2 );

    pQueryPot1 = pGibbsInf->GetQueryJPD();
    pQueryPot2 = pNaiveInf->GetQueryJPD();
    std::cout<<"result of gibbs"<<std::endl<<std::endl;
    pQueryPot1->Dump();
    std::cout<<"result of naive"<<std::endl;
    pQueryPot2->Dump();

    ret = pQueryPot1->IsFactorsDistribFunEqual( pQueryPot2, eps, 0 );

    delete evidences[0];
    delete pNaiveInf;
    delete pGibbsInf;
    delete pBnet;

    return ret;

    ////////////////////////////////////////////////////////////////////////////////////////
}
Exemplo n.º 28
0
CBNet* CreateFourNodeExampleNew(void)
{

    CBNet *pBNet;
    const int nnodes = 4;
    const int numberOfNodeTypes = 2;

    int numOfNeigh[] = { 1, 1, 3, 1 };

    int neigh0[] = { 2 };
    int neigh1[] = { 2 };
    int neigh2[] = { 0, 1, 3 };
    int neigh3[] = { 2 };

    ENeighborType orient0[] = { ntChild };
    ENeighborType orient1[] = { ntChild };
    ENeighborType orient2[] = { ntParent, ntParent, ntChild };
    ENeighborType orient3[] = { ntParent };

    int *neigh[] = { neigh0,  neigh1, neigh2, neigh3 };

    ENeighborType *orient[] = { orient0, orient1, orient2, orient3 };

    CGraph *pGraph = CGraph::Create( nnodes, numOfNeigh, neigh, orient );

    CNodeType *nodeTypes = new CNodeType [numberOfNodeTypes];
    nodeTypes[0].SetType(1, 2);
    nodeTypes[1].SetType(0, 1);

    int *nodeAssociation = new int[nnodes];

    nodeAssociation[0]=0;
    nodeAssociation[1]=1;
    nodeAssociation[2]=1;
    nodeAssociation[3]=1;

    pBNet = CBNet::Create(nnodes, numberOfNodeTypes, nodeTypes, nodeAssociation, pGraph);

    CModelDomain* pMD = pBNet->GetModelDomain();

//number of parameters is the same as number of nodes - one CPD per node
//  CFactor *myParams = new CFactor[1];
    int *nodeNumbers = new int [nnodes];

    int domain0[] = { 0 };
    int domain1[] = { 1 };
    int domain2[] = { 0, 1, 2 };
    int domain3[] = { 2, 3 };
    int *domains[] = { domain0, domain1, domain2, domain3 };
    nodeNumbers[0] = 1;
    nodeNumbers[1] = 1;
    nodeNumbers[2] = 3;
    nodeNumbers[3] = 2;

    pBNet->AllocParameters();

    CFactor *myParams = CTabularCPD::Create( domains[0], nodeNumbers[0], pMD );

// data creation for all CPDs of the model
    float data0[] = { 0.5f, 0.5f };

    myParams->AllocMatrix(data0, matTable);
    pBNet->AttachParameter(myParams);

    float mean0 = 0.0f;
    float cov0 = 1.0f;
    CGaussianCPD* pCPD = CGaussianCPD::Create( domain1, 1, pMD );
    pCPD->AllocDistribution( &mean0, &cov0, 1.0f, NULL);
    pBNet->AttachFactor(pCPD);

    float mean1[] = { 8.0f };
    float mean2[] = { 2.0f };
    float cov1[] = { 1.0f };
    float cov2[] = { 1.0f };

    float weight[] = { 0.01f, 0.03f };
    float weight1[] = { 0.01f };
    const float *pData = weight;
    const float *pData1 = weight1;

    CGaussianCPD* pCPD1 = CGaussianCPD::Create( domain2, 3, pMD );
    int ParentCom[] = { 0, 1 };
    pCPD1->AllocDistribution( mean1, cov1, 0.5f, &pData, &ParentCom[0] );
    pCPD1->AllocDistribution( mean2, cov2, 0.5f, &pData, &ParentCom[1] );
    pBNet->AttachFactor(pCPD1);

    CGaussianCPD* pCPD2 = CGaussianCPD::Create( domain3, 2, pMD );
    pCPD2->AllocDistribution( mean1, cov1, 0.5f, &pData1 );
    pBNet->AttachFactor(pCPD2);


    delete [] nodeTypes;
    return pBNet;
}
Exemplo n.º 29
0
int testEvidence()
{
    int ret = TRS_OK;
    int nnodes = 0;
    int nObsNodes = 0;
    int i,j;
    while(nnodes <= 0)
    {
	trsiRead( &nnodes, "10", "Number of nodes in Model" );
    }
    while((nObsNodes <= 0)||(nObsNodes>nnodes))
    {
        trsiRead( &nObsNodes, "2", "Number of Observed nodes from all nodes in model");
    }
    int seed1 = pnlTestRandSeed();
    /*create string to display the value*/
    char value[42];

    sprintf(value, "%i", seed1);
    trsiRead(&seed1, value, "Seed for srand to define NodeTypes etc.");
    trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "seed for rand = %d\n", seed1);
    CNodeType *modelNodeType = new CNodeType[2];
    modelNodeType[0] = CNodeType( 1, 4 );
    modelNodeType[1] = CNodeType( 0, 3 );
    int *NodeAssociat=new int [nnodes+1];
    for(i=0; i<(nnodes+1)/2; i++)
    {
	NodeAssociat[2*i]=0;
	NodeAssociat[2*i+1]=1;
    }
    //create random graph - number of nodes for every node is rand too
    int lowBorder = nnodes - 1;
    int upperBorder = int((nnodes * (nnodes - 1)) / 2);
    int numEdges = rand()%(upperBorder - lowBorder)+lowBorder;
    CGraph* theGraph = tCreateRandomDAG( nnodes, numEdges, 1 );

    CBNet *grModel = CBNet::Create(nnodes, 2, modelNodeType, NodeAssociat,theGraph);
    int *obsNodes = (int*)trsGuardcAlloc(nObsNodes, sizeof(int));
    srand ((unsigned int)seed1);
    intVector residuaryNodes;
    for (i=0; i<nnodes; i++)
    {
	residuaryNodes.push_back(i);
    }
    int num = 0;
    valueVector Values;
    Values.reserve(3*nnodes);
    Value val;
    for (i = 0; i<nObsNodes; i++)
    {
	num = rand()%(nnodes-i);
	obsNodes[i] = residuaryNodes[num];
	residuaryNodes.erase(residuaryNodes.begin()+num);
	CNodeType nt = modelNodeType[NodeAssociat[obsNodes[i]]];
	if(nt.IsDiscrete())
	{
            val.SetInt(1);
            Values.push_back(val);
	}
	else
	{
	    val.SetFlt(1.0f);
            Values.push_back(val);
            val.SetFlt(2.0f);
            Values.push_back(val);
            val.SetFlt(3.0f);
            Values.push_back(val);
	}

    }
    residuaryNodes.clear();
    CEvidence *pMyEvid = CEvidence::Create(grModel,
	nObsNodes, obsNodes, Values) ;
    int nObsNodesFromEv = pMyEvid->GetNumberObsNodes();
    const int *pObsNodesNow = pMyEvid->GetObsNodesFlags();
    //	const int *myOffset = pMyEvid->GetOffset();
    const int *myNumAllObsNodes = pMyEvid->GetAllObsNodes();
    valueVector ev;
    pMyEvid->GetRawData(&ev);
    const Value* vall = pMyEvid->GetValue(obsNodes[0]);
    if( NodeAssociat[obsNodes[0]] == 0 )
    {
	if( (vall)[0].GetInt() != 1 )
	{
	    ret = TRS_FAIL;
	}
    }
    else
    {
	for( j=0; j<3; j++)
	{
	    if( (vall)[j].GetFlt() != (j+1)*1.0f )
	    {
		ret = TRS_FAIL;
		break;
	    }
	}
    }
    if(nObsNodesFromEv == nObsNodes)
    {
	intVector numbersOfReallyObsNodes;
	int numReallyObsNodes=0;
	for ( i=0; i<nObsNodesFromEv; i++)
	{
	    if (pObsNodesNow[i])
	    {
		numbersOfReallyObsNodes.push_back(myNumAllObsNodes[i]);
		numReallyObsNodes++;
	    }
	}
#if 0
	const CNodeType ** AllNodeTypesFromModel= new const CNodeType*[nnodes];
	for (i=0; i<nnodes; i++)
	{
	    AllNodeTypesFromModel[i] = grModel->GetNodeType(i);
	}
	for (i=0; i<nObsNodesFromEv; i++)
	{
	    //Test the values which are keep in Evidence
	    CNodeType nt = *AllNodeTypesFromModel[myNumAllObsNodes[i]];
	    int IsDiscreteNode = nt.IsDiscrete();
	    if(IsDiscreteNode)
	    {
		int valFromEv = (ev[myOffset[i]].GetInt());
		if(!(Values[i].GetInt() == valFromEv))
		{
		    ret=TRS_FAIL;
		    break;
		}
	    }
	    else
	    {
		;
		for (j=0; j<3; j++)
		{
		    if(!((ev[myOffset[i]+j]).GetFlt() == Values[i+j].GetFlt()))
		    {
			ret=TRS_FAIL;
			break;
		    }
		}
	    }
	}
	delete []AllNodeTypesFromModel;
#endif
    }
    else
    {
	ret = TRS_FAIL;
    }
    //Toggle some Node
    int someNumber = (int)(rand()*nObsNodesFromEv/RAND_MAX);
    int *someOfNodes = new int[someNumber];
    intVector residuaryNums = intVector(myNumAllObsNodes,
	myNumAllObsNodes+nObsNodesFromEv);
    num=0;
    for(i=0; i<someNumber;i++)
    {
	num = (int)(rand()%(nObsNodes-i));
	someOfNodes[i] = residuaryNums[num];
	residuaryNums.erase(residuaryNums.begin()+num);
    }
    residuaryNums.clear();
    pMyEvid->ToggleNodeState(someNumber, someOfNodes);
    const int *pObsNodesAfterToggle = pMyEvid->GetObsNodesFlags();
    for (i=0; i<nObsNodesFromEv; i++)
    {
	//Test the ToggleNode method...
	if(pObsNodesAfterToggle[i])
	{
	    for(j=0; j<someNumber;j++)
	    {
		if(myNumAllObsNodes[i]==someOfNodes[j])
		{
		    ret=TRS_FAIL;
		    break;
		}
	    }
	}
    }

    delete grModel;
    delete pMyEvid;
    delete []modelNodeType;
    delete []NodeAssociat;
    delete []someOfNodes;
    int obsNodes_memory_flag = trsGuardCheck( obsNodes );
    if( obsNodes_memory_flag)
    {
	return trsResult( TRS_FAIL, "Dirty memory");
    }
    trsGuardFree( obsNodes );
    return trsResult( ret, ret == TRS_OK ? "No errors" : "Bad test on Values");
}
Exemplo n.º 30
0
CBNet* CreateSixNodeEx(void)
{
    int i;
    const int numOfNds = 6;
    int numOfNbrs[numOfNds] = { 1, 1, 1, 1, 1, 5 };

    int nbrs0[] = { 5 };
    int nbrs1[] = { 5 };
    int nbrs2[] = { 5 };
    int nbrs3[] = { 5 };
    int nbrs4[] = { 5 };
    int nbrs5[] = { 0, 1, 2, 3, 4 };

    ENeighborType nbrsTypes0[] = { ntChild };
    ENeighborType nbrsTypes1[] = { ntChild };
    ENeighborType nbrsTypes2[] = { ntChild };
    ENeighborType nbrsTypes3[] = { ntChild };
    ENeighborType nbrsTypes4[] = { ntChild };
    ENeighborType nbrsTypes5[] = { ntParent, ntParent, ntParent, ntParent,
                                   ntParent
                                 };

    int *nbrs[] = { nbrs0, nbrs1, nbrs2, nbrs3, nbrs4, nbrs5};
    ENeighborType *nbrsTypes[] = { nbrsTypes0, nbrsTypes1, nbrsTypes2,
                                   nbrsTypes3, nbrsTypes4, nbrsTypes5
                                 };

    CGraph* pGraph = CGraph::Create(numOfNds, numOfNbrs, nbrs, nbrsTypes);

    CModelDomain* pMD;

    nodeTypeVector variableTypes;

    int nVariableTypes = 2;
    variableTypes.resize(nVariableTypes);

    variableTypes[0].SetType(0, 1);
//  variableTypes[0].SetType(1, 4);
    variableTypes[1].SetType(1, 2);

    intVector variableAssociation;
    int nnodes = pGraph->GetNumberOfNodes();
    variableAssociation.assign(nnodes, 1);
    variableAssociation[0] = 0;
    variableAssociation[1] = 0;
    variableAssociation[2] = 0;
    variableAssociation[3] = 0;
    variableAssociation[4] = 0;
    variableAssociation[5] = 1;

    pMD = CModelDomain::Create(variableTypes, variableAssociation);

    CBNet *pBNet = CBNet::Create(pGraph, pMD);

    pBNet->AllocFactors();

    int nnodes0 = 1;
    int domain0[] = { 0 };
    float mean0 = 0.0f;
    float cov0 = 1.0f;

    CGaussianCPD *pCPD0 = CGaussianCPD::Create(domain0, nnodes0, pMD);
    pCPD0->AllocDistribution(&mean0, &cov0, 1.0f, NULL);
    pBNet->AttachFactor(pCPD0);

    int nnodes1 = 1;
    int domain1[] = { 1 };
    float mean1 = 2.0f;
    float cov1 = 1.0f;

    CGaussianCPD *pCPD1 = CGaussianCPD::Create(domain1, nnodes1, pMD);
    pCPD1->AllocDistribution(&mean1, &cov1, 1.0f, NULL);
    pBNet->AttachFactor(pCPD1);

    int nnodes2 = 1;
    int domain2[] = { 2 };
    float mean2 = 1.0f;
    float cov2 = 1.0f;

    CGaussianCPD *pCPD2 = CGaussianCPD::Create(domain2, nnodes2, pMD);
    pCPD2->AllocDistribution(&mean2, &cov2, 1.0f, NULL);
    pBNet->AttachFactor(pCPD2);

    int nnodes3 = 1;
    int domain3[] = { 3 };
    float mean3 = 5.0f;
    float cov3 = 1.0f;

    CGaussianCPD *pCPD3 = CGaussianCPD::Create(domain3, nnodes3, pMD);
    pCPD3->AllocDistribution(&mean3, &cov3, 1.0f, NULL);
    pBNet->AttachFactor(pCPD3);

    int nnodes4 = 1;
    int domain4[] = { 4 };
    float mean4 = 5.0f;
    float cov4 = 1.0f;

    CGaussianCPD *pCPD4 = CGaussianCPD::Create(domain4, nnodes4, pMD);
    pCPD4->AllocDistribution(&mean4, &cov4, 1.0f, NULL);
    pBNet->AttachFactor(pCPD4);

    int nnodes5 = 6;
    int domain5[] = { 0, 1, 2, 3, 4, 5 };

    CSoftMaxCPD *pCPD5 = CSoftMaxCPD::Create(domain5, nnodes5, pMD);

    int parInd0[] = { 0 };

    float *weight30;
    int SizeOfWeight = (variableTypes[1].GetNodeSize()) * (numOfNds - 1);
    weight30 = GenerateFloatArray((variableTypes[1].GetNodeSize()) *
                                  (numOfNds - 1), 1.0f, 5.0f);

#ifdef SM_TEST
    printf("\nweight30\n");
    for (i = 0; i < SizeOfWeight; i++)
    {
        printf("%f\t", weight30[i]);
    }
#endif

    float *offset30;
    int SizeOfOffset = variableTypes[1].GetNodeSize();
    offset30 = GenerateFloatArray(variableTypes[1].GetNodeSize(), 1.0f, 5.0f);

#ifdef SM_TEST
    printf("\noffset30\n");
    for (i = 0; i < SizeOfOffset; i++)
    {
        printf("%f\t", offset30[i] );
    }
    printf("\n\n");
#endif

    pCPD5->AllocDistribution(weight30, offset30, parInd0);

    pBNet->AttachFactor(pCPD5);

    return pBNet;
}