2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
13 #include "lpp_cplex.h"
18 #include <ilcplex/cplex.h>
21 #include "stat_timing.h"
22 #include "sp_matrix.h"
24 static char cpx_cst_encoding[4] = "?ELG";
25 static char cpx_var_encoding[4] = "??CB";
27 typedef struct _cpx_t {
35 static void chk_cpx_err(cpx_t *cpx)
38 if (CPXgeterrorstring(cpx->env, cpx->status, cpx->buf))
39 printf("%s", cpx->buf);
41 printf("Unknown CPLEX error\n");
46 static cpx_t *new_cpx(lpp_t *lpp)
48 cpx_t *cpx = XMALLOCZ(cpx_t);
50 cpx->env = CPXopenCPLEX(&cpx->status);
52 cpx->prob = CPXcreateprob(cpx->env, &cpx->status, lpp->name);
54 CPXchgobjsen(cpx->env, cpx->prob, (lpp->opt_type == lpp_minimize)?1:-1);
56 if (lpp->log && CPXsetlogfile(cpx->env, lpp->log))
61 static void free_cpx(cpx_t *cpx)
63 CPXfreeprob(cpx->env, &cpx->prob);
64 CPXcloseCPLEX(&cpx->env);
69 * Build CPLEX data structure from LPP matrix.
70 * @note: The LPP matrix is freed after this step, to save memory.
72 static void cpx_construct(cpx_t *cpx)
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;
81 lpp_t *lpp = cpx->lpp;
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;
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));
106 /* fill the CPLEX matrix*/
107 for (i = 0; i < numcols; ++i) {
108 lpp_name_t *curr_var = lpp->vars[1+i];
110 obj[i] = matrix_get(lpp->m, 0, 1+i);
112 ub[i] = CPX_INFBOUND;
114 colname[i] = (char*) curr_var->name;
115 vartype[i] = cpx_var_encoding[curr_var->type.var_type];
117 if (curr_var->value_kind == lpp_value_start) {
119 startv[sv_cnt++] = curr_var->value;
124 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
127 matind[o] = elem->row-1;
128 matval[o] = elem->val;
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];
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;
143 cpx->status = CPXcopylpwnames(cpx->env, cpx->prob,
144 numcols, numrows, objsen,
146 matbeg, matcnt, matind, matval,
151 cpx->status = CPXcopyctype(cpx->env, cpx->prob, vartype);
153 cpx->status = CPXcopymipstart(cpx->env, cpx->prob, sv_cnt, indices, startv);
156 obstack_free(&obst, NULL);
157 lpp_free_matrix(lpp);
160 static void cpx_solve(cpx_t *cpx)
162 int i, CPX_state, numcols;
167 lpp_t *lpp = cpx->lpp;
168 numcols = CPXgetnumcols(cpx->env, cpx->prob);
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);
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);
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);
188 * If we have enough time, we instruct cplex to imply some
189 * of its higher order magic to pursue the best solution
192 CPXsetintparam(cpx->env, CPX_PARAM_MIPEMPHASIS, lpp->emphasis);
196 * If a bound of the objective function is supplied,
197 * set it accordingly, dependign on minimization or maximization.
200 CPXsetdblparam(cpx->env, (lpp->opt_type == lpp_minimize
201 ? CPX_PARAM_OBJLLIM : CPX_PARAM_OBJULIM), lpp->bound);
204 /* turn on the fancy messages :) */
205 // CPXsetintparam (cpx->env, CPX_PARAM_SCRIND, CPX_ON);
209 cpx->status = CPXmipopt(cpx->env, cpx->prob);
213 /* get solution status */
214 CPX_state = CPXgetstat(cpx->env, cpx->prob);
217 CPXgetstatstring(cpx->env, CPX_state, buf);
218 fprintf(stderr, "%s\n", buf);
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;
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;
238 /* get variable solution values */
239 values = alloca(numcols * sizeof(*values));
240 CPXgetmipx(cpx->env, cpx->prob, values, 0, numcols-1);
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;
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);
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);
257 void lpp_solve_cplex(lpp_t *lpp)
259 cpx_t *cpx = new_cpx(lpp);