Exemplo n.º 1
0
CBNet* CreateRandomBayessian(CGraph* pGraph, int max_num_states)
{
    PNL_CHECK_LEFT_BORDER( max_num_states, 1 );
    PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph );

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

    int i, j, k;

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

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

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

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

    pBNet->AllocFactors();

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

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

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

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

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

    delete [] nodeTypes;
    delete [] nodeAssociation;

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

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

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

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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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

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

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

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

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

	// start of filling information of node 6
    fnode6.isTerminal = true;
    fnode6.probVect = new float[2];
    fnode6.probVect[0] = 0.2f;
    fnode6.probVect[1] = 0.8f;
	// end of filling information of node 6
   
    TreeNodeFields fields[7];
    fields[0] = fnode0;
    fields[1] = fnode1;
    fields[2] = fnode2;
    fields[3] = fnode3;
    fields[4] = fnode4;
    fields[5] = fnode5;
    fields[6] = fnode6;
   
   
    pCPDChild->UpdateTree(pGraphT,fields);
    pBNet->AttachFactor(pCPDChild);
    return pBNet;
}
Exemplo n.º 3
0
CBNet* CreateFourNodeExampleNew(void)
{

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

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

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

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

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

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

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

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

    int *nodeAssociation = new int[nnodes];

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

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

    CModelDomain* pMD = pBNet->GetModelDomain();

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

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

    pBNet->AllocParameters();

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

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

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

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

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

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

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

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


    delete [] nodeTypes;
    return pBNet;
}
bool CStaticStructLearnSEM::LearnOneStep()
{
	intVecVector decompsition;
	CGraph* graph = m_pCurrBNet->GetGraph();
	graph->GetConnectivityComponents( &decompsition );
	CEMLearningEngine* pEMLearn;
	if(decompsition.size() > 1)
	{
		CExInfEngine< CJtreeInfEngine, CBNet, PNL_EXINFENGINEFLAVOUR_DISCONNECTED > *pInf = 
                	CExInfEngine< CJtreeInfEngine, CBNet, PNL_EXINFENGINEFLAVOUR_DISCONNECTED >::
       		Create( m_pCurrBNet  );
		pEMLearn = CEMLearningEngine::Create(m_pCurrBNet, pInf);
	}
	else
    {
        CJtreeInfEngine *pInf = CJtreeInfEngine::Create(m_pCurrBNet);
        pEMLearn = CEMLearningEngine::Create(m_pCurrBNet, pInf);
    }

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	delete m_pCurrBNet;
	delete pDAG;
	m_pCurrBNet = pBNet;
	m_critValue.push_back(total_score);
	return true;
}
Exemplo n.º 5
0
CBNet* CreateTwoNodeEx(void)
{
    const int numOfNds = 2;
    int numOfNbrs[numOfNds] = { 1, 1 };

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

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

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

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

    CModelDomain* pMD;

    nodeTypeVector variableTypes;

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

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

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

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

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

    pBNet->AllocFactors();

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

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

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

    int parInd0[] = { 0 };

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

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

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

    pBNet->AttachFactor(pCPD3);

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

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

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

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

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

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

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

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

    nodeTypeVector variableTypes;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    int parInd61[] = { 1 };

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

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

    pBNet->AttachFactor( pCPD6 );

    return pBNet;
}
Exemplo n.º 7
0
CBNet* CreateSixNodeEx(void)
{
    int i;
    const int numOfNds = 6;
    int numOfNbrs[numOfNds] = { 1, 1, 1, 1, 1, 5 };

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

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

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

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

    CModelDomain* pMD;

    nodeTypeVector variableTypes;

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

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

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

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

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

    pBNet->AllocFactors();

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

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

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

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

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

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

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

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

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

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

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

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

    int parInd0[] = { 0 };

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

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

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

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

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

    pBNet->AttachFactor(pCPD5);

    return pBNet;
}
Exemplo n.º 8
0
// ----------------------------------------------------------------------------
CBNet* CreateGaussianExample(void)
{
    CBNet *pBNet;
    int i;
    const int nnodes = 3;

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

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

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

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

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

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

    const int numberOfNodeTypes = 1;

    CNodeType *nodeTypes = new CNodeType [numberOfNodeTypes];

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

    int *nodeAssociation = new int[nnodes];

    for (i = 0; i < nnodes; i++)
        nodeAssociation[i] = 0;

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

    CModelDomain* pMD = pBNet->GetModelDomain();

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

    int *domains[] = { domain0, domain1, domain2 };

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

    float meanNode1 = 8.0f;
    float covNode1 = 1.0f;
    float weightNode1[] = { 0.01f };

    pCPD = CGaussianCPD::Create( domain1, 2, pMD );

    const float *pData = weightNode1;
    pCPD->AllocDistribution( &meanNode1, &covNode1, 0.5f, &pData );
    pBNet->AttachFactor(pCPD);

    float meanNode2 = 1.0f ;
    float covNode2 = 0.01f;
    float weightNode2[] = {0.01f};

    pCPD = CGaussianCPD::Create( domain2, 2, pMD );
    const float *pData2 = weightNode2;
    pCPD->AllocDistribution( &meanNode2, &covNode2, 0.5f, &pData2 );

    pBNet->AttachFactor(pCPD);

    delete [] nodeTypes;
    delete [] nodeAssociation;
    return pBNet;
}
Exemplo n.º 9
0
PNL_USING
CBNet* CreateAlarmBNet()
{
    const int nnodes = 37;
    const int numberOfNodeTypes = 37;
    
    int i;
    
    CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL);
    pGraph->AddNodes(37);
    pGraph->AddEdge(0,1,1);
    pGraph->AddEdge(0,3,1);
    pGraph->AddEdge(2,3,1);
    pGraph->AddEdge(3,4,1);
    pGraph->AddEdge(3,5,1);
    pGraph->AddEdge(0,6,1);
    pGraph->AddEdge(2,6,1);
    pGraph->AddEdge(11,12,1);
    pGraph->AddEdge(10,13,1);
    pGraph->AddEdge(12,13,1);
    pGraph->AddEdge(8,14,1);
    pGraph->AddEdge(9,14,1);
    pGraph->AddEdge(13,14,1);
    pGraph->AddEdge(8,15,1);
    pGraph->AddEdge(14,15,1);
    pGraph->AddEdge(15,16,1);
    pGraph->AddEdge(15,19,1);
    pGraph->AddEdge(18,19,1);
    pGraph->AddEdge(8,21,1);
    pGraph->AddEdge(20,21,1);
    pGraph->AddEdge(19,22,1);
    pGraph->AddEdge(21,22,1);
    pGraph->AddEdge(23,24,1);
    pGraph->AddEdge(16,25,1);
    pGraph->AddEdge(17,25,1);
    pGraph->AddEdge(22,25,1);
    pGraph->AddEdge(24,25,1);
    pGraph->AddEdge(25,26,1);
    pGraph->AddEdge(7,27,1);
    pGraph->AddEdge(26,27,1);
    pGraph->AddEdge(26,29,1);
    pGraph->AddEdge(28,29,1);
    pGraph->AddEdge(26,30,1);
    pGraph->AddEdge(28,30,1);
    pGraph->AddEdge(14,31,1);
    pGraph->AddEdge(16,31,1);
    pGraph->AddEdge(8,32,1);
    pGraph->AddEdge(14,32,1);
    pGraph->AddEdge(20,33,1);
    pGraph->AddEdge(8,34,1);
    pGraph->AddEdge(9,34,1);
    pGraph->AddEdge(13,34,1);
    pGraph->AddEdge(6,35,1);
    pGraph->AddEdge(26,35,1);
    pGraph->AddEdge(24,36,1);
    pGraph->AddEdge(35,36,1);
    
    CNodeType *nodeTypes = new CNodeType [37];
    
    nodeTypes[0].SetType(1, 2);
    nodeTypes[1].SetType(1, 2);
    nodeTypes[2].SetType(1, 2);
    nodeTypes[3].SetType(1, 3);
    nodeTypes[4].SetType(1, 3);
    nodeTypes[5].SetType(1, 3);
    nodeTypes[6].SetType(1, 3);
    nodeTypes[7].SetType(1, 2);
    nodeTypes[8].SetType(1, 3);
    nodeTypes[9].SetType(1, 2);
    nodeTypes[10].SetType(1, 2);
    nodeTypes[11].SetType(1, 3);
    nodeTypes[12].SetType(1, 4);
    nodeTypes[13].SetType(1, 4);
    nodeTypes[14].SetType(1, 4);
    nodeTypes[15].SetType(1, 4);
    nodeTypes[16].SetType(1, 3);
    nodeTypes[17].SetType(1, 2);
    nodeTypes[18].SetType(1, 2);
    nodeTypes[19].SetType(1, 3);
    nodeTypes[20].SetType(1, 2);
    nodeTypes[21].SetType(1, 2);
    nodeTypes[22].SetType(1, 3);
    nodeTypes[23].SetType(1, 2);
    nodeTypes[24].SetType(1, 3);
    nodeTypes[25].SetType(1, 2);
    nodeTypes[26].SetType(1, 3);
    nodeTypes[27].SetType(1, 3);
    nodeTypes[28].SetType(1, 2);
    nodeTypes[29].SetType(1, 3);
    nodeTypes[30].SetType(1, 3);
    nodeTypes[31].SetType(1, 4);
    nodeTypes[32].SetType(1, 4);
    nodeTypes[33].SetType(1, 3);
    nodeTypes[34].SetType(1, 4);
    nodeTypes[35].SetType(1, 3);
    nodeTypes[36].SetType(1, 3);
    
    int *nodeAssociation = new int[nnodes];
    
    for ( i = 0; i < nnodes; i++ )
    {
	nodeAssociation[i] = i;
    }
    CBNet *pBNet = CBNet::Create( nnodes, numberOfNodeTypes, nodeTypes,
	nodeAssociation, pGraph );
    
    CModelDomain* pMD = pBNet->GetModelDomain();
    
    //number of factors is the same as number of nodes - one CPD per node
    CFactor **myParams = new CFactor*[nnodes];
    int *nodeNumbers = new int [nnodes];
    
    int domain0[] = { 0 };
    nodeNumbers[0] =  1;
    int domain1[] = { 0, 1 };
    nodeNumbers[1] =  2;
    int domain2[] = { 2 };
    nodeNumbers[2] =  1;
    int domain3[] = { 0, 2, 3 };
    nodeNumbers[3] =  3;
    int domain4[] = { 3, 4 };
    nodeNumbers[4] =  2;
    int domain5[] = { 3, 5 };
    nodeNumbers[5] =  2;
    int domain6[] = { 0, 2, 6 };
    nodeNumbers[6] =  3;
    int domain7[] = { 7 };
    nodeNumbers[7] =  1;
    int domain8[] = { 8 };
    nodeNumbers[8] =  1;
    int domain9[] = { 9 };
    nodeNumbers[9] =  1;
    int domain10[] = { 10 };
    nodeNumbers[10] =  1;
    int domain11[] = { 11 };
    nodeNumbers[11] =  1;
    int domain12[] = { 11, 12 };
    nodeNumbers[12] =  2;
    int domain13[] = { 10, 12, 13 };
    nodeNumbers[13] =  3;
    int domain14[] = { 8, 9, 13, 14 };
    nodeNumbers[14] =  4;
    int domain15[] = { 8, 14, 15 };
    nodeNumbers[15] =  3;
    int domain16[] = { 15, 16 };
    nodeNumbers[16] =  2;
    int domain17[] = { 17 };
    nodeNumbers[17] =  1;
    int domain18[] = { 18 };
    nodeNumbers[18] =  1;
    int domain19[] = { 15, 18, 19 };
    nodeNumbers[19] =  3;
    int domain20[] = { 20 };
    nodeNumbers[20] =  1;
    int domain21[] = { 8, 20, 21 };
    nodeNumbers[21] =  3;
    int domain22[] = { 19, 21, 22 };
    nodeNumbers[22] =  3;
    int domain23[] = { 23 };
    nodeNumbers[23] =  1;
    int domain24[] = { 23, 24 };
    nodeNumbers[24] =  2;
    int domain25[] = { 16, 17, 22, 24, 25 };
    nodeNumbers[25] =  5;
    int domain26[] = { 25, 26 };
    nodeNumbers[26] =  2;
    int domain27[] = { 7, 26, 27 };
    nodeNumbers[27] =  3;
    int domain28[] = { 28 };
    nodeNumbers[28] =  1;
    int domain29[] = { 26, 28, 29 };
    nodeNumbers[29] =  3;
    int domain30[] = { 26, 28, 30 };
    nodeNumbers[30] =  3;
    int domain31[] = { 14, 16, 31 };
    nodeNumbers[31] =  3;
    int domain32[] = { 8, 14, 32 };
    nodeNumbers[32] =  3;
    int domain33[] = { 20, 33 };
    nodeNumbers[33] =  2;
    int domain34[] = { 8, 9, 13, 34 };
    nodeNumbers[34] =  4;
    int domain35[] = { 6, 26, 35 };
    nodeNumbers[35] =  3;
    int domain36[] = { 24, 35, 36 };
    nodeNumbers[36] =  3;
    
    int *domains[] = { domain0, domain1, domain2, domain3, domain4,
	domain5, domain6, domain7, domain8, domain9,
	domain10, domain11, domain12, domain13, domain14,
	domain15, domain16, domain17, domain18, domain19,
	domain20, domain21, domain22, domain23, domain24,
	domain25, domain26, domain27, domain28, domain29,
	domain30, domain31, domain32, domain33, domain34,
	domain35, domain36};
    
    pBNet->AllocFactors();
    
    for( i = 0; i < nnodes; i++ )
    {
	myParams[i] = CTabularCPD::Create( domains[i], nodeNumbers[i], pMD);
    }
    
    // data creation for all CPDs of the model
    float data0[] = {0.050000f, 0.950000f};
    float data1[] = {0.900000f, 0.100000f, 0.010000f, 0.990000f};
    float data2[] = {0.200000f, 0.800000f};
    float data3[] = {0.950000f, 0.040000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.010000f, 0.090000f, 0.900000f, 0.050000f, 0.900000f, 0.050000f};
    float data4[] = {0.950000f, 0.040000f, 0.010000f, 0.040000f, 0.950000f, 0.010000f, 0.010000f, 0.290000f, 0.700000f};
    float data5[] = {0.950000f, 0.040000f, 0.010000f, 0.040000f, 0.950000f, 0.010000f, 0.010000f, 0.040000f, 0.950000f};
    float data6[] = {0.980000f, 0.010000f, 0.010000f, 0.950000f, 0.040000f, 0.010000f, 0.500000f, 0.490000f, 0.010000f, 0.050000f, 0.900000f, 0.050000f};
    float data7[] = {0.050000f, 0.950000f};
    float data8[] = {0.920000f, 0.030000f, 0.050000f};
    float data9[] = {0.040000f, 0.960000f};
    float data10[] = {0.100000f, 0.900000f};
    float data11[] = {0.050000f, 0.900000f, 0.050000f};
    float data12[] = {0.050000f, 0.930000f, 0.010000f, 0.010000f, 0.050000f, 0.010000f, 0.930000f, 0.010000f, 0.050000f, 0.010000f, 0.010000f, 0.930000f};
    float data13[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f};
    float data14[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.300000f, 0.680000f, 0.010000f, 0.010000f, 0.950000f, 0.030000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.950000f, 0.030000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.500000f, 0.480000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.400000f, 0.580000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.300000f, 0.680000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f};
    float data15[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.030000f, 0.950000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.940000f, 0.040000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.880000f, 0.100000f, 0.010000f};
    float data16[] = {0.010000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.980000f, 0.040000f, 0.920000f, 0.040000f, 0.900000f, 0.090000f, 0.010000f};
    float data17[] = {0.100000f, 0.900000f};
    float data18[] = {0.050000f, 0.950000f};
    float data19[] = {1.000000f, 0.000000f, 0.000000f, 0.950000f, 0.040000f, 0.010000f, 1.000000f, 0.000000f, 0.000000f, 0.010000f, 0.950000f, 0.040000f, 0.990000f, 0.010000f, 0.000000f, 0.950000f, 0.040000f, 0.010000f, 0.950000f, 0.040000f, 0.010000f, 0.010000f, 0.010000f, 0.980000f};
    float data20[] = {0.010000f, 0.990000f};
    float data21[] = {0.100000f, 0.900000f, 0.950000f, 0.050000f, 0.100000f, 0.900000f, 0.950000f, 0.050000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f};
    float data22[] = {0.980000f, 0.010000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.010000f, 0.980000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.980000f, 0.690000f, 0.300000f, 0.010000f};
    float data23[] = {0.010000f, 0.990000f};
    float data24[] = {0.980000f, 0.010000f, 0.010000f, 0.300000f, 0.400000f, 0.300000f};
    float data25[] = {0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.950000f, 0.050000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.950000f, 0.050000f, 0.050000f, 0.950000f, 0.050000f, 0.950000f, 0.950000f, 0.050000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.990000f, 0.010000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.700000f, 0.300000f, 0.010000f, 0.990000f, 0.050000f, 0.950000f, 0.990000f, 0.010000f, 0.050000f, 0.950000f, 0.050000f, 0.950000f, 0.990000f, 0.010000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.100000f, 0.900000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.100000f, 0.900000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.300000f, 0.700000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.100000f, 0.900000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.300000f, 0.700000f, 0.010000f, 0.990000f, 0.010000f, 0.990000f, 0.300000f, 0.700000f};
    float data26[] = {0.050000f, 0.900000f, 0.050000f, 0.010000f, 0.090000f, 0.900000f};
    float data27[] = {0.980000f, 0.010000f, 0.010000f, 0.300000f, 0.400000f, 0.300000f, 0.010000f, 0.980000f, 0.010000f, 0.400000f, 0.590000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.980000f};
    float data28[] = {0.100000f, 0.900000f};
    float data29[] = {0.333333f, 0.333333f, 0.333333f, 0.010000f, 0.980000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.010000f, 0.010000f, 0.980000f};
    float data30[] = {0.333333f, 0.333333f, 0.333333f, 0.010000f, 0.980000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.333333f, 0.010000f, 0.010000f, 0.980000f};
    float data31[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f};
    float data32[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.500000f, 0.480000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.500000f, 0.480000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.600000f, 0.380000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f};
    float data33[] = {0.010000f, 0.190000f, 0.800000f, 0.050000f, 0.900000f, 0.050000f};
    float data34[] = {0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.050000f, 0.250000f, 0.250000f, 0.450000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.200000f, 0.750000f, 0.040000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.290000f, 0.300000f, 0.400000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.900000f, 0.080000f, 0.010000f, 0.010000f, 0.300000f, 0.490000f, 0.200000f, 0.010000f, 0.150000f, 0.250000f, 0.590000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.200000f, 0.700000f, 0.090000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.080000f, 0.900000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.380000f, 0.600000f, 0.010000f, 0.010000f, 0.080000f, 0.900000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.010000f, 0.970000f, 0.010000f, 0.010000f, 0.010000f, 0.100000f, 0.840000f, 0.050000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f, 0.400000f, 0.580000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.010000f, 0.970000f};
    float data35[] = {0.980000f, 0.010000f, 0.010000f, 0.950000f, 0.040000f, 0.010000f, 0.800000f, 0.190000f, 0.010000f, 0.950000f, 0.040000f, 0.010000f, 0.040000f, 0.950000f, 0.010000f, 0.010000f, 0.040000f, 0.950000f, 0.300000f, 0.690000f, 0.010000f, 0.010000f, 0.300000f, 0.690000f, 0.010000f, 0.010000f, 0.980000f};
    float data36[] = {0.980000f, 0.010000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.900000f, 0.090000f, 0.010000f, 0.980000f, 0.010000f, 0.010000f, 0.100000f, 0.850000f, 0.050000f, 0.050000f, 0.200000f, 0.750000f, 0.300000f, 0.600000f, 0.100000f, 0.050000f, 0.400000f, 0.550000f, 0.010000f, 0.090000f, 0.900000f};
    float *data[] = { data0, data1, data2, data3, data4,
	data5, data6, data7, data8, data9,
	data10, data11, data12, data13, data14,
	data15, data16, data17, data18, data19,
	data20, data21, data22, data23, data24,
	data25, data26, data27, data28, data29,
	data30, data31, data32, data33, data34,
	data35, data36};
    
    for( i = 0; i < nnodes; i++ )
    {
	myParams[i]->AllocMatrix(data[i], matTable);
	pBNet->AttachFactor(myParams[i]);
    }
    
    delete [] nodeTypes;
    delete[] nodeAssociation;

    CContextPersistence xmlContext;
    xmlContext.Put(pBNet, "MyModel");
    if(!xmlContext.SaveAsXML("myFavoriteObjects.xml"))
    {
	fprintf(stderr, "Persistence: error!\n");
	// something goes wrong – can’t create
    }
    else
    {
	fprintf(stderr, "Persistence: good!\n");
    }
    
    return pBNet;
}