compute_width_directions: only keep list of distinct directions
[barvinok.git] / options.c
blob1dd355bb5af6bc0487553a6dc8b8335ea53cfa8f
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->verbose = 0;
100 options->print_stats = 0;
102 options->gbr_only_first = 0;
104 return options;
107 void barvinok_options_free(struct barvinok_options *options)
109 free(options->stats);
110 free(options);
113 enum {
114 SCALE_FAST,
115 SCALE_SLOW,
116 SCALE_NARROW,
117 SCALE_NARROW2,
118 SCALE_CHAMBER,
121 const char *scale_opts[] = {
122 "fast",
123 "slow",
124 "narrow",
125 "narrow2",
126 "chamber",
127 NULL
130 static struct argp_option approx_argp_options[] = {
131 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
132 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
133 "method to use in polynomial approximation [default: drop]" },
134 { "scale-options", BV_OPT_SCALE,
135 "fast|slow,narrow|narrow2,chamber", 0 },
136 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
137 "type of triangulation to perform in volume computation [default: vertex]" },
138 { 0 }
141 static struct argp_option barvinok_argp_options[] = {
142 { "index", BV_OPT_MAXINDEX, "int", 0,
143 "maximal index of simple cones in decomposition" },
144 { "primal", BV_OPT_PRIMAL, 0, 0 },
145 { "table", BV_OPT_TABLE, 0, 0 },
146 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
147 #ifdef POINTS2TRIANGS_PATH
148 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
149 "tool to use for chamber decomposition [default: polylib]" },
150 #endif
151 { "gbr", BV_OPT_GBR,
152 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
153 "cdd|glpk|pip|pip-dual",
154 #elif defined(HAVE_LIBGLPK)
155 "glpk|pip|pip-dual",
156 #elif defined(HAVE_LIBCDDGMP)
157 "cdd|pip|pip-dual",
158 #else
159 "pip|pip-dual",
160 #endif
161 0, "lp solver to use for basis reduction "
162 #ifdef HAVE_LIBGLPK
163 "[default: glpk]"
164 #elif defined HAVE_LIBCDDGMP
165 "[default: cdd]"
166 #else
167 "[default: pip]"
168 #endif
170 { "lp", BV_OPT_LP,
171 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
172 "cdd|cddf|glpk|pip|polylib",
173 #elif defined(HAVE_LIBGLPK)
174 "glpk|pip|polylib",
175 #elif defined(HAVE_LIBCDDGMP)
176 "cdd|cddf|pip|polylib",
177 #else
178 "pip|polylib",
179 #endif
180 0, "lp solver to use "
181 #if defined(HAVE_LIBGLPK)
182 "[default: glpk]",
183 #elif defined(HAVE_LIBCDDGMP)
184 "[default: cdd]",
185 #else
186 "[default: pip]",
187 #endif
189 { "summation", BV_OPT_SUM, "barvinok|bernoulli|euler", 0,
190 "[default: barvinok]" },
191 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
192 "[default: factors]" },
193 { "recurse", BV_OPT_RECURSE, "",
194 OPTION_ALIAS | OPTION_HIDDEN },
195 { "version", 'V', 0, 0 },
196 { "verbose", 'v' },
197 { "print-stats", BV_OPT_PRINT_STATS, 0, 0 },
198 { 0 }
201 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
203 struct barvinok_options *options = state->input;
204 char *subopt;
206 switch (key) {
207 case BV_OPT_POLAPPROX:
208 if (!arg) {
209 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
210 if (options->approximation_method == BV_APPROX_NONE)
211 options->approximation_method = BV_APPROX_SCALE;
212 } else {
213 if (!strcmp(arg, "lower"))
214 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
215 else if (!strcmp(arg, "upper"))
216 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
217 if (options->approximation_method == BV_APPROX_NONE)
218 options->approximation_method = BV_APPROX_DROP;
220 break;
221 case BV_OPT_APPROX:
222 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
223 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
224 if (!strcmp(arg, "scale"))
225 options->approximation_method = BV_APPROX_SCALE;
226 else if (!strcmp(arg, "drop"))
227 options->approximation_method = BV_APPROX_DROP;
228 else if (!strcmp(arg, "volume"))
229 options->approximation_method = BV_APPROX_VOLUME;
230 else if (!strcmp(arg, "bernoulli"))
231 options->approximation_method = BV_APPROX_BERNOULLI;
232 else
233 argp_error(state, "unknown value for --approximation-method option");
234 break;
235 case BV_OPT_SCALE:
236 options->approximation_method = BV_APPROX_SCALE;
237 while (*arg != '\0')
238 switch (getsubopt(&arg, scale_opts, &subopt)) {
239 case SCALE_FAST:
240 options->scale_flags |= BV_APPROX_SCALE_FAST;
241 break;
242 case SCALE_SLOW:
243 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
244 break;
245 case SCALE_NARROW:
246 options->scale_flags |= BV_APPROX_SCALE_NARROW;
247 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
248 break;
249 case SCALE_NARROW2:
250 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
251 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
252 break;
253 case SCALE_CHAMBER:
254 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
255 break;
256 default:
257 argp_error(state, "unknown suboption '%s'\n", subopt);
259 break;
260 case BV_OPT_VOL:
261 if (!strcmp(arg, "lift"))
262 options->volume_triangulate = BV_VOL_LIFT;
263 else if (!strcmp(arg, "vertex"))
264 options->volume_triangulate = BV_VOL_VERTEX;
265 else if (!strcmp(arg, "barycenter"))
266 options->volume_triangulate = BV_VOL_BARYCENTER;
267 break;
268 case ARGP_KEY_END:
269 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
270 options->approximation_method != BV_APPROX_NONE) {
271 fprintf(stderr,
272 "no polynomial approximation selected; reseting approximation method\n");
273 options->approximation_method = BV_APPROX_NONE;
275 break;
276 default:
277 return ARGP_ERR_UNKNOWN;
279 return 0;
282 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
284 struct barvinok_options *options = state->input;
285 char *subopt;
287 switch (key) {
288 case ARGP_KEY_INIT:
289 state->child_inputs[0] = options;
290 break;
291 case 'v':
292 options->verbose = 1;
293 break;
294 case 'V':
295 printf(barvinok_version());
296 exit(0);
297 case BV_OPT_SPECIALIZATION:
298 if (!strcmp(arg, "bf"))
299 options->incremental_specialization = BV_SPECIALIZATION_BF;
300 else if (!strcmp(arg, "df"))
301 options->incremental_specialization = BV_SPECIALIZATION_DF;
302 else if (!strcmp(arg, "random"))
303 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
304 else if (!strcmp(arg, "todd"))
305 options->incremental_specialization = BV_SPECIALIZATION_TODD;
306 break;
307 case BV_OPT_PRIMAL:
308 options->primal = 1;
309 break;
310 case BV_OPT_TABLE:
311 options->lookup_table = 1;
312 break;
313 case BV_OPT_CHAMBERS:
314 if (!strcmp(arg, "polylib"))
315 options->chambers = BV_CHAMBERS_POLYLIB;
316 if (!strcmp(arg, "topcom"))
317 options->chambers = BV_CHAMBERS_TOPCOM;
318 break;
319 case BV_OPT_GBR:
320 if (!strcmp(arg, "cdd"))
321 options->gbr_lp_solver = BV_GBR_CDD;
322 if (!strcmp(arg, "glpk"))
323 options->gbr_lp_solver = BV_GBR_GLPK;
324 if (!strcmp(arg, "pip"))
325 options->gbr_lp_solver = BV_GBR_PIP;
326 if (!strcmp(arg, "pip-dual"))
327 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
328 break;
329 case BV_OPT_LP:
330 if (!strcmp(arg, "cdd"))
331 options->lp_solver = BV_LP_CDD;
332 if (!strcmp(arg, "cddf"))
333 options->lp_solver = BV_LP_CDDF;
334 if (!strcmp(arg, "glpk"))
335 options->lp_solver = BV_LP_GLPK;
336 if (!strcmp(arg, "pip"))
337 options->lp_solver = BV_LP_PIP;
338 if (!strcmp(arg, "polylib"))
339 options->lp_solver = BV_LP_POLYLIB;
340 break;
341 case BV_OPT_MAXINDEX:
342 options->max_index = strtoul(arg, NULL, 0);
343 break;
344 case BV_OPT_SUM:
345 if (!strcmp(arg, "barvinok"))
346 options->summation = BV_SUM_BARVINOK;
347 if (!strcmp(arg, "euler"))
348 options->summation = BV_SUM_EULER;
349 if (!strcmp(arg, "bernoulli"))
350 options->summation = BV_SUM_BERNOULLI;
351 break;
352 case BV_OPT_RECURSE:
353 if (!strcmp(arg, "none"))
354 options->bernstein_recurse = 0;
355 else if (!strcmp(arg, "factors"))
356 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
357 else if (!strcmp(arg, "intervals"))
358 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
359 else if (!strcmp(arg, "full"))
360 options->bernstein_recurse =
361 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
362 break;
363 case BV_OPT_PRINT_STATS:
364 options->print_stats = 1;
365 break;
366 default:
367 return ARGP_ERR_UNKNOWN;
369 return 0;
372 static struct argp approx_argp = {
373 approx_argp_options, approx_parse_opt, 0, 0
376 static struct argp_child barvinok_children[] = {
377 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
378 { 0 }
381 struct argp barvinok_argp = {
382 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children