Real ExactInf::run() { if( props.verbose >= 1 ) cerr << "Starting " << identify() << "..."; Factor P; for( size_t I = 0; I < nrFactors(); I++ ) P *= factor(I); Real Z = P.sum(); _logZ = std::log(Z); for( size_t i = 0; i < nrVars(); i++ ) _beliefsV[i] = P.marginal(var(i)); for( size_t I = 0; I < nrFactors(); I++ ) _beliefsF[I] = P.marginal(factor(I).vars()); if( props.verbose >= 1 ) cerr << "finished" << endl; return 0.0; }
LC::LC( const FactorGraph & fg, const PropertySet &opts ) : DAIAlgFG(fg), _pancakes(), _cavitydists(), _phis(), _beliefs(), _maxdiff(0.0), _iters(0), props() { setProperties( opts ); // create pancakes _pancakes.resize( nrVars() ); // create cavitydists for( size_t i=0; i < nrVars(); i++ ) _cavitydists.push_back(Factor( delta(i) )); // create phis _phis.reserve( nrVars() ); for( size_t i = 0; i < nrVars(); i++ ) { _phis.push_back( vector<Factor>() ); _phis[i].reserve( nbV(i).size() ); foreach( const Neighbor &I, nbV(i) ) _phis[i].push_back( Factor( factor(I).vars() / var(i) ) ); } // create beliefs _beliefs.reserve( nrVars() ); for( size_t i=0; i < nrVars(); i++ ) _beliefs.push_back(Factor(var(i))); }
void ExactInf::init() { for( size_t i = 0; i < nrVars(); i++ ) _beliefsV[i].fill( 1.0 ); for( size_t I = 0; I < nrFactors(); I++ ) _beliefsF[I].fill( 1.0 ); }
Real MR::calcCavityCorrelations() { Real md = 0.0; for( size_t i = 0; i < nrVars(); i++ ) { vector<Factor> pairq; if( props.inits == Properties::InitType::EXACT ) { JTree jtcav(*this, PropertySet()("updates", string("HUGIN"))("verbose", (size_t)0) ); jtcav.makeCavity( i ); pairq = calcPairBeliefs( jtcav, delta(i), false, true ); } else if( props.inits == Properties::InitType::CLAMPING ) { BP bpcav(*this, PropertySet()("updates", string("SEQMAX"))("tol", (Real)1.0e-9)("maxiter", (size_t)10000)("verbose", (size_t)0)("logdomain", false)); bpcav.makeCavity( i ); pairq = calcPairBeliefs( bpcav, delta(i), false, true ); md = std::max( md, bpcav.maxDiff() ); } else if( props.inits == Properties::InitType::RESPPROP ) { BP bpcav(*this, PropertySet()("updates", string("SEQMAX"))("tol", (Real)1.0e-9)("maxiter", (size_t)10000)("verbose", (size_t)0)("logdomain", false)); bpcav.makeCavity( i ); bpcav.makeCavity( i ); bpcav.init(); bpcav.run(); BBP bbp( &bpcav, PropertySet()("verbose",(size_t)0)("tol",(Real)1.0e-9)("maxiter",(size_t)10000)("damping",(Real)0.0)("updates",string("SEQ_MAX")) ); bforeach( const Neighbor &j, G.nb(i) ) { // Create weights for magnetization of some spin Prob p( 2, 0.0 ); p.set( 0, -1.0 ); p.set( 1, 1.0 ); // BBP cost function would be the magnetization of spin j vector<Prob> b1_adj; b1_adj.reserve( nrVars() ); for( size_t l = 0; l < nrVars(); l++ ) if( l == j ) b1_adj.push_back( p ); else b1_adj.push_back( Prob( 2, 0.0 ) ); bbp.init_V( b1_adj ); // run BBP to estimate adjoints bbp.run(); bforeach( const Neighbor &k, G.nb(i) ) { if( k != j ) cors[i][j.iter][k.iter] = (bbp.adj_psi_V(k)[1] - bbp.adj_psi_V(k)[0]); else cors[i][j.iter][k.iter] = 0.0; } } } if( props.inits != Properties::InitType::RESPPROP ) { for( size_t jk = 0; jk < pairq.size(); jk++ ) { VarSet::const_iterator kit = pairq[jk].vars().begin(); size_t j = findVar( *(kit) ); size_t k = findVar( *(++kit) ); pairq[jk].normalize(); Real cor = (pairq[jk][3] - pairq[jk][2] - pairq[jk][1] + pairq[jk][0]) - (pairq[jk][3] + pairq[jk][2] - pairq[jk][1] - pairq[jk][0]) * (pairq[jk][3] - pairq[jk][2] + pairq[jk][1] - pairq[jk][0]); size_t _j = G.findNb(i,j); size_t _k = G.findNb(i,k); cors[i][_j][_k] = cor; cors[i][_k][_j] = cor; } } }