2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
12 #include "lpp_cplex.h"
20 #include <ilcplex/cplex.h>
23 #include "stat_timing.h"
24 #include "sp_matrix.h"
26 static char cpx_cst_encoding[4] = "?ELG";
27 static char cpx_var_encoding[4] = "??CB";
29 typedef struct _cpx_t {
37 static void chk_cpx_err(cpx_t *cpx)
40 if (CPXgeterrorstring(cpx->env, cpx->status, cpx->buf))
41 printf("%s", cpx->buf);
43 printf("Unknown CPLEX error\n");
48 static cpx_t *new_cpx(lpp_t *lpp)
50 cpx_t *cpx = XMALLOCZ(cpx_t);
52 cpx->env = CPXopenCPLEX(&cpx->status);
54 cpx->prob = CPXcreateprob(cpx->env, &cpx->status, lpp->name);
56 CPXchgobjsen(cpx->env, cpx->prob, (lpp->opt_type == lpp_minimize)?1:-1);
58 if (lpp->log && CPXsetlogfile(cpx->env, lpp->log))
63 static void free_cpx(cpx_t *cpx)
65 CPXfreeprob(cpx->env, &cpx->prob);
66 CPXcloseCPLEX(&cpx->env);
71 * Build CPLEX data structure from LPP matrix.
72 * @note: The LPP matrix is freed after this step, to save memory.
74 static void cpx_construct(cpx_t *cpx)
77 int numcols, numrows, numentries;
78 int objsen, *matbeg, *matcnt, *matind, *indices;
79 double *obj, *rhs, *matval, *lb, *ub, *startv;
80 char *sense, *vartype;
81 char **colname, **rowname;
83 lpp_t *lpp = cpx->lpp;
85 numcols = lpp->var_next-1;
86 numrows = lpp->cst_next-1;
87 numentries = matrix_get_entries(lpp->m);
88 objsen = lpp->opt_type == lpp_minimize ? 1 : -1;
91 obj = obstack_alloc(&obst, numcols * sizeof(*obj));
92 lb = obstack_alloc(&obst, numcols * sizeof(*lb));
93 ub = obstack_alloc(&obst, numcols * sizeof(*ub));
94 colname = obstack_alloc(&obst, numcols * sizeof(*colname));
95 rowname = obstack_alloc(&obst, numrows * sizeof(*rowname));
96 vartype = obstack_alloc(&obst, numcols * sizeof(*vartype));
97 indices = obstack_alloc(&obst, numcols * sizeof(*indices));
98 startv = obstack_alloc(&obst, numcols * sizeof(*startv));
99 matbeg = obstack_alloc(&obst, numcols * sizeof(*matbeg));
100 matcnt = obstack_alloc(&obst, numcols * sizeof(*matcnt));
101 matind = obstack_alloc(&obst, numentries * sizeof(*matind));
102 matval = obstack_alloc(&obst, numentries * sizeof(*matval));
103 rhs = obstack_alloc(&obst, numrows * sizeof(*rhs));
104 sense = obstack_alloc(&obst, numrows * sizeof(*sense));
108 /* fill the CPLEX matrix*/
109 for (i = 0; i < numcols; ++i) {
110 lpp_name_t *curr_var = lpp->vars[1+i];
112 obj[i] = matrix_get(lpp->m, 0, 1+i);
114 ub[i] = CPX_INFBOUND;
116 colname[i] = (char*) curr_var->name;
117 vartype[i] = cpx_var_encoding[curr_var->type.var_type];
119 if (curr_var->value_kind == lpp_value_start) {
121 startv[sv_cnt++] = curr_var->value;
126 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
129 matind[o] = elem->row-1;
130 matval[o] = elem->val;
136 /* get constraint stuff (right hand side, type, name) */
137 for (i = 0; i < numrows; ++i) {
138 lpp_name_t *curr_cst = lpp->csts[1 + i];
140 rhs[i] = matrix_get(lpp->m, 1 + i, 0);
141 sense[i] = cpx_cst_encoding[curr_cst->type.cst_type];
142 rowname[i] = (char*) curr_cst->name;
145 cpx->status = CPXcopylpwnames(cpx->env, cpx->prob,
146 numcols, numrows, objsen,
148 matbeg, matcnt, matind, matval,
153 cpx->status = CPXcopyctype(cpx->env, cpx->prob, vartype);
155 cpx->status = CPXcopymipstart(cpx->env, cpx->prob, sv_cnt, indices, startv);
158 obstack_free(&obst, NULL);
159 lpp_free_matrix(lpp);
162 static void cpx_solve(cpx_t *cpx)
164 int i, CPX_state, numcols;
169 lpp_t *lpp = cpx->lpp;
170 numcols = CPXgetnumcols(cpx->env, cpx->prob);
173 /* set performance parameters */
174 // CPXsetintparam(cpx->env, CPX_PARAM_MIPSTART, CPX_ON);
175 CPXsetintparam(cpx->env, CPX_PARAM_MIPORDTYPE, CPX_MIPORDER_COST);
176 /* output every search tree node */
177 // CPXsetintparam(cpx->env, CPX_PARAM_MIPINTERVAL, 1);
179 /* experimental switches */
180 // CPXsetintparam(cpx->env, CPX_PARAM_VARSEL, CPX_VARSEL_STRONG);
181 // CPXsetdblparam(cpx->env, CPX_PARAM_BTTOL, 1.0);
182 // CPXsetintparam(cpx->env, CPX_PARAM_BRDIR, CPX_BRDIR_UP);
185 /* Set the time limit appropriately */
186 if(lpp->time_limit_secs > 0.0)
187 CPXsetdblparam(cpx->env, CPX_PARAM_TILIM, lpp->time_limit_secs);
190 * If we have enough time, we instruct cplex to imply some
191 * of its higher order magic to pursue the best solution
194 CPXsetintparam(cpx->env, CPX_PARAM_MIPEMPHASIS, lpp->emphasis);
198 * If a bound of the objective function is supplied,
199 * set it accordingly, dependign on minimization or maximization.
202 CPXsetdblparam(cpx->env, (lpp->opt_type == lpp_minimize
203 ? CPX_PARAM_OBJLLIM : CPX_PARAM_OBJULIM), lpp->bound);
206 /* turn on the fancy messages :) */
207 // CPXsetintparam (cpx->env, CPX_PARAM_SCRIND, CPX_ON);
211 cpx->status = CPXmipopt(cpx->env, cpx->prob);
215 /* get solution status */
216 CPX_state = CPXgetstat(cpx->env, cpx->prob);
219 CPXgetstatstring(cpx->env, CPX_state, buf);
220 fprintf(stderr, "%s\n", buf);
223 case CPXMIP_INFEASIBLE:
224 case CPX_STAT_INFEASIBLE: lpp->sol_state = lpp_infeasible; break;
225 case CPXMIP_INForUNBD:
226 case CPX_STAT_INForUNBD: lpp->sol_state = lpp_inforunb; break;
227 case CPXMIP_UNBOUNDED:
228 case CPX_STAT_UNBOUNDED: lpp->sol_state = lpp_unbounded; break;
229 case CPXMIP_ABORT_FEAS:
230 case CPXMIP_FAIL_FEAS:
231 case CPXMIP_MEM_LIM_FEAS:
232 case CPXMIP_NODE_LIM_FEAS:
233 case CPXMIP_TIME_LIM_FEAS: lpp->sol_state = lpp_feasible; break;
235 case CPXMIP_OPTIMAL_TOL: /* TODO: Is this ok? Read the docu more closely */
236 case CPX_STAT_OPTIMAL: lpp->sol_state = lpp_optimal; break;
237 default: lpp->sol_state = lpp_unknown;
240 /* get variable solution values */
241 values = alloca(numcols * sizeof(*values));
242 CPXgetmipx(cpx->env, cpx->prob, values, 0, numcols-1);
244 for(i=0; i<numcols; ++i) {
245 lpp->vars[1+i]->value = values[i];
246 lpp->vars[1+i]->value_kind = lpp_value_solution;
249 /* Get the value of the objective function. */
250 CPXgetmipobjval(cpx->env, cpx->prob, &lpp->objval);
251 CPXgetbestobjval(cpx->env, cpx->prob, &lpp->best_bound);
253 /* get some statistics */
254 timing_ticks_sub(tva, tvb);
255 lpp->sol_time = timing_ticks_dbl(tva);
256 lpp->iterations = CPXgetmipitcnt(cpx->env, cpx->prob);
259 void lpp_solve_cplex(lpp_t *lpp)
261 cpx_t *cpx = new_cpx(lpp);
269 void lpp_solve_cplex(lpp_t *lpp)
272 panic("libfirm compiled without cplex support");