void C1_5SliceInfEngine::Initialization( const CDynamicGraphicalModel *pDBN ) { m_p1_5SliceBNet = Create1_5SliceBNet(); PNL_CHECK_IF_MEMORY_ALLOCATED( m_p1_5SliceBNet ); std::string description; if(! m_p1_5SliceBNet->IsValid( &description) ) { PNL_THROW(CAlgorithmicException, description ) } m_pPriorSliceBNet = static_cast<CBNet *>(pDBN->CreatePriorSliceGrModel()); if(! m_p1_5SliceBNet->IsValid( &description) ) { PNL_THROW(CAlgorithmicException, description) } PNL_CHECK_IF_MEMORY_ALLOCATED( m_pPriorSliceBNet ); intVector interfNds; GrModel()->GetInterfaceNodes( &interfNds ); int numInterfNds = interfNds.size(); m_nIntNodes = numInterfNds; m_VectorIntNodesPriorSlice.assign(interfNds.begin(), interfNds.end()); m_VectorIntNodesISlice.resize(numInterfNds); m_VectorRootNodesISlice.assign(interfNds.begin(), interfNds.end()); int i; for( i = 0; i < numInterfNds; i++ ) { m_VectorIntNodesISlice[i] = i; m_VectorRootNodesISlice[i] += numInterfNds; } }
CSoftMaxCPD* CSoftMaxCPD::Create(const int *domain, int nNodes, CModelDomain* pMD) { PNL_CHECK_IS_NULL_POINTER(domain); PNL_CHECK_IS_NULL_POINTER(pMD); PNL_CHECK_LEFT_BORDER(nNodes, 1); int i; int NumContPar = 0; for(i = 0; i<nNodes; i++) { if (!pMD->GetVariableType(domain[i])->IsDiscrete()) { NumContPar++; } } if(NumContPar == 0 ) { PNL_THROW(CInconsistentType, "SoftMax node does not have continuous parents"); } CSoftMaxCPD *pNewParam = new CSoftMaxCPD(domain, nNodes, pMD); PNL_CHECK_IF_MEMORY_ALLOCATED(pNewParam); return pNewParam; }
CFactor* CGaussianCPD::CloneWithSharedMatrices() { CGaussianCPD* resCPD = new CGaussianCPD(this); PNL_CHECK_IF_MEMORY_ALLOCATED(resCPD); return resCPD; }
CTreeCPD* CTreeCPD::Copy( const CTreeCPD* pTreeCPD ) { PNL_CHECK_IS_NULL_POINTER( pTreeCPD ); CTreeCPD *retCPD = new CTreeCPD( *pTreeCPD ); PNL_CHECK_IF_MEMORY_ALLOCATED( retCPD ); return retCPD; }
CGaussianCPD* CGaussianCPD::Copy( const CGaussianCPD* pGauCPD ) { PNL_CHECK_IS_NULL_POINTER( pGauCPD ); CGaussianCPD *retCPD = new CGaussianCPD( *pGauCPD ); PNL_CHECK_IF_MEMORY_ALLOCATED( retCPD ); return retCPD; }
CMNet* CMNet::Copy(const CMNet* pMNet) { CMNet *pCopyMNet = new CMNet(*pMNet); PNL_CHECK_IF_MEMORY_ALLOCATED(pCopyMNet); return pCopyMNet; }
CSoftMaxCPD* CSoftMaxCPD::Copy(const CSoftMaxCPD* pSMCPD) { PNL_CHECK_IS_NULL_POINTER(pSMCPD); CSoftMaxCPD *retCPD = new CSoftMaxCPD(*pSMCPD); PNL_CHECK_IF_MEMORY_ALLOCATED(retCPD); return retCPD; }
CGraph* CreateGraphWithPyramidSpecific(int& num_nodes, int num_indep_nodes, int num_layers) { PNL_CHECK_LEFT_BORDER( num_indep_nodes, 1 ); PNL_CHECK_LEFT_BORDER( num_layers, 1 ); int i, j, k; CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL); PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph ); srand((unsigned int)time(NULL)); num_nodes = num_indep_nodes; int num_nodes_into_curr_layer = num_indep_nodes; for (i = 0; i < num_layers - 1; i++) { num_nodes += num_nodes_into_curr_layer * 2 + 1; num_nodes_into_curr_layer = num_nodes_into_curr_layer * 2 + 1; } pGraph->AddNodes(num_nodes); int StartParent = 0, EndParent = num_indep_nodes - 1, StartCurrLayer, EndCurrLayer; int Child1, Child2, Child3; int NumParents; for (int layer = 0; layer < num_layers - 1; layer++ ) { StartCurrLayer = EndParent + 1; EndCurrLayer = StartCurrLayer + 2 * (EndParent - StartParent + 1); NumParents = 0; for (j = StartParent; j <= EndParent; j++ ) { Child1 = EndParent + NumParents * 2 + 1; Child2 = EndParent + NumParents * 2 + 2; Child3 = EndParent + NumParents * 2 + 3; pGraph->AddEdge(j, Child1, 1); pGraph->AddEdge(j, Child2, 1); pGraph->AddEdge(j, Child3, 1); NumParents++; } StartParent = StartCurrLayer; EndParent = EndCurrLayer; } return pGraph; }
CGaussianCPD* CGaussianCPD::Create( const int *domain, int nNodes, CModelDomain* pMD ) { PNL_CHECK_IS_NULL_POINTER( domain ); PNL_CHECK_IS_NULL_POINTER( pMD ); PNL_CHECK_LEFT_BORDER( nNodes, 1 ); CGaussianCPD *pNewParam = new CGaussianCPD( domain, nNodes, pMD); PNL_CHECK_IF_MEMORY_ALLOCATED( pNewParam ); return pNewParam; }
CJtreeInfEngine* CJtreeInfEngine::Copy(const CJtreeInfEngine *pJTreeInfEng) { /* bad-args check */ PNL_CHECK_IS_NULL_POINTER(pJTreeInfEng); /* bad-args check end */ CJtreeInfEngine *pJTreeInfEngineCopy = new CJtreeInfEngine(*pJTreeInfEng); PNL_CHECK_IF_MEMORY_ALLOCATED(pJTreeInfEngineCopy); return pJTreeInfEngineCopy; }
CFactors* CFactors::Copy(const CFactors *pFactors) { // bad-args check PNL_CHECK_IS_NULL_POINTER(pFactors); // bad-args check end CFactors *pFactorsCopy = new CFactors(*pFactors); PNL_CHECK_IF_MEMORY_ALLOCATED(pFactorsCopy); return pFactorsCopy; }
CMNet* CMNet::ConvertFromBNet(const CBNet* pBNet) { /* bad-args check */ PNL_CHECK_IS_NULL_POINTER(pBNet); /* bad-args check end */ CMNet *pMNet = new CMNet(pBNet); PNL_CHECK_IF_MEMORY_ALLOCATED(pMNet); return pMNet; }
void CNodeValues::ToggleNodeStateBySerialNumber(int nNodes, const int *nodeNumbers) { /*to change the status of node from really observed to potentially observed and backwards*/ int i,j; if( ( nNodes<0 ) || ( nNodes>m_numberObsNodes ) ) { PNL_THROW(COutOfRange, "number of nodes to toggle must be less than m_numberObsNodes")/**/ } else { int flagAllRight = 1;/*all nodes from nodeIndices are potentially observed (if it is so - all numbers of this nodes are in m_ObsNodes*/ int thereIsSuchNode = 0;/*check-up flag for node - is it from m_ObsNodes?*/ int *nodeIndices = new int[nNodes]; PNL_CHECK_IF_MEMORY_ALLOCATED( nodeIndices ); /*checking up all input data*/ for ( i=0; i < nNodes; i++ ) { for ( j=0; j < m_numberObsNodes; j++ ) { if( nodeNumbers[i] == j ) { thereIsSuchNode = 1; nodeIndices[i] = j; break; } } if( !thereIsSuchNode ) { flagAllRight = 0; break; } } if ( flagAllRight ) { for( i=0; i < nNodes; i++ ) { m_isObsNow[nodeIndices[i]] = 1-m_isObsNow[nodeIndices[i]]; //fixme !!! is it enougth to use offset? - I think yes } } else { PNL_THROW( COutOfRange, "some node has number which is not in m_obsNodes" ) /**/ } delete []nodeIndices; } }
CGraph* CreateGraphWithRegularGridSpecific(int& num_nodes, int width, int height, int num_layers) { PNL_CHECK_LEFT_BORDER( width, 2 ); PNL_CHECK_LEFT_BORDER( height, 2 ); PNL_CHECK_LEFT_BORDER( num_layers, 1 ); int i, j, k; CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL); PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph ); srand((unsigned int)time(NULL)); int num_nodes_one_layer = width * height; num_nodes = num_nodes_one_layer * num_layers; pGraph->AddNodes(num_nodes); for (i = 0; i < num_layers; i++) { for (j = 1; j < width; j++) pGraph->AddEdge( i * num_nodes_one_layer + j - 1, i * num_nodes_one_layer + j, 1); for (k = 1; k < height; k++) pGraph->AddEdge( i * num_nodes_one_layer + (k - 1) * width, i * num_nodes_one_layer + k * width, 1); for (j = 1; j < width; j++) for (k = 1; k < height; k++) { pGraph->AddEdge( i * num_nodes_one_layer + (k - 1) * width + j, i * num_nodes_one_layer + k * width + j, 1); pGraph->AddEdge( i * num_nodes_one_layer + k * width + j - 1, i * num_nodes_one_layer + k * width + j, 1); } if (i) { for (j = 0; j < width; j++) for (k = 0; k < height; k++) pGraph->AddEdge( (i - 1) * num_nodes_one_layer + k * width + j, i * num_nodes_one_layer + k * width + j, 1); } } return pGraph; }
CGraph *C1_5SliceInfEngine::Create1_5SliceGraph() { int node, i, j; CGraph *graph = GrModel()->GetGraph(); int nnodesInDBN = graph->GetNumberOfNodes(); int numberOfInterfaceNodes; const int *interfaceNodes; GrModel()->GetInterfaceNodes(&numberOfInterfaceNodes, &interfaceNodes); int nnodes = nnodesInDBN/2 + numberOfInterfaceNodes; CGraph *pFinalGraph = CGraph::Create( nnodes, NULL, NULL, NULL ); PNL_CHECK_IF_MEMORY_ALLOCATED( pFinalGraph ); int numberOfNeighbors; const int *neighbors; const ENeighborType *orientation; int newNumber; intVector FinalNeighbors; pnlVector<ENeighborType> FinalOrientation; intVector newIntNodes( numberOfInterfaceNodes ); int numberOfNonIntNodes = nnodesInDBN/2 - numberOfInterfaceNodes; for ( node = 0; node < numberOfInterfaceNodes; node++ ) { newIntNodes[node] = interfaceNodes[node] - numberOfNonIntNodes; } for( i = nnodesInDBN/2; i < nnodesInDBN; i++ ) { graph->GetNeighbors(i, &numberOfNeighbors, &neighbors, &orientation); FinalNeighbors.resize(numberOfNeighbors); for ( j = 0; j < numberOfNeighbors; j++ ) { newNumber = neighbors[j] - numberOfNonIntNodes; FinalNeighbors[j] = ( newNumber < numberOfInterfaceNodes ) ? ( std::find( newIntNodes.begin(), newIntNodes.end(), newNumber) - newIntNodes.begin() ) : newNumber; } pFinalGraph->SetNeighbors( i - numberOfNonIntNodes, numberOfNeighbors, &(FinalNeighbors.front()), orientation ); } return pFinalGraph; }
CMlDynamicStructLearn* CMlDynamicStructLearn::Create(CDBN* pGrModel, ELearningTypes LearnType, EOptimizeTypes AlgorithmType, EScoreFunTypes ScoreType, int nMaxFanIn, int nRestarts, int nMaxIters ) { CMlDynamicStructLearn* ret = new CMlDynamicStructLearn( pGrModel, LearnType, AlgorithmType, ScoreType, nMaxFanIn, nRestarts, nMaxIters ); PNL_CHECK_IF_MEMORY_ALLOCATED(ret); return ret; }
PNL_USING CFactors* CFactors::Create(int numberOfFactors) { /* bad-args check */ PNL_CHECK_LEFT_BORDER( numberOfFactors, 1 ); /* bad-args check end */ CFactors *pFactors = new CFactors(numberOfFactors); PNL_CHECK_IF_MEMORY_ALLOCATED(pFactors); return pFactors; }
CJtreeInfEngine* CJtreeInfEngine::Create( const CStaticGraphicalModel *pGraphicalModel, CJunctionTree *pJTree) { /* bad-args check */ PNL_CHECK_IS_NULL_POINTER(pGraphicalModel); PNL_CHECK_IS_NULL_POINTER(pJTree); /* bad-args check end */ CJtreeInfEngine *pJTreeInfEngine = new CJtreeInfEngine( pGraphicalModel, pJTree ); PNL_CHECK_IF_MEMORY_ALLOCATED(pJTreeInfEngine); return pJTreeInfEngine; }
PNL_USING CMNet* CMNet::Create( int numberOfCliques, const int *cliqueSizes, const int **cliques, CModelDomain* pMD ) { /* bad-args check */ PNL_CHECK_LEFT_BORDER( numberOfCliques, 1 ); PNL_CHECK_IS_NULL_POINTER(cliqueSizes); PNL_CHECK_IS_NULL_POINTER(cliques); PNL_CHECK_IS_NULL_POINTER( pMD ); /* bad-args check end */ /* creating the model */ CMNet *pMNet = new CMNet( numberOfCliques, cliqueSizes, cliques, pMD); PNL_CHECK_IF_MEMORY_ALLOCATED(pMNet); return pMNet; }
PNL_USING ///////////////////////////////////////////////////////////////////////////// CSpecPearlInfEngine* CSpecPearlInfEngine::Create(const CStaticGraphicalModel* pGrModel) { PNL_CHECK_IS_NULL_POINTER(pGrModel); if( !IsInputModelValid(pGrModel) ) { PNL_THROW( CInconsistentType, " input model is invalid " ); } CSpecPearlInfEngine* pPearlInfEng = new CSpecPearlInfEngine(pGrModel); PNL_CHECK_IF_MEMORY_ALLOCATED(pPearlInfEng); return pPearlInfEng; }
PNL_USING CJtreeInfEngine* CJtreeInfEngine::Create( const CStaticGraphicalModel *pGraphicalModel, int numOfSubGrToConnect, const int *SubGrToConnectSizes, const int **SubgrToConnect ) { /* bad-args check */ PNL_CHECK_IS_NULL_POINTER(pGraphicalModel); /* bad-args check end */ CJtreeInfEngine *pJTreeInfEngine = new CJtreeInfEngine( pGraphicalModel, numOfSubGrToConnect, SubGrToConnectSizes, SubgrToConnect ); PNL_CHECK_IF_MEMORY_ALLOCATED(pJTreeInfEngine); return pJTreeInfEngine; }
CGraph* CreateCompleteGraph(int num_nodes) { PNL_CHECK_LEFT_BORDER( num_nodes, 1 ); int i, j, k; CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL); PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph ); srand((unsigned int)time(NULL)); pGraph->AddNodes(num_nodes); for (j = 1; j < num_nodes; j++ ) for (i = 0; i < j; i++ ) pGraph->AddEdge(i, j, 1); return pGraph; }
CGraph* CreateRandomGraphWithToyQMRSpecific(int num_nodes, int num_indep_nodes, int max_size_family) { PNL_CHECK_LEFT_BORDER( num_nodes, 10 ); PNL_CHECK_RANGES( num_indep_nodes, 1, num_nodes-1 ); PNL_CHECK_RANGES( max_size_family, 2, num_nodes ); int i, j, k; CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL); PNL_CHECK_IF_MEMORY_ALLOCATED( pGraph ); srand((unsigned int)time(NULL)); pGraph->AddNodes(num_nodes); int num_parents; int ind_parent; intVector prev_nodes(0); for ( i = num_indep_nodes; i < num_nodes; i++) { prev_nodes.resize(0); for ( j = 0; j < num_indep_nodes; j++) prev_nodes.push_back(j); num_parents = rand() % (max_size_family - 1); num_parents += 1; num_parents = (num_parents > i) ? i : num_parents; for ( j = 0; j < num_parents; j++) { ind_parent = rand() % prev_nodes.size(); pGraph->AddEdge(prev_nodes[ind_parent], i, 1); prev_nodes.erase(prev_nodes.begin() + ind_parent); } } return pGraph; }
CMNet* CMNet::Create( int numberOfNodes, int numberOfNodeTypes, const CNodeType *nodeTypes, const int *nodeAssociation, int numberOfCliques, const int *cliqueSizes, const int **cliques ) { /* bad-args check */ PNL_CHECK_LEFT_BORDER( numberOfNodes, 1 ); PNL_CHECK_RANGES( numberOfNodeTypes, 1, numberOfNodes ); PNL_CHECK_IS_NULL_POINTER(nodeTypes); PNL_CHECK_IS_NULL_POINTER(nodeAssociation); PNL_CHECK_LEFT_BORDER( numberOfCliques, 1 ); PNL_CHECK_IS_NULL_POINTER(cliqueSizes); PNL_CHECK_IS_NULL_POINTER(cliques); /* bad-args check end */ /* creating the model */ CMNet *pMNet = new CMNet( numberOfNodes, numberOfNodeTypes, nodeTypes, nodeAssociation, numberOfCliques, cliqueSizes, cliques ); PNL_CHECK_IF_MEMORY_ALLOCATED(pMNet); return pMNet; }
void CDynamicLearningEngine:: SetData( int NumberOfTimeSeries, int *NumberOfSlices, const CEvidence* const* const* pEvidences ) { m_numberOfAllEvISlice = 0; m_numberOfAllEv0Slice = 0; m_FlagsIsAllObserved.assign(NumberOfTimeSeries, 1); m_VecPVecPEvidences.resize(NumberOfTimeSeries); const CEvidence *pEv; for(int i = 0; i < NumberOfTimeSeries; i++) { PNL_CHECK_IS_NULL_POINTER( pEvidences[i]); pConstEvidenceVector *pVecPEv = new pConstEvidenceVector(NumberOfSlices[i]); PNL_CHECK_IF_MEMORY_ALLOCATED(pVecPEv); for(int j = 0; j < NumberOfSlices[i]; j++) { pEv = (pEvidences[i])[j]; PNL_CHECK_IS_NULL_POINTER(pEv); if(IsInfNeed(pEv)) { m_FlagsIsAllObserved[i] = 0; } (*pVecPEv)[j] = pEv; } m_VecPVecPEvidences[i] = pVecPEv; m_numberOfAllEvISlice += (pVecPEv->size()-1); } m_numberOfAllEv0Slice += NumberOfTimeSeries; }
CGraph* CreateRandomAndSpecificForIDNetGraph(int num_nodes, int num_indep_nodes, int max_size_family) { PNL_CHECK_LEFT_BORDER(num_nodes, 10); PNL_CHECK_RANGES(num_indep_nodes, 1, num_nodes-1); PNL_CHECK_RANGES(max_size_family, 2, num_nodes); int i, j, k; CGraph *pGraph = CGraph::Create(0, NULL, NULL, NULL); PNL_CHECK_IF_MEMORY_ALLOCATED(pGraph); srand((unsigned int)time(NULL)); pGraph->AddNodes(num_nodes); int num_parents; int ind_parent; intVector prev_nodes(0); for (i = num_indep_nodes; i < num_nodes; i++) { prev_nodes.resize(0); for (j = 0; j < i; j++) prev_nodes.push_back(j); num_parents = rand() % (max_size_family - 1); num_parents += 1; num_parents = (num_parents > i) ? i : num_parents; for (j = 0; j < num_parents; j++) { ind_parent = rand() % prev_nodes.size(); pGraph->AddEdge(prev_nodes[ind_parent], i, 1); prev_nodes.erase(prev_nodes.begin() + ind_parent); } } intVector parents(0); intVector childs(0); for (i = 0; i < num_nodes; i++) { if (pGraph->GetNumberOfChildren(i) == 0) { pGraph->GetParents(i, &parents); for (j = 0; j < parents.size(); j++) { pGraph->GetChildren(parents[j], &childs); for (k = 0; k < childs.size(); k++) if ((childs[k] != i) && (pGraph->GetNumberOfChildren(childs[k]) == 0) && (pGraph->GetNumberOfParents(childs[k]) == 1)) { if (i < childs[k]) { pGraph->RemoveEdge(parents[j], childs[k]); pGraph->AddEdge(i, childs[k], 1); } else { pGraph->AddEdge(childs[k], i, 1); } } } } } return pGraph; }
CBNet *C1_5SliceInfEngine::Create1_5SliceBNet() { CGraph *p1_5SliceGraph = Create1_5SliceGraph(); PNL_CHECK_IF_MEMORY_ALLOCATED(p1_5SliceGraph); intVecVector comp; p1_5SliceGraph->GetConnectivityComponents(&comp); PNL_CHECK_FOR_NON_ZERO(comp.size() -1 ); nodeTypeVector nodeTypes; int nnodes = p1_5SliceGraph->GetNumberOfNodes(); GrModel()->GetModelDomain()->GetVariableTypes(&nodeTypes); const int *nodeAssociatons = GrModel()->GetNodeAssociations(); intVector FinalNodeAssociations; FinalNodeAssociations.resize(nnodes); int numberOfInterfaceNodes; const int *interfaceNodes; GrModel()->GetInterfaceNodes(&numberOfInterfaceNodes, &interfaceNodes); int nnodesPerSlice = GrModel()->GetNumberOfNodes(); int node; for( node = 0; node < numberOfInterfaceNodes; node++ ) { FinalNodeAssociations[node]= nodeAssociatons[interfaceNodes[node]]; } for ( node = numberOfInterfaceNodes; node < nnodes; node++ ) { FinalNodeAssociations[node]= nodeAssociatons[nnodesPerSlice - numberOfInterfaceNodes + node]; } CBNet *p1_5SliceGrModel = CBNet::Create( nnodes, nodeTypes.size(), &nodeTypes.front(), &FinalNodeAssociations.front(), p1_5SliceGraph ); p1_5SliceGrModel->AllocFactors(); CFactor *pFactor; intVector domain(1); CFactor *pUnitFactor; for ( node = 0; node < numberOfInterfaceNodes; node++ ) { domain[0] = node; if( GrModel()->GetNodeType(interfaceNodes[node])->IsDiscrete() ) { pUnitFactor = CTabularCPD::CreateUnitFunctionCPD( domain, p1_5SliceGrModel->GetModelDomain()); } else { pUnitFactor = CGaussianCPD::CreateUnitFunctionCPD( domain, p1_5SliceGrModel->GetModelDomain()); } PNL_CHECK_IF_MEMORY_ALLOCATED( pUnitFactor ); p1_5SliceGrModel->AttachFactor( pUnitFactor ); } for ( node = numberOfInterfaceNodes; node < nnodes; node++ ) { domain.clear(); p1_5SliceGraph->GetParents(node, &domain); domain.push_back(node); int num = nnodesPerSlice - numberOfInterfaceNodes + node; /* pFactor = CFactor:: CopyWithNewDomain(GrModel()->GetFactor( num ), domain, p1_5SliceGrModel->GetModelDomain()); p1_5SliceGrModel->AttachFactor(pFactor ); */ pFactor = GrModel()->GetFactor(num); if( pFactor->GetDistributionType() == dtMixGaussian ) { floatVector prob; static_cast<CMixtureGaussianCPD *>(pFactor)->GetProbabilities(&prob); CMixtureGaussianCPD *pCPD = CMixtureGaussianCPD::Create(domain, p1_5SliceGrModel->GetModelDomain(), prob ); pCPD->TieDistribFun(pFactor); p1_5SliceGrModel->AttachFactor(pCPD); } else { p1_5SliceGrModel->AllocFactor(node); p1_5SliceGrModel->GetFactor(node)->TieDistribFun(GrModel()->GetFactor(num)); } } return p1_5SliceGrModel; }
CFactor* CFactor::CopyWithNewDomain(const CFactor *factor, intVector &domain, CModelDomain *pMDNew, const intVector& /* obsIndices */) { int domSize = domain.size(); intVector domOld; factor->GetDomain( &domOld ); if( int(domOld.size()) != domSize ) { PNL_THROW( CBadArg, "number of nodes" ); } CModelDomain *pMDOld = factor->GetModelDomain(); //check is the types are the same const pConstNodeTypeVector* ntFactor = factor->GetArgType(); /* const CNodeType *nt; for( int i = 0; i < domSize; i++ ) { nt = (*ntFactor)[i]; if( nt->IsDiscrete() ) { if( nt->GetNodeSize() == 1 ) { if( *pMDOld->GetVariableType(domOld[i]) != *pMDNew->GetVariableType(domain[i])) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } else { if( *nt != *pMDNew->GetVariableType(domain[i]) ) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } } else { if( nt->GetNodeSize() == 0 ) { if( *pMDOld->GetVariableType(domOld[i]) != *pMDNew->GetVariableType(domain[i])) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } else { if( *nt != *pMDNew->GetVariableType(domain[i]) ) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } } } */ const CNodeType *nt; int i; intVector obsPositions; factor->GetObsPositions(&obsPositions); if( obsPositions.size() ) { intVector::iterator iterEnd = obsPositions.end(); for( i = 0; i < domSize; i++) { if( std::find( obsPositions.begin(),iterEnd, i) != iterEnd ) { if( *pMDOld->GetVariableType(domOld[i]) != *pMDNew->GetVariableType(domain[i])) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } } } else { for( i = 0; i < domSize; i++ ) { nt = (*ntFactor)[i]; if( *nt != *pMDNew->GetVariableType(domain[i]) ) { PNL_THROW(CInconsistentType, "types of variables should correspond"); } } } CFactor *pNewFactor; switch ( factor->GetFactorType() ) { case ftPotential: { switch ( factor->GetDistributionType() ) { case dtTabular: { pNewFactor = CTabularPotential:: Copy(static_cast<const CTabularPotential*>(factor) ); break; } case dtGaussian: { pNewFactor = CGaussianPotential:: Copy(static_cast<const CGaussianPotential*>(factor)); break; } case dtScalar: { pNewFactor = CScalarPotential:: Copy( static_cast<const CScalarPotential*>(factor) ); break; } default: { PNL_THROW(CNotImplemented, "distribution type" ); } } break; } case ftCPD: { switch ( factor->GetDistributionType() ) { case dtTabular: { pNewFactor = CTabularCPD:: Copy(static_cast<const CTabularCPD*>(factor)); break; } case dtGaussian: { pNewFactor = CGaussianCPD:: Copy(static_cast<const CGaussianCPD*>(factor)); break; } case dtCondGaussian: { pNewFactor = CGaussianCPD:: Copy(static_cast<const CGaussianCPD*>(factor)); break; } case dtSoftMax: { pNewFactor = CSoftMaxCPD:: Copy(static_cast<const CSoftMaxCPD*>(factor)); break; } case dtCondSoftMax: { pNewFactor = CSoftMaxCPD:: Copy(static_cast<const CSoftMaxCPD*>(factor)); break; } case dtMixGaussian: { pNewFactor = CMixtureGaussianCPD::Copy( static_cast<const CMixtureGaussianCPD*>(factor)); break; } default: { PNL_THROW(CNotImplemented, "distribution type" ); } } break; } default: { PNL_THROW(CNotImplemented, "factor type" ); } } PNL_CHECK_IF_MEMORY_ALLOCATED(pNewFactor); /* if( pMDNew == factor->GetModelDomain()) { return pNewFactor; } else*/ { pNewFactor->m_Domain = intVector(domain); pNewFactor->SetModelDomain(pMDNew, 0); return pNewFactor; } }
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; }