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
30 #include <dai/factorgraph.h>
32 #include <dai/exceptions.h>
41 FactorGraph::FactorGraph( const std::vector
<Factor
> &P
) : G(), _undoProbs() {
42 // add factors, obtain variables
44 factors
.reserve( P
.size() );
46 for( vector
<Factor
>::const_iterator p2
= P
.begin(); p2
!= P
.end(); p2
++ ) {
47 factors
.push_back( *p2
);
48 copy( p2
->vars().begin(), p2
->vars().end(), inserter( _vars
, _vars
.begin() ) );
49 nrEdges
+= p2
->vars().size();
53 vars
.reserve( _vars
.size() );
54 for( set
<Var
>::const_iterator p1
= _vars
.begin(); p1
!= _vars
.end(); p1
++ )
55 vars
.push_back( *p1
);
57 // create graph structure
58 createGraph( nrEdges
);
62 /// Part of constructors (creates edges, neighbours and adjacency matrix)
63 void FactorGraph::createGraph( size_t nrEdges
) {
64 // create a mapping for indices
65 hash_map
<size_t, size_t> hashmap
;
67 for( size_t i
= 0; i
< vars
.size(); i
++ )
68 hashmap
[var(i
).label()] = i
;
72 edges
.reserve( nrEdges
);
73 for( size_t i2
= 0; i2
< nrFactors(); i2
++ ) {
74 const VarSet
& ns
= factor(i2
).vars();
75 for( VarSet::const_iterator q
= ns
.begin(); q
!= ns
.end(); q
++ )
76 edges
.push_back( Edge(hashmap
[q
->label()], i2
) );
79 // create bipartite graph
80 G
.create( nrVars(), nrFactors(), edges
.begin(), edges
.end() );
84 ostream
& operator << (ostream
& os
, const FactorGraph
& fg
) {
85 os
<< fg
.nrFactors() << endl
;
87 for( size_t I
= 0; I
< fg
.nrFactors(); I
++ ) {
89 os
<< fg
.factor(I
).vars().size() << endl
;
90 for( VarSet::const_iterator i
= fg
.factor(I
).vars().begin(); i
!= fg
.factor(I
).vars().end(); i
++ )
91 os
<< i
->label() << " ";
93 for( VarSet::const_iterator i
= fg
.factor(I
).vars().begin(); i
!= fg
.factor(I
).vars().end(); i
++ )
94 os
<< i
->states() << " ";
96 size_t nr_nonzeros
= 0;
97 for( size_t k
= 0; k
< fg
.factor(I
).states(); k
++ )
98 if( fg
.factor(I
)[k
] != 0.0 )
100 os
<< nr_nonzeros
<< endl
;
101 for( size_t k
= 0; k
< fg
.factor(I
).states(); k
++ )
102 if( fg
.factor(I
)[k
] != 0.0 ) {
104 sprintf(buf
,"%18.14g", fg
.factor(I
)[k
]);
105 os
<< k
<< " " << buf
<< endl
;
113 istream
& operator >> (istream
& is
, FactorGraph
& fg
) {
117 vector
<Factor
> factors
;
121 while( (is
.peek()) == '#' )
125 DAI_THROW(INVALID_FACTORGRAPH_FILE
);
127 cout
<< "Reading " << nr_f
<< " factors..." << endl
;
131 DAI_THROW(INVALID_FACTORGRAPH_FILE
);
133 for( size_t I
= 0; I
< nr_f
; I
++ ) {
135 cout
<< "Reading factor " << I
<< "..." << endl
;
137 while( (is
.peek()) == '#' )
141 cout
<< " nr_members: " << nr_members
<< endl
;
144 for( size_t mi
= 0; mi
< nr_members
; mi
++ ) {
146 while( (is
.peek()) == '#' )
149 labels
.push_back(mi_label
);
153 copy (labels
.begin(), labels
.end(), ostream_iterator
<int>(cout
, " "));
158 for( size_t mi
= 0; mi
< nr_members
; mi
++ ) {
160 while( (is
.peek()) == '#' )
163 dims
.push_back(mi_dim
);
166 cout
<< " dimensions: ";
167 copy (dims
.begin(), dims
.end(), ostream_iterator
<int>(cout
, " "));
173 for( size_t mi
= 0; mi
< nr_members
; mi
++ )
174 I_vars
|= Var(labels
[mi
], dims
[mi
]);
175 factors
.push_back(Factor(I_vars
,0.0));
177 // calculate permutation sigma (internally, members are sorted)
178 vector
<size_t> sigma(nr_members
,0);
179 VarSet::iterator j
= I_vars
.begin();
180 for( size_t mi
= 0; mi
< nr_members
; mi
++,j
++ ) {
181 long search_for
= j
->label();
182 vector
<long>::iterator j_loc
= find(labels
.begin(),labels
.end(),search_for
);
183 sigma
[mi
] = j_loc
- labels
.begin();
187 copy( sigma
.begin(), sigma
.end(), ostream_iterator
<int>(cout
," "));
191 // calculate multindices
192 Permute
permindex( dims
, sigma
);
196 while( (is
.peek()) == '#' )
200 cout
<< " nonzeroes: " << nr_nonzeros
<< endl
;
201 for( size_t k
= 0; k
< nr_nonzeros
; k
++ ) {
204 while( (is
.peek()) == '#' )
207 while( (is
.peek()) == '#' )
211 // store value, but permute indices first according
212 // to internal representation
213 factors
.back()[permindex
.convert_linear_index( li
)] = val
;
218 cout
<< "factors:" << endl
;
219 copy(factors
.begin(), factors
.end(), ostream_iterator
<Factor
>(cout
,"\n"));
222 fg
= FactorGraph(factors
);
231 VarSet
FactorGraph::delta( unsigned i
) const {
232 // calculate Markov Blanket
234 foreach( const Neighbor
&I
, nbV(i
) ) // for all neighboring factors I of i
235 foreach( const Neighbor
&j
, nbF(I
) ) // for all neighboring variables j of I
243 VarSet
FactorGraph::Delta( unsigned i
) const {
244 return( delta(i
) | var(i
) );
248 void FactorGraph::makeCavity( unsigned i
) {
249 // fills all Factors that include var(i) with ones
250 foreach( const Neighbor
&I
, nbV(i
) ) // for all neighboring factors I of i
251 factor(I
).fill( 1.0 );
255 bool FactorGraph::hasNegatives() const {
257 for( size_t I
= 0; I
< nrFactors() && !result
; I
++ )
258 if( factor(I
).hasNegatives() )
264 long FactorGraph::ReadFromFile(const char *filename
) {
266 infile
.open (filename
);
267 if (infile
.is_open()) {
272 cout
<< "ERROR OPENING FILE" << endl
;
278 long FactorGraph::WriteToFile(const char *filename
) const {
280 outfile
.open (filename
);
281 if (outfile
.is_open()) {
291 cout
<< "ERROR OPENING FILE" << endl
;
297 long FactorGraph::WriteToDotFile(const char *filename
) const {
299 outfile
.open (filename
);
300 if (outfile
.is_open()) {
302 outfile
<< "graph G {" << endl
;
303 outfile
<< "graph[size=\"9,9\"];" << endl
;
304 outfile
<< "node[shape=circle,width=0.4,fixedsize=true];" << endl
;
305 for( size_t i
= 0; i
< nrVars(); i
++ )
306 outfile
<< "\tx" << var(i
).label() << ";" << endl
;
307 outfile
<< "node[shape=box,style=filled,color=lightgrey,width=0.3,height=0.3,fixedsize=true];" << endl
;
308 for( size_t I
= 0; I
< nrFactors(); I
++ )
309 outfile
<< "\tp" << I
<< ";" << endl
;
310 for( size_t i
= 0; i
< nrVars(); i
++ )
311 foreach( const Neighbor
&I
, nbV(i
) ) // for all neighboring factors I of i
312 outfile
<< "\tx" << var(i
).label() << " -- p" << I
<< ";" << endl
;
313 outfile
<< "}" << endl
;
321 cout
<< "ERROR OPENING FILE" << endl
;
327 bool hasShortLoops( const vector
<Factor
> &P
) {
329 vector
<Factor
>::const_iterator I
, J
;
330 for( I
= P
.begin(); I
!= P
.end(); I
++ ) {
333 for( ; J
!= P
.end(); J
++ )
334 if( (I
->vars() & J
->vars()).size() >= 2 ) {
345 void RemoveShortLoops(vector
<Factor
> &P
) {
349 vector
<Factor
>::iterator I
, J
;
350 for( I
= P
.begin(); I
!= P
.end(); I
++ ) {
353 for( ; J
!= P
.end(); J
++ )
354 if( (I
->vars() & J
->vars()).size() >= 2 ) {
362 cout
<< "Merging factors " << I
->vars() << " and " << J
->vars() << endl
;
370 vector
<VarSet
> FactorGraph::Cliques() const {
371 vector
<VarSet
> result
;
373 for( size_t I
= 0; I
< nrFactors(); I
++ ) {
375 for( size_t J
= 0; (J
< nrFactors()) && maximal
; J
++ )
376 if( (factor(J
).vars() >> factor(I
).vars()) && !(factor(J
).vars() == factor(I
).vars()) )
380 result
.push_back( factor(I
).vars() );
387 void FactorGraph::clamp( const Var
& n
, size_t i
) {
388 assert( i
<= n
.states() );
390 // Multiply each factor that contains the variable with a delta function
392 Factor
delta_n_i(n
,0.0);
395 // For all factors that contain n
396 for( size_t I
= 0; I
< nrFactors(); I
++ )
397 if( factor(I
).vars().contains( n
) )
398 // Multiply it with a delta function
399 factor(I
) *= delta_n_i
;
405 void FactorGraph::saveProb( size_t I
) {
406 map
<size_t,Prob
>::iterator it
= _undoProbs
.find( I
);
407 if( it
!= _undoProbs
.end() )
408 cout
<< "FactorGraph::saveProb: WARNING: _undoProbs[I] already defined!" << endl
;
409 _undoProbs
[I
] = factor(I
).p();
413 void FactorGraph::undoProb( size_t I
) {
414 map
<size_t,Prob
>::iterator it
= _undoProbs
.find( I
);
415 if( it
!= _undoProbs
.end() ) {
416 factor(I
).p() = (*it
).second
;
417 _undoProbs
.erase(it
);
422 void FactorGraph::saveProbs( const VarSet
&ns
) {
423 if( !_undoProbs
.empty() )
424 cout
<< "FactorGraph::saveProbs: WARNING: _undoProbs not empy!" << endl
;
425 for( size_t I
= 0; I
< nrFactors(); I
++ )
426 if( factor(I
).vars().intersects( ns
) )
427 _undoProbs
[I
] = factor(I
).p();
431 void FactorGraph::undoProbs( const VarSet
&ns
) {
432 for( map
<size_t,Prob
>::iterator uI
= _undoProbs
.begin(); uI
!= _undoProbs
.end(); ) {
433 if( factor((*uI
).first
).vars().intersects( ns
) ) {
434 // cout << "undoing " << factor((*uI).first).vars() << endl;
435 // cout << "from " << factor((*uI).first).p() << " to " << (*uI).second << endl;
436 factor((*uI
).first
).p() = (*uI
).second
;
437 _undoProbs
.erase(uI
++);
444 } // end of namespace dai