Multiple changes: changes in build system, one workaround and one bug fix
[libdai.git] / utils / fginfo.cpp
index 80da9d5..25c7a22 100644 (file)
@@ -1,11 +1,8 @@
 /*  This file is part of libDAI - http://www.libdai.org/
  *
- *  libDAI is licensed under the terms of the GNU General Public License version
- *  2, or (at your option) any later version. libDAI is distributed without any
- *  warranty. See the file COPYING for more details.
+ *  Copyright (c) 2006-2011, The libDAI authors. All rights reserved.
  *
- *  Copyright (C) 2006-2010  Joris Mooij  [joris dot mooij at libdai dot org]
- *  Copyright (C) 2006-2007  Radboud University Nijmegen, The Netherlands
+ *  Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
  */
 
 
@@ -75,9 +72,10 @@ int main( int argc, char *argv[] ) {
     if( argc != 3 ) {
         // Display help message if number of command line arguments is incorrect
         cout << "This program is part of libDAI - http://www.libdai.org/" << endl << endl;
-        cout << "Usage: ./fginfo <in.fg> <tw>" << endl << endl;
+        cout << "Usage: ./fginfo <in.fg> <maxstates>" << endl << endl;
         cout << "Reports some detailed information about the factor graph <in.fg>." << endl;
-        cout << "Also calculates treewidth (which may take some time) unless <tw> == 0." << endl;
+        cout << "Also calculates treewidth, with maximum total number of states" << endl;
+        cout << "given by <maxstates>, where 0 means unlimited." << endl << endl;
         return 1;
     } else {
         // Read factorgraph
@@ -95,20 +93,57 @@ int main( int argc, char *argv[] ) {
         cout << "Has negatives:         " << hasNegatives(fg.factors()) << endl;
         cout << "Binary variables?      " << fg.isBinary() << endl;
         cout << "Pairwise interactions? " << fg.isPairwise() << endl;
-        // Calculate treewidth using various heuristics, if requested
-        if( calc_tw ) {
-            std::pair<size_t,double> tw;
-            tw = boundTreewidth(fg, &eliminationCost_MinNeighbors);
-            cout << "Treewidth (MinNeighbors):     " << tw.first << " (" << tw.second << " states)" << endl;
-            tw = boundTreewidth(fg, &eliminationCost_MinWeight);
-            cout << "Treewidth (MinWeight):        " << tw.first << " (" << tw.second << " states)" << endl;
-            tw = boundTreewidth(fg, &eliminationCost_MinFill);
-            cout << "Treewidth (MinFill):          " << tw.first << " (" << tw.second << " states)" << endl;
-            tw = boundTreewidth(fg, &eliminationCost_WeightedMinFill);
-            cout << "Treewidth (WeightedMinFill):  " << tw.first << " (" << tw.second << " states)" << endl;
+        
+        // Calculate treewidth using various heuristics
+#ifdef DAI_WITH_JTREE
+        std::pair<size_t,BigInt> tw;
+        cout << "Treewidth (MinNeighbors):     ";
+        try {
+            tw = boundTreewidth(fg, &eliminationCost_MinNeighbors, maxstates );
+            cout << tw.first << " (" << tw.second << " states)" << endl;
+        } catch( Exception &e ) {
+            if( e.getCode() == Exception::OUT_OF_MEMORY )
+                cout << "> " << maxstates << endl;
+            else
+                cout << "an exception occurred" << endl;
+        }
+        
+        cout << "Treewidth (MinWeight):        ";
+        try {
+            tw = boundTreewidth(fg, &eliminationCost_MinWeight, maxstates );
+            cout << tw.first << " (" << tw.second << " states)" << endl;
+        } catch( Exception &e ) {
+            if( e.getCode() == Exception::OUT_OF_MEMORY )
+                cout << "> " << maxstates << endl;
+            else
+                cout << "an exception occurred" << endl;
+        }
+        
+        cout << "Treewidth (MinFill):          ";
+        try {
+            tw = boundTreewidth(fg, &eliminationCost_MinFill, maxstates );
+            cout << tw.first << " (" << tw.second << " states)" << endl;
+        } catch( Exception &e ) {
+            if( e.getCode() == Exception::OUT_OF_MEMORY )
+                cout << "> " << maxstates << endl;
+            else
+                cout << "an exception occurred" << endl;
+        }
+
+        cout << "Treewidth (WeightedMinFill):  ";
+        try {
+            tw = boundTreewidth(fg, &eliminationCost_WeightedMinFill, maxstates );
+            cout << tw.first << " (" << tw.second << " states)" << endl;
+        } catch( Exception &e ) {
+            if( e.getCode() == Exception::OUT_OF_MEMORY )
+                cout << "> " << maxstates << endl;
+            else
+                cout << "an exception occurred" << endl;
         }
+#endif
+        
         // Calculate total state space
-        long double stsp = 1.0;
+        BigInt stsp = 1;
         for( size_t i = 0; i < fg.nrVars(); i++ )
             stsp *= fg.var(i).states();
         cout << "Total state space:   " << stsp << endl;
@@ -116,9 +151,9 @@ int main( int argc, char *argv[] ) {
         cout << "Type: " << (fg.isPairwise() ? "pairwise" : "higher order") << " interactions, " << (fg.isBinary() ? "binary" : "nonbinary") << " variables" << endl;
 
         // Calculate complexity for LCBP
-        long double cavsum_lcbp = 0.0;
-        long double cavsum_lcbp2 = 0.0;
-        size_t max_Delta_size = 0;
+        BigInt cavsum_lcbp = 0;
+        BigInt cavsum_lcbp2 = 0;
+        BigInt max_Delta_size = 0;
         map<size_t,size_t> cavsizes;
         for( size_t i = 0; i < fg.nrVars(); i++ ) {
             VarSet di = fg.delta(i);
@@ -126,7 +161,7 @@ int main( int argc, char *argv[] ) {
                 cavsizes[di.size()]++;
             else
                 cavsizes[di.size()] = 1;
-            size_t Ds = fg.Delta(i).nrStates();
+            BigInt Ds = fg.Delta(i).nrStates();
             if( Ds > max_Delta_size )
                 max_Delta_size = Ds;
             cavsum_lcbp += di.nrStates();