2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
15 #include <isl_options_private.h>
16 #include <isl_local_space_private.h>
17 #include <isl_aff_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 #include <bset_to_bmap.c>
23 #include <set_to_map.c>
25 static enum isl_lp_result
isl_tab_solve_lp(__isl_keep isl_basic_map
*bmap
,
26 int maximize
, isl_int
*f
, isl_int denom
, isl_int
*opt
,
27 isl_int
*opt_denom
, __isl_give isl_vec
**sol
)
30 enum isl_lp_result res
;
31 isl_size dim
= isl_basic_map_dim(bmap
, isl_dim_all
);
36 isl_seq_neg(f
, f
, 1 + dim
);
38 bmap
= isl_basic_map_gauss(bmap
, NULL
);
39 tab
= isl_tab_from_basic_map(bmap
, 0);
40 res
= isl_tab_min(tab
, f
, denom
, opt
, opt_denom
, 0);
41 if (res
== isl_lp_ok
&& sol
) {
42 *sol
= isl_tab_get_sample_value(tab
);
49 isl_seq_neg(f
, f
, 1 + dim
);
51 isl_int_neg(*opt
, *opt
);
56 /* Given a basic map "bmap" and an affine combination of the variables "f"
57 * with denominator "denom", set *opt / *opt_denom to the minimal
58 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
59 * assuming the basic map is not empty and the expression cannot attain
60 * arbitrarily small (or large) values.
61 * If opt_denom is NULL, then *opt is rounded up (or down)
62 * to the nearest integer.
63 * The return value reflects the nature of the result (empty, unbounded,
64 * minimal or maximal value returned in *opt).
66 enum isl_lp_result
isl_basic_map_solve_lp(__isl_keep isl_basic_map
*bmap
,
67 int max
, isl_int
*f
, isl_int d
, isl_int
*opt
, isl_int
*opt_denom
,
68 __isl_give isl_vec
**sol
)
76 return isl_tab_solve_lp(bmap
, max
, f
, d
, opt
, opt_denom
, sol
);
79 enum isl_lp_result
isl_basic_set_solve_lp(__isl_keep isl_basic_set
*bset
,
80 int max
, isl_int
*f
, isl_int d
, isl_int
*opt
, isl_int
*opt_denom
,
81 __isl_give isl_vec
**sol
)
83 return isl_basic_map_solve_lp(bset_to_bmap(bset
), max
,
84 f
, d
, opt
, opt_denom
, sol
);
87 enum isl_lp_result
isl_map_solve_lp(__isl_keep isl_map
*map
, int max
,
88 isl_int
*f
, isl_int d
, isl_int
*opt
,
90 __isl_give isl_vec
**sol
)
97 enum isl_lp_result res
;
107 for (i
= 0; i
< map
->n
; ++i
)
108 if (map
->p
[i
]->n_div
> max_div
)
109 max_div
= map
->p
[i
]->n_div
;
111 isl_size total
= isl_map_dim(map
, isl_dim_all
);
114 v
= isl_vec_alloc(map
->ctx
, 1 + total
+ max_div
);
117 isl_seq_cpy(v
->el
, f
, 1 + total
);
118 isl_seq_clr(v
->el
+ 1 + total
, max_div
);
122 if (!opt
&& map
->n
> 1 && sol
) {
128 if (map
->n
> 0 && opt_denom
) {
129 isl_int_init(opt_denom_i
);
133 res
= isl_basic_map_solve_lp(map
->p
[0], max
, f
, d
,
134 opt
, opt_denom
, sol
);
135 if (res
== isl_lp_error
|| res
== isl_lp_unbounded
)
141 for (i
= 1; i
< map
->n
; ++i
) {
142 isl_vec
*sol_i
= NULL
;
143 enum isl_lp_result res_i
;
146 res_i
= isl_basic_map_solve_lp(map
->p
[i
], max
, f
, d
,
148 opt_denom
? &opt_denom_i
: NULL
,
149 sol
? &sol_i
: NULL
);
150 if (res_i
== isl_lp_error
|| res_i
== isl_lp_unbounded
) {
154 if (res_i
== isl_lp_empty
)
156 if (res
== isl_lp_empty
) {
158 } else if (!opt_denom
) {
160 better
= isl_int_gt(opt_i
, *opt
);
162 better
= isl_int_lt(opt_i
, *opt
);
164 isl_int_mul(t
, opt_i
, *opt_denom
);
165 isl_int_submul(t
, *opt
, opt_denom_i
);
167 better
= isl_int_is_pos(t
);
169 better
= isl_int_is_neg(t
);
174 isl_int_set(*opt
, opt_i
);
176 isl_int_set(*opt_denom
, opt_denom_i
);
187 if (map
->n
> 0 && opt_denom
) {
188 isl_int_clear(opt_denom_i
);
192 isl_int_clear(opt_i
);
198 enum isl_lp_result
isl_set_solve_lp(__isl_keep isl_set
*set
, int max
,
199 isl_int
*f
, isl_int d
, isl_int
*opt
,
201 __isl_give isl_vec
**sol
)
203 return isl_map_solve_lp(set_to_map(set
), max
,
204 f
, d
, opt
, opt_denom
, sol
);
207 /* Return the optimal (rational) value of "obj" over "bset", assuming
208 * that "obj" and "bset" have aligned parameters and divs.
209 * If "max" is set, then the maximal value is computed.
210 * Otherwise, the minimal value is computed.
212 * Return infinity or negative infinity if the optimal value is unbounded and
213 * NaN if "bset" is empty.
215 * Call isl_basic_set_solve_lp and translate the results.
217 static __isl_give isl_val
*basic_set_opt_lp(
218 __isl_keep isl_basic_set
*bset
, int max
, __isl_keep isl_aff
*obj
)
222 enum isl_lp_result lp_res
;
227 ctx
= isl_aff_get_ctx(obj
);
228 res
= isl_val_alloc(ctx
);
231 lp_res
= isl_basic_set_solve_lp(bset
, max
, obj
->v
->el
+ 1,
232 obj
->v
->el
[0], &res
->n
, &res
->d
, NULL
);
233 if (lp_res
== isl_lp_ok
)
234 return isl_val_normalize(res
);
236 if (lp_res
== isl_lp_error
)
238 if (lp_res
== isl_lp_empty
)
239 return isl_val_nan(ctx
);
241 return isl_val_infty(ctx
);
243 return isl_val_neginfty(ctx
);
246 /* Return the optimal (rational) value of "obj" over "bset", assuming
247 * that "obj" and "bset" have aligned parameters.
248 * If "max" is set, then the maximal value is computed.
249 * Otherwise, the minimal value is computed.
251 * Return infinity or negative infinity if the optimal value is unbounded and
252 * NaN if "bset" is empty.
254 * Align the divs of "bset" and "obj" and call basic_set_opt_lp.
256 static __isl_give isl_val
*isl_basic_set_opt_lp_val_aligned(
257 __isl_keep isl_basic_set
*bset
, int max
, __isl_keep isl_aff
*obj
)
262 isl_mat
*bset_div
= NULL
;
265 isl_size bset_n_div
, obj_n_div
;
270 ctx
= isl_aff_get_ctx(obj
);
271 if (!isl_space_is_equal(bset
->dim
, obj
->ls
->dim
))
272 isl_die(ctx
, isl_error_invalid
,
273 "spaces don't match", return NULL
);
275 bset_n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
276 obj_n_div
= isl_aff_dim(obj
, isl_dim_div
);
277 if (bset_n_div
< 0 || obj_n_div
< 0)
279 if (bset_n_div
== 0 && obj_n_div
== 0)
280 return basic_set_opt_lp(bset
, max
, obj
);
282 bset
= isl_basic_set_copy(bset
);
283 obj
= isl_aff_copy(obj
);
285 bset_div
= isl_basic_set_get_divs(bset
);
286 exp1
= isl_alloc_array(ctx
, int, bset_n_div
);
287 exp2
= isl_alloc_array(ctx
, int, obj_n_div
);
288 if (!bset_div
|| (bset_n_div
&& !exp1
) || (obj_n_div
&& !exp2
))
291 div
= isl_merge_divs(bset_div
, obj
->ls
->div
, exp1
, exp2
);
293 bset
= isl_basic_set_expand_divs(bset
, isl_mat_copy(div
), exp1
);
294 obj
= isl_aff_expand_divs(obj
, isl_mat_copy(div
), exp2
);
296 res
= basic_set_opt_lp(bset
, max
, obj
);
298 isl_mat_free(bset_div
);
302 isl_basic_set_free(bset
);
308 isl_mat_free(bset_div
);
311 isl_basic_set_free(bset
);
316 /* Return the optimal (rational) value of "obj" over "bset".
317 * If "max" is set, then the maximal value is computed.
318 * Otherwise, the minimal value is computed.
320 * Return infinity or negative infinity if the optimal value is unbounded and
321 * NaN if "bset" is empty.
323 static __isl_give isl_val
*isl_basic_set_opt_lp_val(
324 __isl_keep isl_basic_set
*bset
, int max
, __isl_keep isl_aff
*obj
)
332 equal
= isl_basic_set_space_has_equal_params(bset
, obj
->ls
->dim
);
336 return isl_basic_set_opt_lp_val_aligned(bset
, max
, obj
);
338 bset
= isl_basic_set_copy(bset
);
339 obj
= isl_aff_copy(obj
);
340 bset
= isl_basic_set_align_params(bset
, isl_aff_get_domain_space(obj
));
341 obj
= isl_aff_align_params(obj
, isl_basic_set_get_space(bset
));
343 res
= isl_basic_set_opt_lp_val_aligned(bset
, max
, obj
);
345 isl_basic_set_free(bset
);
351 /* Return the minimal (rational) value of "obj" over "bset".
353 * Return negative infinity if the minimal value is unbounded and
354 * NaN if "bset" is empty.
356 __isl_give isl_val
*isl_basic_set_min_lp_val(__isl_keep isl_basic_set
*bset
,
357 __isl_keep isl_aff
*obj
)
359 return isl_basic_set_opt_lp_val(bset
, 0, obj
);
362 /* Return the maximal (rational) value of "obj" over "bset".
364 * Return infinity if the maximal value is unbounded and
365 * NaN if "bset" is empty.
367 __isl_give isl_val
*isl_basic_set_max_lp_val(__isl_keep isl_basic_set
*bset
,
368 __isl_keep isl_aff
*obj
)
370 return isl_basic_set_opt_lp_val(bset
, 1, obj
);