File: check-representation.cpp

package info (click to toggle)
latte-int 1.7.6%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 38,260 kB
  • sloc: cpp: 32,231; sh: 4,413; makefile: 811; perl: 300
file content (152 lines) | stat: -rw-r--r-- 4,116 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* check-representation.cpp -- Check whether a vector is generated by given generators
	       
   Copyright 2007 Matthias Koeppe

   This file is part of LattE.
   
   LattE is free software; you can redistribute it and/or modify it
   under the terms of the version 2 of the GNU General Public License
   as published by the Free Software Foundation.

   LattE is distributed in the hope that it will be useful, but
   WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with LattE; if not, write to the Free Software Foundation,
   Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
*/

#include <cstdlib>
#include <cassert>
#include <climits>
#include <cstring>
#include <string>
#include <iostream>
#include <fstream>

#include <cplex.h>

#include "ReductionTest.h"

using namespace std;

int verbose = false;

ReductionTest *reduction_test = NULL;
ReductionTestFactory reduction_test_factory;

static void usage()
{
  cerr << "usage: check-representation [OPTIONS] VECTORS" << endl;
  cerr << "Options are: " << endl
       << "  --no-header                              Read an input file without a dimensions header" << endl;
  reduction_test_factory.show_options(cerr);
}

void
open_matrix_file(const string &filename, ifstream &file,
		 int &num, int &dim)
{
  file.open(filename.c_str());
  if (!file.good()) {
    cerr << "Failed to open " << filename << endl;
    exit(1);
  }
  file >> num >> dim;
  if (!file.good()) {
    cerr << "Parse error reading file " << filename << endl;
    exit(1);
  }
}

int main(int argc, char **argv)
{
  if (argc < 2) {
    usage();
    exit(1);
  }
  // Set default.
  reduction_test_factory.type = ReductionTestFactory::ReductionWithCPLEX;
  
  bool vectors_file_no_header = false;
  {
    int i;
    for (i = 1; i<argc-1; i++) {
      if (reduction_test_factory.parse_option(argv[i])) {}
      else if (strcmp(argv[i], "--no-header") == 0) {
	vectors_file_no_header = true;
      }
      else if (strcmp(argv[i], "--verbose") == 0) {
	reduction_test_factory.verbose = true;
      }
      else {
	cerr << "Unknown option: " << argv[i] << endl;
	exit(1);
      }
    }
  }
  reduction_test = reduction_test_factory.CreateReductionTest();

  int dim_generators = reduction_test->GetDimension();
  
  string vectors_filename(argv[argc - 1]);
  int num_vectors, dim_vectors;
  ifstream vectors_file;
  if (vectors_file_no_header) {
    vectors_file.open(vectors_filename.c_str());
    if (!vectors_file.good()) {
      cerr << "Failed to open file " << vectors_filename << endl;
      exit(1);
    }
    if (dim_generators < 0) {
      cerr << "Sorry, I do not know the dimension of the vectors." << endl;
      exit(1);
    }
    dim_vectors = dim_generators;
    num_vectors = INT_MAX;
  }
  else {
    open_matrix_file(vectors_filename, vectors_file, num_vectors, dim_vectors);
    if (dim_generators >= 0) {
      assert(dim_vectors == dim_generators);
    }
  }

  cerr << "Checking that all vectors are generated over the nonnegative reals, and the nonnegative integers." << endl;
  vector<int> v(dim_vectors);
  int i;
  for (i = 0; i<num_vectors; i++) {
    if (i % 1000 == 0) {
      cerr << i << "/" << num_vectors << " done. " << endl;
    }
    if (verbose)
    	cerr << "Checking vector: ";
    int j;
    for (j = 0; j<dim_vectors; j++) {
      vectors_file >> v[j];
      if (verbose)
    	  cerr << v[j] << " ";
    }

    if (vectors_file_no_header && vectors_file.eof()) {
    	cerr << "After reading " << i << " vectors, end of file." << endl;
      exit(0);
    }
    if (!vectors_file.good()) {
    	cerr << "After reading " << i << " vectors, parse error reading vectors file" << endl;
      exit(1);
    }
    if (verbose)
    	cerr << endl;

    if (!reduction_test->IsReducible(v)) {
    	cerr << "Irreducible: ";
      for (j = 0; j<dim_vectors; j++)
    	  cerr << v[j] << " ";
      cerr << endl;
    }
  }
  delete reduction_test;
}