1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
4 This file is part of libDAI.
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.
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.
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
22 #ifndef __defined_libdai_factorgraph_h
23 #define __defined_libdai_factorgraph_h
28 #include <tr1/unordered_map>
29 #include <dai/bipgraph.h>
30 #include <dai/factor.h>
36 bool hasShortLoops( const std::vector
<Factor
> &P
);
37 void RemoveShortLoops( std::vector
<Factor
> &P
);
43 std::vector
<Var
> vars
;
44 std::vector
<Factor
> factors
;
45 typedef BipartiteGraph::Neighbor Neighbor
;
46 typedef BipartiteGraph::Neighbors Neighbors
;
49 std::map
<size_t,Prob
> _undoProbs
;
50 Prob::NormType _normtype
;
53 /// Default constructor
54 FactorGraph() : G(), vars(), factors(), _undoProbs(), _normtype(Prob::NORMPROB
) {};
56 FactorGraph(const FactorGraph
& x
) : G(x
.G
), vars(x
.vars
), factors(x
.factors
), _undoProbs(x
._undoProbs
), _normtype(x
._normtype
) {};
57 /// Construct FactorGraph from vector of Factors
58 FactorGraph(const std::vector
<Factor
> &P
);
59 // Construct a FactorGraph from given factor and variable iterators
60 template<typename FactorInputIterator
, typename VarInputIterator
>
61 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 );
63 /// Assignment operator
64 FactorGraph
& operator=(const FactorGraph
& x
) {
69 _undoProbs
= x
._undoProbs
;
70 _normtype
= x
._normtype
;
74 virtual ~FactorGraph() {}
77 Var
& var(size_t i
) { return vars
[i
]; }
78 const Var
& var(size_t i
) const { return vars
[i
]; }
79 Factor
& factor(size_t I
) { return factors
[I
]; }
80 const Factor
& factor(size_t I
) const { return factors
[I
]; }
82 size_t nrVars() const { return vars
.size(); }
83 size_t nrFactors() const { return factors
.size(); }
84 size_t nrEdges() const { return G
.nrEdges(); }
86 /// Provides read access to neighbors of variable
87 const Neighbors
& nbV( size_t i
) const { return G
.nb1(i
); }
88 /// Provides full access to neighbors of variable
89 Neighbors
& nbV( size_t i
) { return G
.nb1(i
); }
90 /// Provides read access to neighbors of factor
91 const Neighbors
& nbF( size_t I
) const { return G
.nb2(I
); }
92 /// Provides full access to neighbors of factor
93 Neighbors
& nbF( size_t I
) { return G
.nb2(I
); }
94 /// Provides read access to neighbor of variable
95 const Neighbor
& nbV( size_t i
, size_t _I
) const { return G
.nb1(i
)[_I
]; }
96 /// Provides full access to neighbor of variable
97 Neighbor
& nbV( size_t i
, size_t _I
) { return G
.nb1(i
)[_I
]; }
98 /// Provides read access to neighbor of factor
99 const Neighbor
& nbF( size_t I
, size_t _i
) const { return G
.nb2(I
)[_i
]; }
100 /// Provides full access to neighbor of factor
101 Neighbor
& nbF( size_t I
, size_t _i
) { return G
.nb2(I
)[_i
]; }
103 size_t findVar(const Var
& n
) const {
104 size_t i
= find( vars
.begin(), vars
.end(), n
) - vars
.begin();
105 assert( i
!= nrVars() );
108 size_t findFactor(const VarSet
&ns
) const {
110 for( I
= 0; I
< nrFactors(); I
++ )
111 if( factor(I
).vars() == ns
)
113 assert( I
!= nrFactors() );
117 friend std::ostream
& operator << (std::ostream
& os
, const FactorGraph
& fg
);
118 friend std::istream
& operator >> (std::istream
& is
, FactorGraph
& fg
);
120 VarSet
delta( unsigned i
) const;
121 VarSet
Delta( unsigned i
) const;
122 virtual void makeCavity( unsigned i
);
124 long ReadFromFile(const char *filename
);
125 long WriteToFile(const char *filename
) const;
126 long WriteToDotFile(const char *filename
) const;
128 virtual void clamp( const Var
& n
, size_t i
);
130 bool hasNegatives() const;
131 Prob::NormType
NormType() const { return _normtype
; }
133 std::vector
<VarSet
> Cliques() const;
135 virtual void undoProbs( const VarSet
&ns
);
136 void saveProbs( const VarSet
&ns
);
137 virtual void undoProb( size_t I
);
138 void saveProb( size_t I
);
140 virtual void updatedFactor( size_t /*I*/ ) {};
143 /// Part of constructors (creates edges, neighbors and adjacency matrix)
144 void createGraph( size_t nrEdges
);
148 // 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)
149 template<typename FactorInputIterator
, typename VarInputIterator
>
150 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(), _normtype(Prob::NORMPROB
) {
153 factors
.reserve( nr_fact_hint
);
154 for( FactorInputIterator p2
= fact_begin
; p2
!= fact_end
; ++p2
) {
155 factors
.push_back( *p2
);
156 nrEdges
+= p2
->vars().size();
160 vars
.reserve( nr_var_hint
);
161 for( VarInputIterator p1
= var_begin
; p1
!= var_end
; ++p1
)
162 vars
.push_back( *p1
);
164 // create graph structure
165 createGraph( nrEdges
);
169 } // end of namespace dai