示例#1
0
int CMNet::GetFactors( int numberOfNodes, const int *nodes,
		               pFactorVector *params ) const
{
    // the function is to find all the factors which are set on
	// the domains	which contain "nodes" as a subset

	// bad-args check
    PNL_CHECK_LEFT_BORDER( numberOfNodes, 1 );
    PNL_CHECK_IS_NULL_POINTER(nodes);
    PNL_CHECK_IS_NULL_POINTER(params);
	// bad-args check end

    params->clear();


    int       numOfClqs;
    const int *clqsNums;

    GetClqsNumsForNode( *nodes, &numOfClqs, &clqsNums );

    assert( numOfClqs > 0 );

    intVector factsNums( clqsNums, clqsNums + numOfClqs );

    const int *ndsIt   = nodes + 1,
              *nds_end = nodes + numberOfNodes;

    for( ; ndsIt != nds_end; ++ndsIt )
    {
        GetClqsNumsForNode( *ndsIt, &numOfClqs, &clqsNums );

        intVector::iterator       factsNumsIt = factsNums.end() - 1;
        intVector::const_iterator factsNums_before_begin
            = factsNums.begin() - 1;

        for( ; factsNumsIt != factsNums_before_begin; --factsNumsIt )
        {
            if( std::find( clqsNums, clqsNums + numOfClqs, *factsNumsIt )
                == clqsNums + numOfClqs )
            {
                factsNums.erase(factsNumsIt);
            }

            if( factsNums.empty() )
            {
                return 0;
            }
        }
    }

    intVector::const_iterator factsNumsIt   = factsNums.begin(),
	factsNums_end = factsNums.end();

    for( ; factsNumsIt != factsNums_end; ++factsNumsIt )
    {
	params->push_back(GetFactor(*factsNumsIt));
    }

    return 1;
}
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;
}
PNL_USING
PNL_BEGIN

/////////////////////////////////////////////////////////////////////////////
//float C2DNumericDenseMatrix

C2DNumericDenseMatrix<float> *C2DNumericDenseMatrix<float>::Create( const int *lineSizes,
                                                                    const float *data, int Clamp )
{
    PNL_CHECK_IS_NULL_POINTER( lineSizes );
    PNL_CHECK_LEFT_BORDER( Clamp, 0 );
    PNL_CHECK_IS_NULL_POINTER( data );

    int i = 0;
    for( i = 0; i < 2; i++ )
    {
        if( lineSizes[i] < 0 )
        {
            PNL_THROW( COutOfRange, "range is negative" )
        }
    }
    PNL_CHECK_IS_NULL_POINTER( data );
    C2DNumericDenseMatrix<float> *pxMatrix = 
                new C2DNumericDenseMatrix<float>( 2, lineSizes, data, (Clamp>0));
    return pxMatrix;
}
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;
}
示例#5
0
CMNet* CMNet::ConvertFromBNetUsingEvidence( const CBNet *pBNet,
					   const CEvidence *pEvidence )
{
    /* bad-args check */
    PNL_CHECK_IS_NULL_POINTER(pBNet);
    PNL_CHECK_IS_NULL_POINTER(pEvidence);
    /* bad-args check end */

    CMNet *pMNet = new CMNet( pBNet, pEvidence );

    PNL_CHECK_IF_MEMORY_ALLOCATED(pMNet);

    return pMNet;
}
示例#6
0
int CMRF2::GetFactors( int numberOfNodes, const int *nodes,
                      pFactorVector *params ) const
{
    /* bad-args check */
    PNL_CHECK_RANGES( numberOfNodes, 1, 2 );
    PNL_CHECK_IS_NULL_POINTER(nodes);
    PNL_CHECK_IS_NULL_POINTER(params);
    /* bad-args check end */
    
    params->clear();
    
    int       numOfClqsFrstNode;
    const int *clqsFrstNode;
    
    GetClqsNumsForNode( *nodes, &numOfClqsFrstNode, &clqsFrstNode );
    
    if( numberOfNodes == 1 )
    {
        const int *clqsIt = clqsFrstNode,
            *clqs_end = clqsFrstNode + numOfClqsFrstNode;
        
        for( ; clqs_end - clqsIt; ++clqsIt )
        {
            params->push_back(GetFactor(*clqsIt));
        }
    }
    else
    {
        int       numOfClqsScndNode;
        const int *clqsScndNode;
        
        GetClqsNumsForNode( *(nodes + 1), &numOfClqsScndNode, &clqsScndNode );
        
        const int *pClqNum = std::find_first_of( clqsFrstNode,
            clqsFrstNode + numOfClqsFrstNode, clqsScndNode,
            clqsScndNode + numOfClqsScndNode );
        
        if( pClqNum == clqsFrstNode + numOfClqsFrstNode )
        {
            return 0;
        }
        
        params->push_back(GetFactor(*pClqNum));
    }
    
    assert( params->size() != 0 );
    
    return 1;
}
void CBKInfEngine::ForwardFirst(const CEvidence *pEvidence, int maximize )
{
    
    PNL_CHECK_FOR_NON_ZERO(GetTime());
    m_JTreeInfIter = m_CRingJtreeInf.begin();
    
    PNL_CHECK_IS_NULL_POINTER(*m_JTreeInfIter);
    PNL_CHECK_IS_NULL_POINTER(pEvidence);
    
    (*m_JTreeInfIter)->ShrinkObserved( pEvidence, maximize );
    (*m_JTreeInfIter)->CollectEvidence();
    
    m_CurrentTime++;
    
}
void
CGaussianCPD::AllocDistribution(const float* pMean, const float* pCov,
				float normCoeff, const float* const* pWeights,
				const int* parentCombination )
{
    PNL_CHECK_IS_NULL_POINTER( pMean );
    PNL_CHECK_IS_NULL_POINTER( pCov );
    if( m_CorrespDistribFun->GetDistributionType() == dtGaussian )
    {
        SetCoefficient( normCoeff, 0 );
	    AllocMatrix( pMean, matMean );
	    AllocMatrix( pCov, matCovariance );
	    if( pWeights )
	    {
	        int numParents = m_Domain.size() - 1;
	        for( int i = 0; i < numParents; i++ )
	        {
		        if( pWeights[i] )
		        {
		            m_CorrespDistribFun->AllocMatrix( pWeights[i],
		                matWeights, i );
		        }
	        }
	    }
    }
    else
    {
	    PNL_CHECK_IS_NULL_POINTER( parentCombination );
        SetCoefficient( normCoeff, parentCombination );
	    AllocMatrix( pMean, matMean, -1, parentCombination );
	    AllocMatrix( pCov, matCovariance,  -1, parentCombination );
	    if( pWeights )
	    {
	        intVector contParentsIndex;
	        static_cast<CCondGaussianDistribFun*>(m_CorrespDistribFun)->
		    GetContinuousParentsIndices(&contParentsIndex);
	        int numParents = contParentsIndex.size();
	        for( int i = 0; i < numParents; i++ )
	        {
		        if( pWeights[i] )
		        {
		            m_CorrespDistribFun->AllocMatrix( pWeights[i],
		                matWeights, i, parentCombination );
		        }
	        }
	    }
    }
}
void CLWSamplingInfEngine::
EnterEvidenceProbability( floatVecVector *pEvidenceProbIn )
{
	PNL_CHECK_IS_NULL_POINTER(pEvidenceProbIn);

	int i, j;
	float w;
	int iSamples = pEvidenceProbIn->size();
	int iSampleSize = m_currentEvVec.size();
	
	if(iSamples != iSampleSize) return;

	for( i = 0; i < iSamples; i++)
	{
		w = 1;
			
		for( j = 0; j < pEvidenceProbIn[i].size(); j++)
		{
		 	w = w * (*pEvidenceProbIn)[i][j];
		}

		m_particleWeight[i] = w;
	}
	
	NormalizeWeight();
}
void CLWSamplingInfEngine::
EnterEvidence( const CEvidence *pEvidenceIn , int maximize , int sumOnMixtureNode  )
{
    PNL_CHECK_IS_NULL_POINTER(pEvidenceIn);
  
    LWSampling(pEvidenceIn);

	// Given evidencs, calculate particle weight by CPD 
	float w;
	int iSampleSize = m_currentEvVec.size();
	const int* ObsNodes = pEvidenceIn->GetAllObsNodes();
	int NumberObsNodes  = pEvidenceIn->GetNumberObsNodes();

	int i, j;

	for( i = 0; i < iSampleSize; i++)
	{
		w = 0;
		for( j = 0; j < NumberObsNodes; j++)
		{
			if(pEvidenceIn->IsNodeObserved(ObsNodes[j]))
			{
				CFactor* pFactor = m_pGraphicalModel->GetFactor(ObsNodes[j]);
		 		w = w +  pFactor->GetLogLik( m_currentEvVec[i]); 
			}
		}

		m_particleWeight[i] = (float)exp(w);
	}

	NormalizeWeight();
}
示例#11
0
void
CGraphPersistence::Save(CPNLBase *pObj, CContextSave *pContext)
{
    CGraph *pG = dynamic_cast<CGraph*>(pObj);
    std::stringstream buf;

    PNL_CHECK_IS_NULL_POINTER(pG);
    int nNode = pG->GetNumberOfNodes();
    intVector neig;
    neighborTypeVector neigType;

    pContext->AddAttribute("SavingType", "ByEdges");

    {
        char buf2[12];

        sprintf(buf2, "%i", nNode);
        pContext->AddAttribute("NumberOfNodes", buf2);
    }

    for(int i = 0; i < nNode; ++i)
    {
        pG->GetNeighbors(i, &neig, &neigType);
        buf << i << ":";
        for(int j = neig.size(); --j >= 0;)
        {
            buf << neig[j] << "_(" << neighTypeSymbols[neigType[j]] << ") ";
        }
        buf << '\n';
    }

    pContext->AddText(buf.str().c_str());
}
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;
}
示例#13
0
void CMNet::ConstructClqsFromBNetsFamiliesAndInternalGraph(const CBNet *pBNet)
{
    // bad-args check
    PNL_CHECK_IS_NULL_POINTER(pBNet);
    // bad-args check end

}
inline void CReferenceCounter::Release(void* pObject)
{
    PNL_CHECK_IS_NULL_POINTER(pObject);
    
    omp_set_lock(&m_release_lock);
    std::list<void*>::iterator location = std::find( m_refList.begin(),
        m_refList.end(), pObject );
    
    assert( location != m_refList.end() );
       
    while( location != m_refList.end() )
    {
        location = std::find( m_refList.erase(location), m_refList.end(),
            pObject );
    }
    
    if( m_refList.empty() )
    {
	omp_unset_lock(&m_release_lock);
        delete this;
    }
    else
    {
        omp_unset_lock(&m_release_lock);
    };
}
示例#15
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 );
}
void CGaussianCPD::UpdateStatisticsEM( const CPotential *pMargPot,
		                     const CEvidence *pEvidence )
{
    if( !pMargPot )
    {
	PNL_THROW( CNULLPointer, "evidences" )//no corresp evidences
    }

    intVector obsPos;
    pMargPot->GetObsPositions(&obsPos);

    if( obsPos.size() && (this->GetDistribFun()->GetDistributionType() != dtCondGaussian) )
    {
	PNL_CHECK_IS_NULL_POINTER(pEvidence);
	CPotential *pExpandPot = pMargPot->ExpandObservedNodes(pEvidence, 0);
	m_CorrespDistribFun->UpdateStatisticsEM(pExpandPot->GetDistribFun(), pEvidence, 1.0f,
	    &m_Domain.front());
	delete pExpandPot;
    }
    else
    {
	m_CorrespDistribFun->UpdateStatisticsEM( pMargPot->GetDistribFun(), pEvidence, 1.0f,
	    &m_Domain.front() );
    }

}
PNL_USING


CStaticStructLearnSEM::CStaticStructLearnSEM( CBNet* pBNet, 
						  ELearningTypes LearnType, 
						  int nMaxFanIn,
						  intVector* vAncestor,
						  intVector* vDescent ):
						  CStaticLearningEngine( pBNet, LearnType )
{
	PNL_CHECK_IS_NULL_POINTER(pBNet);
	int i;
	m_nMaxFanIn = nMaxFanIn;
	m_nNodes = pBNet->GetNumberOfNodes();
	if(vAncestor)
		m_vAncestor.assign(vAncestor->begin(), vAncestor->end());
	if(vDescent)
		m_vDescent.assign(vDescent->begin(), vDescent->end());
	m_pCurrBNet = pBNet->Copy(pBNet);
	m_vGlobalRenaming.assign(m_nNodes, 0);
	for(i=0; i<m_nNodes; i++)
	{
		m_vGlobalRenaming[i] = i;
	}
	m_IterEM = 8;
	m_maxLoop = 50;
	m_minProgress = (float)1e-3;
}
示例#18
0
CBKInfEngine* CBKInfEngine::
Create(const CDynamicGraphicalModel *pGrModel, intVecVector& clusters)
{
    PNL_CHECK_IS_NULL_POINTER(pGrModel);
    std::string description;

    if( !pGrModel->IsValid( &description ))
    {
	PNL_THROW( CBadArg, description );
    }
    if( pGrModel->GetModelType() != mtDBN )
    {
	PNL_THROW( CInconsistentType, " input model is invalid " );
    }
    
    intVector interfNds;
    pGrModel->GetInterfaceNodes( &interfNds );
    
    if( !CheckClustersValidity(clusters, interfNds) )
    {
	PNL_THROW( CBadArg, " clusters are invalid " );
    } 
    
    return new CBKInfEngine(pGrModel, clusters);
    
}
void CSoftMaxCPD::AllocDistribution(const float* pWeights, 
                                    const float* pOffsets, const int* parentCombination)
{
    PNL_CHECK_IS_NULL_POINTER(pWeights);
    PNL_CHECK_IS_NULL_POINTER(pOffsets);
    
////////////////////////////////////////////////
    const CNodeType *nt;
    nt = GetModelDomain()->GetVariableType( m_Domain[m_Domain.size()-1] );
    int SoftMaxSize = nt->GetNodeSize();

    if (SoftMaxSize == 2)
    {
      int matSize = 0;
      int i;
      for (i = 0; i < m_Domain.size(); i++)
      {
         nt = GetModelDomain()->GetVariableType( m_Domain[i] );
         if(!(nt->IsDiscrete()))
         {
            matSize ++;
         }
      }
      //matSize = matSize;

      for (i = 0; i < 2*matSize-1; i+=2)
      {
        if (pWeights[i] - pWeights[i+1] == 0)
          PNL_THROW(CNotImplemented, "sigmoid must have distinct weights");
      }
    }
////////////////////////////////////////////////

    if (m_CorrespDistribFun->GetDistributionType() == dtSoftMax)
    {
        AllocMatrix(pWeights, matWeights);
        static_cast<CSoftMaxDistribFun*>(m_CorrespDistribFun)->
            AllocOffsetVector(pOffsets);
    }
    else
    {
        PNL_CHECK_IS_NULL_POINTER(parentCombination);
        AllocMatrix(pWeights, matWeights, -1, parentCombination);
        static_cast<CCondSoftMaxDistribFun*>(m_CorrespDistribFun)->
            AllocOffsetVector(pOffsets, parentCombination);
    }
}
inline void CReferenceCounter::AddRef(void* pObject)
{
    PNL_CHECK_IS_NULL_POINTER(pObject);
    
    omp_set_lock(&m_release_lock);
    m_refList.push_back(pObject);
    omp_unset_lock(&m_release_lock);
}
示例#21
0
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;
}
示例#22
0
PNL_USING

void CGraphicalModel::GetNodeTypes(nodeTypeVector* nodeTypesOut)
{
    PNL_CHECK_IS_NULL_POINTER(nodeTypesOut);

    m_pMD->GetVariableTypes(nodeTypesOut);
};
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;
}
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;
}
CGaussianCPD*
CGaussianCPD::CreateUnitFunctionCPD(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* resCPD = new CGaussianCPD( domain, nNodes, pMD );
    intVector dom = intVector( domain, domain + nNodes );
    pConstNodeTypeVector ntVec;
    pMD->GetVariableTypes( dom, &ntVec );
    CGaussianDistribFun* UniData =
	CGaussianDistribFun::CreateUnitFunctionDistribution( nNodes,
	&ntVec.front(), 0, 0);
    delete (resCPD->m_CorrespDistribFun);
    resCPD->m_CorrespDistribFun = UniData;
    return resCPD;
}
示例#26
0
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;
}
示例#27
0
void CFactor::AttachMatrix(CMatrix<float> *matrix, 
                           EMatrixType mType, int numberOfMatrix, 
                           const int* discrParentValuesIndices)
{
    /*Attach the matrix as m_Type for this type of DistribFun*/
    PNL_CHECK_IS_NULL_POINTER( matrix );
    
    m_CorrespDistribFun->AttachMatrix( matrix, mType, numberOfMatrix,
        discrParentValuesIndices );
}
void CJtreeInfEngine::
DivideJTreeNodePotByDistribFun( int clqPotNum, const int *domain,
			       const CDistribFun *pDistrFun )
{
    // bad-args check
    PNL_CHECK_RANGES( clqPotNum, 0, m_pJTree->GetNumberOfNodes() - 1 );
    PNL_CHECK_IS_NULL_POINTER(domain);
    PNL_CHECK_IS_NULL_POINTER(pDistrFun);
    // bad-args check end

    CPotential *pNodePot = m_pJTree->GetNodePotential(clqPotNum);

    int       nodePotDomSz;
    const int *nodePotDomain;

    pNodePot->GetDomain( &nodePotDomSz, &nodePotDomain );

    pNodePot->GetDistribFun()->DivideInSelfData( nodePotDomain, domain,
	pDistrFun );
}
示例#29
0
void CBKInfEngine::BackwardT()
{
    PNL_CHECK_IS_NULL_POINTER(*m_JTreeInfIter);
    
    (*m_JTreeInfIter)->DistributeEvidence();
    
    if(m_CRingDistrOnSep.size())
    {
	m_CDistrOnSepIter--;
    }
}
示例#30
0
void CMNet::GetFactors( int numberOfNodes, const int *nodes,
					    int *numberOfFactors, CFactor ***params ) const
{
	// bad-args check
    PNL_CHECK_LEFT_BORDER( numberOfNodes, 1 );
    PNL_CHECK_IS_NULL_POINTER(nodes);
    PNL_CHECK_IS_NULL_POINTER(numberOfFactors);
    PNL_CHECK_IS_NULL_POINTER(params);
	// bad-args check end

	if( GetFactors( numberOfNodes, nodes, &m_paramsForNodes ) )
	{
		*numberOfFactors = m_paramsForNodes.size();
        *params             = &m_paramsForNodes.front();
	}
    else
    {
        *numberOfFactors = 0;
        *params             = NULL;
    }
}