verify.c: extract some helper functions for isl based verification
[barvinok.git] / options.c
blobe33965ab04440b008260128af87468c277fd0e40
1 #include <assert.h>
2 #include <unistd.h>
3 #include <barvinok/options.h>
4 #include <barvinok/util.h>
5 #include "argp.h"
6 #include "config.h"
8 #define MAXRAYS (POL_NO_DUAL | POL_INTEGER)
10 #define ALLOC(type) (type*)malloc(sizeof(type))
12 void barvinok_stats_clear(struct barvinok_stats *stats)
14 memset(stats, 0, sizeof(*stats));
17 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
19 fprintf(out, "Base cones: %ld\n", stats->base_cones);
20 if (stats->volume_simplices)
21 fprintf(out, "Volume simplices: %ld\n", stats->volume_simplices);
22 if (stats->topcom_chambers) {
23 fprintf(out, "TOPCOM empty chambers: %ld\n",
24 stats->topcom_empty_chambers);
25 fprintf(out, "TOPCOM chambers: %ld\n", stats->topcom_chambers);
26 fprintf(out, "TOPCOM distinct chambers: %ld\n",
27 stats->topcom_distinct_chambers);
29 if (stats->gbr_solved_lps)
30 fprintf(out, "LPs solved during GBR: %ld\n", stats->gbr_solved_lps);
31 if (stats->bernoulli_sums)
32 fprintf(out, "Bernoulli sums: %ld\n", stats->bernoulli_sums);
35 struct barvinok_options *barvinok_options_new_with_defaults()
37 struct barvinok_options *options = ALLOC(struct barvinok_options);
38 if (!options)
39 return NULL;
41 options->stats = ALLOC(struct barvinok_stats);
42 if (!options->stats) {
43 free(options);
44 return NULL;
47 barvinok_stats_clear(options->stats);
49 options->LLL_a = 1;
50 options->LLL_b = 1;
52 options->MaxRays = MAXRAYS;
54 #ifdef USE_INCREMENTAL_BF
55 options->incremental_specialization = 2;
56 #elif defined USE_INCREMENTAL_DF
57 options->incremental_specialization = 1;
58 #else
59 options->incremental_specialization = 0;
60 #endif
61 options->max_index = 1;
62 options->primal = 0;
63 #ifdef USE_MODULO
64 options->lookup_table = 0;
65 #else
66 options->lookup_table = 1;
67 #endif
68 options->count_sample_infinite = 1;
69 options->try_Delaunay_triangulation = 0;
71 options->bound = BV_BOUND_BERNSTEIN;
72 options->chambers = BV_CHAMBERS_POLYLIB;
74 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
75 options->approximation_method = BV_APPROX_NONE;
76 options->scale_flags = 0;
77 options->volume_triangulate = BV_VOL_VERTEX;
79 #ifdef HAVE_LIBGLPK
80 options->gbr_lp_solver = BV_GBR_GLPK;
81 #elif defined HAVE_LIBCDDGMP
82 options->gbr_lp_solver = BV_GBR_CDD;
83 #else
84 options->gbr_lp_solver = BV_GBR_PIP;
85 #endif
87 #ifdef HAVE_LIBGLPK
88 options->lp_solver = BV_LP_GLPK;
89 #elif defined HAVE_LIBCDDGMP
90 options->lp_solver = BV_LP_CDD;
91 #else
92 options->lp_solver = BV_LP_PIP;
93 #endif
95 options->summation = BV_SUM_LAURENT;
97 options->bernstein_optimize = BV_BERNSTEIN_NONE;
99 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
101 options->integer_hull = BV_HULL_GBR;
103 options->verbose = 0;
105 options->print_stats = 0;
107 options->gbr_only_first = 0;
109 return options;
112 void barvinok_options_free(struct barvinok_options *options)
114 free(options->stats);
115 free(options);
118 enum {
119 SCALE_FAST,
120 SCALE_SLOW,
121 SCALE_NARROW,
122 SCALE_NARROW2,
123 SCALE_CHAMBER,
126 const char *scale_opts[] = {
127 "fast",
128 "slow",
129 "narrow",
130 "narrow2",
131 "chamber",
132 NULL
135 static struct argp_option approx_argp_options[] = {
136 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
137 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
138 "method to use in polynomial approximation [default: drop]" },
139 { "scale-options", BV_OPT_SCALE,
140 "fast|slow,narrow|narrow2,chamber", 0 },
141 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
142 "type of triangulation to perform in volume computation [default: vertex]" },
143 { 0 }
146 static struct argp_option barvinok_argp_options[] = {
147 { "index", BV_OPT_MAXINDEX, "int", 0,
148 "maximal index of simple cones in decomposition" },
149 { "primal", BV_OPT_PRIMAL, 0, 0 },
150 { "table", BV_OPT_TABLE, 0, 0 },
151 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
152 #ifdef HAVE_GINAC
153 { "bound", BV_OPT_BOUND, "bernstein|range", 0,
154 "algorithm to use for computing bounds [default: bernstein]" },
155 #endif
156 #ifdef POINTS2TRIANGS_PATH
157 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
158 "tool to use for chamber decomposition [default: polylib]" },
159 #endif
160 { "gbr", BV_OPT_GBR,
161 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
162 "cdd|glpk|pip|pip-dual",
163 #elif defined(HAVE_LIBGLPK)
164 "glpk|pip|pip-dual",
165 #elif defined(HAVE_LIBCDDGMP)
166 "cdd|pip|pip-dual",
167 #else
168 "pip|pip-dual",
169 #endif
170 0, "lp solver to use for basis reduction "
171 #ifdef HAVE_LIBGLPK
172 "[default: glpk]"
173 #elif defined HAVE_LIBCDDGMP
174 "[default: cdd]"
175 #else
176 "[default: pip]"
177 #endif
179 { "lp", BV_OPT_LP,
180 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
181 "cdd|cddf|glpk|pip|polylib",
182 #elif defined(HAVE_LIBGLPK)
183 "glpk|pip|polylib",
184 #elif defined(HAVE_LIBCDDGMP)
185 "cdd|cddf|pip|polylib",
186 #else
187 "pip|polylib",
188 #endif
189 0, "lp solver to use "
190 #if defined(HAVE_LIBGLPK)
191 "[default: glpk]",
192 #elif defined(HAVE_LIBCDDGMP)
193 "[default: cdd]",
194 #else
195 "[default: pip]",
196 #endif
198 { "summation", BV_OPT_SUM, "box|bernoulli|euler|laurent", 0,
199 "[default: laurent]" },
200 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
201 "[default: factors]" },
202 { "recurse", BV_OPT_RECURSE, "",
203 OPTION_ALIAS | OPTION_HIDDEN },
204 { "integer-hull", BV_OPT_HULL,
205 #ifdef USE_ZSOLVE
206 "gbr|hilbert",
207 #else
208 "gbr",
209 #endif
210 0, "[default: gbr]" },
211 { "version", 'V', 0, 0 },
212 { "verbose", 'v' },
213 { "print-stats", BV_OPT_PRINT_STATS, 0, 0 },
214 { 0 }
217 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
219 struct barvinok_options *options = state->input;
220 char *subopt;
222 switch (key) {
223 case BV_OPT_POLAPPROX:
224 if (!arg) {
225 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
226 if (options->approximation_method == BV_APPROX_NONE)
227 options->approximation_method = BV_APPROX_SCALE;
228 } else {
229 if (!strcmp(arg, "lower"))
230 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
231 else if (!strcmp(arg, "upper"))
232 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
233 if (options->approximation_method == BV_APPROX_NONE)
234 options->approximation_method = BV_APPROX_DROP;
236 break;
237 case BV_OPT_APPROX:
238 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
239 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
240 if (!strcmp(arg, "scale"))
241 options->approximation_method = BV_APPROX_SCALE;
242 else if (!strcmp(arg, "drop"))
243 options->approximation_method = BV_APPROX_DROP;
244 else if (!strcmp(arg, "volume"))
245 options->approximation_method = BV_APPROX_VOLUME;
246 else if (!strcmp(arg, "bernoulli"))
247 options->approximation_method = BV_APPROX_BERNOULLI;
248 else
249 argp_error(state, "unknown value for --approximation-method option");
250 break;
251 case BV_OPT_SCALE:
252 options->approximation_method = BV_APPROX_SCALE;
253 while (*arg != '\0')
254 switch (getsubopt(&arg, scale_opts, &subopt)) {
255 case SCALE_FAST:
256 options->scale_flags |= BV_APPROX_SCALE_FAST;
257 break;
258 case SCALE_SLOW:
259 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
260 break;
261 case SCALE_NARROW:
262 options->scale_flags |= BV_APPROX_SCALE_NARROW;
263 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
264 break;
265 case SCALE_NARROW2:
266 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
267 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
268 break;
269 case SCALE_CHAMBER:
270 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
271 break;
272 default:
273 argp_error(state, "unknown suboption '%s'\n", subopt);
275 break;
276 case BV_OPT_VOL:
277 if (!strcmp(arg, "lift"))
278 options->volume_triangulate = BV_VOL_LIFT;
279 else if (!strcmp(arg, "vertex"))
280 options->volume_triangulate = BV_VOL_VERTEX;
281 else if (!strcmp(arg, "barycenter"))
282 options->volume_triangulate = BV_VOL_BARYCENTER;
283 break;
284 case ARGP_KEY_END:
285 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
286 options->approximation_method != BV_APPROX_NONE) {
287 fprintf(stderr,
288 "no polynomial approximation selected; reseting approximation method\n");
289 options->approximation_method = BV_APPROX_NONE;
291 break;
292 default:
293 return ARGP_ERR_UNKNOWN;
295 return 0;
298 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
300 struct barvinok_options *options = state->input;
301 char *subopt;
303 switch (key) {
304 case ARGP_KEY_INIT:
305 state->child_inputs[0] = options;
306 break;
307 case 'v':
308 options->verbose++;
309 break;
310 case 'V':
311 printf(barvinok_version());
312 exit(0);
313 case BV_OPT_SPECIALIZATION:
314 if (!strcmp(arg, "bf"))
315 options->incremental_specialization = BV_SPECIALIZATION_BF;
316 else if (!strcmp(arg, "df"))
317 options->incremental_specialization = BV_SPECIALIZATION_DF;
318 else if (!strcmp(arg, "random"))
319 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
320 else if (!strcmp(arg, "todd"))
321 options->incremental_specialization = BV_SPECIALIZATION_TODD;
322 break;
323 case BV_OPT_PRIMAL:
324 options->primal = 1;
325 break;
326 case BV_OPT_TABLE:
327 options->lookup_table = 1;
328 break;
329 case BV_OPT_BOUND:
330 if (!strcmp(arg, "bernstein"))
331 options->bound = BV_BOUND_BERNSTEIN;
332 if (!strcmp(arg, "range"))
333 options->bound = BV_BOUND_RANGE;
334 break;
335 case BV_OPT_CHAMBERS:
336 if (!strcmp(arg, "polylib"))
337 options->chambers = BV_CHAMBERS_POLYLIB;
338 if (!strcmp(arg, "topcom"))
339 options->chambers = BV_CHAMBERS_TOPCOM;
340 break;
341 case BV_OPT_GBR:
342 if (!strcmp(arg, "cdd"))
343 options->gbr_lp_solver = BV_GBR_CDD;
344 if (!strcmp(arg, "glpk"))
345 options->gbr_lp_solver = BV_GBR_GLPK;
346 if (!strcmp(arg, "pip"))
347 options->gbr_lp_solver = BV_GBR_PIP;
348 if (!strcmp(arg, "pip-dual"))
349 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
350 break;
351 case BV_OPT_LP:
352 if (!strcmp(arg, "cdd"))
353 options->lp_solver = BV_LP_CDD;
354 if (!strcmp(arg, "cddf"))
355 options->lp_solver = BV_LP_CDDF;
356 if (!strcmp(arg, "glpk"))
357 options->lp_solver = BV_LP_GLPK;
358 if (!strcmp(arg, "pip"))
359 options->lp_solver = BV_LP_PIP;
360 if (!strcmp(arg, "polylib"))
361 options->lp_solver = BV_LP_POLYLIB;
362 break;
363 case BV_OPT_MAXINDEX:
364 options->max_index = strtoul(arg, NULL, 0);
365 break;
366 case BV_OPT_SUM:
367 if (!strcmp(arg, "box"))
368 options->summation = BV_SUM_BOX;
369 else if (!strcmp(arg, "barvinok"))
370 options->summation = BV_SUM_BOX;
371 else if (!strcmp(arg, "euler"))
372 options->summation = BV_SUM_EULER;
373 else if (!strcmp(arg, "bernoulli"))
374 options->summation = BV_SUM_BERNOULLI;
375 else if (!strcmp(arg, "laurent"))
376 options->summation = BV_SUM_LAURENT;
377 else if (!strcmp(arg, "laurent_old"))
378 options->summation = BV_SUM_LAURENT_OLD;
379 else
380 argp_error(state, "unknown summation method '%s'\n", arg);
381 break;
382 case BV_OPT_RECURSE:
383 if (!strcmp(arg, "none"))
384 options->bernstein_recurse = 0;
385 else if (!strcmp(arg, "factors"))
386 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
387 else if (!strcmp(arg, "intervals"))
388 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
389 else if (!strcmp(arg, "full"))
390 options->bernstein_recurse =
391 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
392 break;
393 case BV_OPT_HULL:
394 if (!strcmp(arg, "gbr"))
395 options->integer_hull = BV_HULL_GBR;
396 else if (!strcmp(arg, "hilbert"))
397 options->integer_hull = BV_HULL_HILBERT;
398 break;
399 case BV_OPT_PRINT_STATS:
400 options->print_stats = 1;
401 break;
402 default:
403 return ARGP_ERR_UNKNOWN;
405 return 0;
408 static struct argp approx_argp = {
409 approx_argp_options, approx_parse_opt, 0, 0
412 static struct argp_child barvinok_children[] = {
413 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
414 { 0 }
417 struct argp barvinok_argp = {
418 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children