libDAI version 0.2.3
[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 = treewidth(fg);
97 cout << "Treewidth: " << tw.first << endl;
98 cout << "Largest cluster for JTree has " << tw.second << " states " << endl;
99 }
100 long double stsp = 1.0;
101 for( size_t i = 0; i < fg.nrVars(); i++ )
102 stsp *= fg.var(i).states();
103 cout << "Total state space: " << stsp << endl;
104
105 long double cavsum_lcbp = 0.0;
106 long double cavsum_lcbp2 = 0.0;
107 size_t max_Delta_size = 0;
108 map<size_t,size_t> cavsizes;
109 for( size_t i = 0; i < fg.nrVars(); i++ ) {
110 VarSet di = fg.delta(i);
111 if( cavsizes.count(di.size()) )
112 cavsizes[di.size()]++;
113 else
114 cavsizes[di.size()] = 1;
115 size_t Ds = fg.Delta(i).nrStates();
116 if( Ds > max_Delta_size )
117 max_Delta_size = Ds;
118 cavsum_lcbp += di.nrStates();
119 for( VarSet::const_iterator j = di.begin(); j != di.end(); j++ )
120 cavsum_lcbp2 += j->states();
121 }
122 cout << "Maximum pancake has " << max_Delta_size << " states" << endl;
123 cout << "LCBP with full cavities needs " << cavsum_lcbp << " BP runs" << endl;
124 cout << "LCBP with only pairinteractions needs " << cavsum_lcbp2 << " BP runs" << endl;
125 cout << "Cavity sizes: ";
126 for( map<size_t,size_t>::const_iterator it = cavsizes.begin(); it != cavsizes.end(); it++ )
127 cout << it->first << "(" << it->second << ") ";
128 cout << endl;
129
130 cout << "Type: " << (fg.isPairwise() ? "pairwise" : "higher order") << " interactions, " << (fg.isBinary() ? "binary" : "nonbinary") << " variables" << endl;
131
132 if( fg.isPairwise() ) {
133 bool girth_reached = false;
134 size_t loopdepth;
135 for( loopdepth = 2; loopdepth <= fg.nrVars() && !girth_reached; loopdepth++ ) {
136 size_t nr_loops = countLoops( fg, loopdepth );
137 cout << "Loops up to " << loopdepth << " variables: " << nr_loops << endl;
138 if( nr_loops > 0 )
139 girth_reached = true;
140 }
141 if( girth_reached )
142 cout << "Girth: " << loopdepth-1 << endl;
143 else
144 cout << "Girth: infinity" << endl;
145 }
146
147 map<size_t,size_t> facsizes;
148 for( size_t I = 0; I < fg.nrFactors(); I++ ) {
149 size_t Isize = fg.factor(I).vars().size();
150 if( facsizes.count( Isize ) )
151 facsizes[Isize]++;
152 else
153 facsizes[Isize] = 1;
154 }
155 cout << "Factor sizes: ";
156 for( map<size_t,size_t>::const_iterator it = facsizes.begin(); it != facsizes.end(); it++ )
157 cout << it->first << "(" << it->second << ") ";
158 cout << endl;
159
160 return 0;
161 }
162 }