コード例 #1
0
ファイル: AGibbsInfEngine.cpp プロジェクト: billryan/OpenPNL
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;

}
コード例 #2
0
ファイル: SEEvidence.cpp プロジェクト: JacobCWard/PyPNL
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");
}
コード例 #3
0
ファイル: AGibbsInfEngine.cpp プロジェクト: billryan/OpenPNL
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;

}
コード例 #4
0
ファイル: AGibbsInfEngine.cpp プロジェクト: billryan/OpenPNL
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;

    ////////////////////////////////////////////////////////////////////////////////////////
}
コード例 #5
0
ファイル: AMarginalize.cpp プロジェクト: JacobCWard/PyPNL
int testMarginalize()
{
    int ret = TRS_OK;
    
    const int nnodes = 4;
    const int numnt = 2;
    
    intVector nodeAssociation = intVector( nnodes );
    
    nodeTypeVector nodeTypes;
    nodeTypes.assign( numnt, CNodeType() );
    
    nodeTypes[0].SetType(1, 2);
    nodeTypes[1].SetType(1, 3);
    
    nodeAssociation[0] = 0;
    nodeAssociation[1] = 0;
    nodeAssociation[2] = 0;
    nodeAssociation[3] = 1;
    
    CModelDomain* pMD = CModelDomain::Create( nodeTypes, nodeAssociation );
    
    
    int *domain = (int*)trsGuardcAlloc( nnodes, sizeof(int) );
    domain[0]=0; domain[1]=1; domain[2]=2; domain[3]=3;
    
    float* data = (float*)trsGuardcAlloc( 24, sizeof(float) );
    for (int i0=0; i0<24; data[i0]=i0*0.01f, i0++){};
    
    EMatrixType mType=matTable;
    
    CTabularPotential *pxParam1=CTabularPotential::Create( domain, nnodes, pMD); 
    
    pxParam1->AllocMatrix(data, mType);
    pxParam1->Dump();
    
    int domSize=2;
    int *pSmallDom = (int*)trsGuardcAlloc( domSize, sizeof(int) );
    pSmallDom[0] = 1;	pSmallDom[1] = 3;
    
    CFactor *pxParam2=pxParam1->Marginalize(pSmallDom, domSize, 0);
    //	CFactor *pxParam2=pxParam1->Marginalize(pSmallDom, 0, 0);	
    
    const CNumericDenseMatrix<float> *pxMatrix = static_cast<
        CNumericDenseMatrix<float>*>(pxParam2->GetMatrix(mType));
    
    const float* dmatrix1;
    //	const float* dmatrix1 = (const float*)trsGuardcAlloc( 1, sizeof(float) );
    int n;
    pxMatrix->GetRawData(&n, &dmatrix1);
    
    float *testdata1=new float[6];
    testdata1[0]=0.30f; testdata1[1]=0.34f; testdata1[2]=0.38f;
    testdata1[3]=0.54f; testdata1[4]=0.58f; testdata1[5]=0.62f;
    
    for(int i1 = 0; i1 < n; i1++)
    {	// Test the values...
        //printf("%d   %f %f", i1, dmatrix1[i1], testdata1[i1] );
        if(fabs(testdata1[i1] - dmatrix1[i1]) > eps)
        {
            return trsResult(TRS_FAIL, "data doesn't agree at max=0");
        }
    }
    
    CFactor *pxParam3=pxParam1->Marginalize(pSmallDom, domSize, 1);
    
    const CNumericDenseMatrix<float> *pxMatrix1 = static_cast<
        CNumericDenseMatrix<float>*>(pxParam3->GetMatrix(mType));
    
    const float *dmatrix2;
    pxMatrix1->GetRawData(&n, &dmatrix2);
    float *testdata2 = new float[6];
    testdata2[0]=0.15f;
    testdata2[1]=0.16f; testdata2[2]=0.17f;
    testdata2[3]=0.21f; testdata2[4]=0.22f; testdata2[5]=0.23f;
    
    for(int i2 = 0; i2 < 6; i2++)
    {	// Test the values...
        //	printf("%d   %f %f", i2, dmatrix2[i2], testdata2[i2]);
        if( fabs(dmatrix2[i2] - testdata2[i2]) > eps)
        {
            return trsResult(TRS_FAIL, "data doesn't agree at max=1");
        }
    }

    //we can check some methods of Tabular
    CTabularPotential* pUniPot = 
        CTabularPotential::CreateUnitFunctionDistribution( domain, nnodes,
        pMD, 1 );
    CTabularPotential* pCopyUniPot = static_cast<CTabularPotential*>(
        pUniPot->CloneWithSharedMatrices());
    CTabularPotential* pNormUniPot = static_cast<CTabularPotential*>(
        pUniPot->GetNormalized());
    pUniPot->Dump();
    pUniPot->ConvertToDense();
    pUniPot->ConvertToSparse();
    (*pCopyUniPot) = (*pCopyUniPot);
    pxParam1->AllocMatrix(data, matTable);
    intVector indices;
    indices.assign(4,0);
    pxParam1->GetMatrix(matTable)->SetElementByIndexes(-1.0f,&indices.front());
    //we've just damaged the potential
    std::string s;
    if( pxParam1->IsValid(&s) )
    {
        ret = TRS_FAIL;
    }
    else
    {
        std::cout<<s<<std::endl;
    }
    intVector pos;
    floatVector vals;
    intVector offsets;
    pUniPot->GetMultipliedDelta(&pos, &vals, &offsets);

    delete pNormUniPot;
    delete pCopyUniPot;
    delete pUniPot;
    
    delete pxParam1; 
    delete pxParam2; 
    delete pxParam3;
    delete testdata1; 
    delete testdata2;
    delete pMD;
    
    int data_memory_flag = trsGuardCheck( data );
    int domain_memory_flag = trsGuardCheck( domain );
    int Smalldomain_memory_flag = trsGuardCheck( pSmallDom );
    
    trsGuardFree( data );
    trsGuardFree( domain );
    trsGuardFree( pSmallDom );
    
    if( data_memory_flag || domain_memory_flag || Smalldomain_memory_flag )
    {
        return trsResult( TRS_FAIL, "Dirty memory");
    }
    
    return trsResult( ret, ret == TRS_OK ? "No errors" : "Marginalize FAILED");
}
コード例 #6
0
ファイル: AGibbsInfEngine.cpp プロジェクト: billryan/OpenPNL
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;
}
コード例 #7
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");
}
コード例 #8
0
int testShrinkObservedNodes()
{
    int i/*,j*/;
    int ret = TRS_OK;
    /*prepare to read the values from console*/
    EDistributionType dt;
    int disType = -1;
    EFactorType pt;
    int paramType = -1;
    /*read int disType corresponding DistributionType*/
    while((disType<0)||(disType>0))/*now we have only Tabulars&Gaussian*/
    {
	trsiRead( &disType, "0", "DistributionType");
    }
    /*read int paramType corresponding FactorType*/
    while((paramType<0)||(paramType>2))
    {
	trsiRead( &paramType, "0", "FactorType");
    }
    dt = EDistributionType(disType);
    pt = EFactorType(paramType);
    int numberOfNodes = 0;
    /*read number of nodes in Factor domain*/
    while(numberOfNodes<=0)
    {
	trsiRead( &numberOfNodes, "1", "Number of Nodes in domain");
    }
    int numNodeTypes = 0;
    /*read number of node types in model*/
    while(numNodeTypes<=0)
    {
	trsiRead( &numNodeTypes, "1", "Number of node types in Domain");
    }
    //int seed1 = pnlTestRandSeed()/*%100000*/;
    /*create string to display the value*/
    /*	char *value = new char[20];
    value = _itoa(seed1, value, 10);
    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);
    int *domain = (int *)trsGuardcAlloc(numberOfNodes, sizeof(int));
    CNodeType * allNodeTypes = (CNodeType*)trsGuardcAlloc(numNodeTypes,
    sizeof(CNodeType));
    //To generate the NodeTypes we use rand()% and creates only Tabular now
    for(i=0; i<numNodeTypes; i++)
    {
    allNodeTypes[i] = CNodeType(1, 1+rand()%(numNodeTypes+3));
    }
    */	
    
    /*load data for parameter::ShrinkObservedNodes from console*/
    intVector domain;
    domain.assign( numberOfNodes, 0 );
    nodeTypeVector allNodeTypes;
    allNodeTypes.assign( numNodeTypes, CNodeType() );
    /*read node types*/
    for(i=0; i < numNodeTypes; i++)
    {
	int IsDiscrete = -1;
	int NodeSize = -1;
	while((IsDiscrete<0)||(IsDiscrete>1))
	    /*now we have tabular & Gaussian nodes!! */
	    trsiRead(&IsDiscrete, "1", "Is the node discrete?");
	while(NodeSize<0)
	    trsiRead(&NodeSize, "2", "NodeSize of node");
	allNodeTypes[i] = CNodeType( IsDiscrete != 0, NodeSize );
    }
    const CNodeType **nodeTypesOfDomain = (const CNodeType**)
	trsGuardcAlloc(numberOfNodes, sizeof(CNodeType*));
    int numData = 1;
    int *Ranges = (int*)trsGuardcAlloc(numberOfNodes, sizeof(int));
    /*associate nodes to node types*/
    for(i=0; i<numberOfNodes; i++)
    {
	domain[i] = i;
	int nodeAssociationToNodeType = -1;
	while((nodeAssociationToNodeType<0)||(nodeAssociationToNodeType>=
	    numNodeTypes))
	    trsiRead(&nodeAssociationToNodeType, "0", 
	    "node i has type nodeAssociationToNodeType");
	nodeTypesOfDomain[i] = &allNodeTypes[nodeAssociationToNodeType];
	//	nodeTypesOfDomain[i] = &allNodeTypes[rand()%numNodeTypes];
	Ranges[i] = nodeTypesOfDomain[i]->GetNodeSize();
	numData=numData*Ranges[i];
    }
    
    CModelDomain* pMD = CModelDomain::Create( allNodeTypes, domain );
    
    /*create factor according all information*/
    CFactor *pMyParam = NULL;
    float *data = (float *)trsGuardcAlloc(numData, sizeof(float));
    char *stringVal;/* = (char*)trsGuardcAlloc(50, sizeof(char));*/
    double val=0;
    /*read the values from console*/
    if(pt == ftPotential)
    {
	pMyParam = CTabularPotential::Create( &domain.front(), numberOfNodes, pMD );
	/*here we can create data by multiply on 0.1 - numbers are nonnormalized*/
	for(i=0; i<numData; i++)
	{
	    val = 0.1*i;
	    stringVal = trsDouble(val);
	    trsdRead(&val, stringVal, "value of i's data position");
	    data[i] = (float)val;
	    //data[i] = (float)rand()/1000;
	}
    }
    else
    {
    /*we can only read data from console - it must be normalized!!
	(according their dimensions) - or we can normalize it by function!*/
	if(pt == ftCPD)
	    pMyParam = CTabularCPD::Create( &domain.front(), numberOfNodes, pMD );
	for(i=0; i<numData; i++)
	{
	    val = -1;
	    while((val<0)||(val>1))
	    {
		trsdRead(&val, "-1", "value of (2*i)'s data position");
	    }
	    data[i] = (float)val;
	}
    }
    //trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "data for Factor = %d\n", data[i]);
    pMyParam->AllocMatrix(data,matTable);
    int nObsNodes = 0;	/*rand()%numberOfNodes;*/
    while((nObsNodes<=0)||(nObsNodes>numberOfNodes))
    {
	trsiRead(&nObsNodes, "1", "Number of Observed Nodes");
    }
    intVector myHelpForEvidence = intVector(domain.begin(), domain.end() );
    int *ObsNodes = (int *)trsGuardcAlloc(nObsNodes, sizeof(int));
    valueVector TabularValues;
    TabularValues.assign( nObsNodes, (Value)0 );
    char *strVal;
    for(i=0; i<nObsNodes; i++)
    {
	//fixme - we need to have noncopy only different ObsNodes
	/*		j = rand()%(numberOfNodes-i);*/
	int numberOfObsNode = -1;
	strVal = trsInt(i);
        intVector::iterator j = std::find( myHelpForEvidence.begin(), myHelpForEvidence.end(), numberOfObsNode );
	while((numberOfObsNode<0)||(numberOfObsNode>numberOfNodes)||
	    (j==myHelpForEvidence.end()))
	{
	    trsiRead(&numberOfObsNode, strVal,"Number of i's observed node");
	    j = std::find(myHelpForEvidence.begin(), myHelpForEvidence.end(),
		numberOfObsNode);
	}
	//ObsNodes[i] = myHelpForEvidence[j];
	myHelpForEvidence.erase( j );
	ObsNodes[i] = numberOfObsNode;
	int valueOfNode = -1;
	int maxValue = (*nodeTypesOfDomain[ObsNodes[i]]).GetNodeSize();
	while((valueOfNode<0)||(valueOfNode>=maxValue))
	{
	    trsiRead(&valueOfNode,"0","this is i's observed node value");
	}
	TabularValues[i].SetInt(valueOfNode);
	/*rand()%((*nodeTypesOfDomain[ObsNodes[i]]).pgmGetNodeSize());*/
    }
    CEvidence* pEvidence = CEvidence::Create( pMD, nObsNodes, ObsNodes, TabularValues );
    myHelpForEvidence.clear();
    CNodeType *ObservedNodeType = (CNodeType*)trsGuardcAlloc(1, 
	sizeof(CNodeType));
    *ObservedNodeType = CNodeType(1,1);
    CPotential *myTakedInFactor = static_cast<CPotential*>(pMyParam)->ShrinkObservedNodes(pEvidence);
    const int *myfactorDomain;
    int factorDomSize ;
    myTakedInFactor->GetDomain(&factorDomSize, &myfactorDomain);
#if 0
    CNumericDenseMatrix<float> *mySmallMatrix = static_cast<
        CNumericDenseMatrix<float>*>(myTakedInFactor->GetMatrix(matTable));
    int n;
    const float* mySmallData;
    mySmallMatrix->GetRawData(&n, &mySmallData);
    int nDims; // = mySmallMatrix->GetNumberDims();
    const int * mySmallRanges;
    mySmallMatrix->GetRanges(&nDims, &mySmallRanges);
    
    if(nDims!=numberOfNodes)
    {
	ret = TRS_FAIL;
	trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "nDims = %d\n", nDims);
    }
    else
    {
	int numSmallData = 1;
	for(i=0; i<nDims; i++)
	{
	    numSmallData = numSmallData*mySmallRanges[i];
	    trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "Range[%d] = %d\n", i, 
		mySmallRanges[i]);
	}
	for(i=0; i<numSmallData; i++)
	{	
	    trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "mySmallData[%d] = %f ",
		i, mySmallData[i]);
	}
    }
#endif
    //getchar();
    delete(myTakedInFactor);
    delete (pMyParam);
    delete pMD;
    //test gaussian parameter
    nodeTypeVector nTypes;
    nTypes.assign( 2, CNodeType() );
    nTypes[0] = CNodeType( 0, 2 );
    nTypes[1] = CNodeType( 0,1 );
    intVector domn = intVector(3,0);
    domn[1] = 1;
    domn[2] = 1;
    
    CModelDomain* pMD1 = CModelDomain::Create( nTypes, domn );
    
    domn[2] = 2;
    
    CPotential *BigFactor = CGaussianPotential::CreateUnitFunctionDistribution( 
	&domn.front(), domn.size(), pMD1,0 );
    float mean[] = { 1.0f, 3.2f};
    CPotential *SmallDelta = CGaussianPotential::CreateDeltaFunction( &domn.front(), 1, pMD1, mean, 1 );
    domn.resize( 2 );
    domn[0] = 1;
    domn[1] = 2;
    CPotential *SmallFunct = CGaussianPotential::Create( &domn.front(),
	domn.size(),  pMD1);
    float datH[] = { 1.1f, 2.2f, 3.3f };
    float datK[] = { 1.2f, 2.3f, 2.3f, 3.4f, 5.6f, 6.7f, 3.4f, 6.7f, 9.0f };
    SmallFunct->AllocMatrix( datH, matH );
    SmallFunct->AllocMatrix( datK, matK );
    static_cast<CGaussianPotential*>(SmallFunct)->SetCoefficient( 0.2f, 1 );
    CPotential* multFact = BigFactor->Multiply( SmallDelta );
    CPotential* nextMultFact = multFact->Multiply( SmallFunct );
    domn[0] = 0;
    domn[1] = 1;
    CPotential *marginalized = static_cast<CPotential*>(nextMultFact->Marginalize( &domn.front(), domn.size() ));
    int isSpecific = marginalized->IsDistributionSpecific();
    if( isSpecific )
    {
	trsWrite(TW_CON|TW_RUN|TW_DEBUG|TW_LST, "\nGaussian Distribution is specific");
    }
    delete BigFactor;
    delete SmallFunct;
    delete SmallDelta;
    delete pMD1;
    
    int ranges_memory_flag = trsGuardCheck(Ranges);
    int data_memory_flag = trsGuardCheck(data);
    int nodeTypesOfDomain_mem_b = trsGuardCheck(nodeTypesOfDomain);
    int ObsNodes_mem_b = trsGuardCheck(ObsNodes);
    int ObsNodeType_mem_b = trsGuardCheck(ObservedNodeType);
    if(((ranges_memory_flag)||(data_memory_flag)||
	(nodeTypesOfDomain_mem_b)||
	(ObsNodes_mem_b)||(ObsNodeType_mem_b)))
    {
	ret = TRS_FAIL;
	return trsResult( ret, ret == TRS_OK ? "No errors" : 
	"Bad test on ShrinkObservedNodes Method - memory");
    }
    else
    {
	trsGuardFree(ObservedNodeType);
	trsGuardFree(ObsNodes);
	trsGuardFree(nodeTypesOfDomain);
	trsGuardFree(data);
	trsGuardFree(Ranges);
    }			
    return trsResult( ret, ret == TRS_OK ? "No errors" : 
    "Bad test on ShrinkObservedNodes Method");
}
コード例 #9
0
ファイル: ARandomFactors.cpp プロジェクト: JacobCWard/PyPNL
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");
}
コード例 #10
0
ファイル: SEGaussian.cpp プロジェクト: JacobCWard/PyPNL
int testSEGaussian()
{
    PNL_USING
   
    int i;
    int ret = TRS_OK;
    float eps = 1e-4f;
    //create Gaussian distribution (inside the potential) and try to multiply
    //is by Delta
    
    //create Model Domain
    int nSimNodes = 3;
    CNodeType simNT = CNodeType(0, 2);
    CModelDomain* pSimDomain = CModelDomain::Create( nSimNodes, simNT );
    //create 2 potentials
    intVector dom;
    dom.assign(3,0);
    dom[1] = 1;
    dom[2] = 2;
    floatVector mean;
    mean.assign(6, 0);
    floatVector cov;
    cov.assign(36, 0.1f);
    for( i = 0; i < 6; i++ )
    {
        mean[i] = 1.1f*i;
        cov[i*7] = 2.0f;
    }

	CGaussianPotential* pPot = CGaussianPotential::Create( dom, pSimDomain, 1,
        mean, cov, 1.0f );

    //create gaussian CPD with gaussian parent
    const pConstNodeTypeVector* pTypes = pPot->GetArgType();
    //create data weigth
    floatVector weights1;
    weights1.assign(4, 1.0f);
    floatVector weights2;
    weights2.assign(4, 2.0f);
    const float* weights[] = { &weights1.front(), &weights2.front()};
    CGaussianDistribFun* pGauCPD = CGaussianDistribFun::CreateInMomentForm( 0,
        3, &pTypes->front(), &mean.front(), &cov.front(), weights );

    pPot->Dump();
    pPot->Normalize();
    //try to get multiplied delta
    intVector pos;
    floatVector valuesDelta;
    intVector offsets;
    pPot->GetMultipliedDelta(&pos, &valuesDelta, &offsets);
    if( pos.size() != 0 )
    {
        ret = TRS_FAIL;
    }


    //enter evidence to the pot and after that expand and compare results
    //create evidence object
/*    valueVector obsVals;
    obsVals.assign(6,Value(0) );
    obsVals[0].SetFlt(1.0f);
    obsVals[1].SetFlt(1.5f);
    obsVals[2].SetFlt(2.0f);
    obsVals[3].SetFlt(2.5f);
    obsVals[4].SetFlt(3.0f);
    obsVals[5].SetFlt(3.5f);
    CEvidence* pEvid = CEvidence::Create( pSimDomain, 3, &dom.front(), obsVals );
    CPotential* pShrPot = pPot->ShrinkObservedNodes( pEvid );*/
    
    
    CGaussianPotential* pCanonicalPot = CGaussianPotential::Create( dom,
        pSimDomain, 0, mean, cov, 1.0f );
    CMatrix<float>* matrK = pCanonicalPot->GetMatrix(matK);
    intVector multIndex;
    multIndex.assign(2,5);
    matrK->SetElementByIndexes( 3.0f, &multIndex.front() );
    CMatrix<float>* matrH = pCanonicalPot->GetMatrix(matH);
    multIndex[0] = 0;
    matrH->SetElementByIndexes(0.0f, &multIndex.front());
    pPot->ConvertToSparse();
    pPot->ConvertToDense();
    //create other Gaussian potential for division
    i = 1;
    floatVector meanSmall;
    meanSmall.assign(2, 1.0f);
    floatVector covSmall;
    covSmall.assign(4, 0.0f);
    covSmall[0] = 1.0f;
    covSmall[3] = 1.0f;
    CGaussianPotential* pSmallPot = CGaussianPotential::Create( &i, 1,
        pSimDomain, 1, &meanSmall.front(), &covSmall.front(), 1.0f );
    //divide by distribution in moment form
    (*pCanonicalPot) /= (*pSmallPot);

    //create big unit function distribution and marginalize it
    CGaussianPotential* pBigUnitPot = 
        CGaussianPotential::CreateUnitFunctionDistribution(dom, pSimDomain, 1);
	CGaussianPotential* pCloneBigUniPot = static_cast<CGaussianPotential*>(
		pBigUnitPot->CloneWithSharedMatrices());
	if( !pCloneBigUniPot->IsFactorsDistribFunEqual(pBigUnitPot, eps) )
	{
		ret = TRS_FAIL;
	}
    CPotential* pMargUniPot = pBigUnitPot->Marginalize(&dom.front(), 1, 0);
    (*pBigUnitPot) /= (*pSmallPot);
    (*pBigUnitPot) *= (*pSmallPot);
    
    //check if there are some problems
    static_cast<CGaussianDistribFun*>(pBigUnitPot->GetDistribFun())->CheckCanonialFormValidity();

    if( pBigUnitPot->IsDistributionSpecific() != 1 )
    {
        ret = TRS_FAIL;
    }

    (*pBigUnitPot) /= (*pCanonicalPot);
    (*pBigUnitPot) *= (*pCanonicalPot);

    static_cast<CGaussianDistribFun*>(pBigUnitPot->GetDistribFun())->CheckCanonialFormValidity();

    if( pBigUnitPot->IsDistributionSpecific() != 1 )
    {
        ret = TRS_FAIL;
    }

    static_cast<CGaussianDistribFun*>(pSmallPot->GetDistribFun())->
        UpdateCanonicalForm();
    (*pPot) /= (*pSmallPot);
    pSmallPot->SetCoefficient(0.0f,1);
    pSmallPot->Dump();

    //create canonical potential without coefficient
    i = 0;

    CDistribFun* pCopyPotDistr = pPot->GetDistribFun()->ConvertCPDDistribFunToPot();
    CGaussianPotential* pCanSmallPot = CGaussianPotential::Create( &i, 1,
        pSimDomain, 0, &meanSmall.front(), &covSmall.front(), 1.0f );
    CGaussianPotential* pCanPotCopy = 
        static_cast<CGaussianPotential*>(pCanSmallPot->Clone());
    CGaussianPotential* pCanPotCopy1 = 
        static_cast<CGaussianPotential*>(pCanPotCopy->CloneWithSharedMatrices());

    (*pPot) /= (*pCanSmallPot);
    (*pPot) *= (*pCanSmallPot);
    //can compare results, if we want
    CDistribFun* pMultDivRes = pPot->GetDistribFun();
    float diff = 0;
    if( !pMultDivRes->IsEqual(pCopyPotDistr, eps,1, &diff) )
    {
        ret = TRS_FAIL;
        std::cout<<"the diff is "<<diff<<std::endl;
    }
    delete pCopyPotDistr;
    
    //create delta distribution
    floatVector deltaMean;
    deltaMean.assign( 2, 1.5f );
    i = 0;
    CGaussianPotential* pDeltaPot = CGaussianPotential::CreateDeltaFunction( 
        &i, 1, pSimDomain, &deltaMean.front(), 1 );
    
    CGaussianDistribFun* pDeltaDistr = static_cast<CGaussianDistribFun*>(
        pDeltaPot->GetDistribFun());
    pDeltaDistr->CheckMomentFormValidity();
    pDeltaDistr->CheckCanonialFormValidity();
    pDeltaPot->Dump();


    //multiply some potential by delta
    (*pCanSmallPot) *= (*pDeltaPot);

    (*pPot) *= (*pDeltaPot);
    //(*pPot) *= (*pDeltaPot);
    
    pPot->GetMultipliedDelta(&pos, &valuesDelta, &offsets);
    (*pCanonicalPot) *= (*pDeltaPot);
    //marginalize this distribFun multiplied by delta
    intVector margDims;
    margDims.assign(2,1);
    margDims[1] = 2;
    CPotential* pMargPot = pPot->Marginalize( &margDims.front(), 2 );
    i = 0;
    CPotential* pSmallMargPot = pPot->Marginalize(&i, 1);
    //marginalize in canonical form
    CPotential* pMargCanPot = pCanonicalPot->Marginalize( &margDims.front(), 2 );
    CPotential* pSmallCanPot = pCanonicalPot->Marginalize(&i,1);
    
    //create unit function distribution in canonical form
    i = 0;
    CGaussianPotential* pUnitPot = 
        CGaussianPotential::CreateUnitFunctionDistribution( &i, 1, pSimDomain,
        1);
    pUnitPot->Dump();
    CGaussianDistribFun* pUnitDistr = static_cast<CGaussianDistribFun*>(
        pUnitPot->GetDistribFun());
    pUnitDistr->CheckCanonialFormValidity();
    pUnitDistr->CheckMomentFormValidity();

    (*pPot) *= (*pUnitPot);

    if( pUnitPot->IsFactorsDistribFunEqual(pBigUnitPot, eps, 1) )
    {
        ret = TRS_FAIL;
    }

    deltaMean.resize(6);
    deltaMean[2] = 2.5f;
    deltaMean[3] = 2.5f;
    deltaMean[4] = 3.5f;
    deltaMean[5] = 3.5f;
    CGaussianPotential* pBigDeltaPot = CGaussianPotential::CreateDeltaFunction( 
        dom, pSimDomain, deltaMean, 1 );
    CGaussianPotential* pCloneBigDeltaPot = static_cast<CGaussianPotential*>(
		pBigDeltaPot->CloneWithSharedMatrices());
    //we can shrink observed nodes in this potential
    valueVector vals;
    vals.resize(2);
    vals[0].SetFlt(1.5f);
    vals[1].SetFlt(1.5f);
    i = 0;
    CEvidence* pDeltaEvid = CEvidence::Create( pSimDomain, 1, &i,vals ); 
    CGaussianPotential* pShrGauDeltaPot = static_cast<CGaussianPotential*>(
        pBigDeltaPot->ShrinkObservedNodes( pDeltaEvid ));
    delete pDeltaEvid;
    pShrGauDeltaPot->Dump();
    delete pShrGauDeltaPot;
    CPotential* pSmallDeltaMarg = pBigDeltaPot->Marginalize( &dom.front(), 1, 0 );
    pSmallDeltaMarg->Normalize();
    pDeltaPot->Normalize();
    if( !pSmallDeltaMarg->IsFactorsDistribFunEqual( pDeltaPot, eps, 1 ) )
    {
        ret = TRS_FAIL;
    }
    //call operator = for delta distributions
    (*pSmallDeltaMarg) = (*pDeltaPot);

    //call operator = for canonical and delta distribution
    (*pCanPotCopy) = (*pUnitPot);

    //call operator = for canonical and unit distribution
    (*pCanPotCopy1) = (*pDeltaPot);

    (*pUnitPot) = (*pUnitPot);
    

    (*pPot) *= (*pBigDeltaPot);

    (*pCloneBigDeltaPot) *= (*pDeltaPot);

	if( !pCloneBigDeltaPot->IsFactorsDistribFunEqual(pBigDeltaPot, eps) )
	{
		ret = TRS_FAIL;
	}

    //we can create the matrix which will be almost delta distribution,
    //but covariance matrix will contain almost zeros
    floatVector almostDeltaCov;
    almostDeltaCov.assign(4, 0.0f);
    almostDeltaCov[0] = 0.00001f;
    almostDeltaCov[3] = 0.00001f;
    i = 0;
    CGaussianPotential* pAlmostDeltaPot = CGaussianPotential::Create( &i, 1, 
        pSimDomain, 1, &deltaMean.front(), &almostDeltaCov.front(), 1.0f );
    if( !pDeltaPot->IsFactorsDistribFunEqual( pAlmostDeltaPot, eps, 0 ) )
    {
        ret = TRS_FAIL;
    }
    if( !pAlmostDeltaPot->IsFactorsDistribFunEqual( pDeltaPot, eps, 0 ) )
    {
        ret = TRS_FAIL;
    }
	(*pCloneBigUniPot ) = ( *pPot );
	if( !(pCloneBigUniPot->IsFactorsDistribFunEqual(pPot, eps)) )
	{
		ret = TRS_FAIL;
	}
	(*pCloneBigDeltaPot) = (*pPot);
	if( !(pCloneBigDeltaPot->IsFactorsDistribFunEqual(pPot, eps))  )
	{
		ret = TRS_FAIL;
	}

    delete pCanPotCopy;
    delete pCanPotCopy1;
    delete pAlmostDeltaPot;
    delete pMargUniPot;
	delete pCloneBigUniPot;
    delete pBigUnitPot;
    delete pCanSmallPot;
    delete pDeltaPot; 
    delete pUnitPot;
	delete pCloneBigDeltaPot;
    delete pBigDeltaPot;
    delete pMargCanPot;
    delete pSmallCanPot;
    delete pMargPot;
    delete pSmallMargPot;
    delete pCanonicalPot;
    //delete pShrPot;
    //delete pEvid;
	delete pGauCPD;
    delete pPot;
    delete pSimDomain;
    return trsResult( ret, ret == TRS_OK ? "No errors" : 
                        "Bad test on SEGaussian");

}
コード例 #11
0
{
    int nodeSize;
    bool bDiscrete;
    int nodeState = 0;
    char ch;

    *pStr >> bDiscrete;
    *pStr >> ch;
    ASSERT(ch == ':');
    *pStr >> nodeSize;
    *pStr >> ch;
    if(ch == ':')
    {
	*pStr >> nodeState >> ch;
    }
    *pNodeType = CNodeType(bDiscrete, nodeSize, (EIDNodeState)nodeState);
    ASSERT(bLast || ch == ',');
}

void
CPersistNodeTypeVector::Save(CPNLBase *pObj, CContextSave *pContext)
{
    nodeTypeVector *paNodeType = static_cast<CCover<nodeTypeVector>*>(pObj)->GetPointer();
    pnlString buf;

    for(int i = 0; i < paNodeType->size(); ++i)
    {
	SaveNodeType(&buf, paNodeType[0][i], i >= paNodeType->size() - 1);
    }

    pContext->AddAttribute("NumberOfNodes", int(paNodeType->size()));
コード例 #12
0
ファイル: ANormalizeFactor.cpp プロジェクト: JacobCWard/PyPNL
int testNormalizePotential()
{
    int ret = TRS_OK;
    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);

    srand ((unsigned int)seed1);
    int a1 = 2+rand()%((int) 20);

    const int nnodes = a1;
    const int numnt = 2;
    intVector nodeAssociation;
    nodeAssociation.assign( nnodes, 0 );

    nodeTypeVector nodeTypes;
    nodeTypes.assign( numnt, CNodeType() );

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

    int * domain = (int*)trsGuardcAlloc( nnodes, sizeof(int) );
    int Nlength = 1;
    for (int i2=0; i2 < nnodes; i2++)
    {	int n = rand()%((int) numnt);
    nodeAssociation[i2] = n;
    domain[i2] = i2;
    Nlength = Nlength *(n+1);
    }
    CModelDomain* pMD = CModelDomain::Create( nodeTypes,nodeAssociation);

    float * data = (float*)trsGuardcAlloc( Nlength, sizeof(float) );
    for (int i0=0; i0 < Nlength; data[i0]=1, i0++){};

    EMatrixType mType=matTable;

    CTabularPotential *pxFactor = CTabularPotential::Create( domain, nnodes, pMD);

    pxFactor->AllocMatrix(data, mType);

    CPotential *pxFactor_res=pxFactor->GetNormalized() ;

    const CNumericDenseMatrix<float> *pxMatrix=static_cast<
	CNumericDenseMatrix<float>*>(pxFactor_res->GetMatrix(mType));

    const float* dmatrix1 = (const float*)trsGuardcAlloc( Nlength, sizeof(float) );
    int n;
    pxMatrix->GetRawData(&n, &dmatrix1);

    float *testdata = new float[Nlength];
    for (int k1=0; k1 < Nlength; testdata[k1]=1.0f/Nlength,  k1++);

    for(int j = 0; j < Nlength; j++)
    {	// Test the values...
	//printf("%3d   %4.2f   %4.2f\n", j,  dmatrix1[j], testdata[j] );
	if(fabs(testdata[j] - dmatrix1[j]) > eps )
	{
	    return trsResult(TRS_FAIL, "data doesn't agree at max=0");
	}
    }


    int data_memory_flag = trsGuardCheck( data);
    int domain_memory_flag = trsGuardCheck( domain );

    trsGuardFree( data );
    trsGuardFree( domain );

    if(data_memory_flag || domain_memory_flag )
    {
	return trsResult( TRS_FAIL, "Dirty memory");
    }
    delete [] testdata;
    delete pxFactor;
    delete pxFactor_res;
    delete pMD;

    return trsResult( ret, ret == TRS_OK ? "No errors" : "Normalize FAILED");
}
コード例 #13
0
ファイル: learn_param.cpp プロジェクト: JacobCWard/PyPNL
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;
}