Significant improvement of documentation
[libdai.git] / include / dai / daialg.h
1 /* Copyright (C) 2006-2008 Joris Mooij [joris dot mooij at tuebingen dot mpg dot de]
2 Radboud University Nijmegen, The Netherlands /
3 Max Planck Institute for Biological Cybernetics, Germany
4
5 This file is part of libDAI.
6
7 libDAI is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 libDAI is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with libDAI; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22
23 /// \file
24 /// \brief Defines abstract base class InfAlg, its descendants DAIAlg<T>, the specializations DAIAlgFG and DAIAlgRG and some generic inference methods.
25
26
27 #ifndef __defined_libdai_daialg_h
28 #define __defined_libdai_daialg_h
29
30
31 #include <string>
32 #include <iostream>
33 #include <vector>
34 #include <dai/factorgraph.h>
35 #include <dai/regiongraph.h>
36
37
38 namespace dai {
39
40
41 /// InfAlg is an abstract base class, defining the common interface of all inference algorithms in libDAI.
42 class InfAlg {
43 public:
44 /// Virtual desctructor (needed because this class contains virtual functions)
45 virtual ~InfAlg() {}
46
47 public:
48 /// Returns a pointer to a new, cloned copy of *this (i.e., virtual copy constructor)
49 virtual InfAlg* clone() const = 0;
50
51 /// Returns a pointer to a newly constructed object *this (i.e., virtual default constructor)
52 virtual InfAlg* create() const = 0;
53
54 /// Identifies itself for logging purposes
55 virtual std::string identify() const = 0;
56
57 /// Returns the "belief" (i.e., approximate marginal probability distribution) of a variable
58 virtual Factor belief( const Var &n ) const = 0;
59
60 /// Returns the "belief" (i.e., approximate marginal probability distribution) of a set of variables
61 virtual Factor belief( const VarSet &n ) const = 0;
62
63 /// Returns all "beliefs" (i.e., approximate marginal probability distribution) calculated by the algorithm
64 virtual std::vector<Factor> beliefs() const = 0;
65
66 /// Returns the logarithm of the (approximated) partition sum (normalizing constant of the factor graph)
67 virtual Real logZ() const = 0;
68
69 /// Initializes all data structures of the approximate inference algorithm
70 /** This method should be called at least once before run() is called
71 */
72 virtual void init() = 0;
73
74 /// Initializes all data structures corresponding to some set of variables
75 /** This method can be used to do a partial initialization after a part of the factor graph has changed.
76 * Instead of initializing all data structures, it only initializes those involving the variables in ns.
77 */
78 virtual void init( const VarSet &ns ) = 0;
79
80 /// Runs the approximate inference algorithm
81 /* Before run() is called the first time, init() should be called.
82 * If run() returns successfully, the results can be queried using the methods belief(), beliefs() and logZ().
83 */
84 virtual double run() = 0;
85
86 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
87 virtual void clamp( const Var & n, size_t i, bool backup = false ) = 0;
88
89 /// Set all factors interacting with var(i) to 1
90 virtual void makeCavity( size_t i, bool backup = false ) = 0;
91
92 /// Return maximum difference between single node beliefs in the last pass
93 /// \throw Exception if not implemented/supported
94 virtual double maxDiff() const = 0;
95
96 /// Return number of passes over the factorgraph
97 /// \throw Exception if not implemented/supported
98 virtual size_t Iterations() const = 0;
99
100
101 /// Get reference to underlying FactorGraph
102 virtual FactorGraph &fg() = 0;
103
104 /// Get const reference to underlying FactorGraph
105 virtual const FactorGraph &fg() const = 0;
106
107 /// Save factor I
108 virtual void backupFactor( size_t I ) = 0;
109 /// Save Factors involving ns
110 virtual void backupFactors( const VarSet &ns ) = 0;
111
112 /// Restore factor I
113 virtual void restoreFactor( size_t I ) = 0;
114 /// Restore Factors involving ns
115 virtual void restoreFactors( const VarSet &ns ) = 0;
116 };
117
118
119 /// Combines an InfAlg and a graphical model, e.g., a FactorGraph or RegionGraph
120 /** \tparam GRM Should be castable to FactorGraph
121 */
122 template <class GRM>
123 class DAIAlg : public InfAlg, public GRM {
124 public:
125 /// Default constructor
126 DAIAlg() : InfAlg(), GRM() {}
127
128 /// Construct from GRM
129 DAIAlg( const GRM &grm ) : InfAlg(), GRM(grm) {}
130
131 /// Copy constructor
132 DAIAlg( const DAIAlg & x ) : InfAlg(x), GRM(x) {}
133
134 /// Assignment operator
135 DAIAlg & operator=( const DAIAlg &x ) {
136 if( this != &x ) {
137 InfAlg::operator=(x);
138 GRM::operator=(x);
139 }
140 return *this;
141 }
142
143 /// Save factor I
144 void backupFactor( size_t I ) { GRM::backupFactor( I ); }
145 /// Save Factors involving ns
146 void backupFactors( const VarSet &ns ) { GRM::backupFactors( ns ); }
147
148 /// Restore factor I
149 void restoreFactor( size_t I ) { GRM::restoreFactor( I ); }
150 /// Restore Factors involving ns
151 void restoreFactors( const VarSet &ns ) { GRM::restoreFactors( ns ); }
152
153 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
154 void clamp( const Var & n, size_t i, bool backup = false ) { GRM::clamp( n, i, backup ); }
155
156 /// Set all factors interacting with var(i) to 1
157 void makeCavity( size_t i, bool backup = false ) { GRM::makeCavity( i, backup ); }
158
159 /// Get reference to underlying FactorGraph
160 FactorGraph &fg() { return (FactorGraph &)(*this); }
161
162 /// Get const reference to underlying FactorGraph
163 const FactorGraph &fg() const { return (const FactorGraph &)(*this); }
164 };
165
166
167 /// Base class for inference algorithms that operate on a FactorGraph
168 typedef DAIAlg<FactorGraph> DAIAlgFG;
169
170 /// Base class for inference algorithms that operate on a RegionGraph
171 typedef DAIAlg<RegionGraph> DAIAlgRG;
172
173
174 Factor calcMarginal( const InfAlg & obj, const VarSet & ns, bool reInit );
175 std::vector<Factor> calcPairBeliefs( const InfAlg & obj, const VarSet& ns, bool reInit );
176 std::vector<Factor> calcPairBeliefsNew( const InfAlg & obj, const VarSet& ns, bool reInit );
177 Factor calcMarginal2ndO( const InfAlg & obj, const VarSet& ns, bool reInit );
178
179
180 } // end of namespace dai
181
182
183 #endif