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

    pContext->Put(pModel->GetGraph(), "Graph");
    TraverseSubobjectOfGrModel(pModel, pContext);
}
Exemple #2
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;
}
Exemple #3
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;
}
Exemple #4
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;
}
Exemple #5
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;
}
Exemple #6
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");
}
Exemple #7
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");
    
    
}
Exemple #8
0
int main()
{
    int i, j;
    CBNet* pAlarm = CreateAlarmBNet();
    const int nnodes = pAlarm->GetNumberOfNodes();
    pAlarm->GetGraph()->Dump();
    
    CGraph *graph = CGraph::Create( nnodes, NULL, NULL, NULL );	
    for(i=0; i<nnodes-1; i++)
    {
	graph->AddEdge(i, i+1, 1);
    }
    
    
    CBNet *bnet = CBNet::CreateWithRandomMatrices( graph, pAlarm->GetModelDomain() );
    
      
    int nEv = 50;
  
    
    CEvidence **pEvidences = new CEvidence *[nEv];
    int dataSize = nnodes;//summ all sizes
    
    int* obs_nodes = new int[nnodes];
    for(i=0; i<nnodes; i++)obs_nodes[i] = i;
    
    //read data from file alarm.dat to evidences
    FILE * fp = fopen("../../examples/Data/alarm.dat", "r");
    
    if( !fp && !(fp = fopen("../../c_pgmtk/examples/Data/alarm.dat", "r")) )
    {
        std::cout<<"can't open cases file"<<std::endl;
        exit(1);
    }
    
    valueVector input_data;
    input_data.resize(dataSize);
    
    for(i = 0 ; i < nEv; i++)
    {
        for (j = 0; j < dataSize ; j++)
        {
            int val;
            fscanf(fp, "%d,", &val);
            input_data[j].SetInt(val);
        }
        pEvidences[i] = CEvidence::Create(pAlarm, nnodes, obs_nodes, input_data);
    }
    
    fclose(fp);
    intVector vA;
    intVector vD;
    CMlStaticStructLearn *pLearn = CMlStaticStructLearnHC::Create(bnet, itStructLearnML, 
	StructLearnHC, BIC, 5, vA, vD, 1);
    
    pLearn->SetData(nEv, pEvidences);
    pLearn->Learn();
    //	pLearn->CreateResultBNet(const_cast<CDAG*>(pLearn->GetResultDAG()));
    //	const CBNet* pBNet = pLearn->GetResultBNet();
    ///////////////////////////////////////////////////////////////////////////////
    const CDAG* pDAG = pLearn->GetResultDAG();
    CDAG* iDAG = CDAG::Create(*(pAlarm->GetGraph()));
    int diff = iDAG->SymmetricDifference(pDAG);
    pDAG->Dump();
    delete pLearn;
    delete iDAG;
    delete[] obs_nodes;
    delete pAlarm;
    delete bnet;
   
    for( i = 0; i < nEv; i++)
    {
	delete pEvidences[i];
    }
    return 1;
}