14 #ifndef __defined_libdai_glc_h
15 #define __defined_libdai_glc_h
57 std::map<int, size_t> _g2l;
63 Cobweb(
const std::map<int, size_t>& g2l ): cav(0), _g2l(g2l) {}
66 virtual ~Cobweb(){
delete cav; }
74 Factor factor(
size_t I ) {
75 return cav->factor( _g2l[I] );
84 Factor marginal(
const VarSet& ns,
const std::vector<size_t>& rmgind ) {
87 std::vector<size_t> rmlinds;
88 rmlinds.reserve( rmgind.size() );
89 for( std::vector<size_t>::const_iterator it = rmgind.begin(); it != rmgind.end(); it++ ) {
90 vs |= cav->factor( _g2l[*it] ).vars();
91 rmlinds.push_back( _g2l[*it] );
93 cav->makeRegionCavity( rmlinds,
true );
98 result = cav->belief( ns );
99 cav->restoreFactors();
106 Factor marginal(
const int& outmsgind,
const std::vector<size_t>& rmgind ) {
109 std::vector<size_t> rmlinds;
110 rmlinds.reserve( rmgind.size() );
111 for( std::vector<size_t>::const_iterator it = rmgind.begin(); it != rmgind.end(); it++ ) {
112 vs |= cav->factor( _g2l[*it] ).vars();
113 rmlinds.push_back( _g2l[*it] );
115 cav->makeRegionCavity( rmlinds,
true );
120 result = cav->beliefF( _g2l[outmsgind] );
121 cav->restoreFactors();
126 void updateFactor(
int gind,
const Factor& msg,
bool multiply=
false ) {
128 cav->setFactor( _g2l[gind], msg,
false );
130 cav->setFactor( _g2l[gind], (msg*(cav->factor( _g2l[gind] ))).normalized(), false );
134 Factor belief(
const Var v ) {
136 return cav->belief( v );
140 Factor belief(
const VarSet& vs ) {
142 return cav->belief( vs );
161 std::vector<Cobweb> _CWs;
164 std::vector<int> _outOffset;
172 std::vector<std::vector<Factor> > _cavitydists;
175 std::vector<Factor> _beliefs;
178 std::map<VarSet, Factor> _factorBeliefs;
196 DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM);
210 DAI_ENUM(RegionType,SINGLE,FACTOR,OVFACTOR,LOOP,OVLOOP,DELTA,OVDELTA)
259 CobwebGraph::NeighborType neighbors;
262 std::
string cavainame;
265 PropertySet cavaiopts;
268 std::
string inainame;
271 PropertySet inaiopts;
277 GLC() :
DAIAlgCG(), _CWs(),_outOffset(), _cavitydists(), _beliefs(), _factorBeliefs(), _maxdiff(), _iters(), props() {}
283 GLC(
const FactorGraph &fg,
const PropertySet &opts );
287 virtual GLC* clone()
const {
return new GLC(*
this); }
288 virtual GLC* construct(
const FactorGraph &fg,
const PropertySet &opts )
const {
return new GLC( fg, opts ); }
289 virtual std::string name()
const {
return "GLC"; }
290 virtual Factor belief(
const Var &v )
const {
return beliefV( findVar( v ) ); }
291 virtual Factor belief(
const VarSet &vs )
const;
292 virtual Factor beliefV(
size_t i )
const {
return _beliefs[i]; }
293 virtual std::vector<Factor> beliefs()
const;
294 virtual Real logZ()
const { DAI_THROW(NOT_IMPLEMENTED);
return 0.0; }
295 virtual void init(){ initCWs(); }
296 virtual void init(
const VarSet & ) { init(); }
298 virtual Real maxDiff()
const {
return _maxdiff; }
299 virtual size_t Iterations()
const {
return _iters; }
300 virtual void setMaxIter(
size_t maxiter ) { props.maxiter = maxiter; }
301 virtual void setProperties(
const PropertySet &opts );
302 virtual PropertySet getProperties()
const;
303 virtual std::string printProperties()
const;
308 const Cobweb& CW(
size_t alpha )
const {
315 Cobweb& CW(
size_t alpha ) {
321 Real CalcCavityDist(
size_t i,
const std::string &name,
const PropertySet &opts );
324 Real InitCavityDists(
const std::string &name,
const PropertySet &opts );
327 void NewPancake(
size_t R,
size_t _R2 );
332 void OVNewPancake(
size_t R );
335 void CalcBelief(
size_t i,
bool isFinal =
false );
338 void CalcFactorBelief(
size_t I );
341 std::vector<SmallSet<size_t> > calcRegions();
351 void setCWs(
const std::string &name,
const PropertySet &opts );
356 void findLoopClusters( SmallSet<size_t>& remaining, std::set<SmallSet<size_t> > &allcl, SmallSet<size_t> newcl,
const size_t& root,
size_t length, SmallSet<size_t> vars );
361 void findOVLoopClusters( SmallSet<size_t>& remaining, std::set<SmallSet<size_t> > &allcl, SmallSet<size_t> newcl,
const size_t& root,
size_t length, SmallSet<size_t> vars );
TFactor< Real > Factor
Represents a factor with values of type dai::Real.
Definition: factor.h:640
double Real
Real number (alias for double, which could be changed to long double if necessary) ...
Definition: util.h:98
Defines the DAI_ENUM macro, which can be used to define an enum with additional functionality.
Defines class CobwebGraph, which implements a type of region graph used by GLC.
DAIAlg< CobwebGraph > DAIAlgCG
Base class for GLC that operates on CobwebGraph.
Definition: daialg.h:269
Defines the Exception class and macros for throwing exceptions and doing assertions.
DAI_ENUM(LDPCType, SMALL, GROUP, RANDOM)
Possible LDPC structures.
Defines general utility functions and adds an abstraction layer for platform-dependent functionality...
Defines the Property and PropertySet classes, which are mainly used for managing parameters of infere...
DAIAlg< FactorGraph > DAIAlgFG
Base class for inference algorithms that operate on a FactorGraph.
Definition: daialg.h:263
Namespace for libDAI.
Definition: alldai.cpp:16
Defines the general interface for inference methods in libDAI (classes InfAlg, DaiAlg<>, DaiAlgFG and DaiAlgRG).
Allows the user to specify which algorithms will be built into libDAI.
#define DAI_DEBASSERT(x)
Assertion mechanism similar to DAI_ASSERT which is only active if DAI_DEBUG is defined.
Definition: exceptions.h:65