//------------------------------------------------------------------------------------------------ // function for belief propagation //------------------------------------------------------------------------------------------------ double BPFlow::MessagePassing(int nIterations,int nHierarchy,double* pEnergyList) { AllocateMessage(); if(nHierarchy>0) { BPFlow bp; generateCoarserLevel(bp); bp.MessagePassing(20,nHierarchy-1); bp.propagateFinerLevel(*this); } if(pX!=NULL) _Release1DBuffer(pX); pX=new int[Area*2]; double energy; for(int count=0;count<nIterations;count++) { //Bipartite(count); BP_S(count); //TRW_S(count); //FindOptimalSolutionSequential(); ComputeBelief(); FindOptimalSolution(); energy=GetEnergy(); if(IsDisplay) printf("No. %d energy: %f...\n",count,energy); if(pEnergyList!=NULL) pEnergyList[count]=energy; } return energy; }
void CSpecPearlInfEngine::ParallelProtocol() { if( m_maxNumberOfIterations == 0 ) { SetMaxNumberOfIterations(m_numOfNdsInModel); } int i, j; int converged = 0; int changed = 0; int iter = 0; const CGraph *pGraph = m_pGraphicalModel->GetGraph(); int nNodes = m_connNodes.size(); //set current state m_curState = 0; m_nextState = 1; while ((!converged)&&( iter<m_maxNumberOfIterations )) { //work with new data int numOfNeighb; const int *neighbors; const ENeighborType *orientation; for( i = 0; i < nNodes; i++ ) { pGraph->GetNeighbors( m_connNodes[i], &numOfNeighb, &neighbors, &orientation ); for( j = 0; j < numOfNeighb; j++) { //delete m_newMessages[m_connNodes[i]][j]; ComputeMessage( m_connNodes[i], neighbors[j], orientation[j], m_curMessages[m_nextState][m_connNodes[i]][j] ); } } //need to replace all old messages with new m_curState = m_nextState; m_nextState = 1 - m_curState; //compute beliefs changed = 0; for( i = 0; i < nNodes; i++ ) { if( !m_areReallyObserved[m_connNodes[i]]) { ComputeBelief( m_connNodes[i] ); changed += !m_beliefs[m_curState][m_connNodes[i]]->IsFactorsDistribFunEqual( m_beliefs[m_nextState][m_connNodes[i]], m_tolerance); } } converged = !(changed); iter++; }//while ((!converged)&&(iter<m_numberOfIterations)) m_IterationCounter = iter; //need to set both states to valid state m_curState = m_nextState; }