Various cleanups
[libdai.git] / include / dai / factorgraph.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 the FactorGraph class
25 /// \todo Improve documentation
26
27
28 #ifndef __defined_libdai_factorgraph_h
29 #define __defined_libdai_factorgraph_h
30
31
32 #include <iostream>
33 #include <map>
34 #include <dai/bipgraph.h>
35 #include <dai/factor.h>
36
37
38 namespace dai {
39
40
41 /// Represents a factor graph.
42 /** Both Bayesian Networks and Markov random fields can be represented in a
43 * unifying representation, called <em>factor graph</em> [\ref KFL01],
44 * implemented in libDAI by the FactorGraph class.
45 *
46 * Consider a probability distribution over \f$N\f$ discrete random variables
47 * \f$x_0,x_1,\dots,x_N\f$ that factorizes as a product of factors, each of
48 * which depends on some subset of the variables:
49 * \f[
50 * P(x_0,x_1,\dots,x_N) = \frac{1}{Z} \prod_{I=0}^M f_I(x_I), \qquad
51 * Z = \sum_{x_0}\dots\sum_{x_N} \prod_{I=0}^M f_I(X_I).
52 * \f]
53 * Each factor \f$f_I\f$ is a function from an associated subset
54 * of variables \f$X_I \subset \{x_0,x_1,\dots,x_N\}\f$ to the nonnegative
55 * real numbers.
56 *
57 * For a Bayesian network, each factor corresponds to a (conditional)
58 * probability table, whereas for a Markov random field, each factor
59 * corresponds to a maximal clique of the undirected graph.
60 *
61 * Factor graphs explicitly express the factorization structure of the
62 * corresponding probability distribution.
63 *
64 * \todo Alternative implementation of undo factor changes: the only things that have to be
65 * undone currently are setting a factor to 1 and setting a factor to a Kronecker delta. This
66 * could also be implemented in the TFactor itself, which could maintain its state
67 * (ones/delta/full) and act accordingly.
68 */
69 class FactorGraph {
70 public:
71 /// Stores the neighborhood structure
72 BipartiteGraph G;
73
74 /// Shorthand for BipartiteGraph::Neighbor
75 typedef BipartiteGraph::Neighbor Neighbor;
76
77 /// Shorthand for BipartiteGraph::Neighbors
78 typedef BipartiteGraph::Neighbors Neighbors;
79
80 /// Shorthand for BipartiteGraph::Edge
81 typedef BipartiteGraph::Edge Edge;
82
83 /// Iterator over factors
84 typedef std::vector<Factor>::iterator iterator;
85
86 /// Const iterator over factors
87 typedef std::vector<Factor>::const_iterator const_iterator;
88
89
90 private:
91 std::vector<Var> _vars;
92 std::vector<Factor> _factors;
93 std::map<size_t,Factor> _backup;
94
95 public:
96 /// Default constructor
97 FactorGraph() : G(), _vars(), _factors(), _backup() {}
98
99 /// Constructs a FactorGraph from a vector of factors
100 FactorGraph(const std::vector<Factor> &P);
101
102 /// Constructs a FactorGraph from given factor and variable iterators
103 /** \tparam FactorInputIterator Iterator with value_type Factor
104 * \tparam VarInputIterator Iterator with value_type Var
105 * \pre Assumes that the set of variables in [var_begin,var_end) is the union of the variables in the factors in [fact_begin, fact_end)
106 */
107 template<typename FactorInputIterator, typename VarInputIterator>
108 FactorGraph(FactorInputIterator fact_begin, FactorInputIterator fact_end, VarInputIterator var_begin, VarInputIterator var_end, size_t nr_fact_hint = 0, size_t nr_var_hint = 0 );
109
110 /// Destructor
111 virtual ~FactorGraph() {}
112
113 /// Clone *this (virtual copy constructor)
114 virtual FactorGraph* clone() const { return new FactorGraph(); }
115
116 /// Create (virtual default constructor)
117 virtual FactorGraph* create() const { return new FactorGraph(*this); }
118
119 /// Returns const reference to i'th variable
120 const Var & var(size_t i) const { return _vars[i]; }
121 /// Returns const reference to all factors
122 const std::vector<Var> & vars() const { return _vars; }
123 /// Returns reference to I'th factor
124 Factor & factor(size_t I) { return _factors[I]; }
125 /// Returns const reference to I'th factor
126 const Factor & factor(size_t I) const { return _factors[I]; }
127 /// Returns const reference to all factors
128 const std::vector<Factor> & factors() const { return _factors; }
129 /// Returns iterator pointing to first factor
130 iterator begin() { return _factors.begin(); }
131 /// Returns const iterator pointing to first factor
132 const_iterator begin() const { return _factors.begin(); }
133 /// Returns iterator pointing beyond last factor
134 iterator end() { return _factors.end(); }
135 /// Returns const iterator pointing beyond last factor
136 const_iterator end() const { return _factors.end(); }
137
138 /// Returns number of variables
139 size_t nrVars() const { return vars().size(); }
140 /// Returns number of factors
141 size_t nrFactors() const { return factors().size(); }
142 /// Calculates number of edges
143 size_t nrEdges() const { return G.nrEdges(); }
144
145 /// Provides read access to neighbors of variable
146 const Neighbors & nbV( size_t i ) const { return G.nb1(i); }
147 /// Provides full access to neighbors of variable
148 Neighbors & nbV( size_t i ) { return G.nb1(i); }
149 /// Provides read access to neighbors of factor
150 const Neighbors & nbF( size_t I ) const { return G.nb2(I); }
151 /// Provides full access to neighbors of factor
152 Neighbors & nbF( size_t I ) { return G.nb2(I); }
153 /// Provides read access to neighbor of variable
154 const Neighbor & nbV( size_t i, size_t _I ) const { return G.nb1(i)[_I]; }
155 /// Provides full access to neighbor of variable
156 Neighbor & nbV( size_t i, size_t _I ) { return G.nb1(i)[_I]; }
157 /// Provides read access to neighbor of factor
158 const Neighbor & nbF( size_t I, size_t _i ) const { return G.nb2(I)[_i]; }
159 /// Provides full access to neighbor of factor
160 Neighbor & nbF( size_t I, size_t _i ) { return G.nb2(I)[_i]; }
161
162 /// Returns the index of a particular variable
163 size_t findVar( const Var & n ) const {
164 size_t i = find( vars().begin(), vars().end(), n ) - vars().begin();
165 assert( i != nrVars() );
166 return i;
167 }
168
169 /// Returns a set of indexes corresponding to a set of variables
170 std::set<size_t> findVars( VarSet &ns ) const {
171 std::set<size_t> indexes;
172 for( VarSet::const_iterator n = ns.begin(); n != ns.end(); n++ )
173 indexes.insert( findVar( *n ) );
174 return indexes;
175 }
176
177 /// Returns index of the first factor that depends on the variables
178 size_t findFactor(const VarSet &ns) const {
179 size_t I;
180 for( I = 0; I < nrFactors(); I++ )
181 if( factor(I).vars() == ns )
182 break;
183 assert( I != nrFactors() );
184 return I;
185 }
186
187 /// Return all variables that occur in a factor involving the i'th variable, itself included
188 VarSet Delta( unsigned i ) const;
189
190 /// Return all variables that occur in a factor involving some variable in ns, ns itself included
191 VarSet Delta( const VarSet &ns ) const;
192
193 /// Return all variables that occur in a factor involving the i'th variable, n itself excluded
194 VarSet delta( unsigned i ) const;
195
196 /// Return all variables that occur in a factor involving some variable in ns, ns itself excluded
197 VarSet delta( const VarSet & ns ) const {
198 return Delta( ns ) / ns;
199 }
200
201 /// Set the content of the I'th factor and make a backup of its old content if backup == true
202 virtual void setFactor( size_t I, const Factor &newFactor, bool backup = false ) {
203 assert( newFactor.vars() == factor(I).vars() );
204 if( backup )
205 backupFactor( I );
206 _factors[I] = newFactor;
207 }
208
209 /// Set the contents of all factors as specified by facs and make a backup of the old contents if backup == true
210 virtual void setFactors( const std::map<size_t, Factor> & facs, bool backup = false ) {
211 for( std::map<size_t, Factor>::const_iterator fac = facs.begin(); fac != facs.end(); fac++ ) {
212 if( backup )
213 backupFactor( fac->first );
214 setFactor( fac->first, fac->second );
215 }
216 }
217
218 /// Clamp variable n to value i (i.e. multiply with a Kronecker delta \f$\delta_{x_n, i}\f$)
219 /** If backup == true, make a backup of all factors that are changed
220 */
221 virtual void clamp( const Var & n, size_t i, bool backup = false );
222
223 /// Clamp a variable in a factor graph to have one out of a list of values
224 /** If backup == true, make a backup of all factors that are changed
225 */
226 void clampVar( size_t i, const std::vector<size_t> &xis, bool backup = false );
227
228 /// Clamp a factor in a factor graph to have one out of a list of values
229 /** If backup == true, make a backup of all factors that are changed
230 */
231 void clampFactor( size_t I, const std::vector<size_t> &xIs, bool backup = false );
232
233 /// Set all factors interacting with the i'th variable 1
234 virtual void makeCavity( unsigned i, bool backup = false );
235
236 /// Backup the factors specified by indices in facs
237 virtual void backupFactors( const std::set<size_t> & facs );
238
239 /// Restore all factors to the backup copies
240 virtual void restoreFactors();
241
242 /// Returns true if the FactorGraph is connected
243 bool isConnected() const { return G.isConnected(); }
244
245 /// Returns true if the FactorGraph is a tree
246 bool isTree() const { return G.isTree(); }
247
248 /// Returns true if each factor depends on at most two variables
249 bool isPairwise() const;
250
251 /// Returns true if each variable has only two possible values
252 bool isBinary() const;
253
254 /// Reads a FactorGraph from a file
255 void ReadFromFile(const char *filename);
256
257 /// Writes a FactorGraph to a file
258 void WriteToFile(const char *filename) const;
259
260 /// Writes a FactorGraph to a GraphViz .dot file
261 void printDot( std::ostream& os ) const;
262
263 /// Returns the cliques in this FactorGraph
264 std::vector<VarSet> Cliques() const;
265
266 /// Clamp variable v_i to value state (i.e. multiply with a Kronecker delta \f$\delta_{x_{v_i},x}\f$);
267 /** This version changes the factor graph structure and thus returns a newly constructed FactorGraph
268 * and keeps the current one constant, contrary to clamp()
269 */
270 FactorGraph clamped( const Var & v_i, size_t x ) const;
271
272 /// Returns a copy of *this, where all factors that are subsumed by some larger factor are merged with the larger factors.
273 FactorGraph maximalFactors() const;
274
275 /// Makes a backup of the I'th Factor
276 void restoreFactor( size_t I );
277
278 /// Restores the I'th Factor from the backup (it should be backed up first)
279 void backupFactor( size_t I );
280
281 /// Makes a backup of all factors connected to a set of variables
282 void backupFactors( const VarSet &ns );
283 /// Restores all factors connected to a set of variables from their backups
284 void restoreFactors( const VarSet &ns );
285
286 // Friends
287 friend std::ostream& operator << (std::ostream& os, const FactorGraph& fg);
288 friend std::istream& operator >> (std::istream& is, FactorGraph& fg);
289
290 /// @name Backwards compatibility layer (to be removed soon)
291 //@{
292 size_t VV2E(size_t n1, size_t n2) const { return G.VV2E(n1,n2); }
293 const Edge& edge(size_t e) const { return G.edge(e); }
294 void indexEdges() { G.indexEdges(); }
295 size_t nr_edges() const { return G.nr_edges(); }
296 const std::vector<Edge>& edges() const { return G.edges(); }
297 //}@
298
299 private:
300 /// Part of constructors (creates edges, neighbors and adjacency matrix)
301 void constructGraph( size_t nrEdges );
302 };
303
304
305 template<typename FactorInputIterator, typename VarInputIterator>
306 FactorGraph::FactorGraph(FactorInputIterator fact_begin, FactorInputIterator fact_end, VarInputIterator var_begin, VarInputIterator var_end, size_t nr_fact_hint, size_t nr_var_hint ) : G(), _backup() {
307 // add factors
308 size_t nrEdges = 0;
309 _factors.reserve( nr_fact_hint );
310 for( FactorInputIterator p2 = fact_begin; p2 != fact_end; ++p2 ) {
311 _factors.push_back( *p2 );
312 nrEdges += p2->vars().size();
313 }
314
315 // add variables
316 _vars.reserve( nr_var_hint );
317 for( VarInputIterator p1 = var_begin; p1 != var_end; ++p1 )
318 _vars.push_back( *p1 );
319
320 // create graph structure
321 constructGraph( nrEdges );
322 }
323
324
325 } // end of namespace dai
326
327
328 #endif