本文整理汇总了C++中CBNet类的典型用法代码示例。如果您正苦于以下问题:C++ CBNet类的具体用法?C++ CBNet怎么用?C++ CBNet使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CBNet类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: TraverseSubobjectOfGrModel
void
CPersistBNet::TraverseSubobject(CPNLBase *pObj, CContext *pContext)
{
CBNet *pModel = dynamic_cast<CBNet*>(pObj);
pContext->Put(pModel->GetGraph(), "Graph");
TraverseSubobjectOfGrModel(pModel, pContext);
}
示例2: CreateTwoNodeExDiscrete
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;
}
示例3: GibbsForSparseBNet
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;
}
示例4: Create_BNet_CompleteGraph
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;
}
示例5: Create_BNet_RegularGrid
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;
}
示例6: Create_BNet_toyQMR
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;
}
示例7: Create_BNet_Pyramid
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;
}
示例8: GibbsMPEforScalarGaussianBNet
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;
}
示例9: GibbsForSingleGaussian
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;
}
示例10: pEv
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
//.........这里部分代码省略.........
示例11: 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;
}
示例12: testRandomFactors
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++ )
{
//.........这里部分代码省略.........
示例13: testBayesLearningEngine
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;
//.........这里部分代码省略.........
示例14: GibbsForSimplestGaussianBNet
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());
//.........这里部分代码省略.........
示例15: timeJTreeInfEngine
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");
}