doc: document new options and new applications
[barvinok.git] / options.c
blobc11de578fbc15340a172bac23508fc8c62f010ce
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: %d\n", stats->base_cones);
20 if (stats->volume_simplices)
21 fprintf(out, "Volume simplices: %d\n", stats->volume_simplices);
22 if (stats->topcom_chambers) {
23 fprintf(out, "TOPCOM empty chambers: %d\n",
24 stats->topcom_empty_chambers);
25 fprintf(out, "TOPCOM chambers: %d\n", stats->topcom_chambers);
26 fprintf(out, "TOPCOM distinct chambers: %d\n",
27 stats->topcom_distinct_chambers);
29 if (stats->gbr_solved_lps)
30 fprintf(out, "LPs solved during GBR: %d\n", stats->gbr_solved_lps);
33 struct barvinok_options *barvinok_options_new_with_defaults()
35 struct barvinok_options *options = ALLOC(struct barvinok_options);
36 if (!options)
37 return NULL;
39 options->stats = ALLOC(struct barvinok_stats);
40 if (!options->stats) {
41 free(options);
42 return NULL;
45 barvinok_stats_clear(options->stats);
47 options->LLL_a = 1;
48 options->LLL_b = 1;
50 options->MaxRays = MAXRAYS;
52 #ifdef USE_INCREMENTAL_BF
53 options->incremental_specialization = 2;
54 #elif defined USE_INCREMENTAL_DF
55 options->incremental_specialization = 1;
56 #else
57 options->incremental_specialization = 0;
58 #endif
59 options->max_index = 1;
60 options->primal = 0;
61 #ifdef USE_MODULO
62 options->lookup_table = 0;
63 #else
64 options->lookup_table = 1;
65 #endif
66 options->count_sample_infinite = 1;
67 options->try_Delaunay_triangulation = 0;
69 options->chambers = BV_CHAMBERS_POLYLIB;
71 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
72 options->approximation_method = BV_APPROX_NONE;
73 options->scale_flags = 0;
74 options->volume_triangulate = BV_VOL_VERTEX;
76 #ifdef HAVE_LIBGLPK
77 options->gbr_lp_solver = BV_GBR_GLPK;
78 #elif defined HAVE_LIBCDDGMP
79 options->gbr_lp_solver = BV_GBR_CDD;
80 #else
81 options->gbr_lp_solver = BV_GBR_PIP;
82 #endif
84 #ifdef HAVE_LIBGLPK
85 options->lp_solver = BV_LP_GLPK;
86 #elif defined HAVE_LIBCDDGMP
87 options->lp_solver = BV_LP_CDD;
88 #else
89 options->lp_solver = BV_LP_PIP;
90 #endif
92 options->summation = BV_SUM_BARVINOK;
94 options->bernstein_optimize = BV_BERNSTEIN_NONE;
96 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
98 options->integer_hull = BV_HULL_GBR;
100 options->verbose = 0;
102 options->print_stats = 0;
104 options->gbr_only_first = 0;
106 return options;
109 void barvinok_options_free(struct barvinok_options *options)
111 free(options->stats);
112 free(options);
115 enum {
116 SCALE_FAST,
117 SCALE_SLOW,
118 SCALE_NARROW,
119 SCALE_NARROW2,
120 SCALE_CHAMBER,
123 const char *scale_opts[] = {
124 "fast",
125 "slow",
126 "narrow",
127 "narrow2",
128 "chamber",
129 NULL
132 static struct argp_option approx_argp_options[] = {
133 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
134 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
135 "method to use in polynomial approximation [default: drop]" },
136 { "scale-options", BV_OPT_SCALE,
137 "fast|slow,narrow|narrow2,chamber", 0 },
138 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
139 "type of triangulation to perform in volume computation [default: vertex]" },
140 { 0 }
143 static struct argp_option barvinok_argp_options[] = {
144 { "index", BV_OPT_MAXINDEX, "int", 0,
145 "maximal index of simple cones in decomposition" },
146 { "primal", BV_OPT_PRIMAL, 0, 0 },
147 { "table", BV_OPT_TABLE, 0, 0 },
148 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
149 #ifdef POINTS2TRIANGS_PATH
150 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
151 "tool to use for chamber decomposition [default: polylib]" },
152 #endif
153 { "gbr", BV_OPT_GBR,
154 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
155 "cdd|glpk|pip|pip-dual",
156 #elif defined(HAVE_LIBGLPK)
157 "glpk|pip|pip-dual",
158 #elif defined(HAVE_LIBCDDGMP)
159 "cdd|pip|pip-dual",
160 #else
161 "pip|pip-dual",
162 #endif
163 0, "lp solver to use for basis reduction "
164 #ifdef HAVE_LIBGLPK
165 "[default: glpk]"
166 #elif defined HAVE_LIBCDDGMP
167 "[default: cdd]"
168 #else
169 "[default: pip]"
170 #endif
172 { "lp", BV_OPT_LP,
173 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
174 "cdd|cddf|glpk|pip|polylib",
175 #elif defined(HAVE_LIBGLPK)
176 "glpk|pip|polylib",
177 #elif defined(HAVE_LIBCDDGMP)
178 "cdd|cddf|pip|polylib",
179 #else
180 "pip|polylib",
181 #endif
182 0, "lp solver to use "
183 #if defined(HAVE_LIBGLPK)
184 "[default: glpk]",
185 #elif defined(HAVE_LIBCDDGMP)
186 "[default: cdd]",
187 #else
188 "[default: pip]",
189 #endif
191 { "summation", BV_OPT_SUM, "barvinok|bernoulli|euler", 0,
192 "[default: barvinok]" },
193 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
194 "[default: factors]" },
195 { "recurse", BV_OPT_RECURSE, "",
196 OPTION_ALIAS | OPTION_HIDDEN },
197 { "integer-hull", BV_OPT_HULL,
198 #ifdef USE_ZSOLVE
199 "gbr|hilbert",
200 #else
201 "gbr",
202 #endif
203 0, "[default: gbr]" },
204 { "version", 'V', 0, 0 },
205 { "verbose", 'v' },
206 { "print-stats", BV_OPT_PRINT_STATS, 0, 0 },
207 { 0 }
210 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
212 struct barvinok_options *options = state->input;
213 char *subopt;
215 switch (key) {
216 case BV_OPT_POLAPPROX:
217 if (!arg) {
218 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
219 if (options->approximation_method == BV_APPROX_NONE)
220 options->approximation_method = BV_APPROX_SCALE;
221 } else {
222 if (!strcmp(arg, "lower"))
223 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
224 else if (!strcmp(arg, "upper"))
225 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
226 if (options->approximation_method == BV_APPROX_NONE)
227 options->approximation_method = BV_APPROX_DROP;
229 break;
230 case BV_OPT_APPROX:
231 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
232 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
233 if (!strcmp(arg, "scale"))
234 options->approximation_method = BV_APPROX_SCALE;
235 else if (!strcmp(arg, "drop"))
236 options->approximation_method = BV_APPROX_DROP;
237 else if (!strcmp(arg, "volume"))
238 options->approximation_method = BV_APPROX_VOLUME;
239 else if (!strcmp(arg, "bernoulli"))
240 options->approximation_method = BV_APPROX_BERNOULLI;
241 else
242 argp_error(state, "unknown value for --approximation-method option");
243 break;
244 case BV_OPT_SCALE:
245 options->approximation_method = BV_APPROX_SCALE;
246 while (*arg != '\0')
247 switch (getsubopt(&arg, scale_opts, &subopt)) {
248 case SCALE_FAST:
249 options->scale_flags |= BV_APPROX_SCALE_FAST;
250 break;
251 case SCALE_SLOW:
252 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
253 break;
254 case SCALE_NARROW:
255 options->scale_flags |= BV_APPROX_SCALE_NARROW;
256 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
257 break;
258 case SCALE_NARROW2:
259 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
260 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
261 break;
262 case SCALE_CHAMBER:
263 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
264 break;
265 default:
266 argp_error(state, "unknown suboption '%s'\n", subopt);
268 break;
269 case BV_OPT_VOL:
270 if (!strcmp(arg, "lift"))
271 options->volume_triangulate = BV_VOL_LIFT;
272 else if (!strcmp(arg, "vertex"))
273 options->volume_triangulate = BV_VOL_VERTEX;
274 else if (!strcmp(arg, "barycenter"))
275 options->volume_triangulate = BV_VOL_BARYCENTER;
276 break;
277 case ARGP_KEY_END:
278 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
279 options->approximation_method != BV_APPROX_NONE) {
280 fprintf(stderr,
281 "no polynomial approximation selected; reseting approximation method\n");
282 options->approximation_method = BV_APPROX_NONE;
284 break;
285 default:
286 return ARGP_ERR_UNKNOWN;
288 return 0;
291 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
293 struct barvinok_options *options = state->input;
294 char *subopt;
296 switch (key) {
297 case ARGP_KEY_INIT:
298 state->child_inputs[0] = options;
299 break;
300 case 'v':
301 options->verbose = 1;
302 break;
303 case 'V':
304 printf(barvinok_version());
305 exit(0);
306 case BV_OPT_SPECIALIZATION:
307 if (!strcmp(arg, "bf"))
308 options->incremental_specialization = BV_SPECIALIZATION_BF;
309 else if (!strcmp(arg, "df"))
310 options->incremental_specialization = BV_SPECIALIZATION_DF;
311 else if (!strcmp(arg, "random"))
312 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
313 else if (!strcmp(arg, "todd"))
314 options->incremental_specialization = BV_SPECIALIZATION_TODD;
315 break;
316 case BV_OPT_PRIMAL:
317 options->primal = 1;
318 break;
319 case BV_OPT_TABLE:
320 options->lookup_table = 1;
321 break;
322 case BV_OPT_CHAMBERS:
323 if (!strcmp(arg, "polylib"))
324 options->chambers = BV_CHAMBERS_POLYLIB;
325 if (!strcmp(arg, "topcom"))
326 options->chambers = BV_CHAMBERS_TOPCOM;
327 break;
328 case BV_OPT_GBR:
329 if (!strcmp(arg, "cdd"))
330 options->gbr_lp_solver = BV_GBR_CDD;
331 if (!strcmp(arg, "glpk"))
332 options->gbr_lp_solver = BV_GBR_GLPK;
333 if (!strcmp(arg, "pip"))
334 options->gbr_lp_solver = BV_GBR_PIP;
335 if (!strcmp(arg, "pip-dual"))
336 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
337 break;
338 case BV_OPT_LP:
339 if (!strcmp(arg, "cdd"))
340 options->lp_solver = BV_LP_CDD;
341 if (!strcmp(arg, "cddf"))
342 options->lp_solver = BV_LP_CDDF;
343 if (!strcmp(arg, "glpk"))
344 options->lp_solver = BV_LP_GLPK;
345 if (!strcmp(arg, "pip"))
346 options->lp_solver = BV_LP_PIP;
347 if (!strcmp(arg, "polylib"))
348 options->lp_solver = BV_LP_POLYLIB;
349 break;
350 case BV_OPT_MAXINDEX:
351 options->max_index = strtoul(arg, NULL, 0);
352 break;
353 case BV_OPT_SUM:
354 if (!strcmp(arg, "barvinok"))
355 options->summation = BV_SUM_BARVINOK;
356 if (!strcmp(arg, "euler"))
357 options->summation = BV_SUM_EULER;
358 if (!strcmp(arg, "bernoulli"))
359 options->summation = BV_SUM_BERNOULLI;
360 break;
361 case BV_OPT_RECURSE:
362 if (!strcmp(arg, "none"))
363 options->bernstein_recurse = 0;
364 else if (!strcmp(arg, "factors"))
365 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
366 else if (!strcmp(arg, "intervals"))
367 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
368 else if (!strcmp(arg, "full"))
369 options->bernstein_recurse =
370 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
371 break;
372 case BV_OPT_HULL:
373 if (!strcmp(arg, "gbr"))
374 options->integer_hull = BV_HULL_GBR;
375 else if (!strcmp(arg, "hilbert"))
376 options->integer_hull = BV_HULL_HILBERT;
377 break;
378 case BV_OPT_PRINT_STATS:
379 options->print_stats = 1;
380 break;
381 default:
382 return ARGP_ERR_UNKNOWN;
384 return 0;
387 static struct argp approx_argp = {
388 approx_argp_options, approx_parse_opt, 0, 0
391 static struct argp_child barvinok_children[] = {
392 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
393 { 0 }
396 struct argp barvinok_argp = {
397 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children