当前位置: 首页>>代码示例>>C++>>正文


C++ CFactor::AllocMatrix方法代码示例

本文整理汇总了C++中CFactor::AllocMatrix方法的典型用法代码示例。如果您正苦于以下问题:C++ CFactor::AllocMatrix方法的具体用法?C++ CFactor::AllocMatrix怎么用?C++ CFactor::AllocMatrix使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在CFactor的用法示例。


在下文中一共展示了CFactor::AllocMatrix方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: CreateBNet

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;
}
开发者ID:caseypp,项目名称:Bayesian_Attack_Graph,代码行数:85,代码来源:inf_learn_bnet.cpp

示例2: testShrinkObservedNodes


//.........这里部分代码省略.........
    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");
开发者ID:JacobCWard,项目名称:PyPNL,代码行数:67,代码来源:AParameterTakeObsInto.cpp

示例3: CreateFourNodeExampleNew

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;
}
开发者ID:JacobCWard,项目名称:PyPNL,代码行数:94,代码来源:SamplesOfSMNet.cpp


注:本文中的CFactor::AllocMatrix方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。