Small changes
[libdai.git] / include / dai / factorgraph.h
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #ifndef __defined_libdai_factorgraph_h
23 #define __defined_libdai_factorgraph_h
24
25
26 #include <iostream>
27 #include <map>
28 #include <dai/bipgraph.h>
29 #include <dai/factor.h>
30
31
32 namespace dai {
33
34
35 bool hasShortLoops( const std::vector<Factor> &P );
36 void RemoveShortLoops( std::vector<Factor> &P );
37
38
39 class FactorGraph {
40 public:
41 BipartiteGraph G;
42 std::vector<Var> vars;
43 std::vector<Factor> factors;
44 typedef BipartiteGraph::Neighbor Neighbor;
45 typedef BipartiteGraph::Neighbors Neighbors;
46 typedef BipartiteGraph::Edge Edge;
47
48 protected:
49 std::map<size_t,Prob> _undoProbs;
50
51 public:
52 /// Default constructor
53 FactorGraph() : G(), vars(), factors(), _undoProbs() {};
54 /// Copy constructor
55 FactorGraph(const FactorGraph & x) : G(x.G), vars(x.vars), factors(x.factors), _undoProbs(x._undoProbs) {};
56 /// Construct FactorGraph from vector of Factors
57 FactorGraph(const std::vector<Factor> &P);
58 // Construct a FactorGraph from given factor and variable iterators
59 template<typename FactorInputIterator, typename VarInputIterator>
60 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 );
61
62 /// Assignment operator
63 FactorGraph & operator=(const FactorGraph & x) {
64 if( this != &x ) {
65 G = x.G;
66 vars = x.vars;
67 factors = x.factors;
68 _undoProbs = x._undoProbs;
69 }
70 return *this;
71 }
72 virtual ~FactorGraph() {}
73
74 // aliases
75 Var & var(size_t i) { return vars[i]; }
76 const Var & var(size_t i) const { return vars[i]; }
77 Factor & factor(size_t I) { return factors[I]; }
78 const Factor & factor(size_t I) const { return factors[I]; }
79
80 size_t nrVars() const { return vars.size(); }
81 size_t nrFactors() const { return factors.size(); }
82 size_t nrEdges() const { return G.nrEdges(); }
83
84 /// Provides read access to neighbors of variable
85 const Neighbors & nbV( size_t i ) const { return G.nb1(i); }
86 /// Provides full access to neighbors of variable
87 Neighbors & nbV( size_t i ) { return G.nb1(i); }
88 /// Provides read access to neighbors of factor
89 const Neighbors & nbF( size_t I ) const { return G.nb2(I); }
90 /// Provides full access to neighbors of factor
91 Neighbors & nbF( size_t I ) { return G.nb2(I); }
92 /// Provides read access to neighbor of variable
93 const Neighbor & nbV( size_t i, size_t _I ) const { return G.nb1(i)[_I]; }
94 /// Provides full access to neighbor of variable
95 Neighbor & nbV( size_t i, size_t _I ) { return G.nb1(i)[_I]; }
96 /// Provides read access to neighbor of factor
97 const Neighbor & nbF( size_t I, size_t _i ) const { return G.nb2(I)[_i]; }
98 /// Provides full access to neighbor of factor
99 Neighbor & nbF( size_t I, size_t _i ) { return G.nb2(I)[_i]; }
100
101 size_t findVar(const Var & n) const {
102 size_t i = find( vars.begin(), vars.end(), n ) - vars.begin();
103 assert( i != nrVars() );
104 return i;
105 }
106 size_t findFactor(const VarSet &ns) const {
107 size_t I;
108 for( I = 0; I < nrFactors(); I++ )
109 if( factor(I).vars() == ns )
110 break;
111 assert( I != nrFactors() );
112 return I;
113 }
114
115 friend std::ostream& operator << (std::ostream& os, const FactorGraph& fg);
116 friend std::istream& operator >> (std::istream& is, FactorGraph& fg);
117
118 VarSet delta( unsigned i ) const;
119 VarSet Delta( unsigned i ) const;
120 virtual void makeCavity( unsigned i );
121
122 long ReadFromFile(const char *filename);
123 long WriteToFile(const char *filename) const;
124 long WriteToDotFile(const char *filename) const;
125
126 virtual void clamp( const Var & n, size_t i );
127
128 bool hasNegatives() const;
129
130 std::vector<VarSet> Cliques() const;
131
132 virtual void undoProbs( const VarSet &ns );
133 void saveProbs( const VarSet &ns );
134 virtual void undoProb( size_t I );
135 void saveProb( size_t I );
136
137 private:
138 /// Part of constructors (creates edges, neighbors and adjacency matrix)
139 void createGraph( size_t nrEdges );
140 };
141
142
143 // 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)
144 template<typename FactorInputIterator, typename VarInputIterator>
145 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(), _undoProbs() {
146 // add factors
147 size_t nrEdges = 0;
148 factors.reserve( nr_fact_hint );
149 for( FactorInputIterator p2 = fact_begin; p2 != fact_end; ++p2 ) {
150 factors.push_back( *p2 );
151 nrEdges += p2->vars().size();
152 }
153
154 // add variables
155 vars.reserve( nr_var_hint );
156 for( VarInputIterator p1 = var_begin; p1 != var_end; ++p1 )
157 vars.push_back( *p1 );
158
159 // create graph structure
160 createGraph( nrEdges );
161 }
162
163
164 } // end of namespace dai
165
166
167 #endif