add generic isl_pw_qpolynomial_bound
[barvinok.git] / barvinok_enumerate_e.cc
blob657f0fc3fc1605fba9af5b08e7d8b0f42e58a051
1 #include <ctype.h>
2 #include <unistd.h>
3 #include <stdlib.h>
4 #include <assert.h>
5 #include <barvinok/util.h>
6 #include <barvinok/barvinok.h>
7 #include "argp.h"
8 #include "progname.h"
9 #include "error.h"
10 #include "config.h"
11 #ifdef HAVE_OMEGA
12 #include "omega_interface/convert.h"
13 #include "omega_interface/count.h"
14 #endif
15 #include "skewed_genfun.h"
16 #include "verify.h"
17 #include "verif_ehrhart.h"
18 #include "verify_series.h"
19 #include "evalue_convert.h"
21 /* The input of this example program is a polytope in combined
22 * data and parameter space followed by two lines indicating
23 * the number of existential variables and parameters respectively.
24 * The first lines starts with "E ", followed by a number.
25 * The second lines starts with "P ", followed by a number.
26 * These two lines are (optionally) followed by the names of the parameters.
27 * The polytope is in PolyLib notation.
30 struct argp_option argp_options[] = {
31 { "isl", 'i', 0, 0 },
32 { "omega", 'o', 0, 0 },
33 { "parker", 'P', 0, 0 },
34 { "series", 's', 0, 0 },
35 { "series", 's', 0, 0, "compute rational generating function" },
36 { "explicit", 'e', 0, 0, "convert rgf to psp" },
37 { "scarf", 'S', 0, 0 },
38 { 0 }
41 struct arguments {
42 struct verify_options verify;
43 struct convert_options convert;
44 int isl;
45 int omega;
46 int parker;
47 int scarf;
48 int series;
49 int function;
52 error_t parse_opt(int key, char *arg, struct argp_state *state)
54 struct arguments *arguments = (struct arguments *)(state->input);
56 switch (key) {
57 case ARGP_KEY_INIT:
58 state->child_inputs[0] = arguments->verify.barvinok;
59 state->child_inputs[1] = &arguments->verify;
60 state->child_inputs[2] = &arguments->convert;
61 break;
62 case 'e':
63 arguments->function = 1;
64 /* fall through */
65 case 's':
66 arguments->series = 1;
67 break;
68 case 'S':
69 arguments->scarf = 1;
70 break;
71 case 'o':
72 #ifdef HAVE_OMEGA
73 arguments->omega = 1;
74 #else
75 error(0, 0, "--omega option not supported");
76 #endif
77 break;
78 case 'P':
79 #ifdef USE_PARKER
80 arguments->parker = 1;
81 #else
82 error(0, 0, "--parker option not supported");
83 #endif
84 break;
85 case 'i':
86 arguments->isl = 1;
87 break;
88 default:
89 return ARGP_ERR_UNKNOWN;
91 return 0;
94 #ifdef HAVE_OMEGA
96 Polyhedron *Omega_simplify(Polyhedron *P,
97 unsigned exist, unsigned nparam, const char **parms,
98 unsigned MaxRays)
100 varvector varv;
101 varvector paramv;
102 Relation r = Polyhedron2relation(P, exist, nparam, parms);
103 Polyhedron_Free(P);
104 return relation2Domain(r, varv, paramv, MaxRays);
106 #else
107 Polyhedron *Omega_simplify(Polyhedron *P,
108 unsigned exist, unsigned nparam, const char **parms,
109 unsigned MaxRays)
111 return P;
114 evalue *barvinok_enumerate_parker(Polyhedron *P,
115 unsigned exist, unsigned nparam,
116 unsigned MaxRays)
118 assert(0);
119 return NULL;
121 #endif
123 static void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
124 int exist, int nparam,
125 arguments *options);
127 static char *next_line(FILE *input, char *line, unsigned len)
129 char *p;
131 do {
132 if (!(p = fgets(line, len, input)))
133 return NULL;
134 while (isspace(*p) && *p != '\n')
135 ++p;
136 } while (*p == '#' || *p == '\n');
138 return p;
141 int main(int argc, char **argv)
143 Polyhedron *A;
144 Matrix *MA;
145 const char **param_name;
146 int exist, nparam, nvar;
147 char s[128];
148 evalue *EP = NULL;
149 gen_fun *gf = NULL;
150 int print_solution = 1;
151 struct arguments arguments;
152 static struct argp_child argp_children[] = {
153 { &barvinok_argp, 0, 0, 0 },
154 { &verify_argp, 0, "verification", BV_GRP_LAST+1 },
155 { &convert_argp, 0, "output conversion", BV_GRP_LAST+2 },
156 { 0 }
158 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
159 struct barvinok_options *options = barvinok_options_new_with_defaults();
161 arguments.verify.barvinok = options;
162 arguments.isl = 0;
163 arguments.omega = 0;
164 arguments.parker = 0;
165 arguments.scarf = 0;
166 arguments.series = 0;
167 arguments.function = 0;
169 set_program_name(argv[0]);
170 argp_parse(&argp, argc, argv, 0, 0, &arguments);
172 MA = Matrix_Read();
173 A = Constraints2Polyhedron(MA, options->MaxRays);
174 Matrix_Free(MA);
176 exist = -1;
177 while (next_line(stdin, s, sizeof(s)))
178 if (sscanf(s, "E %d", &exist) == 1)
179 break;
180 assert(exist >= 0);
182 nparam = -1;
183 while (next_line(stdin, s, sizeof(s)))
184 if (sscanf(s, "P %d", &nparam) == 1)
185 break;
186 assert(nparam >= 0);
188 /******* Read the options: initialize Min and Max ********/
190 if (arguments.verify.verify) {
191 verify_options_set_range(&arguments.verify, A->Dimension);
192 if (!options->verbose)
193 print_solution = 0;
196 if (print_solution && options->verbose) {
197 Polyhedron_Print(stdout, P_VALUE_FMT, A);
198 printf("exist: %d, nparam: %d\n", exist, nparam);
200 param_name = Read_ParamNames(stdin, nparam);
201 nvar = A->Dimension - exist - nparam;
202 if (arguments.omega) {
203 A = Omega_simplify(A, exist, nparam, param_name, options->MaxRays);
204 assert(!A->next);
205 exist = A->Dimension - nvar - nparam;
207 if (arguments.series) {
208 if (arguments.scarf)
209 gf = barvinok_enumerate_scarf_series(A, exist, nparam, options);
210 else
211 gf = barvinok_enumerate_e_series(A, exist, nparam, options);
212 if (print_solution) {
213 gf->print(std::cout, nparam, param_name);
214 puts("");
216 if (arguments.function) {
217 EP = *gf;
218 if (print_solution)
219 print_evalue(stdout, EP, param_name);
221 } else {
222 if (arguments.parker)
223 EP = barvinok_enumerate_parker(A, A->Dimension-nparam-exist,
224 nparam, options->MaxRays);
225 else if (arguments.scarf)
226 EP = barvinok_enumerate_scarf(A, exist, nparam, options);
227 else if (arguments.isl && exist > 0)
228 EP = barvinok_enumerate_isl(A, exist, nparam, options);
229 else
230 EP = barvinok_enumerate_e_with_options(A, exist, nparam, options);
231 reduce_evalue(EP);
232 if (evalue_convert(EP, &arguments.convert, options->verbose, nparam,
233 param_name))
234 print_solution = 0;
235 if (print_solution)
236 print_evalue(stdout, EP, param_name);
238 if (arguments.verify.verify) {
239 arguments.verify.params = param_name;
240 verify_results(A, EP, gf, exist, nparam, &arguments);
242 if (gf)
243 delete gf;
244 if (EP)
245 evalue_free(EP);
247 if (options->print_stats)
248 barvinok_stats_print(options->stats, stdout);
250 Free_ParamNames(param_name, nparam);
251 Polyhedron_Free(A);
252 barvinok_options_free(options);
253 return 0;
256 void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
257 int exist, int nparam,
258 arguments *options)
260 int i;
261 int res = 0;
262 Vector *p;
263 Value tmp;
264 Polyhedron *S, *CS;
265 unsigned MaxRays = options->verify.barvinok->MaxRays;
266 Polyhedron *C = NULL;
267 value_init(tmp);
269 p = Vector_Alloc(P->Dimension+2);
270 value_set_si(p->p[P->Dimension+1], 1);
272 CS = check_poly_context_scan(P, &C, nparam, &options->verify);
273 if (!C)
274 C = Universe_Polyhedron(nparam);
276 /* S = scanning list of polyhedra */
277 S = Polyhedron_Scan(P, C, MaxRays & POL_NO_DUAL ? 0 : MaxRays);
279 check_poly_init(C, &options->verify);
281 /******* CHECK NOW *********/
282 if (S) {
283 if (!options->series || options->function) {
284 if (!check_poly_EP(S, CS, EP, exist, nparam, 0, p->p,
285 &options->verify))
286 res = -1;
287 } else {
288 skewed_gen_fun *sgf = new skewed_gen_fun(new gen_fun(gf));
289 if (!check_poly_gf(S, CS, sgf, exist, nparam, 0, p->p,
290 &options->verify))
291 res = -1;
292 delete sgf;
296 if (!options->verify.print_all)
297 printf( "\n" );
299 Vector_Free(p);
300 value_clear(tmp);
301 Domain_Free(S);
302 Polyhedron_Free(C);
303 if (CS)
304 Domain_Free(CS);