Merge branch 'master' of git.tuebingen.mpg.de:libdai
[libdai.git] / utils / fginfo.cpp
1 /* This file is part of libDAI - http://www.libdai.org/
2 *
3 * libDAI is licensed under the terms of the GNU General Public License version
4 * 2, or (at your option) any later version. libDAI is distributed without any
5 * warranty. See the file COPYING for more details.
6 *
7 * Copyright (C) 2006-2009 Joris Mooij [joris dot mooij at libdai dot org]
8 * Copyright (C) 2006-2007 Radboud University Nijmegen, The Netherlands
9 */
10
11
12 #include <iostream>
13 #include <cstdlib>
14 #include <dai/factorgraph.h>
15 #include <dai/jtree.h>
16
17
18 using namespace std;
19 using namespace dai;
20
21
22 void findLoopClusters( const FactorGraph & fg, std::set<VarSet> &allcl, VarSet newcl, const Var & root, size_t length, VarSet vars ) {
23 for( VarSet::const_iterator in = vars.begin(); in != vars.end(); in++ ) {
24 VarSet ind = fg.delta( *in );
25 if( (newcl.size()) >= 2 && ind.contains( root ) ) {
26 allcl.insert( newcl | *in );
27 }
28 else if( length > 1 )
29 findLoopClusters( fg, allcl, newcl | *in, root, length - 1, ind / newcl );
30 }
31 }
32
33
34 size_t countLoops( const FactorGraph & fg, size_t loopdepth ) {
35 set<VarSet> loops;
36 for( vector<Var>::const_iterator i0 = fg.vars().begin(); i0 != fg.vars().end(); i0++ ) {
37 VarSet i0d = fg.delta(*i0);
38 if( loopdepth > 1 )
39 findLoopClusters( fg, loops, *i0, *i0, loopdepth - 1, i0d );
40 }
41 return loops.size();
42 }
43
44
45 bool hasShortLoops( const std::vector<Factor> &P ) {
46 bool found = false;
47 vector<Factor>::const_iterator I, J;
48 for( I = P.begin(); I != P.end(); I++ ) {
49 J = I;
50 J++;
51 for( ; J != P.end(); J++ )
52 if( (I->vars() & J->vars()).size() >= 2 ) {
53 found = true;
54 break;
55 }
56 if( found )
57 break;
58 }
59 return found;
60 }
61
62
63 bool hasNegatives( const std::vector<Factor> &P ) {
64 bool found = false;
65 for( size_t I = 0; I < P.size(); I++ )
66 if( P[I].hasNegatives() ) {
67 found = true;
68 break;
69 }
70 return found;
71 }
72
73
74 int main( int argc, char *argv[] ) {
75 if( argc != 3 ) {
76 cout << "Usage: " << argv[0] << " <in.fg> <tw>" << endl << endl;
77 cout << "Reports some characteristics of the .fg network." << endl;
78 cout << "Also calculates treewidth (which may take some time) unless <tw> == 0." << endl;
79 return 1;
80 } else {
81 // Read factorgraph
82 FactorGraph fg;
83 char *infile = argv[1];
84 int calc_tw = atoi(argv[2]);
85 fg.ReadFromFile( infile );
86
87 cout << "Number of variables: " << fg.nrVars() << endl;
88 cout << "Number of factors: " << fg.nrFactors() << endl;
89 cout << "Connected: " << fg.isConnected() << endl;
90 cout << "Tree: " << fg.isTree() << endl;
91 cout << "Has short loops: " << hasShortLoops(fg.factors()) << endl;
92 cout << "Has negatives: " << hasNegatives(fg.factors()) << endl;
93 cout << "Binary variables? " << fg.isBinary() << endl;
94 cout << "Pairwise interactions? " << fg.isPairwise() << endl;
95 if( calc_tw ) {
96 std::pair<size_t,size_t> tw;
97 tw = boundTreewidth(fg, &eliminationCost_MinNeighbors);
98 cout << "Treewidth (MinNeighbors): " << tw.first << " (" << tw.second << " states)" << endl;
99 tw = boundTreewidth(fg, &eliminationCost_MinWeight);
100 cout << "Treewidth (MinWeight): " << tw.first << " (" << tw.second << " states)" << endl;
101 tw = boundTreewidth(fg, &eliminationCost_MinFill);
102 cout << "Treewidth (MinFill): " << tw.first << " (" << tw.second << " states)" << endl;
103 tw = boundTreewidth(fg, &eliminationCost_WeightedMinFill);
104 cout << "Treewidth (WeightedMinFill): " << tw.first << " (" << tw.second << " states)" << endl;
105 }
106 long double stsp = 1.0;
107 for( size_t i = 0; i < fg.nrVars(); i++ )
108 stsp *= fg.var(i).states();
109 cout << "Total state space: " << stsp << endl;
110
111 long double cavsum_lcbp = 0.0;
112 long double cavsum_lcbp2 = 0.0;
113 size_t max_Delta_size = 0;
114 map<size_t,size_t> cavsizes;
115 for( size_t i = 0; i < fg.nrVars(); i++ ) {
116 VarSet di = fg.delta(i);
117 if( cavsizes.count(di.size()) )
118 cavsizes[di.size()]++;
119 else
120 cavsizes[di.size()] = 1;
121 size_t Ds = fg.Delta(i).nrStates();
122 if( Ds > max_Delta_size )
123 max_Delta_size = Ds;
124 cavsum_lcbp += di.nrStates();
125 for( VarSet::const_iterator j = di.begin(); j != di.end(); j++ )
126 cavsum_lcbp2 += j->states();
127 }
128 cout << "Maximum pancake has " << max_Delta_size << " states" << endl;
129 cout << "LCBP with full cavities needs " << cavsum_lcbp << " BP runs" << endl;
130 cout << "LCBP with only pairinteractions needs " << cavsum_lcbp2 << " BP runs" << endl;
131 cout << "Cavity sizes: ";
132 for( map<size_t,size_t>::const_iterator it = cavsizes.begin(); it != cavsizes.end(); it++ )
133 cout << it->first << "(" << it->second << ") ";
134 cout << endl;
135
136 cout << "Type: " << (fg.isPairwise() ? "pairwise" : "higher order") << " interactions, " << (fg.isBinary() ? "binary" : "nonbinary") << " variables" << endl;
137
138 if( fg.isPairwise() ) {
139 bool girth_reached = false;
140 size_t loopdepth;
141 for( loopdepth = 2; loopdepth <= fg.nrVars() && !girth_reached; loopdepth++ ) {
142 size_t nr_loops = countLoops( fg, loopdepth );
143 cout << "Loops up to " << loopdepth << " variables: " << nr_loops << endl;
144 if( nr_loops > 0 )
145 girth_reached = true;
146 }
147 if( girth_reached )
148 cout << "Girth: " << loopdepth-1 << endl;
149 else
150 cout << "Girth: infinity" << endl;
151 }
152
153 map<size_t,size_t> facsizes;
154 for( size_t I = 0; I < fg.nrFactors(); I++ ) {
155 size_t Isize = fg.factor(I).vars().size();
156 if( facsizes.count( Isize ) )
157 facsizes[Isize]++;
158 else
159 facsizes[Isize] = 1;
160 }
161 cout << "Factor sizes: ";
162 for( map<size_t,size_t>::const_iterator it = facsizes.begin(); it != facsizes.end(); it++ )
163 cout << it->first << "(" << it->second << ") ";
164 cout << endl;
165
166 return 0;
167 }
168 }