024d229419494012807d1896cea943a5bb2ef6be
[libdai.git] / utils / fginfo.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
4 *
5 * Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
6 */
7
8
9 #include <iostream>
10 #include <cstdlib>
11 #include <dai/factorgraph.h>
12 #include <dai/jtree.h>
13
14
15 using namespace std;
16 using namespace dai;
17
18
19 void findLoopClusters( const FactorGraph & fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars ) {
20 for( VarSet::const_iterator in = vars.begin(); in != vars.end(); in++ ) {
21 VarSet ind = fg.delta( *in );
22 if( (newcl.size()) >= 2 && ind.contains( root ) ) {
23 allcl.insert( newcl | *in );
24 }
25 else if( length > 1 )
26 findLoopClusters( fg, allcl, newcl | *in, root, length - 1, ind / newcl );
27 }
28 }
29
30
31 size_t countLoops( const FactorGraph & fg, size_t loopdepth ) {
32 set<VarSet> loops;
33 for( vector<Var>::const_iterator i0 = fg.vars().begin(); i0 != fg.vars().end(); i0++ ) {
34 VarSet i0d = fg.delta(*i0);
35 if( loopdepth > 1 )
36 findLoopClusters( fg, loops, *i0, *i0, loopdepth - 1, i0d );
37 }
38 return loops.size();
39 }
40
41
42 bool hasShortLoops( const std::vector<Factor> &P ) {
43 bool found = false;
44 vector<Factor>::const_iterator I, J;
45 for( I = P.begin(); I != P.end(); I++ ) {
46 J = I;
47 J++;
48 for( ; J != P.end(); J++ )
49 if( (I->vars() & J->vars()).size() >= 2 ) {
50 found = true;
51 break;
52 }
53 if( found )
54 break;
55 }
56 return found;
57 }
58
59
60 bool hasNegatives( const std::vector<Factor> &P ) {
61 bool found = false;
62 for( size_t I = 0; I < P.size(); I++ )
63 if( P[I].hasNegatives() ) {
64 found = true;
65 break;
66 }
67 return found;
68 }
69
70
71 int main( int argc, char *argv[] ) {
72 if( argc != 3 ) {
73 // Display help message if number of command line arguments is incorrect
74 cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
75 cout << "Usage: ./fginfo <in.fg> <maxstates>" << endl << endl;
76 cout << "Reports some detailed information about the factor graph <in.fg>." << endl;
77 cout << "Also calculates treewidth, with maximum total number of states" << endl;
78 cout << "given by <maxstates>, where 0 means unlimited." << endl << endl;
79 return 1;
80 } else {
81 // Read factorgraph
82 FactorGraph fg;
83 char *infile = argv[1];
84 size_t maxstates = fromString<size_t>( argv[2] );
85 fg.ReadFromFile( infile );
86
87 // Output various statistics
88 cout << "Number of variables: " << fg.nrVars() << endl;
89 cout << "Number of factors: " << fg.nrFactors() << endl;
90 cout << "Connected: " << fg.isConnected() << endl;
91 cout << "Tree: " << fg.isTree() << endl;
92 cout << "Has short loops: " << hasShortLoops(fg.factors()) << endl;
93 cout << "Has negatives: " << hasNegatives(fg.factors()) << endl;
94 cout << "Binary variables? " << fg.isBinary() << endl;
95 cout << "Pairwise interactions? " << fg.isPairwise() << endl;
96
97 // Calculate treewidth using various heuristics
98 std::pair<size_t,BigInt> tw;
99 cout << "Treewidth (MinNeighbors): ";
100 try {
101 tw = boundTreewidth(fg, &eliminationCost_MinNeighbors, maxstates );
102 cout << tw.first << " (" << tw.second << " states)" << endl;
103 } catch( Exception &e ) {
104 if( e.getCode() == Exception::OUT_OF_MEMORY )
105 cout << "> " << maxstates << endl;
106 else
107 cout << "an exception occurred" << endl;
108 }
109
110 cout << "Treewidth (MinWeight): ";
111 try {
112 tw = boundTreewidth(fg, &eliminationCost_MinWeight, maxstates );
113 cout << tw.first << " (" << tw.second << " states)" << endl;
114 } catch( Exception &e ) {
115 if( e.getCode() == Exception::OUT_OF_MEMORY )
116 cout << "> " << maxstates << endl;
117 else
118 cout << "an exception occurred" << endl;
119 }
120
121 cout << "Treewidth (MinFill): ";
122 try {
123 tw = boundTreewidth(fg, &eliminationCost_MinFill, maxstates );
124 cout << tw.first << " (" << tw.second << " states)" << endl;
125 } catch( Exception &e ) {
126 if( e.getCode() == Exception::OUT_OF_MEMORY )
127 cout << "> " << maxstates << endl;
128 else
129 cout << "an exception occurred" << endl;
130 }
131
132 cout << "Treewidth (WeightedMinFill): ";
133 try {
134 tw = boundTreewidth(fg, &eliminationCost_WeightedMinFill, maxstates );
135 cout << tw.first << " (" << tw.second << " states)" << endl;
136 } catch( Exception &e ) {
137 if( e.getCode() == Exception::OUT_OF_MEMORY )
138 cout << "> " << maxstates << endl;
139 else
140 cout << "an exception occurred" << endl;
141 }
142
143 // Calculate total state space
144 BigInt stsp = 1;
145 for( size_t i = 0; i < fg.nrVars(); i++ )
146 stsp *= fg.var(i).states();
147 cout << "Total state space: " << stsp << endl;
148 // Output type of factor graph
149 cout << "Type: " << (fg.isPairwise() ? "pairwise" : "higher order") << " interactions, " << (fg.isBinary() ? "binary" : "nonbinary") << " variables" << endl;
150
151 // Calculate complexity for LCBP
152 BigInt cavsum_lcbp = 0;
153 BigInt cavsum_lcbp2 = 0;
154 BigInt max_Delta_size = 0;
155 map<size_t,size_t> cavsizes;
156 for( size_t i = 0; i < fg.nrVars(); i++ ) {
157 VarSet di = fg.delta(i);
158 if( cavsizes.count(di.size()) )
159 cavsizes[di.size()]++;
160 else
161 cavsizes[di.size()] = 1;
162 BigInt Ds = fg.Delta(i).nrStates();
163 if( Ds > max_Delta_size )
164 max_Delta_size = Ds;
165 cavsum_lcbp += di.nrStates();
166 for( VarSet::const_iterator j = di.begin(); j != di.end(); j++ )
167 cavsum_lcbp2 += j->states();
168 }
169 cout << "Maximum pancake has " << max_Delta_size << " states" << endl;
170 cout << "LCBP with full cavities needs " << cavsum_lcbp << " BP runs" << endl;
171 cout << "LCBP with only pairinteractions needs " << cavsum_lcbp2 << " BP runs" << endl;
172 cout << "Cavity sizes: ";
173 for( map<size_t,size_t>::const_iterator it = cavsizes.begin(); it != cavsizes.end(); it++ )
174 cout << it->first << "(" << it->second << ") ";
175 cout << endl;
176
177 // Calculate girth and length of loops
178 if( fg.isPairwise() ) {
179 bool girth_reached = false;
180 size_t loopdepth;
181 for( loopdepth = 2; loopdepth <= fg.nrVars() && !girth_reached; loopdepth++ ) {
182 size_t nr_loops = countLoops( fg, loopdepth );
183 cout << "Loops up to " << loopdepth << " variables: " << nr_loops << endl;
184 if( nr_loops > 0 )
185 girth_reached = true;
186 }
187 if( girth_reached )
188 cout << "Girth: " << loopdepth-1 << endl;
189 else
190 cout << "Girth: infinity" << endl;
191 }
192
193 // Output factor state spaces
194 map<size_t,size_t> facsizes;
195 for( size_t I = 0; I < fg.nrFactors(); I++ ) {
196 size_t Isize = fg.factor(I).vars().size();
197 if( facsizes.count( Isize ) )
198 facsizes[Isize]++;
199 else
200 facsizes[Isize] = 1;
201 }
202 cout << "Factor sizes: ";
203 for( map<size_t,size_t>::const_iterator it = facsizes.begin(); it != facsizes.end(); it++ )
204 cout << it->first << "(" << it->second << ") ";
205 cout << endl;
206
207 return 0;
208 }
209 }