bernoulli.c: perform nested sums approximation depth first
[barvinok.git] / options.c
blob96e3df147f83960bc9992d5f55a0941986935fba
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 options->bernstein_optimize = BV_BERNSTEIN_NONE;
90 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
92 return options;
95 void barvinok_options_free(struct barvinok_options *options)
97 free(options->stats);
98 free(options);
101 enum {
102 SCALE_FAST,
103 SCALE_SLOW,
104 SCALE_NARROW,
105 SCALE_NARROW2,
106 SCALE_CHAMBER,
109 const char *scale_opts[] = {
110 "fast",
111 "slow",
112 "narrow",
113 "narrow2",
114 "chamber",
115 NULL
118 static struct argp_option approx_argp_options[] = {
119 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
120 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume|bernouilli", 0,
121 "method to use in polynomial approximation [default: drop]" },
122 { "scale-options", BV_OPT_SCALE,
123 "fast|slow,narrow|narrow2,chamber", 0 },
124 { "volume-triangulation", BV_OPT_VOL, "lift|vertex|barycenter", 0,
125 "type of triangulation to perform in volume computation [default: vertex]" },
126 { 0 }
129 static struct argp_option barvinok_argp_options[] = {
130 { "index", BV_OPT_MAXINDEX, "int", 0,
131 "maximal index of simple cones in decomposition" },
132 { "primal", BV_OPT_PRIMAL, 0, 0 },
133 { "table", BV_OPT_TABLE, 0, 0 },
134 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random|todd]" },
135 { "gbr", BV_OPT_GBR, "[cdd]", 0,
136 "solver to use for basis reduction" },
137 { "bernstein-recurse", BV_OPT_RECURSE, "none|factors|intervals|full", 0,
138 "[default: factors]" },
139 { "recurse", BV_OPT_RECURSE, "",
140 OPTION_ALIAS | OPTION_HIDDEN },
141 { "version", 'V', 0, 0 },
142 { 0 }
145 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
147 struct barvinok_options *options = state->input;
148 char *subopt;
150 switch (key) {
151 case BV_OPT_POLAPPROX:
152 if (!arg) {
153 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
154 if (options->approximation_method == BV_APPROX_NONE)
155 options->approximation_method = BV_APPROX_SCALE;
156 } else {
157 if (!strcmp(arg, "lower"))
158 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
159 else if (!strcmp(arg, "upper"))
160 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
161 if (options->approximation_method == BV_APPROX_NONE)
162 options->approximation_method = BV_APPROX_DROP;
164 break;
165 case BV_OPT_APPROX:
166 if (!strcmp(arg, "scale"))
167 options->approximation_method = BV_APPROX_SCALE;
168 else if (!strcmp(arg, "drop"))
169 options->approximation_method = BV_APPROX_DROP;
170 else if (!strcmp(arg, "volume"))
171 options->approximation_method = BV_APPROX_VOLUME;
172 else if (!strcmp(arg, "bernoulli"))
173 options->approximation_method = BV_APPROX_BERNOULLI;
174 else
175 argp_error(state, "unknown value for --approximation-method option");
176 break;
177 case BV_OPT_SCALE:
178 options->approximation_method = BV_APPROX_SCALE;
179 while (*arg != '\0')
180 switch (getsubopt(&arg, scale_opts, &subopt)) {
181 case SCALE_FAST:
182 options->scale_flags |= BV_APPROX_SCALE_FAST;
183 break;
184 case SCALE_SLOW:
185 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
186 break;
187 case SCALE_NARROW:
188 options->scale_flags |= BV_APPROX_SCALE_NARROW;
189 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
190 break;
191 case SCALE_NARROW2:
192 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
193 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
194 break;
195 case SCALE_CHAMBER:
196 options->scale_flags |= BV_APPROX_SCALE_CHAMBER;
197 break;
198 default:
199 argp_error(state, "unknown suboption '%s'\n", subopt);
201 break;
202 case BV_OPT_VOL:
203 if (!strcmp(arg, "lift"))
204 options->volume_triangulate = BV_VOL_LIFT;
205 else if (!strcmp(arg, "vertex"))
206 options->volume_triangulate = BV_VOL_VERTEX;
207 else if (!strcmp(arg, "barycenter"))
208 options->volume_triangulate = BV_VOL_BARYCENTER;
209 break;
210 case ARGP_KEY_END:
211 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
212 options->approximation_method != BV_APPROX_NONE) {
213 fprintf(stderr,
214 "no polynomial approximation selected; reseting approximation method\n");
215 options->approximation_method = BV_APPROX_NONE;
217 break;
218 default:
219 return ARGP_ERR_UNKNOWN;
221 return 0;
224 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
226 struct barvinok_options *options = state->input;
227 char *subopt;
229 switch (key) {
230 case ARGP_KEY_INIT:
231 state->child_inputs[0] = options;
232 break;
233 case 'V':
234 printf(barvinok_version());
235 exit(0);
236 case BV_OPT_SPECIALIZATION:
237 if (!strcmp(arg, "bf"))
238 options->incremental_specialization = BV_SPECIALIZATION_BF;
239 else if (!strcmp(arg, "df"))
240 options->incremental_specialization = BV_SPECIALIZATION_DF;
241 else if (!strcmp(arg, "random"))
242 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
243 else if (!strcmp(arg, "todd"))
244 options->incremental_specialization = BV_SPECIALIZATION_TODD;
245 break;
246 case BV_OPT_PRIMAL:
247 options->primal = 1;
248 break;
249 case BV_OPT_TABLE:
250 options->lookup_table = 1;
251 break;
252 case BV_OPT_GBR:
253 if (!strcmp(arg, "cdd"))
254 options->gbr_lp_solver = BV_GBR_CDD;
255 break;
256 case BV_OPT_MAXINDEX:
257 options->max_index = strtoul(arg, NULL, 0);
258 break;
259 case BV_OPT_RECURSE:
260 if (!strcmp(arg, "none"))
261 options->bernstein_recurse = 0;
262 else if (!strcmp(arg, "factors"))
263 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
264 else if (!strcmp(arg, "intervals"))
265 options->bernstein_recurse = BV_BERNSTEIN_INTERVALS;
266 else if (!strcmp(arg, "full"))
267 options->bernstein_recurse =
268 BV_BERNSTEIN_FACTORS | BV_BERNSTEIN_INTERVALS;
269 break;
270 default:
271 return ARGP_ERR_UNKNOWN;
273 return 0;
276 static struct argp approx_argp = {
277 approx_argp_options, approx_parse_opt, 0, 0
280 static struct argp_child barvinok_children[] = {
281 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
282 { 0 }
285 struct argp barvinok_argp = {
286 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children