volume.c: fix typo in comment
[barvinok.git] / options.c
blob7f50ba4d441c6155d46b44b43cf9950de5be59d3
1 #include <unistd.h>
2 #include <barvinok/options.h>
3 #include <barvinok/util.h>
4 #include "argp.h"
5 #include "config.h"
7 #ifdef HAVE_GROWING_CHERNIKOVA
8 #define MAXRAYS (POL_NO_DUAL | POL_INTEGER)
9 #else
10 #define MAXRAYS 600
11 #endif
13 #ifndef HAVE_LIBGLPK
14 Vector *Polyhedron_Sample(Polyhedron *P, struct barvinok_options *options)
16 assert(0);
18 #endif
20 #define ALLOC(type) (type*)malloc(sizeof(type))
22 void barvinok_stats_clear(struct barvinok_stats *stats)
24 stats->base_cones = 0;
25 stats->volume_simplices = 0;
28 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
30 fprintf(out, "Base cones: %d\n", stats->base_cones);
31 if (stats->volume_simplices)
32 fprintf(out, "Volume simplices: %d\n", stats->volume_simplices);
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 #ifdef HAVE_LIBGLPK
69 options->count_sample_infinite = 1;
70 #else
71 options->count_sample_infinite = 0;
72 #endif
73 options->try_Delaunay_triangulation = 0;
75 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
76 options->approximation_method = BV_APPROX_NONE;
77 options->scale_flags = 0;
78 options->volume_triangulate = BV_VOL_VERTEX;
80 #ifdef HAVE_LIBGLPK
81 options->gbr_lp_solver = BV_GBR_GLPK;
82 #elif defined HAVE_LIBCDDGMP
83 options->gbr_lp_solver = BV_GBR_CDD;
84 #else
85 options->gbr_lp_solver = BV_GBR_NONE;
86 #endif
88 #ifdef HAVE_LIBGLPK
89 options->lp_solver = BV_LP_GLPK;
90 #elif defined HAVE_LIBCDDGMP
91 options->lp_solver = BV_LP_CDD;
92 #else
93 options->lp_solver = BV_LP_POLYLIB;
94 #endif
96 options->bernstein_optimize = BV_BERNSTEIN_NONE;
98 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
100 return options;
103 void barvinok_options_free(struct barvinok_options *options)
105 free(options->stats);
106 free(options);
109 enum {
110 SCALE_FAST,
111 SCALE_SLOW,
112 SCALE_NARROW,
113 SCALE_NARROW2,
114 SCALE_CHAMBER,
117 const char *scale_opts[] = {
118 "fast",
119 "slow",
120 "narrow",
121 "narrow2",
122 "chamber",
123 NULL
126 static struct argp_option approx_argp_options[] = {
127 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
128 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernouilli", 0,
129 "method to use in polynomial approximation [default: drop]" },
130 { "scale-options", BV_OPT_SCALE,
131 "fast|slow,narrow|narrow2,chamber", 0 },
132 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
133 "type of triangulation to perform in volume computation [default: vertex]" },
134 { 0 }
137 static struct argp_option barvinok_argp_options[] = {
138 { "index", BV_OPT_MAXINDEX, "int", 0,
139 "maximal index of simple cones in decomposition" },
140 { "primal", BV_OPT_PRIMAL, 0, 0 },
141 { "table", BV_OPT_TABLE, 0, 0 },
142 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
143 #if defined(HAVE_LIBGLPK) || defined(HAVE_LIBCDDGMP)
144 { "gbr", BV_OPT_GBR,
145 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
146 "cdd|glpk",
147 #elif defined(HAVE_LIBGLPK)
148 "glpk",
149 #elif defined(HAVE_LIBCDDGMP)
150 "cdd",
151 #endif
152 0, "lp solver to use for basis reduction "
153 #ifdef HAVE_LIBGLPK
154 "[default: glpk]"
155 #elif defined HAVE_LIBCDDGMP
156 "[default: cdd]"
157 #endif
159 #endif
160 { "lp", BV_OPT_LP,
161 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
162 "cdd|cddf|glpk|polylib",
163 #elif defined(HAVE_LIBGLPK)
164 "glpk|polylib",
165 #elif defined(HAVE_LIBCDDGMP)
166 "cdd|cddf|polylib",
167 #else
168 "polylib",
169 #endif
170 0, "lp solver to use "
171 #if defined(HAVE_LIBGLPK)
172 "[default: glpk]",
173 #elif defined(HAVE_LIBCDDGMP)
174 "[default: cdd]",
175 #else
176 "[default: polylib]",
177 #endif
179 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
180 "[default: factors]" },
181 { "recurse", BV_OPT_RECURSE, "",
182 OPTION_ALIAS | OPTION_HIDDEN },
183 { "version", 'V', 0, 0 },
184 { 0 }
187 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
189 struct barvinok_options *options = state->input;
190 char *subopt;
192 switch (key) {
193 case BV_OPT_POLAPPROX:
194 if (!arg) {
195 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
196 if (options->approximation_method == BV_APPROX_NONE)
197 options->approximation_method = BV_APPROX_SCALE;
198 } else {
199 if (!strcmp(arg, "lower"))
200 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
201 else if (!strcmp(arg, "upper"))
202 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
203 if (options->approximation_method == BV_APPROX_NONE)
204 options->approximation_method = BV_APPROX_DROP;
206 break;
207 case BV_OPT_APPROX:
208 if (!strcmp(arg, "scale"))
209 options->approximation_method = BV_APPROX_SCALE;
210 else if (!strcmp(arg, "drop"))
211 options->approximation_method = BV_APPROX_DROP;
212 else if (!strcmp(arg, "volume"))
213 options->approximation_method = BV_APPROX_VOLUME;
214 else if (!strcmp(arg, "bernoulli"))
215 options->approximation_method = BV_APPROX_BERNOULLI;
216 else
217 argp_error(state, "unknown value for --approximation-method option");
218 break;
219 case BV_OPT_SCALE:
220 options->approximation_method = BV_APPROX_SCALE;
221 while (*arg != '\0')
222 switch (getsubopt(&arg, scale_opts, &subopt)) {
223 case SCALE_FAST:
224 options->scale_flags |= BV_APPROX_SCALE_FAST;
225 break;
226 case SCALE_SLOW:
227 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
228 break;
229 case SCALE_NARROW:
230 options->scale_flags |= BV_APPROX_SCALE_NARROW;
231 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
232 break;
233 case SCALE_NARROW2:
234 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
235 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
236 break;
237 case SCALE_CHAMBER:
238 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
239 break;
240 default:
241 argp_error(state, "unknown suboption '%s'\n", subopt);
243 break;
244 case BV_OPT_VOL:
245 if (!strcmp(arg, "lift"))
246 options->volume_triangulate = BV_VOL_LIFT;
247 else if (!strcmp(arg, "vertex"))
248 options->volume_triangulate = BV_VOL_VERTEX;
249 else if (!strcmp(arg, "barycenter"))
250 options->volume_triangulate = BV_VOL_BARYCENTER;
251 break;
252 case ARGP_KEY_END:
253 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
254 options->approximation_method != BV_APPROX_NONE) {
255 fprintf(stderr,
256 "no polynomial approximation selected; reseting approximation method\n");
257 options->approximation_method = BV_APPROX_NONE;
259 break;
260 default:
261 return ARGP_ERR_UNKNOWN;
263 return 0;
266 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
268 struct barvinok_options *options = state->input;
269 char *subopt;
271 switch (key) {
272 case ARGP_KEY_INIT:
273 state->child_inputs[0] = options;
274 break;
275 case 'V':
276 printf(barvinok_version());
277 exit(0);
278 case BV_OPT_SPECIALIZATION:
279 if (!strcmp(arg, "bf"))
280 options->incremental_specialization = BV_SPECIALIZATION_BF;
281 else if (!strcmp(arg, "df"))
282 options->incremental_specialization = BV_SPECIALIZATION_DF;
283 else if (!strcmp(arg, "random"))
284 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
285 else if (!strcmp(arg, "todd"))
286 options->incremental_specialization = BV_SPECIALIZATION_TODD;
287 break;
288 case BV_OPT_PRIMAL:
289 options->primal = 1;
290 break;
291 case BV_OPT_TABLE:
292 options->lookup_table = 1;
293 break;
294 case BV_OPT_GBR:
295 if (!strcmp(arg, "cdd"))
296 options->gbr_lp_solver = BV_GBR_CDD;
297 if (!strcmp(arg, "glpk"))
298 options->gbr_lp_solver = BV_GBR_GLPK;
299 break;
300 case BV_OPT_LP:
301 if (!strcmp(arg, "cdd"))
302 options->lp_solver = BV_LP_CDD;
303 if (!strcmp(arg, "cddf"))
304 options->lp_solver = BV_LP_CDDF;
305 if (!strcmp(arg, "glpk"))
306 options->lp_solver = BV_LP_GLPK;
307 if (!strcmp(arg, "polylib"))
308 options->lp_solver = BV_LP_POLYLIB;
309 break;
310 case BV_OPT_MAXINDEX:
311 options->max_index = strtoul(arg, NULL, 0);
312 break;
313 case BV_OPT_RECURSE:
314 if (!strcmp(arg, "none"))
315 options->bernstein_recurse = 0;
316 else if (!strcmp(arg, "factors"))
317 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
318 else if (!strcmp(arg, "intervals"))
319 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
320 else if (!strcmp(arg, "full"))
321 options->bernstein_recurse =
322 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
323 break;
324 default:
325 return ARGP_ERR_UNKNOWN;
327 return 0;
330 static struct argp approx_argp = {
331 approx_argp_options, approx_parse_opt, 0, 0
334 static struct argp_child barvinok_children[] = {
335 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
336 { 0 }
339 struct argp barvinok_argp = {
340 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children