options.c: separate polynomial approximation related options
[barvinok.git] / options.c
blob1fc7552ed294daae555c3ffd694ec2b36edbb429
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;
27 void barvinok_stats_print(struct barvinok_stats *stats, FILE *out)
29 fprintf(out, "Base cones: %d\n", stats->base_cones);
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;
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_NONE;
82 #endif
84 options->bernstein_optimize = BV_BERNSTEIN_NONE;
86 options->bernstein_recurse = BV_BERNSTEIN_FACTORS;
88 return options;
91 void barvinok_options_free(struct barvinok_options *options)
93 free(options->stats);
94 free(options);
97 enum {
98 SCALE_FAST,
99 SCALE_SLOW,
100 SCALE_NARROW,
101 SCALE_NARROW2
104 const char *scale_opts[] = {
105 "fast",
106 "slow",
107 "narrow",
108 "narrow2",
109 NULL
112 static struct argp_option approx_argp_options[] = {
113 { "polynomial-approximation", BV_OPT_POLAPPROX, "lower|upper", 1 },
114 { "approximation-method", BV_OPT_APPROX, "scale|drop|volume", 0,
115 "method to use in polynomial approximation [default: drop]" },
116 { "scale-options", BV_OPT_SCALE, "fast|slow,narrow|narrow2", 0 },
117 { 0 }
120 static struct argp_option barvinok_argp_options[] = {
121 { "index", BV_OPT_MAXINDEX, "int", 0,
122 "maximal index of simple cones in decomposition" },
123 { "primal", BV_OPT_PRIMAL, 0, 0 },
124 { "table", BV_OPT_TABLE, 0, 0 },
125 { "specialization", BV_OPT_SPECIALIZATION, "[bf|df|random]", 0 },
126 { "gbr", BV_OPT_GBR, "[cdd]", 0,
127 "solver to use for basis reduction" },
128 { "version", 'V', 0, 0 },
129 { 0 }
132 static error_t approx_parse_opt(int key, char *arg, struct argp_state *state)
134 struct barvinok_options *options = state->input;
135 char *subopt;
137 switch (key) {
138 case BV_OPT_POLAPPROX:
139 if (!arg) {
140 options->polynomial_approximation = BV_APPROX_SIGN_APPROX;
141 if (options->approximation_method == BV_APPROX_NONE)
142 options->approximation_method = BV_APPROX_SCALE;
143 } else {
144 if (!strcmp(arg, "lower"))
145 options->polynomial_approximation = BV_APPROX_SIGN_LOWER;
146 else if (!strcmp(arg, "upper"))
147 options->polynomial_approximation = BV_APPROX_SIGN_UPPER;
148 if (options->approximation_method == BV_APPROX_NONE)
149 options->approximation_method = BV_APPROX_DROP;
151 break;
152 case BV_OPT_APPROX:
153 if (!strcmp(arg, "scale"))
154 options->approximation_method = BV_APPROX_SCALE;
155 else if (!strcmp(arg, "drop"))
156 options->approximation_method = BV_APPROX_DROP;
157 else if (!strcmp(arg, "volume"))
158 options->approximation_method = BV_APPROX_VOLUME;
159 else
160 argp_error(state, "unknown value for --approximation-method option");
161 break;
162 case BV_OPT_SCALE:
163 options->approximation_method = BV_APPROX_SCALE;
164 while (*arg != '\0')
165 switch (getsubopt(&arg, scale_opts, &subopt)) {
166 case SCALE_FAST:
167 options->scale_flags |= BV_APPROX_SCALE_FAST;
168 break;
169 case SCALE_SLOW:
170 options->scale_flags &= ~BV_APPROX_SCALE_FAST;
171 break;
172 case SCALE_NARROW:
173 options->scale_flags |= BV_APPROX_SCALE_NARROW;
174 options->scale_flags &= ~BV_APPROX_SCALE_NARROW2;
175 break;
176 case SCALE_NARROW2:
177 options->scale_flags |= BV_APPROX_SCALE_NARROW2;
178 options->scale_flags &= ~BV_APPROX_SCALE_NARROW;
179 break;
180 default:
181 argp_error(state, "unknown suboption '%s'\n", subopt);
183 break;
184 case ARGP_KEY_END:
185 if (options->polynomial_approximation == BV_APPROX_SIGN_NONE &&
186 options->approximation_method != BV_APPROX_NONE) {
187 fprintf(stderr,
188 "no polynomial approximation selected; reseting approximation method\n");
189 options->approximation_method = BV_APPROX_NONE;
191 break;
192 default:
193 return ARGP_ERR_UNKNOWN;
195 return 0;
198 static error_t barvinok_parse_opt(int key, char *arg, struct argp_state *state)
200 struct barvinok_options *options = state->input;
201 char *subopt;
203 switch (key) {
204 case ARGP_KEY_INIT:
205 state->child_inputs[0] = options;
206 break;
207 case 'V':
208 printf(barvinok_version());
209 exit(0);
210 case BV_OPT_SPECIALIZATION:
211 if (!strcmp(arg, "bf"))
212 options->incremental_specialization = BV_SPECIALIZATION_BF;
213 else if (!strcmp(arg, "df"))
214 options->incremental_specialization = BV_SPECIALIZATION_DF;
215 else if (!strcmp(arg, "random"))
216 options->incremental_specialization = BV_SPECIALIZATION_RANDOM;
217 break;
218 case BV_OPT_PRIMAL:
219 options->primal = 1;
220 break;
221 case BV_OPT_TABLE:
222 options->lookup_table = 1;
223 break;
224 case BV_OPT_GBR:
225 if (!strcmp(arg, "cdd"))
226 options->gbr_lp_solver = BV_GBR_CDD;
227 break;
228 case BV_OPT_MAXINDEX:
229 options->max_index = strtoul(arg, NULL, 0);
230 break;
231 default:
232 return ARGP_ERR_UNKNOWN;
234 return 0;
237 static struct argp approx_argp = {
238 approx_argp_options, approx_parse_opt, 0, 0
241 static struct argp_child barvinok_children[] = {
242 { &approx_argp, 0, "polynomial approximation", BV_GRP_APPROX },
243 { 0 }
246 struct argp barvinok_argp = {
247 barvinok_argp_options, barvinok_parse_opt, 0, 0, barvinok_children