libDAI
glc.h
Go to the documentation of this file.
1 /* This file is part of libDAI - http://www.libdai.org/
2  *
3  * Copyright (c) 2006-2012, The libDAI authors. All rights reserved.
4  *
5  * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6  */
7 
8 
12 
13 
14 #ifndef __defined_libdai_glc_h
15 #define __defined_libdai_glc_h
16 
17 
18 #include <dai/dai_config.h>
19 #ifdef DAI_WITH_GLC
20 
21 
22 #include <algorithm>
23 #include <set>
24 #include <string>
25 #include <dai/util.h>
26 #include <dai/daialg.h>
27 #include <dai/enum.h>
28 #include <dai/cobwebgraph.h>
29 #include <dai/properties.h>
30 #include <dai/exceptions.h>
31 
32 
33 namespace dai {
34 
35 
37 
43 class Cobweb {
44  public:
46  DAIAlgFG *cav;
47 
49 
57  std::map<int, size_t> _g2l;
58 
60  Cobweb(){}
61 
63  Cobweb( const std::map<int, size_t>& g2l ): cav(0), _g2l(g2l) {}
64 
66  virtual ~Cobweb(){ delete cav; }
67 
69  void setInfAlg( DAIAlgFG* alg ) {
70  cav = alg;
71  }
72 
74  Factor factor( size_t I ) {
75  return cav->factor( _g2l[I] );
76  }
77 
79  void initialize() {
80  cav->init();
81  }
82 
84  Factor marginal( const VarSet& ns, const std::vector<size_t>& rmgind ) {
85  // Set the factors in rmgind to one
86  VarSet vs;
87  std::vector<size_t> rmlinds; // local index
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] );
92  }
93  cav->makeRegionCavity( rmlinds, true );
94  // Initialize if necessary
95  cav->init( vs );
96  cav->run();
97  Factor result;
98  result = cav->belief( ns );
99  cav->restoreFactors();
100  return result;
101  }
102 
104 
106  Factor marginal( const int& outmsgind, const std::vector<size_t>& rmgind ) {
107  // set the factors in rmgind to one
108  VarSet vs;
109  std::vector<size_t> rmlinds; // local index
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] );
114  }
115  cav->makeRegionCavity( rmlinds, true );
116  // Initialize if necessary
117  cav->init( vs );
118  cav->run();
119  Factor result;
120  result = cav->beliefF( _g2l[outmsgind] );
121  cav->restoreFactors();
122  return result;
123  }
124 
126  void updateFactor( int gind, const Factor& msg, bool multiply=false ) {
127  if( !multiply )
128  cav->setFactor( _g2l[gind], msg, false );
129  else
130  cav->setFactor( _g2l[gind], (msg*(cav->factor( _g2l[gind] ))).normalized(), false );
131  }
132 
134  Factor belief( const Var v ) {
135  cav->run();
136  return cav->belief( v );
137  }
138 
140  Factor belief( const VarSet& vs ) {
141  cav->run();
142  return cav->belief( vs );
143  }
144 };
145 
146 
148 
158 class GLC : public DAIAlgCG {
159  private:
161  std::vector<Cobweb> _CWs;
162 
164  std::vector<int> _outOffset;
165 
167 
172  std::vector<std::vector<Factor> > _cavitydists;
173 
175  std::vector<Factor> _beliefs;
176 
178  std::map<VarSet, Factor> _factorBeliefs;
179 
181  Real _maxdiff;
182 
184  size_t _iters;
185 
186  public:
188  struct Properties {
190 
196  DAI_ENUM(CavityType,FULL,PAIR,PAIR2,UNIFORM);
197 
199 
210  DAI_ENUM(RegionType,SINGLE,FACTOR,OVFACTOR,LOOP,OVLOOP,DELTA,OVDELTA)
211 
212 
213 
217  DAI_ENUM(UpdateType,SEQFIX,SEQRND);
218 
220  size_t verbose;
221 
222  // Maximum length of the loops to use in construction of loopy regions
223  size_t loopdepth;
224 
226  size_t maxiter;
227 
229  Real maxtime;
230 
232  Real tol;
233 
235  bool reinit;
236 
238 
241  bool auxfactors;
242 
244  CavityType cavity;
245 
246  // Type of region
247  RegionType rgntype;
248 
250  UpdateType updates;
251 
259  CobwebGraph::NeighborType neighbors;
260 
262  std::string cavainame;
263 
265  PropertySet cavaiopts;
266 
267  // Name of the algorithm used for marginalization over a region (EXACT is fine)
268  std::string inainame;
269 
270  // Parameters for algorithm used for marginalization over a region
271  PropertySet inaiopts;
272  } props;
273 
274 
275  public:
277  GLC() : DAIAlgCG(), _CWs(),_outOffset(), _cavitydists(), _beliefs(), _factorBeliefs(), _maxdiff(), _iters(), props() {}
278 
280 
283  GLC( const FactorGraph &fg, const PropertySet &opts );
284 
286 
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 &/*ns*/ ) { init(); }
297  virtual Real run();
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;
305 
307 
308  const Cobweb& CW( size_t alpha ) const {
310  DAI_DEBASSERT( alpha < nrCWs() );
311  return _CWs[alpha];
312  }
313 
315  Cobweb& CW( size_t alpha ) {
316  DAI_DEBASSERT( alpha < nrCWs() );
317  return _CWs[alpha];
318  }
319 
321  Real CalcCavityDist( size_t i, const std::string &name, const PropertySet &opts );
322 
324  Real InitCavityDists( const std::string &name, const PropertySet &opts );
325 
327  void NewPancake( size_t R, size_t _R2 );
328 
330 
332  void OVNewPancake( size_t R );
333 
335  void CalcBelief( size_t i, bool isFinal = false );
336 
338  void CalcFactorBelief( size_t I );
339 
341  std::vector<SmallSet<size_t> > calcRegions();
342 
344  void initCWs();
345 
347 
351  void setCWs( const std::string &name, const PropertySet &opts );
352 
354 
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 );
357 
359 
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 );
363 };
364 
365 
366 } // end of namespace dai
367 
368 
369 #endif
370 
371 
372 #endif
TFactor< Real > Factor
Represents a factor with values of type dai::Real.
Definition: factor.h:640
STL namespace.
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