add piplib module
[barvinok.git] / options.c
blobafd97158df9522afe82848c93f79720832329163
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 #ifndef HAVE_LIBGLPK
11 Vector *Polyhedron_Sample(Polyhedron *P, struct barvinok_options *options)
13 assert(0);
15 #endif
17 #define ALLOC(type) (type*)malloc(sizeof(type))
19 void barvinok_stats_clear(struct barvinok_stats *stats)
21 stats->base_cones = 0;
22 stats->volume_simplices = 0;
25 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
27 fprintf(out, "Base cones: %d\n", stats->base_cones);
28 if (stats->volume_simplices)
29 fprintf(out, "Volume simplices: %d\n", stats->volume_simplices);
32 struct barvinok_options *barvinok_options_new_with_defaults()
34 struct barvinok_options *options = ALLOC(struct barvinok_options);
35 if (!options)
36 return NULL;
38 options->stats = ALLOC(struct barvinok_stats);
39 if (!options->stats) {
40 free(options);
41 return NULL;
44 barvinok_stats_clear(options->stats);
46 options->LLL_a = 1;
47 options->LLL_b = 1;
49 options->MaxRays = MAXRAYS;
51 #ifdef USE_INCREMENTAL_BF
52 options->incremental_specialization = 2;
53 #elif defined USE_INCREMENTAL_DF
54 options->incremental_specialization = 1;
55 #else
56 options->incremental_specialization = 0;
57 #endif
58 options->max_index = 1;
59 options->primal = 0;
60 #ifdef USE_MODULO
61 options->lookup_table = 0;
62 #else
63 options->lookup_table = 1;
64 #endif
65 #ifdef HAVE_LIBGLPK
66 options->count_sample_infinite = 1;
67 #else
68 options->count_sample_infinite = 0;
69 #endif
70 options->try_Delaunay_triangulation = 0;
72 options->polynomial_approximation = BV_APPROX_SIGN_NONE;
73 options->approximation_method = BV_APPROX_NONE;
74 options->scale_flags = 0;
75 options->volume_triangulate = BV_VOL_VERTEX;
77 #ifdef HAVE_LIBGLPK
78 options->gbr_lp_solver = BV_GBR_GLPK;
79 #elif defined HAVE_LIBCDDGMP
80 options->gbr_lp_solver = BV_GBR_CDD;
81 #else
82 options->gbr_lp_solver = BV_GBR_NONE;
83 #endif
85 #ifdef HAVE_LIBGLPK
86 options->lp_solver = BV_LP_GLPK;
87 #elif defined HAVE_LIBCDDGMP
88 options->lp_solver = BV_LP_CDD;
89 #else
90 options->lp_solver = BV_LP_POLYLIB;
91 #endif
93 options->bernstein_optimize = BV_BERNSTEIN_NONE;
95 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
97 return options;
100 void barvinok_options_free(struct barvinok_options *options)
102 free(options->stats);
103 free(options);
106 enum {
107 SCALE_FAST,
108 SCALE_SLOW,
109 SCALE_NARROW,
110 SCALE_NARROW2,
111 SCALE_CHAMBER,
114 const char *scale_opts[] = {
115 "fast",
116 "slow",
117 "narrow",
118 "narrow2",
119 "chamber",
120 NULL
123 static struct argp_option approx_argp_options[] = {
124 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
125 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernouilli", 0,
126 "method to use in polynomial approximation [default: drop]" },
127 { "scale-options", BV_OPT_SCALE,
128 "fast|slow,narrow|narrow2,chamber", 0 },
129 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
130 "type of triangulation to perform in volume computation [default: vertex]" },
131 { 0 }
134 static struct argp_option barvinok_argp_options[] = {
135 { "index", BV_OPT_MAXINDEX, "int", 0,
136 "maximal index of simple cones in decomposition" },
137 { "primal", BV_OPT_PRIMAL, 0, 0 },
138 { "table", BV_OPT_TABLE, 0, 0 },
139 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
140 #if defined(HAVE_LIBGLPK) || defined(HAVE_LIBCDDGMP)
141 { "gbr", BV_OPT_GBR,
142 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
143 "cdd|glpk",
144 #elif defined(HAVE_LIBGLPK)
145 "glpk",
146 #elif defined(HAVE_LIBCDDGMP)
147 "cdd",
148 #endif
149 0, "lp solver to use for basis reduction "
150 #ifdef HAVE_LIBGLPK
151 "[default: glpk]"
152 #elif defined HAVE_LIBCDDGMP
153 "[default: cdd]"
154 #endif
156 #endif
157 { "lp", BV_OPT_LP,
158 #if defined(HAVE_LIBGLPK) && defined(HAVE_LIBCDDGMP)
159 "cdd|cddf|glpk|polylib",
160 #elif defined(HAVE_LIBGLPK)
161 "glpk|polylib",
162 #elif defined(HAVE_LIBCDDGMP)
163 "cdd|cddf|polylib",
164 #else
165 "polylib",
166 #endif
167 0, "lp solver to use "
168 #if defined(HAVE_LIBGLPK)
169 "[default: glpk]",
170 #elif defined(HAVE_LIBCDDGMP)
171 "[default: cdd]",
172 #else
173 "[default: polylib]",
174 #endif
176 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
177 "[default: factors]" },
178 { "recurse", BV_OPT_RECURSE, "",
179 OPTION_ALIAS | OPTION_HIDDEN },
180 { "version", 'V', 0, 0 },
181 { 0 }
184 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
186 struct barvinok_options *options = state->input;
187 char *subopt;
189 switch (key) {
190 case BV_OPT_POLAPPROX:
191 if (!arg) {
192 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
193 if (options->approximation_method == BV_APPROX_NONE)
194 options->approximation_method = BV_APPROX_SCALE;
195 } else {
196 if (!strcmp(arg, "lower"))
197 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
198 else if (!strcmp(arg, "upper"))
199 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
200 if (options->approximation_method == BV_APPROX_NONE)
201 options->approximation_method = BV_APPROX_DROP;
203 break;
204 case BV_OPT_APPROX:
205 if (!strcmp(arg, "scale"))
206 options->approximation_method = BV_APPROX_SCALE;
207 else if (!strcmp(arg, "drop"))
208 options->approximation_method = BV_APPROX_DROP;
209 else if (!strcmp(arg, "volume"))
210 options->approximation_method = BV_APPROX_VOLUME;
211 else if (!strcmp(arg, "bernoulli"))
212 options->approximation_method = BV_APPROX_BERNOULLI;
213 else
214 argp_error(state, "unknown value for --approximation-method option");
215 break;
216 case BV_OPT_SCALE:
217 options->approximation_method = BV_APPROX_SCALE;
218 while (*arg != '\0')
219 switch (getsubopt(&arg, scale_opts, &subopt)) {
220 case SCALE_FAST:
221 options->scale_flags |= BV_APPROX_SCALE_FAST;
222 break;
223 case SCALE_SLOW:
224 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
225 break;
226 case SCALE_NARROW:
227 options->scale_flags |= BV_APPROX_SCALE_NARROW;
228 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
229 break;
230 case SCALE_NARROW2:
231 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
232 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
233 break;
234 case SCALE_CHAMBER:
235 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
236 break;
237 default:
238 argp_error(state, "unknown suboption '%s'\n", subopt);
240 break;
241 case BV_OPT_VOL:
242 if (!strcmp(arg, "lift"))
243 options->volume_triangulate = BV_VOL_LIFT;
244 else if (!strcmp(arg, "vertex"))
245 options->volume_triangulate = BV_VOL_VERTEX;
246 else if (!strcmp(arg, "barycenter"))
247 options->volume_triangulate = BV_VOL_BARYCENTER;
248 break;
249 case ARGP_KEY_END:
250 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
251 options->approximation_method != BV_APPROX_NONE) {
252 fprintf(stderr,
253 "no polynomial approximation selected; reseting approximation method\n");
254 options->approximation_method = BV_APPROX_NONE;
256 break;
257 default:
258 return ARGP_ERR_UNKNOWN;
260 return 0;
263 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
265 struct barvinok_options *options = state->input;
266 char *subopt;
268 switch (key) {
269 case ARGP_KEY_INIT:
270 state->child_inputs[0] = options;
271 break;
272 case 'V':
273 printf(barvinok_version());
274 exit(0);
275 case BV_OPT_SPECIALIZATION:
276 if (!strcmp(arg, "bf"))
277 options->incremental_specialization = BV_SPECIALIZATION_BF;
278 else if (!strcmp(arg, "df"))
279 options->incremental_specialization = BV_SPECIALIZATION_DF;
280 else if (!strcmp(arg, "random"))
281 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
282 else if (!strcmp(arg, "todd"))
283 options->incremental_specialization = BV_SPECIALIZATION_TODD;
284 break;
285 case BV_OPT_PRIMAL:
286 options->primal = 1;
287 break;
288 case BV_OPT_TABLE:
289 options->lookup_table = 1;
290 break;
291 case BV_OPT_GBR:
292 if (!strcmp(arg, "cdd"))
293 options->gbr_lp_solver = BV_GBR_CDD;
294 if (!strcmp(arg, "glpk"))
295 options->gbr_lp_solver = BV_GBR_GLPK;
296 break;
297 case BV_OPT_LP:
298 if (!strcmp(arg, "cdd"))
299 options->lp_solver = BV_LP_CDD;
300 if (!strcmp(arg, "cddf"))
301 options->lp_solver = BV_LP_CDDF;
302 if (!strcmp(arg, "glpk"))
303 options->lp_solver = BV_LP_GLPK;
304 if (!strcmp(arg, "polylib"))
305 options->lp_solver = BV_LP_POLYLIB;
306 break;
307 case BV_OPT_MAXINDEX:
308 options->max_index = strtoul(arg, NULL, 0);
309 break;
310 case BV_OPT_RECURSE:
311 if (!strcmp(arg, "none"))
312 options->bernstein_recurse = 0;
313 else if (!strcmp(arg, "factors"))
314 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
315 else if (!strcmp(arg, "intervals"))
316 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
317 else if (!strcmp(arg, "full"))
318 options->bernstein_recurse =
319 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
320 break;
321 default:
322 return ARGP_ERR_UNKNOWN;
324 return 0;
327 static struct argp approx_argp = {
328 approx_argp_options, approx_parse_opt, 0, 0
331 static struct argp_child barvinok_children[] = {
332 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
333 { 0 }
336 struct argp barvinok_argp = {
337 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children