2 * Copyright (C) 2005-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @author Daniel Grund
27 #include "lpp_cplex.h"
32 #include <ilcplex/cplex.h>
35 #include "stat_timing.h"
36 #include "sp_matrix.h"
38 static char cpx_cst_encoding[4] = "?ELG";
39 static char cpx_var_encoding[4] = "??CB";
41 typedef struct _cpx_t {
49 static void chk_cpx_err(cpx_t *cpx)
52 if (CPXgeterrorstring(cpx->env, cpx->status, cpx->buf))
53 printf("%s", cpx->buf);
55 printf("Unknown CPLEX error\n");
60 static cpx_t *new_cpx(lpp_t *lpp)
62 cpx_t *cpx = XMALLOCZ(cpx_t);
64 cpx->env = CPXopenCPLEX(&cpx->status);
66 cpx->prob = CPXcreateprob(cpx->env, &cpx->status, lpp->name);
68 CPXchgobjsen(cpx->env, cpx->prob, (lpp->opt_type == lpp_minimize)?1:-1);
70 if (lpp->log && CPXsetlogfile(cpx->env, lpp->log))
75 static void free_cpx(cpx_t *cpx)
77 CPXfreeprob(cpx->env, &cpx->prob);
78 CPXcloseCPLEX(&cpx->env);
83 * Build CPLEX data structure from LPP matrix.
84 * @note: The LPP matrix is freed after this step, to save memory.
86 static void cpx_construct(cpx_t *cpx)
89 int numcols, numrows, numentries;
90 int objsen, *matbeg, *matcnt, *matind, *indices;
91 double *obj, *rhs, *matval, *lb, *ub, *startv;
92 char *sense, *vartype;
93 char **colname, **rowname;
95 lpp_t *lpp = cpx->lpp;
97 numcols = lpp->var_next-1;
98 numrows = lpp->cst_next-1;
99 numentries = matrix_get_entries(lpp->m);
100 objsen = lpp->opt_type == lpp_minimize ? 1 : -1;
103 obj = obstack_alloc(&obst, numcols * sizeof(*obj));
104 lb = obstack_alloc(&obst, numcols * sizeof(*lb));
105 ub = obstack_alloc(&obst, numcols * sizeof(*ub));
106 colname = obstack_alloc(&obst, numcols * sizeof(*colname));
107 rowname = obstack_alloc(&obst, numrows * sizeof(*rowname));
108 vartype = obstack_alloc(&obst, numcols * sizeof(*vartype));
109 indices = obstack_alloc(&obst, numcols * sizeof(*indices));
110 startv = obstack_alloc(&obst, numcols * sizeof(*startv));
111 matbeg = obstack_alloc(&obst, numcols * sizeof(*matbeg));
112 matcnt = obstack_alloc(&obst, numcols * sizeof(*matcnt));
113 matind = obstack_alloc(&obst, numentries * sizeof(*matind));
114 matval = obstack_alloc(&obst, numentries * sizeof(*matval));
115 rhs = obstack_alloc(&obst, numrows * sizeof(*rhs));
116 sense = obstack_alloc(&obst, numrows * sizeof(*sense));
120 /* fill the CPLEX matrix*/
121 for (i = 0; i < numcols; ++i) {
122 lpp_name_t *curr_var = lpp->vars[1+i];
124 obj[i] = matrix_get(lpp->m, 0, 1+i);
126 ub[i] = CPX_INFBOUND;
128 colname[i] = (char*) curr_var->name;
129 vartype[i] = cpx_var_encoding[curr_var->type.var_type];
131 if (curr_var->value_kind == lpp_value_start) {
133 startv[sv_cnt++] = curr_var->value;
138 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
141 matind[o] = elem->row-1;
142 matval[o] = elem->val;
148 /* get constraint stuff (right hand side, type, name) */
149 for (i = 0; i < numrows; ++i) {
150 lpp_name_t *curr_cst = lpp->csts[1 + i];
152 rhs[i] = matrix_get(lpp->m, 1 + i, 0);
153 sense[i] = cpx_cst_encoding[curr_cst->type.cst_type];
154 rowname[i] = (char*) curr_cst->name;
157 cpx->status = CPXcopylpwnames(cpx->env, cpx->prob,
158 numcols, numrows, objsen,
160 matbeg, matcnt, matind, matval,
165 cpx->status = CPXcopyctype(cpx->env, cpx->prob, vartype);
167 cpx->status = CPXcopymipstart(cpx->env, cpx->prob, sv_cnt, indices, startv);
170 obstack_free(&obst, NULL);
171 lpp_free_matrix(lpp);
174 static void cpx_solve(cpx_t *cpx)
176 int i, CPX_state, numcols;
181 lpp_t *lpp = cpx->lpp;
182 numcols = CPXgetnumcols(cpx->env, cpx->prob);
185 /* set performance parameters */
186 // CPXsetintparam(cpx->env, CPX_PARAM_MIPSTART, CPX_ON);
187 CPXsetintparam(cpx->env, CPX_PARAM_MIPORDTYPE, CPX_MIPORDER_COST);
188 /* output every search tree node */
189 // CPXsetintparam(cpx->env, CPX_PARAM_MIPINTERVAL, 1);
191 /* experimental switches */
192 // CPXsetintparam(cpx->env, CPX_PARAM_VARSEL, CPX_VARSEL_STRONG);
193 // CPXsetdblparam(cpx->env, CPX_PARAM_BTTOL, 1.0);
194 // CPXsetintparam(cpx->env, CPX_PARAM_BRDIR, CPX_BRDIR_UP);
197 /* Set the time limit appropriately */
198 if(lpp->time_limit_secs > 0.0)
199 CPXsetdblparam(cpx->env, CPX_PARAM_TILIM, lpp->time_limit_secs);
202 * If we have enough time, we instruct cplex to imply some
203 * of its higher order magic to pursue the best solution
206 CPXsetintparam(cpx->env, CPX_PARAM_MIPEMPHASIS, lpp->emphasis);
210 * If a bound of the objective function is supplied,
211 * set it accordingly, dependign on minimization or maximization.
214 CPXsetdblparam(cpx->env, (lpp->opt_type == lpp_minimize
215 ? CPX_PARAM_OBJLLIM : CPX_PARAM_OBJULIM), lpp->bound);
218 /* turn on the fancy messages :) */
219 // CPXsetintparam (cpx->env, CPX_PARAM_SCRIND, CPX_ON);
223 cpx->status = CPXmipopt(cpx->env, cpx->prob);
227 /* get solution status */
228 CPX_state = CPXgetstat(cpx->env, cpx->prob);
231 CPXgetstatstring(cpx->env, CPX_state, buf);
232 fprintf(stderr, "%s\n", buf);
235 case CPXMIP_INFEASIBLE:
236 case CPX_STAT_INFEASIBLE: lpp->sol_state = lpp_infeasible; break;
237 case CPXMIP_INForUNBD:
238 case CPX_STAT_INForUNBD: lpp->sol_state = lpp_inforunb; break;
239 case CPXMIP_UNBOUNDED:
240 case CPX_STAT_UNBOUNDED: lpp->sol_state = lpp_unbounded; break;
241 case CPXMIP_ABORT_FEAS:
242 case CPXMIP_FAIL_FEAS:
243 case CPXMIP_MEM_LIM_FEAS:
244 case CPXMIP_NODE_LIM_FEAS:
245 case CPXMIP_TIME_LIM_FEAS: lpp->sol_state = lpp_feasible; break;
247 case CPXMIP_OPTIMAL_TOL: /* TODO: Is this ok? Read the docu more closely */
248 case CPX_STAT_OPTIMAL: lpp->sol_state = lpp_optimal; break;
249 default: lpp->sol_state = lpp_unknown;
252 /* get variable solution values */
253 values = alloca(numcols * sizeof(*values));
254 CPXgetmipx(cpx->env, cpx->prob, values, 0, numcols-1);
256 for(i=0; i<numcols; ++i) {
257 lpp->vars[1+i]->value = values[i];
258 lpp->vars[1+i]->value_kind = lpp_value_solution;
261 /* Get the value of the objective function. */
262 CPXgetmipobjval(cpx->env, cpx->prob, &lpp->objval);
263 CPXgetbestobjval(cpx->env, cpx->prob, &lpp->best_bound);
265 /* get some statistics */
266 timing_ticks_sub(tva, tvb);
267 lpp->sol_time = timing_ticks_dbl(tva);
268 lpp->iterations = CPXgetmipitcnt(cpx->env, cpx->prob);
271 void lpp_solve_cplex(lpp_t *lpp)
273 cpx_t *cpx = new_cpx(lpp);