Polly 20.0.0git
polyhedron_minimize.c
Go to the documentation of this file.
1/*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
3 *
4 * Use of this software is governed by the MIT license
5 *
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
9
10#include <assert.h>
11#include <isl/set.h>
12#include <isl/vec.h>
13#include <isl_ilp_private.h>
14#include <isl_seq.h>
15#include <isl_vec_private.h>
16
17/* The input of this program is the same as that of the "polytope_minimize"
18 * program from the barvinok distribution.
19 *
20 * Constraints of set is PolyLib format.
21 * Linear or affine objective function in PolyLib format.
22 */
23
25{
26 struct isl_vec *aff;
27
28 if (!vec)
29 return NULL;
30 aff = isl_vec_alloc(vec->ctx, 1 + vec->size);
31 if (!aff)
32 goto error;
33 isl_int_set_si(aff->el[0], 0);
34 isl_seq_cpy(aff->el + 1, vec->el, vec->size);
35 isl_vec_free(vec);
36 return aff;
37error:
38 isl_vec_free(vec);
39 return NULL;
40}
41
42/* Rotate elements of vector right.
43 * In particular, move the constant term from the end of the
44 * vector to the start of the vector.
45 */
47{
48 int i;
49
50 if (!vec)
51 return NULL;
52 for (i = vec->size - 2; i >= 0; --i)
53 isl_int_swap(vec->el[i], vec->el[i + 1]);
54 return vec;
55}
56
57int main(int argc, char **argv)
58{
59 struct isl_ctx *ctx = isl_ctx_alloc();
60 struct isl_basic_set *bset;
61 struct isl_vec *obj;
62 struct isl_vec *sol;
63 isl_int opt;
64 isl_size dim;
65 enum isl_lp_result res;
67
68 isl_int_init(opt);
69 bset = isl_basic_set_read_from_file(ctx, stdin);
70 dim = isl_basic_set_dim(bset, isl_dim_all);
71 assert(dim >= 0);
73 assert(obj);
74 assert(obj->size >= dim && obj->size <= dim + 1);
75 if (obj->size != dim + 1)
77 else
78 obj = vec_ror(obj);
79 res = isl_basic_set_solve_ilp(bset, 0, obj->el, &opt, &sol);
80 switch (res) {
81 case isl_lp_error:
82 fprintf(stderr, "error\n");
83 return -1;
84 case isl_lp_empty:
85 fprintf(stdout, "empty\n");
86 break;
88 fprintf(stdout, "unbounded\n");
89 break;
90 case isl_lp_ok:
91 p = isl_printer_to_file(ctx, stdout);
97 }
100 isl_vec_free(sol);
102 isl_int_clear(opt);
103
104 return 0;
105}
#define __isl_take
Definition: ctx.h:22
#define __isl_give
Definition: ctx.h:19
isl_ctx * isl_ctx_alloc(void)
Definition: isl_ctx.c:261
int isl_size
Definition: ctx.h:96
void isl_ctx_free(isl_ctx *ctx)
Definition: isl_ctx.c:288
enum isl_lp_result isl_basic_set_solve_ilp(__isl_keep isl_basic_set *bset, int max, isl_int *f, isl_int *opt, __isl_give isl_vec **sol_p)
Definition: isl_ilp.c:311
__isl_give isl_printer * isl_printer_print_isl_int(__isl_take isl_printer *p, isl_int i)
Definition: isl_printer.c:649
#define isl_int_set_si(r, i)
Definition: isl_int_gmp.h:15
#define isl_int_swap(i, j)
Definition: isl_int_gmp.h:25
#define isl_int_init(i)
Definition: isl_int_gmp.h:11
#define isl_int_clear(i)
Definition: isl_int_gmp.h:12
void isl_seq_cpy(isl_int *dst, isl_int *src, unsigned len)
Definition: isl_seq.c:42
const char * p
Definition: isl_test.c:8643
const char * aff
Definition: isl_test.c:7278
const char * obj
Definition: isl_test.c:3316
const char * res
Definition: isl_test.c:775
#define assert(exp)
isl_lp_result
Definition: lp.h:17
@ isl_lp_error
Definition: lp.h:18
@ isl_lp_ok
Definition: lp.h:19
@ isl_lp_empty
Definition: lp.h:21
@ isl_lp_unbounded
Definition: lp.h:20
struct isl_basic_set isl_basic_set
Definition: map_type.h:20
static __isl_give isl_vec * isl_vec_lin_to_aff(__isl_take isl_vec *vec)
static __isl_give isl_vec * vec_ror(__isl_take isl_vec *vec)
__isl_null isl_printer * isl_printer_free(__isl_take isl_printer *printer)
Definition: isl_printer.c:269
__isl_give isl_printer * isl_printer_to_file(isl_ctx *ctx, FILE *file)
Definition: isl_printer.c:217
__isl_give isl_printer * isl_printer_end_line(__isl_take isl_printer *p)
Definition: isl_printer.c:667
isl_size isl_basic_set_dim(__isl_keep isl_basic_set *bset, enum isl_dim_type type)
Definition: isl_map.c:201
__isl_give isl_basic_set * isl_basic_set_read_from_file(isl_ctx *ctx, FILE *input)
Definition: isl_input.c:3296
__isl_null isl_basic_set * isl_basic_set_free(__isl_take isl_basic_set *bset)
Definition: isl_map.c:1523
@ isl_dim_all
Definition: space_type.h:20
struct isl_ctx * ctx
__isl_null isl_vec * isl_vec_free(__isl_take isl_vec *vec)
Definition: isl_vec.c:234
__isl_give isl_printer * isl_printer_print_vec(__isl_take isl_printer *printer, __isl_keep isl_vec *vec)
Definition: isl_vec.c:352
__isl_give isl_vec * isl_vec_alloc(isl_ctx *ctx, unsigned size)
Definition: isl_vec.c:33
__isl_give isl_vec * isl_vec_read_from_file(isl_ctx *ctx, FILE *input)
Definition: isl_input.c:3418