bernstein: add piecewise_lst::is_equal
[barvinok.git] / options.c
blob34da260899d56c2a2688806237ed3b30fbd1f355
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);
31 struct barvinok_options *barvinok_options_new_with_defaults()
33 struct barvinok_options *options = ALLOC(struct barvinok_options);
34 if (!options)
35 return NULL;
37 options->stats = ALLOC(struct barvinok_stats);
38 if (!options->stats) {
39 free(options);
40 return NULL;
43 barvinok_stats_clear(options->stats);
45 options->LLL_a = 1;
46 options->LLL_b = 1;
48 options->MaxRays = MAXRAYS;
50 #ifdef USE_INCREMENTAL_BF
51 options->incremental_specialization = 2;
52 #elif defined USE_INCREMENTAL_DF
53 options->incremental_specialization = 1;
54 #else
55 options->incremental_specialization = 0;
56 #endif
57 options->max_index = 1;
58 options->primal = 0;
59 #ifdef USE_MODULO
60 options->lookup_table = 0;
61 #else
62 options->lookup_table = 1;
63 #endif
64 options->count_sample_infinite = 1;
65 options->try_Delaunay_triangulation = 0;
67 options->chambers = BV_CHAMBERS_POLYLIB;
69 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
70 options->approximation_method = BV_APPROX_NONE;
71 options->scale_flags = 0;
72 options->volume_triangulate = BV_VOL_VERTEX;
74 #ifdef HAVE_LIBGLPK
75 options->gbr_lp_solver = BV_GBR_GLPK;
76 #elif defined HAVE_LIBCDDGMP
77 options->gbr_lp_solver = BV_GBR_CDD;
78 #else
79 options->gbr_lp_solver = BV_GBR_PIP;
80 #endif
82 #ifdef HAVE_LIBGLPK
83 options->lp_solver = BV_LP_GLPK;
84 #elif defined HAVE_LIBCDDGMP
85 options->lp_solver = BV_LP_CDD;
86 #else
87 options->lp_solver = BV_LP_PIP;
88 #endif
90 options->summation = BV_SUM_BARVINOK;
92 options->bernstein_optimize = BV_BERNSTEIN_NONE;
94 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
96 options->verbose = 0;
98 return options;
101 void barvinok_options_free(struct barvinok_options *options)
103 free(options->stats);
104 free(options);
107 enum {
108 SCALE_FAST,
109 SCALE_SLOW,
110 SCALE_NARROW,
111 SCALE_NARROW2,
112 SCALE_CHAMBER,
115 const char *scale_opts[] = {
116 "fast",
117 "slow",
118 "narrow",
119 "narrow2",
120 "chamber",
121 NULL
124 static struct argp_option approx_argp_options[] = {
125 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
126 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernoulli", 0,
127 "method to use in polynomial approximation [default: drop]" },
128 { "scale-options", BV_OPT_SCALE,
129 "fast|slow,narrow|narrow2,chamber", 0 },
130 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
131 "type of triangulation to perform in volume computation [default: vertex]" },
132 { 0 }
135 static struct argp_option barvinok_argp_options[] = {
136 { "index", BV_OPT_MAXINDEX, "int", 0,
137 "maximal index of simple cones in decomposition" },
138 { "primal", BV_OPT_PRIMAL, 0, 0 },
139 { "table", BV_OPT_TABLE, 0, 0 },
140 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
141 #ifdef POINTS2TRIANGS_PATH
142 { "chamber-decomposition", BV_OPT_CHAMBERS, "polylib|topcom", 0,
143 "tool to use for chamber decomposition [default: polylib]" },
144 #endif
145 { "gbr", BV_OPT_GBR,
146 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
147 "cdd|glpk|pip|pip-dual",
148 #elif defined(HAVE_LIBGLPK)
149 "glpk|pip|pip-dual",
150 #elif defined(HAVE_LIBCDDGMP)
151 "cdd|pip|pip-dual",
152 #else
153 "pip|pip-dual",
154 #endif
155 0, "lp solver to use for basis reduction "
156 #ifdef HAVE_LIBGLPK
157 "[default: glpk]"
158 #elif defined HAVE_LIBCDDGMP
159 "[default: cdd]"
160 #else
161 "[default: pip]"
162 #endif
164 { "lp", BV_OPT_LP,
165 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
166 "cdd|cddf|glpk|pip|polylib",
167 #elif defined(HAVE_LIBGLPK)
168 "glpk|pip|polylib",
169 #elif defined(HAVE_LIBCDDGMP)
170 "cdd|cddf|pip|polylib",
171 #else
172 "pip|polylib",
173 #endif
174 0, "lp solver to use "
175 #if defined(HAVE_LIBGLPK)
176 "[default: glpk]",
177 #elif defined(HAVE_LIBCDDGMP)
178 "[default: cdd]",
179 #else
180 "[default: pip]",
181 #endif
183 { "summation", BV_OPT_SUM, "barvinok|bernoulli|euler", 0,
184 "[default: barvinok]" },
185 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
186 "[default: factors]" },
187 { "recurse", BV_OPT_RECURSE, "",
188 OPTION_ALIAS | OPTION_HIDDEN },
189 { "version", 'V', 0, 0 },
190 { "verbose", 'v' },
191 { 0 }
194 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
196 struct barvinok_options *options = state->input;
197 char *subopt;
199 switch (key) {
200 case BV_OPT_POLAPPROX:
201 if (!arg) {
202 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
203 if (options->approximation_method == BV_APPROX_NONE)
204 options->approximation_method = BV_APPROX_SCALE;
205 } else {
206 if (!strcmp(arg, "lower"))
207 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
208 else if (!strcmp(arg, "upper"))
209 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
210 if (options->approximation_method == BV_APPROX_NONE)
211 options->approximation_method = BV_APPROX_DROP;
213 break;
214 case BV_OPT_APPROX:
215 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE)
216 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
217 if (!strcmp(arg, "scale"))
218 options->approximation_method = BV_APPROX_SCALE;
219 else if (!strcmp(arg, "drop"))
220 options->approximation_method = BV_APPROX_DROP;
221 else if (!strcmp(arg, "volume"))
222 options->approximation_method = BV_APPROX_VOLUME;
223 else if (!strcmp(arg, "bernoulli"))
224 options->approximation_method = BV_APPROX_BERNOULLI;
225 else
226 argp_error(state, "unknown value for --approximation-method option");
227 break;
228 case BV_OPT_SCALE:
229 options->approximation_method = BV_APPROX_SCALE;
230 while (*arg != '\0')
231 switch (getsubopt(&arg, scale_opts, &subopt)) {
232 case SCALE_FAST:
233 options->scale_flags |= BV_APPROX_SCALE_FAST;
234 break;
235 case SCALE_SLOW:
236 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
237 break;
238 case SCALE_NARROW:
239 options->scale_flags |= BV_APPROX_SCALE_NARROW;
240 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
241 break;
242 case SCALE_NARROW2:
243 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
244 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
245 break;
246 case SCALE_CHAMBER:
247 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
248 break;
249 default:
250 argp_error(state, "unknown suboption '%s'\n", subopt);
252 break;
253 case BV_OPT_VOL:
254 if (!strcmp(arg, "lift"))
255 options->volume_triangulate = BV_VOL_LIFT;
256 else if (!strcmp(arg, "vertex"))
257 options->volume_triangulate = BV_VOL_VERTEX;
258 else if (!strcmp(arg, "barycenter"))
259 options->volume_triangulate = BV_VOL_BARYCENTER;
260 break;
261 case ARGP_KEY_END:
262 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
263 options->approximation_method != BV_APPROX_NONE) {
264 fprintf(stderr,
265 "no polynomial approximation selected; reseting approximation method\n");
266 options->approximation_method = BV_APPROX_NONE;
268 break;
269 default:
270 return ARGP_ERR_UNKNOWN;
272 return 0;
275 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
277 struct barvinok_options *options = state->input;
278 char *subopt;
280 switch (key) {
281 case ARGP_KEY_INIT:
282 state->child_inputs[0] = options;
283 break;
284 case 'v':
285 options->verbose = 1;
286 break;
287 case 'V':
288 printf(barvinok_version());
289 exit(0);
290 case BV_OPT_SPECIALIZATION:
291 if (!strcmp(arg, "bf"))
292 options->incremental_specialization = BV_SPECIALIZATION_BF;
293 else if (!strcmp(arg, "df"))
294 options->incremental_specialization = BV_SPECIALIZATION_DF;
295 else if (!strcmp(arg, "random"))
296 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
297 else if (!strcmp(arg, "todd"))
298 options->incremental_specialization = BV_SPECIALIZATION_TODD;
299 break;
300 case BV_OPT_PRIMAL:
301 options->primal = 1;
302 break;
303 case BV_OPT_TABLE:
304 options->lookup_table = 1;
305 break;
306 case BV_OPT_CHAMBERS:
307 if (!strcmp(arg, "polylib"))
308 options->chambers = BV_CHAMBERS_POLYLIB;
309 if (!strcmp(arg, "topcom"))
310 options->chambers = BV_CHAMBERS_TOPCOM;
311 break;
312 case BV_OPT_GBR:
313 if (!strcmp(arg, "cdd"))
314 options->gbr_lp_solver = BV_GBR_CDD;
315 if (!strcmp(arg, "glpk"))
316 options->gbr_lp_solver = BV_GBR_GLPK;
317 if (!strcmp(arg, "pip"))
318 options->gbr_lp_solver = BV_GBR_PIP;
319 if (!strcmp(arg, "pip-dual"))
320 options->gbr_lp_solver = BV_GBR_PIP_DUAL;
321 break;
322 case BV_OPT_LP:
323 if (!strcmp(arg, "cdd"))
324 options->lp_solver = BV_LP_CDD;
325 if (!strcmp(arg, "cddf"))
326 options->lp_solver = BV_LP_CDDF;
327 if (!strcmp(arg, "glpk"))
328 options->lp_solver = BV_LP_GLPK;
329 if (!strcmp(arg, "pip"))
330 options->lp_solver = BV_LP_PIP;
331 if (!strcmp(arg, "polylib"))
332 options->lp_solver = BV_LP_POLYLIB;
333 break;
334 case BV_OPT_MAXINDEX:
335 options->max_index = strtoul(arg, NULL, 0);
336 break;
337 case BV_OPT_SUM:
338 if (!strcmp(arg, "barvinok"))
339 options->summation = BV_SUM_BARVINOK;
340 if (!strcmp(arg, "euler"))
341 options->summation = BV_SUM_EULER;
342 if (!strcmp(arg, "bernoulli"))
343 options->summation = BV_SUM_BERNOULLI;
344 break;
345 case BV_OPT_RECURSE:
346 if (!strcmp(arg, "none"))
347 options->bernstein_recurse = 0;
348 else if (!strcmp(arg, "factors"))
349 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
350 else if (!strcmp(arg, "intervals"))
351 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
352 else if (!strcmp(arg, "full"))
353 options->bernstein_recurse =
354 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
355 break;
356 default:
357 return ARGP_ERR_UNKNOWN;
359 return 0;
362 static struct argp approx_argp = {
363 approx_argp_options, approx_parse_opt, 0, 0
366 static struct argp_child barvinok_children[] = {
367 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
368 { 0 }
371 struct argp barvinok_argp = {
372 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children