Added toString(const T&), cleaned up utils/uai2fg and added tests/twofactors.fg
[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-2010 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 // Display help message if number of command line arguments is incorrect
77 cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
78 cout << "Usage: ./fginfo <in.fg> <tw>" << endl << endl;
79 cout << "Reports some detailed information about the factor graph <in.fg>." << endl;
80 cout << "Also calculates treewidth (which may take some time) unless <tw> == 0." << endl;
81 return 1;
82 } else {
83 // Read factorgraph
84 FactorGraph fg;
85 char *infile = argv[1];
86 int calc_tw = atoi(argv[2]);
87 fg.ReadFromFile( infile );
88
89 // Output various statistics
90 cout << "Number of variables: " << fg.nrVars() << endl;
91 cout << "Number of factors: " << fg.nrFactors() << endl;
92 cout << "Connected: " << fg.isConnected() << endl;
93 cout << "Tree: " << fg.isTree() << endl;
94 cout << "Has short loops: " << hasShortLoops(fg.factors()) << endl;
95 cout << "Has negatives: " << hasNegatives(fg.factors()) << endl;
96 cout << "Binary variables? " << fg.isBinary() << endl;
97 cout << "Pairwise interactions? " << fg.isPairwise() << endl;
98 // Calculate treewidth using various heuristics, if requested
99 if( calc_tw ) {
100 std::pair<size_t,double> tw;
101 tw = boundTreewidth(fg, &eliminationCost_MinNeighbors);
102 cout << "Treewidth (MinNeighbors): " << tw.first << " (" << tw.second << " states)" << endl;
103 tw = boundTreewidth(fg, &eliminationCost_MinWeight);
104 cout << "Treewidth (MinWeight): " << tw.first << " (" << tw.second << " states)" << endl;
105 tw = boundTreewidth(fg, &eliminationCost_MinFill);
106 cout << "Treewidth (MinFill): " << tw.first << " (" << tw.second << " states)" << endl;
107 tw = boundTreewidth(fg, &eliminationCost_WeightedMinFill);
108 cout << "Treewidth (WeightedMinFill): " << tw.first << " (" << tw.second << " states)" << endl;
109 }
110 // Calculate total state space
111 long double stsp = 1.0;
112 for( size_t i = 0; i < fg.nrVars(); i++ )
113 stsp *= fg.var(i).states();
114 cout << "Total state space: " << stsp << endl;
115 // Output type of factor graph
116 cout << "Type: " << (fg.isPairwise() ? "pairwise" : "higher order") << " interactions, " << (fg.isBinary() ? "binary" : "nonbinary") << " variables" << endl;
117
118 // Calculate complexity for LCBP
119 long double cavsum_lcbp = 0.0;
120 long double cavsum_lcbp2 = 0.0;
121 size_t max_Delta_size = 0;
122 map<size_t,size_t> cavsizes;
123 for( size_t i = 0; i < fg.nrVars(); i++ ) {
124 VarSet di = fg.delta(i);
125 if( cavsizes.count(di.size()) )
126 cavsizes[di.size()]++;
127 else
128 cavsizes[di.size()] = 1;
129 size_t Ds = fg.Delta(i).nrStates();
130 if( Ds > max_Delta_size )
131 max_Delta_size = Ds;
132 cavsum_lcbp += di.nrStates();
133 for( VarSet::const_iterator j = di.begin(); j != di.end(); j++ )
134 cavsum_lcbp2 += j->states();
135 }
136 cout << "Maximum pancake has " << max_Delta_size << " states" << endl;
137 cout << "LCBP with full cavities needs " << cavsum_lcbp << " BP runs" << endl;
138 cout << "LCBP with only pairinteractions needs " << cavsum_lcbp2 << " BP runs" << endl;
139 cout << "Cavity sizes: ";
140 for( map<size_t,size_t>::const_iterator it = cavsizes.begin(); it != cavsizes.end(); it++ )
141 cout << it->first << "(" << it->second << ") ";
142 cout << endl;
143
144 // Calculate girth and length of loops
145 if( fg.isPairwise() ) {
146 bool girth_reached = false;
147 size_t loopdepth;
148 for( loopdepth = 2; loopdepth <= fg.nrVars() && !girth_reached; loopdepth++ ) {
149 size_t nr_loops = countLoops( fg, loopdepth );
150 cout << "Loops up to " << loopdepth << " variables: " << nr_loops << endl;
151 if( nr_loops > 0 )
152 girth_reached = true;
153 }
154 if( girth_reached )
155 cout << "Girth: " << loopdepth-1 << endl;
156 else
157 cout << "Girth: infinity" << endl;
158 }
159
160 // Output factor state spaces
161 map<size_t,size_t> facsizes;
162 for( size_t I = 0; I < fg.nrFactors(); I++ ) {
163 size_t Isize = fg.factor(I).vars().size();
164 if( facsizes.count( Isize ) )
165 facsizes[Isize]++;
166 else
167 facsizes[Isize] = 1;
168 }
169 cout << "Factor sizes: ";
170 for( map<size_t,size_t>::const_iterator it = facsizes.begin(); it != facsizes.end(); it++ )
171 cout << it->first << "(" << it->second << ") ";
172 cout << endl;
173
174 return 0;
175 }
176 }