Small misc changes
[libdai.git] / utils / remove_short_loops.cpp
1 /* Copyright (C) 2006-2008 Joris Mooij [j dot mooij at science dot ru dot nl]
2 Radboud University Nijmegen, The Netherlands
3
4 This file is part of libDAI.
5
6 libDAI is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 libDAI is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with libDAI; if not, write to the Free Software
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21
22 #include <iostream>
23 #include <cstdlib>
24 #include <dai/factorgraph.h>
25
26
27 using namespace dai;
28 using namespace std;
29
30
31 int main( int argc, char *argv[] ) {
32 if( argc != 3 ) {
33 cout << "Usage: " << argv[0] << " <in.fg> <out.fg>" << endl << endl;
34 cout << "Merges short loops (of length 4 in the factor graph) in <in.fg>" << endl;
35 cout << "and writes result to <out.fg>." << endl;
36 cout << "Returns 2 in case of error reading <in.fg>, 1 if there are no" << endl;
37 cout << "short loops present and 0 if short loops have been merged." << endl;
38 return 1;
39 } else {
40 // Read factorgraph
41 FactorGraph fg;
42 char *infile = argv[1];
43
44 if( fg.ReadFromFile( infile ) ) {
45 cerr << "Error reading file " << infile << endl;
46 return 2;
47 } else {
48 vector<Factor> fg_facs;
49
50 fg_facs.reserve( fg.nrFactors() );
51 for( size_t I = 0; I < fg.nrFactors(); I++ )
52 fg_facs.push_back( fg.factor(I) );
53
54 if( hasShortLoops( fg_facs) ) {
55 RemoveShortLoops( fg_facs );
56 FactorGraph newfg( fg_facs );
57 newfg.WriteToFile( argv[2] );
58 return 0;
59 } else {
60 fg.WriteToFile( argv[2] );
61 return 1;
62 }
63 }
64 }
65 }