3 #include <barvinok/util.h>
4 #include <barvinok/barvinok.h>
7 #include "omega/convert.h"
10 /* The input of this example program is a polytope in combined
11 * data and parameter space followed by two lines indicating
12 * the number of existential variables and parameters respectively.
13 * The first lines starts with "E ", followed by a number.
14 * The second lines starts with "P ", followed by a number.
15 * These two lines are (optionally) followed by the names of the parameters.
16 * The polytope is in PolyLib notation.
19 #ifdef HAVE_GROWING_CHERNIKOVA
20 #define MAXRAYS POL_NO_DUAL
26 #define getopt_long(a,b,c,d,e) getopt(a,b,c)
29 struct option options
[] = {
31 { "omega", no_argument
, 0, 'o' },
34 { "pip", no_argument
, 0, 'p' },
36 { "series", no_argument
, 0, 's' },
37 { "scarf", no_argument
, 0, 'S' },
38 { "convert", no_argument
, 0, 'c' },
39 { "floor", no_argument
, 0, 'f' },
40 { "range-reduction", no_argument
, 0, 'R' },
41 { "verify", no_argument
, 0, 'T' },
42 { "print-all", no_argument
, 0, 'A' },
43 { "min", required_argument
, 0, 'm' },
44 { "max", required_argument
, 0, 'M' },
45 { "range", required_argument
, 0, 'r' },
46 { "version", no_argument
, 0, 'V' },
52 #define PIPLIB_OPT "p"
60 Polyhedron
*Omega_simplify(Polyhedron
*P
,
61 unsigned exist
, unsigned nparam
, char **parms
)
65 Relation r
= Polyhedron2relation(P
, exist
, nparam
, parms
);
67 return relation2Domain(r
, varv
, paramv
);
71 Polyhedron
*Omega_simplify(Polyhedron
*P
,
72 unsigned exist
, unsigned nparam
, char **parms
)
78 /* define this to continue the test after first error found */
79 /* #define DONT_BREAK_ON_ERROR */
81 /* RANGE : normal range for evalutations (-RANGE -> RANGE) */
84 /* SRANGE : small range for evalutations */
87 /* if dimension >= BIDDIM, use SRANGE */
90 /* VSRANGE : very small range for evalutations */
93 /* if dimension >= VBIDDIM, use VSRANGE */
96 static Value min_val
, max_val
;
102 static int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
103 int exist
, int nparam
, int pos
, Value
*z
, int print_all
);
104 static void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
,
105 int m
, int M
, int print_all
);
107 int main(int argc
, char **argv
)
112 int exist
, nparam
, nvar
;
125 int m
= INT_MAX
, M
= INT_MIN
, r
;
126 int print_solution
= 1;
128 while ((c
= getopt_long(argc
, argv
,
129 OMEGA_OPT PIPLIB_OPT
"sSfcRTAm:M:r:V", options
, &ind
)) != -1) {
172 printf(barvinok_version());
178 if (series
&& !scarf
) {
180 "--series currently only available if --scarf is specified\n");
185 A
= Constraints2Polyhedron(MA
, MAXRAYS
);
188 fgets(s
, 128, stdin
);
189 while ((*s
=='#') || (sscanf(s
, "E %d", &exist
)<1))
190 fgets(s
, 128, stdin
);
192 fgets(s
, 128, stdin
);
193 while ((*s
=='#') || (sscanf(s
, "P %d", &nparam
)<1))
194 fgets(s
, 128, stdin
);
196 /******* Read the options: initialize Min and Max ********/
197 if (A
->Dimension
>= VBIGDIM
)
199 else if (A
->Dimension
>= BIGDIM
)
208 if (verify
&& m
> M
) {
209 fprintf(stderr
,"Nothing to do: min > max !\n");
215 if (print_solution
) {
216 Polyhedron_Print(stdout
, P_VALUE_FMT
, A
);
217 printf("exist: %d, nparam: %d\n", exist
, nparam
);
219 param_name
= Read_ParamNames(stdin
, nparam
);
220 nvar
= A
->Dimension
- exist
- nparam
;
222 A
= Omega_simplify(A
, exist
, nparam
, param_name
);
224 exist
= A
->Dimension
- nvar
- nparam
;
228 barvinok_options
*options
= barvinok_options_new_with_defaults();
230 gf
= barvinok_enumerate_scarf_series(A
, exist
, nparam
, options
);
231 if (print_solution
) {
232 gf
->print(std::cout
, nparam
, param_name
);
239 barvinok_options
*options
= barvinok_options_new_with_defaults();
240 EP
= barvinok_enumerate_scarf(A
, exist
, nparam
, options
);
242 } else if (pip
&& exist
> 0)
243 EP
= barvinok_enumerate_pip(A
, exist
, nparam
, MAXRAYS
);
245 EP
= barvinok_enumerate_e(A
, exist
, nparam
, MAXRAYS
);
249 evalue_range_reduction(EP
);
251 print_evalue(stdout
, EP
, param_name
);
253 fprintf(stderr
, "WARNING: floor conversion not supported\n");
254 evalue_frac2floor(EP
);
256 print_evalue(stdout
, EP
, param_name
);
257 } else if (convert
) {
258 evalue_mod2table(EP
, nparam
);
260 print_evalue(stdout
, EP
, param_name
);
263 verify_results(A
, EP
, exist
, nparam
, m
, M
, print_all
);
264 free_evalue_refs(EP
);
267 Free_ParamNames(param_name
, nparam
);
272 void verify_results(Polyhedron
*P
, evalue
*EP
, int exist
, int nparam
, int m
, int M
,
279 Polyhedron
*C
= Polyhedron_Project(P
, nparam
);
282 value_set_si(min_val
,m
);
283 value_set_si(max_val
,M
);
286 p
= (Value
*)malloc(sizeof(Value
) * (P
->Dimension
+2));
287 for(i
=0;i
<=P
->Dimension
;i
++) {
289 value_set_si(p
[i
],0);
292 value_set_si(p
[i
],1);
294 /* S = scanning list of polyhedra */
295 S
= Polyhedron_Scan(P
, C
, MAXRAYS
& POL_NO_DUAL
? 0 : MAXRAYS
);
298 if (C
->Dimension
> 0) {
299 value_subtract(tmp
,max_val
,min_val
);
300 if (VALUE_TO_INT(tmp
) > 80)
301 st
= 1+(VALUE_TO_INT(tmp
))/80;
304 for(i
=VALUE_TO_INT(min_val
);i
<=VALUE_TO_INT(max_val
);i
+=st
)
311 /******* CHECK NOW *********/
313 if(S
&& !check_poly(S
, C
, EP
, exist
, nparam
, 0, p
, print_all
)) {
314 fprintf(stderr
,"Check failed !\n");
321 for(i
=0;i
<=(P
->Dimension
+1);i
++)
329 /****************************************************/
330 /* function check_poly : */
331 /* scans the parameter space from min to max (all */
332 /* directions). Computes the number of points in */
333 /* the polytope using both methods, and compare them*/
334 /* returns 1 on success */
335 /****************************************************/
337 int check_poly(Polyhedron
*S
, Polyhedron
*C
, evalue
*EP
,
338 int exist
, int nparam
, int pos
, Value
*z
, int print_all
)
343 value_init(c
); value_init(tmp
);
347 /* Computes the ehrhart polynomial */
348 value_set_double(c
, compute_evalue(EP
,&z
[S
->Dimension
-nparam
+1])+.25);
349 /* if c=0 we may be out of context. */
350 /* scanning is useless in this case*/
351 if(!in_domain(C
,&z
[S
->Dimension
-nparam
+1])) {
359 value_print(stdout
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
360 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
362 value_print(stdout
,VALUE_FMT
,z
[k
]);
365 value_print(stdout
,VALUE_FMT
,c
);
369 /* Manually count the number of points */
370 count_points_e(1, S
, exist
, nparam
, z
, &tmp
);
372 printf(", count = ");
373 value_print(stdout
, P_VALUE_FMT
, tmp
);
377 if(value_ne(tmp
,c
)) {
380 fprintf(stderr
,"Error !\n");
381 fprintf(stderr
,"EP( ");
382 value_print(stderr
,VALUE_FMT
,z
[S
->Dimension
-nparam
+1]);
383 for(k
=S
->Dimension
-nparam
+2;k
<=S
->Dimension
;++k
) {
384 fprintf(stderr
,", ");
385 value_print(stderr
,VALUE_FMT
,z
[k
]);
387 fprintf(stderr
," ) should be ");
388 value_print(stderr
,VALUE_FMT
,tmp
);
389 fprintf(stderr
,", while EP eval gives ");
390 value_print(stderr
,VALUE_FMT
,c
);
391 fprintf(stderr
,".\n");
392 print_evalue(stderr
, EP
, params
);
393 #ifndef DONT_BREAK_ON_ERROR
394 value_clear(c
); value_clear(tmp
);
403 for(value_assign(tmp
,min_val
); value_le(tmp
,max_val
); value_increment(tmp
,tmp
)) {
405 k
= VALUE_TO_INT(tmp
);
406 if(!pos
&& !(k
%st
)) {
412 value_assign(z
[pos
+S
->Dimension
-nparam
+1],tmp
);
413 if(!check_poly(S
, C
, EP
, exist
, nparam
, pos
+1, z
, print_all
)) {
414 value_clear(c
); value_clear(tmp
);
418 value_clear(c
); value_clear(tmp
);