rename barvinok_maximize to barvinok_bound
[barvinok.git] / barvinok_enumerate_e.cc
blobe90b343cd9cbd51b620f8ab7dfe1a7c73f03de9d
1 #include <unistd.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <barvinok/util.h>
5 #include <barvinok/barvinok.h>
6 #include "argp.h"
7 #include "progname.h"
8 #include "error.h"
9 #include "config.h"
10 #ifdef HAVE_OMEGA
11 #include "omega/convert.h"
12 #endif
13 #include "skewed_genfun.h"
14 #include "verify.h"
15 #include "verif_ehrhart.h"
16 #include "verify_series.h"
17 #include "evalue_convert.h"
19 /* The input of this example program is a polytope in combined
20 * data and parameter space followed by two lines indicating
21 * the number of existential variables and parameters respectively.
22 * The first lines starts with "E ", followed by a number.
23 * The second lines starts with "P ", followed by a number.
24 * These two lines are (optionally) followed by the names of the parameters.
25 * The polytope is in PolyLib notation.
28 struct argp_option argp_options[] = {
29 { "omega", 'o', 0, 0 },
30 { "pip", 'p', 0, 0 },
31 { "series", 's', 0, 0 },
32 { "series", 's', 0, 0, "compute rational generating function" },
33 { "explicit", 'e', 0, 0, "convert rgf to psp" },
34 { "scarf", 'S', 0, 0 },
35 { 0 }
38 struct arguments {
39 struct verify_options verify;
40 struct convert_options convert;
41 int omega;
42 int pip;
43 int scarf;
44 int series;
45 int function;
48 error_t parse_opt(int key, char *arg, struct argp_state *state)
50 struct arguments *arguments = (struct arguments *)(state->input);
52 switch (key) {
53 case ARGP_KEY_INIT:
54 state->child_inputs[0] = arguments->verify.barvinok;
55 state->child_inputs[1] = &arguments->verify;
56 state->child_inputs[2] = &arguments->convert;
57 break;
58 case 'e':
59 arguments->function = 1;
60 /* fall through */
61 case 's':
62 arguments->series = 1;
63 break;
64 case 'S':
65 arguments->scarf = 1;
66 break;
67 case 'o':
68 #ifdef HAVE_OMEGA
69 arguments->omega = 1;
70 #else
71 error(0, 0, "--omega option not supported");
72 #endif
73 break;
74 case 'p':
75 arguments->pip = 1;
76 break;
77 default:
78 return ARGP_ERR_UNKNOWN;
80 return 0;
83 #ifdef HAVE_OMEGA
85 Polyhedron *Omega_simplify(Polyhedron *P,
86 unsigned exist, unsigned nparam, char **parms)
88 varvector varv;
89 varvector paramv;
90 Relation r = Polyhedron2relation(P, exist, nparam, parms);
91 Polyhedron_Free(P);
92 return relation2Domain(r, varv, paramv);
94 #else
95 Polyhedron *Omega_simplify(Polyhedron *P,
96 unsigned exist, unsigned nparam, char **parms)
98 return P;
100 #endif
102 static void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
103 int exist, int nparam,
104 arguments *options);
106 int main(int argc, char **argv)
108 Polyhedron *A;
109 Matrix *MA;
110 char **param_name;
111 int exist, nparam, nvar;
112 char s[128];
113 evalue *EP = NULL;
114 gen_fun *gf = NULL;
115 int print_solution = 1;
116 struct arguments arguments;
117 static struct argp_child argp_children[] = {
118 { &barvinok_argp, 0, 0, 0 },
119 { &verify_argp, 0, "verification", BV_GRP_LAST+1 },
120 { &convert_argp, 0, "output conversion", BV_GRP_LAST+2 },
121 { 0 }
123 static struct argp argp = { argp_options, parse_opt, 0, 0, argp_children };
124 struct barvinok_options *options = barvinok_options_new_with_defaults();
126 arguments.verify.barvinok = options;
127 arguments.omega = 0;
128 arguments.pip = 0;
129 arguments.scarf = 0;
130 arguments.series = 0;
131 arguments.function = 0;
133 set_program_name(argv[0]);
134 argp_parse(&argp, argc, argv, 0, 0, &arguments);
136 MA = Matrix_Read();
137 A = Constraints2Polyhedron(MA, options->MaxRays);
138 Matrix_Free(MA);
140 fgets(s, 128, stdin);
141 while ((*s=='#') || (sscanf(s, "E %d", &exist)<1))
142 fgets(s, 128, stdin);
144 fgets(s, 128, stdin);
145 while ((*s=='#') || (sscanf(s, "P %d", &nparam)<1))
146 fgets(s, 128, stdin);
148 /******* Read the options: initialize Min and Max ********/
150 if (arguments.verify.verify) {
151 verify_options_set_range(&arguments.verify, A->Dimension);
152 if (!options->verbose)
153 print_solution = 0;
156 if (print_solution && options->verbose) {
157 Polyhedron_Print(stdout, P_VALUE_FMT, A);
158 printf("exist: %d, nparam: %d\n", exist, nparam);
160 param_name = Read_ParamNames(stdin, nparam);
161 nvar = A->Dimension - exist - nparam;
162 if (arguments.omega) {
163 A = Omega_simplify(A, exist, nparam, param_name);
164 assert(!A->next);
165 exist = A->Dimension - nvar - nparam;
167 if (arguments.series) {
168 if (arguments.scarf)
169 gf = barvinok_enumerate_scarf_series(A, exist, nparam, options);
170 else
171 gf = barvinok_enumerate_e_series(A, exist, nparam, options);
172 if (print_solution) {
173 gf->print(std::cout, nparam, param_name);
174 puts("");
176 if (arguments.function) {
177 EP = *gf;
178 if (print_solution)
179 print_evalue(stdout, EP, param_name);
181 } else {
182 if (arguments.scarf)
183 EP = barvinok_enumerate_scarf(A, exist, nparam, options);
184 else if (arguments.pip && exist > 0)
185 EP = barvinok_enumerate_pip_with_options(A, exist, nparam, options);
186 else
187 EP = barvinok_enumerate_e_with_options(A, exist, nparam, options);
188 reduce_evalue(EP);
189 if (evalue_convert(EP, &arguments.convert, options->verbose, nparam,
190 param_name))
191 print_solution = 0;
192 if (print_solution)
193 print_evalue(stdout, EP, param_name);
195 if (arguments.verify.verify) {
196 arguments.verify.params = param_name;
197 verify_results(A, EP, gf, exist, nparam, &arguments);
199 if (gf)
200 delete gf;
201 if (EP)
202 evalue_free(EP);
204 if (options->print_stats)
205 barvinok_stats_print(options->stats, stdout);
207 Free_ParamNames(param_name, nparam);
208 Polyhedron_Free(A);
209 barvinok_options_free(options);
210 return 0;
213 void verify_results(Polyhedron *P, evalue *EP, gen_fun *gf,
214 int exist, int nparam,
215 arguments *options)
217 int i;
218 int res = 0;
219 Vector *p;
220 Value tmp;
221 Polyhedron *S, *CS;
222 unsigned MaxRays = options->verify.barvinok->MaxRays;
223 Polyhedron *C = NULL;
224 value_init(tmp);
226 p = Vector_Alloc(P->Dimension+2);
227 value_set_si(p->p[P->Dimension+1], 1);
229 CS = check_poly_context_scan(P, &C, nparam, &options->verify);
230 if (!C)
231 C = Universe_Polyhedron(nparam);
233 /* S = scanning list of polyhedra */
234 S = Polyhedron_Scan(P, C, MaxRays & POL_NO_DUAL ? 0 : MaxRays);
236 check_poly_init(C, &options->verify);
238 /******* CHECK NOW *********/
239 if (S) {
240 if (!options->series || options->function) {
241 if (!check_poly_EP(S, CS, EP, exist, nparam, 0, p->p,
242 &options->verify))
243 res = -1;
244 } else {
245 skewed_gen_fun *sgf = new skewed_gen_fun(new gen_fun(gf));
246 if (!check_poly_gf(S, CS, sgf, exist, nparam, 0, p->p,
247 &options->verify))
248 res = -1;
249 delete sgf;
253 if (!options->verify.print_all)
254 printf( "\n" );
256 Vector_Free(p);
257 value_clear(tmp);
258 Domain_Free(S);
259 Polyhedron_Free(C);
260 if (CS)
261 Domain_Free(CS);