示例#1
0
void CFactor::SetStatistics( const CMatrix<float> *pMat, 
			    EStatisticalMatrix matrix, const int* parentsComb)
{
    CDistribFun *pDistr = GetDistribFun();
    PNL_CHECK_IS_NULL_POINTER(pDistr);
    pDistr->SetStatistics( pMat, matrix, parentsComb );
}
//-----------------------------------------------------------------------------
CPotential *CSoftMaxCPD::ConvertToTabularPotential(const CEvidence* pEvidence) const
{
    //searching discrite nodes in domain
    intVector discriteNodesPosInDom;
    int domSize = m_Domain.size();
    int numSoftMaxNode;
    int discrDomSize = 0;
    int *parentIndexes;
    int SoftMaxSize;
    const pConstNodeTypeVector* ntVec = GetDistribFun()->GetNodeTypesVector();
    int i;
    for (i = 0; i < domSize; i++)
    {
        if ((*ntVec)[i]->IsDiscrete())
        {
            discriteNodesPosInDom.push_back(m_Domain[i]);
            SoftMaxSize = (*ntVec)[i]->GetNodeSize();
            numSoftMaxNode = i;
        }
    };

    discrDomSize = discriteNodesPosInDom.size();
    
    //fill parents indexes vector
    parentIndexes = new int[discrDomSize-1];
    for( i = 0; i < discrDomSize-1; i++ )
    {
        parentIndexes[i] = discriteNodesPosInDom[i];
    }    
 
    // creating new evidece that contain all observed nodes in this domain
    intVector pObsNodes;
    pConstValueVector pObsValues;
    pConstNodeTypeVector pNodeTypes;
    pEvidence->GetObsNodesWithValues(&pObsNodes,&pObsValues,&pNodeTypes);
    int *obsNodes;
    int obsNodesSize;
    obsNodesSize=pObsNodes.size();
    obsNodes = new int[obsNodesSize];
    for(i = 0;i < obsNodesSize; i++)
    {
        obsNodes[i] = pObsNodes[i];
    }

    CEvidence *pCopyEvidence;
    valueVector cpyValVect(0);
    for(i = 0; i < obsNodesSize; i++)
    {
        cpyValVect.push_back(*(pObsValues[i]));
    }
    
    pCopyEvidence = CEvidence::Create(pEvidence->GetModelDomain(), obsNodesSize, 
        obsNodes,(const valueVector&)cpyValVect);

    for(i = 0; i < pObsNodes.size(); i++)
    {
        if((std::find(m_Domain.begin(),m_Domain.end(), pObsNodes[i])) == m_Domain.end())
        {
            pCopyEvidence->MakeNodeHidden(pObsNodes[i]);
        }
    };
    
    //creating tabular potential 
    CTabularPotential *resFactor = CTabularPotential::Create(
        GetModelDomain(),discriteNodesPosInDom);

    if( m_DistributionType == dtSoftMax)
    {
        resFactor->AttachMatrix(((CSoftMaxDistribFun*)m_CorrespDistribFun)->
        GetProbMatrix(pCopyEvidence),matTable);
    }
    else
    {
        if(m_DistributionType == dtCondSoftMax)
        {
            resFactor->AttachMatrix(((CCondSoftMaxDistribFun*)m_CorrespDistribFun)->
            GetProbMatrix(pCopyEvidence),matTable);
        }
        else
        {
            PNL_THROW( CInconsistentType,
                "distribution must be SoftMax or conditional SoftMax" )
        }
    }

    delete [] parentIndexes;
    delete [] obsNodes;
    delete pCopyEvidence;
    return resFactor;
}
示例#3
0
void CFactor::SetModelDomain( CModelDomain* pMD, bool checkNodeTypesinMD )
{
    if( pMD == m_pMD )
    {
        return;
    }
    if( checkNodeTypesinMD )
    {
        //check if node types are the same - check only node types in models!
        int i;
        int domSize = m_Domain.size();
    
        for( i = 0; i < domSize; i++ )
        {
            if( *(m_pMD->GetVariableType(m_Domain[i])) != *(pMD->GetVariableType(m_Domain[i]) ) )
            {
                PNL_THROW( CInconsistentType, "types of variables should correspond" );
            }
        }
    }
    //need to set new ModelDomain
    CDistribFun *pNewDistribFun;
    pNewDistribFun = GetDistribFun();
    pConstNodeTypeVector ntFromNewModelDomain;
    pMD->GetVariableTypes( m_Domain, &ntFromNewModelDomain );

    const pConstNodeTypeVector *nodeTypes = GetDistribFun()->GetNodeTypesVector();
    const CNodeType *nt;
    int i;
    int numObsPos = m_obsPositions.size();
    if( numObsPos )
    {
        for( i = 0; i < numObsPos; i++ )
        {
            nt = (*nodeTypes)[m_obsPositions[i]];
            if( nt->IsDiscrete() )
            {
                
                ntFromNewModelDomain[m_obsPositions[i]] = pMD->GetObsTabVarType();
            }
            else
            {
                ntFromNewModelDomain[m_obsPositions[i]] = pMD->GetObsGauVarType();
                
            }
            
        }
    }
    pNewDistribFun->ResetNodeTypes( ntFromNewModelDomain );
    CFactor* obj = this;
    if( m_pMD->IsAFactorOwner(obj) )
    {
        m_pMD->ReleaseFactor(obj);
        m_pMD = pMD;
        int num = m_pMD->AttachFactor( obj );
        m_factNumInHeap = num;
    }
    else
    {
        m_pMD = pMD;
    }
}
         }
     }
 }
 else 
 {
     for( domainNodes = 0; domainNodes < numberOfFactors; domainNodes++ )
     {
         if( !m_Vector_pEvidences[m_numberOfLearnedEvidences])
         {
             PNL_THROW(CNULLPointer, "evidence")
         }
         factor = grmodel->GetFactor( domainNodes );
         int DomainSize;
         const int *domain;
         factor->GetDomain( &DomainSize, &domain );
         const pConstNodeTypeVector *ntCorr = factor->GetDistribFun()->GetNodeTypesVector();
         if ((*ntCorr)[DomainSize - 1]->GetNodeSize() != 1)
         {
             m_updateCPD = 1;
         }
         const CEvidence ** pEv = new const CEvidence* [m_numberOfAllEvidences - m_numberOfLearnedEvidences];
         int c;
         for( c = 0; c < m_numberOfAllEvidences - m_numberOfLearnedEvidences; ++c)
         {
             pEv[c] = m_Vector_pEvidences[m_numberOfLearnedEvidences + c];
         }
         const CEvidence * const* pEvidences = pEv;
         CGaussianDistribFun* pDistribFun = (CGaussianDistribFun*)(factor->GetDistribFun());
         pDistribFun->BayesUpdateFactor(pEvidences, m_numberOfAllEvidences - m_numberOfLearnedEvidences, domain);
     }
 }