CMlStaticStructLearn::CMlStaticStructLearn(CStaticGraphicalModel *pGrModel,
        ELearningTypes LearnType,
        EOptimizeTypes AlgorithmType,
        EScoreFunTypes ScoreType, int nMaxFanIn,
        intVector& vAncestor, intVector& vDescent)
    :CStaticLearningEngine(pGrModel, LearnType),
     m_pResultBNet(NULL), m_pResultDAG(NULL)
{
    int nnodes = pGrModel->GetNumberOfNodes();
    m_nNodes = nnodes;
    int i;
    for (i = 0; i<nnodes; i++) m_vResultRenaming.push_back(i);

    m_ScoreType = ScoreType;
    m_Algorithm = AlgorithmType;
    m_nMaxFanIn = nMaxFanIn;
    m_ScoreMethod = MaxLh;
    m_priorType = Dirichlet;
    m_K2alfa = 0;
    m_vAncestor.assign(vAncestor.begin(), vAncestor.end());
    m_vDescent.assign(vDescent.begin(),vDescent.end());
    PNL_CHECK_RANGES(nMaxFanIn, 1, nnodes);
    intVector ranges(nMaxFanIn);
    for(i=0; i<nMaxFanIn; i++) ranges[i] = nnodes+1;
    float max = 0.0f;
    m_pNodeScoreCache =	(CSparseMatrix<float>**)malloc(nnodes * sizeof(CSparseMatrix<float>*));
    for(i=0; i<nnodes; i++)
    {
        m_pNodeScoreCache[i] = CSparseMatrix<float>::Create(nMaxFanIn,
                               &ranges.front(), max, 0);
    }
}
bool CBKInfEngine::
CheckClustersValidity( intVecVector& clusters, intVector& interfNds )
{
    if( clusters.empty() )
    {
	return false;
    }
    
    
    intVector::iterator interfNdsLit = interfNds.begin();
    intVector::iterator interfNdsRit = interfNds.end();
    
    int i;
    for( i = 0; i < clusters.size(); i++ )
    {
	if( clusters[i].empty() )
	{
	    return false;
	}
	else
	{
	    int j;
	    for( j = 0; j < clusters[i].size(); j++ )
	    {
		if( std::find(interfNdsLit, interfNdsRit, clusters[i][j]) == interfNdsRit )
		{
		    return false;
		}
	    }
	}
    }
    return true;
}
void CBKInfEngine::
Get1_5Clusters(int nnodesPerSlice, intVector& interfNds, intVecVector& clusters, 
	       intVecVector* clusters1_5Sl) const
{
    
    int nInterfNds = interfNds.size();
    
    clusters1_5Sl->assign( clusters.begin(), clusters.end() );
    clusters1_5Sl->insert( clusters1_5Sl->end(), clusters.begin(), clusters.end() );
    
    int nClusters = clusters.size();
    int i,j;
    for( i = 0; i < nClusters; i++ )
    {
	int nnodesPerClust = clusters[i].size();
	
	for( j = 0; j < nnodesPerClust; j++ )
	{
	    intVector::iterator loc = std::find( interfNds.begin(),
			    interfNds.end(), clusters[i][j] );
	    
	    (*clusters1_5Sl)[i][j] = loc - interfNds.begin();
	    (*clusters1_5Sl)[i + nClusters][j] += nInterfNds;
	}
	
    }
    
}
Example #4
0
CFactor::CFactor( EDistributionType dt,
                  EFactorType pt,
                  const int *domain, int nNodes, CModelDomain* pMD,
                  const intVector& obsIndices )
                  : m_Domain( domain, domain + nNodes )
{	
    /*fill enum fields:*/
    m_DistributionType = dt;
    m_FactorType = pt;
    m_pMD = pMD;
    m_factNumInHeap = m_pMD->AttachFactor(this);
    int i;
    pConstNodeTypeVector nt;
    intVector dom = intVector( domain, domain+nNodes );
    pMD->GetVariableTypes( dom, &nt );
    m_obsPositions.assign( obsIndices.begin(), obsIndices.end() );
    int numObsNodesHere = obsIndices.size();
    switch (dt)
    {
    case dtScalar:
        {
            if( pt == ftCPD )
            {
                PNL_THROW( CInvalidOperation, "scalar is only potential - to multiply" );
            }
            //if there are observed nodes - get corresponding node types
            if( numObsNodesHere )
            {
                if ( numObsNodesHere != nNodes )
                {
                    PNL_THROW( CInconsistentType,
                        "all nodes in scalar distribution must be observed" )
                }
                //need to find observed nodes in domain and check including their changed types
                for( i = 0; i < numObsNodesHere; i++ )
                {
                    nt[obsIndices[i]] = nt[obsIndices[i]]->IsDiscrete() ? pMD->GetObsTabVarType():
                        pMD->GetObsGauVarType();
                }
            }
            m_CorrespDistribFun = CScalarDistribFun::Create(nNodes, &nt.front());
            break;
        }
	case dtTree:
        {
            if( pt != ftCPD )
            {
                PNL_THROW( CInvalidOperation, "Tree is only CPD" );
            }
            m_CorrespDistribFun = CTreeDistribFun::Create(nNodes, &nt.front());
            break;
        }
    case dtTabular:
        {
            
            if(( pt == ftPotential )&&( numObsNodesHere ))
            {
                //need to find observed nodes in domain and check including their changed types
                for( i = 0; i < numObsNodesHere; i++ )
                {
                    //change node type for this node
                    nt[obsIndices[i]] = nt[obsIndices[i]]->IsDiscrete() ? pMD->GetObsTabVarType():
                    pMD->GetObsGauVarType();
                }
            }
            //check all node types corresponds Tabular distribution
            for( i = 0; i < nNodes; i++ )
            {
                if((!(( nt[i]->IsDiscrete() )||
                    ( !nt[i]->IsDiscrete()&&(nt[i]->GetNodeSize() == 0)))))
                {
                    PNL_THROW( CInconsistentType, 
                        "node types must corresponds Tabular type" );
                }
            }
            m_CorrespDistribFun = CTabularDistribFun::Create( nNodes,
                &nt.front(), NULL );
            break;
        }
    case dtGaussian:
        {
            switch (pt)
            {
            case ftPotential:
                {
                    //need to find observed nodes in domain and check including their changed types
                    for( i = 0; i < numObsNodesHere; i++ )
                    {
                        //change node type for this node
                        nt[obsIndices[i]] = nt[obsIndices[i]]->IsDiscrete() ?
                                pMD->GetObsTabVarType():pMD->GetObsGauVarType();
                    }
                    for( i = 0; i < nNodes; i++ )
                    {
                        if( nt[i]->IsDiscrete() && (nt[i]->GetNodeSize() != 1))
                        {
                            PNL_THROW( CInvalidOperation,
                                "Gaussian potential must be of Gaussian nodes only" )
                        }
                    }
                    m_CorrespDistribFun = 
                        CGaussianDistribFun::CreateInMomentForm( 1, nNodes,
                        &nt.front(), NULL, NULL, NULL  );
                    break;
                }
            case ftCPD:
                {
                    //can check if there are both Continuous & Discrete nodes
                    int noDiscrete = 1;
                    for( int i = 0; i < nNodes; i++ )
                    {
                        if( nt[i]->IsDiscrete() )
                        {
                            noDiscrete = 0;
                            break;
                        }
                    }
                    if( noDiscrete )
                    {
                        m_CorrespDistribFun = 
                            CGaussianDistribFun::CreateInMomentForm( 0, nNodes,
                            &nt.front(), NULL, NULL, NULL );
                        break;
                    }
                    else
                    {
                        m_CorrespDistribFun = 
                            CCondGaussianDistribFun::Create( 0, nNodes, &nt.front() );
                        break;
                    }
                }
            default:
                {
                    PNL_THROW( CBadConst, 
                        "no competent type as EFactorType" );
                    break;
                }
            }
            break;
        }
    case dtMixGaussian:
        {
            switch(pt)
            {
            case ftCPD:
                {
                    //check if where is discrete node - mixture node
                    int noDiscrete = 1;
                    for( int i = 0; i < nNodes; i++ )
                    {
                        if( nt[i]->IsDiscrete() )
                        {
                            noDiscrete = 0;
                            break;
                        }
                    }
                    if( !noDiscrete  )
                    {
                        m_CorrespDistribFun = CCondGaussianDistribFun::Create( 0,
                            nNodes, &nt.front() );
                    }
                    else
                    {
                        PNL_THROW( CInconsistentType, 
                            "mixture Gaussian CPD must have mixture node - discrete" );
                    }
                    break;
                }
            default:
                {
                    PNL_THROW( CNotImplemented, "mixture gaussian potential" );  
                }
            }
            break;
        }
    case dtSoftMax:
        {
            switch (pt)
            {
            case ftPotential:
                {
                  PNL_THROW( CNotImplemented, "only CPD yet" );
                    break;
                }
            case ftCPD:
                {
                    //can check if there are both Continuous & Discrete nodes
                    int noDiscrete = 1;
                    for( int i = 0; i < nNodes-1; i++ )
                    {
                        if( nt[i]->IsDiscrete() )
                        {
                            noDiscrete = 0;
                            break;
                        }
                    }
                    if( noDiscrete )
                    {
                        m_CorrespDistribFun = 
                            CSoftMaxDistribFun::Create( nNodes,
                            &nt.front(), NULL, NULL );
                        break;
                    }
                    else
                    {
                        m_CorrespDistribFun = 
                            CCondSoftMaxDistribFun::Create( nNodes, &nt.front() );
                        break;
                    }
/*
                  //can check if there are both Continuous & Discrete nodes
                        m_CorrespDistribFun = 
                            CSoftMaxDistribFun::CreateUnitFunctionDistribution( nNodes, &nt.front() );
                        break;
*/
                }
            default:
                {
                    PNL_THROW( CBadConst, 
                        "no competent type as EFactorType" );
                    break;
                }
            }
            break;
        }
    default:
        {
            PNL_THROW ( CBadConst,
                "we have no such factor type at EDistributionType");
        }
    }
}