beifg: Simplify the quite complicated way to divide a number by 2 in be_ifg_stat().
[libfirm] / ir / lpp / lpp_cplex.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @author  Daniel Grund
9  */
10 #include "config.h"
11
12 #ifdef WITH_CPLEX
13 #include "lpp_cplex.h"
14
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <assert.h>
18 #include <ilcplex/cplex.h>
19
20 #include "obst.h"
21 #include "stat_timing.h"
22 #include "sp_matrix.h"
23
24 static char cpx_cst_encoding[4] = "?ELG";
25 static char cpx_var_encoding[4] = "??CB";
26
27 typedef struct _cpx_t {
28         lpp_t *lpp;
29         CPXENVptr env;
30         CPXLPptr prob;
31         int status;
32         char buf[1024];
33 } cpx_t;
34
35 static void chk_cpx_err(cpx_t *cpx)
36 {
37         if (cpx->status) {
38                 if (CPXgeterrorstring(cpx->env, cpx->status, cpx->buf))
39                         printf("%s", cpx->buf);
40                 else
41                         printf("Unknown CPLEX error\n");
42                 assert(0);
43         }
44 }
45
46 static cpx_t *new_cpx(lpp_t *lpp)
47 {
48         cpx_t *cpx = XMALLOCZ(cpx_t);
49         cpx->lpp = lpp;
50         cpx->env = CPXopenCPLEX(&cpx->status);
51         chk_cpx_err(cpx);
52         cpx->prob = CPXcreateprob(cpx->env, &cpx->status, lpp->name);
53         chk_cpx_err(cpx);
54         CPXchgobjsen(cpx->env, cpx->prob, (lpp->opt_type == lpp_minimize)?1:-1);
55         chk_cpx_err(cpx);
56         if (lpp->log && CPXsetlogfile(cpx->env, lpp->log))
57                 lpp->log = NULL;
58         return cpx;
59 }
60
61 static void free_cpx(cpx_t *cpx)
62 {
63         CPXfreeprob(cpx->env, &cpx->prob);
64         CPXcloseCPLEX(&cpx->env);
65         free(cpx);
66 }
67
68 /**
69  * Build CPLEX data structure from LPP matrix.
70  * @note: The LPP matrix is freed after this step, to save memory.
71  */
72 static void cpx_construct(cpx_t *cpx)
73 {
74         int            i, o, sv_cnt;
75         int            numcols, numrows, numentries;
76         int            objsen, *matbeg, *matcnt, *matind, *indices;
77         double        *obj, *rhs, *matval, *lb, *ub, *startv;
78         char          *sense, *vartype;
79         char         **colname, **rowname;
80         struct obstack obst;
81         lpp_t         *lpp = cpx->lpp;
82
83         numcols    = lpp->var_next-1;
84         numrows    = lpp->cst_next-1;
85         numentries = matrix_get_entries(lpp->m);
86         objsen     = lpp->opt_type == lpp_minimize ? 1 : -1;
87         obstack_init(&obst);
88
89         obj     = obstack_alloc(&obst, numcols * sizeof(*obj));
90         lb      = obstack_alloc(&obst, numcols * sizeof(*lb));
91         ub      = obstack_alloc(&obst, numcols * sizeof(*ub));
92         colname = obstack_alloc(&obst, numcols * sizeof(*colname));
93         rowname = obstack_alloc(&obst, numrows * sizeof(*rowname));
94         vartype = obstack_alloc(&obst, numcols * sizeof(*vartype));
95         indices = obstack_alloc(&obst, numcols * sizeof(*indices));
96         startv  = obstack_alloc(&obst, numcols * sizeof(*startv));
97         matbeg  = obstack_alloc(&obst, numcols * sizeof(*matbeg));
98         matcnt  = obstack_alloc(&obst, numcols * sizeof(*matcnt));
99         matind  = obstack_alloc(&obst, numentries * sizeof(*matind));
100         matval  = obstack_alloc(&obst, numentries * sizeof(*matval));
101         rhs     = obstack_alloc(&obst, numrows * sizeof(*rhs));
102         sense   = obstack_alloc(&obst, numrows * sizeof(*sense));
103
104         o      = 0;
105         sv_cnt = 0;
106         /* fill the CPLEX matrix*/
107         for (i = 0; i < numcols; ++i) {
108                 lpp_name_t *curr_var = lpp->vars[1+i];
109
110                 obj[i] = matrix_get(lpp->m, 0, 1+i);
111                 lb[i]  = 0.0;
112                 ub[i]  = CPX_INFBOUND;
113
114                 colname[i] = (char*) curr_var->name;
115                 vartype[i] = cpx_var_encoding[curr_var->type.var_type];
116
117                 if (curr_var->value_kind == lpp_value_start) {
118                         indices[sv_cnt]  = i;
119                         startv[sv_cnt++] = curr_var->value;
120                 }
121
122                 matbeg[i] = o;
123                 matcnt[i] = 0;
124                 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
125                         if (elem->row == 0)
126                                 continue;
127                         matind[o] = elem->row-1;
128                         matval[o] = elem->val;
129                         matcnt[i]++;
130                         o++;
131                 }
132         }
133
134         /* get constraint stuff (right hand side, type, name) */
135         for (i = 0; i < numrows; ++i) {
136                 lpp_name_t *curr_cst = lpp->csts[1 + i];
137
138                 rhs[i]     = matrix_get(lpp->m, 1 + i, 0);
139                 sense[i]   = cpx_cst_encoding[curr_cst->type.cst_type];
140                 rowname[i] = (char*) curr_cst->name;
141         }
142
143         cpx->status = CPXcopylpwnames(cpx->env, cpx->prob,
144                                                 numcols, numrows, objsen,
145                                                 obj, rhs, sense,
146                                                 matbeg, matcnt, matind, matval,
147                                                 lb, ub, NULL,
148                                                 colname, rowname);
149         chk_cpx_err(cpx);
150
151         cpx->status = CPXcopyctype(cpx->env, cpx->prob, vartype);
152         chk_cpx_err(cpx);
153         cpx->status = CPXcopymipstart(cpx->env, cpx->prob, sv_cnt, indices, startv);
154         chk_cpx_err(cpx);
155
156         obstack_free(&obst, NULL);
157         lpp_free_matrix(lpp);
158 }
159
160 static void cpx_solve(cpx_t *cpx)
161 {
162         int i, CPX_state, numcols;
163         double *values;
164         timing_ticks_t tvb;
165         timing_ticks_t tva;
166
167         lpp_t *lpp = cpx->lpp;
168         numcols = CPXgetnumcols(cpx->env, cpx->prob);
169         chk_cpx_err(cpx);
170
171         /* set performance parameters */
172         // CPXsetintparam(cpx->env, CPX_PARAM_MIPSTART, CPX_ON);
173         CPXsetintparam(cpx->env, CPX_PARAM_MIPORDTYPE, CPX_MIPORDER_COST);
174         /* output every search tree node */
175         // CPXsetintparam(cpx->env, CPX_PARAM_MIPINTERVAL, 1);
176
177         /* experimental switches */
178         // CPXsetintparam(cpx->env, CPX_PARAM_VARSEL, CPX_VARSEL_STRONG);
179         // CPXsetdblparam(cpx->env, CPX_PARAM_BTTOL, 1.0);
180         // CPXsetintparam(cpx->env, CPX_PARAM_BRDIR, CPX_BRDIR_UP);
181
182
183         /* Set the time limit appropriately */
184         if(lpp->time_limit_secs > 0.0)
185                 CPXsetdblparam(cpx->env, CPX_PARAM_TILIM, lpp->time_limit_secs);
186
187         /*
188          * If we have enough time, we instruct cplex to imply some
189          * of its higher order magic to pursue the best solution
190          */
191         if(lpp->emphasis) {
192           CPXsetintparam(cpx->env, CPX_PARAM_MIPEMPHASIS, lpp->emphasis);
193         }
194
195         /*
196          * If a bound of the objective function is supplied,
197          * set it accordingly, dependign on minimization or maximization.
198          */
199         if(lpp->set_bound) {
200                 CPXsetdblparam(cpx->env, (lpp->opt_type == lpp_minimize
201                                         ? CPX_PARAM_OBJLLIM : CPX_PARAM_OBJULIM), lpp->bound);
202         }
203
204         /* turn on the fancy messages :) */
205         // CPXsetintparam (cpx->env, CPX_PARAM_SCRIND, CPX_ON);
206
207         /* solve */
208         timing_ticks(tvb);
209         cpx->status = CPXmipopt(cpx->env, cpx->prob);
210         timing_ticks(tva);
211         chk_cpx_err(cpx);
212
213         /* get solution status */
214         CPX_state = CPXgetstat(cpx->env, cpx->prob);
215         {
216           char buf[512];
217           CPXgetstatstring(cpx->env, CPX_state, buf);
218           fprintf(stderr, "%s\n", buf);
219         }
220         switch (CPX_state) {
221                 case CPXMIP_INFEASIBLE:
222                 case CPX_STAT_INFEASIBLE:   lpp->sol_state = lpp_infeasible; break;
223                 case CPXMIP_INForUNBD:
224                 case CPX_STAT_INForUNBD:    lpp->sol_state = lpp_inforunb; break;
225                 case CPXMIP_UNBOUNDED:
226                 case CPX_STAT_UNBOUNDED:    lpp->sol_state = lpp_unbounded; break;
227                 case CPXMIP_ABORT_FEAS:
228                 case CPXMIP_FAIL_FEAS:
229                 case CPXMIP_MEM_LIM_FEAS:
230                 case CPXMIP_NODE_LIM_FEAS:
231                 case CPXMIP_TIME_LIM_FEAS:  lpp->sol_state = lpp_feasible; break;
232                 case CPXMIP_OPTIMAL:
233                 case CPXMIP_OPTIMAL_TOL:    /* TODO: Is this ok? Read the docu more closely */
234                 case CPX_STAT_OPTIMAL:      lpp->sol_state = lpp_optimal; break;
235                 default:                    lpp->sol_state = lpp_unknown;
236         }
237
238         /* get variable solution values */
239         values = alloca(numcols * sizeof(*values));
240         CPXgetmipx(cpx->env, cpx->prob, values, 0, numcols-1);
241         chk_cpx_err(cpx);
242         for(i=0; i<numcols; ++i) {
243                 lpp->vars[1+i]->value = values[i];
244                 lpp->vars[1+i]->value_kind = lpp_value_solution;
245         }
246
247         /* Get the value of the objective function. */
248         CPXgetmipobjval(cpx->env, cpx->prob, &lpp->objval);
249         CPXgetbestobjval(cpx->env, cpx->prob, &lpp->best_bound);
250
251         /* get some statistics */
252         timing_ticks_sub(tva, tvb);
253         lpp->sol_time = timing_ticks_dbl(tva);
254         lpp->iterations = CPXgetmipitcnt(cpx->env, cpx->prob);
255 }
256
257 void lpp_solve_cplex(lpp_t *lpp)
258 {
259         cpx_t *cpx = new_cpx(lpp);
260         cpx_construct(cpx);
261         cpx_solve(cpx);
262         free_cpx(cpx);
263 }
264
265 #endif