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 Matthias Braun
27 #include "lpp_gurobi.h"
38 #include "sp_matrix.h"
40 static char gurobi_cst_encoding[4] = { 0, GRB_EQUAL, GRB_LESS_EQUAL, GRB_GREATER_EQUAL };
41 static char gurobi_var_encoding[4] = { 0, 0, GRB_CONTINUOUS, GRB_BINARY };
43 typedef struct _gurobi_t {
50 static void check_gurobi_error(gurobi_t *grb, int error)
53 panic("gurobi error: %s", GRBgeterrormsg(grb->env));
57 static gurobi_t *new_gurobi(lpp_t *lpp)
61 gurobi_t *grb = XMALLOCZ(gurobi_t);
63 /* /tmp/firm_gurobi.log is a hack (see below) */
64 error = GRBloadenv(&grb->env, "/tmp/firm_gurobi.log");
65 check_gurobi_error(grb, error);
66 /* Matze: do not set the FILE* for logging output. Because:
67 * a) the function is deprecated
68 * b) gurobi closes the FILE handle when it is done, which leads to
69 * very unexpected effects when you pass stdout or stderr as logging
71 * The only thing gurobi sanely supports is giving a string with a filename
72 * :-( ...so we use /tmp/firm_gurobi.log as a temporary measure...
75 error = GRBsetlogfile(grb->env, lpp->log);
76 check_gurobi_error(grb, error);
78 if (lpp->log != stdout && lpp->log != stderr) {
79 error = GRBsetintparam(grb->env, GRB_INT_PAR_OUTPUTFLAG, 0);
80 check_gurobi_error(grb, error);
87 static void free_gurobi(gurobi_t *grb)
89 GRBfreemodel(grb->model);
95 * Build CPLEX data structure from LPP matrix.
96 * @note: The LPP matrix is freed after this step, to save memory.
98 static void gurobi_construct(gurobi_t *grb)
104 int numcols, numrows, numentries;
105 int objsen, *matbeg, *matcnt, *matind;
106 double *obj, *rhs, *matval, *lb;
107 char *sense, *vartype;
108 char **colname, **rowname;
110 lpp_t *lpp = grb->lpp;
113 numcols = lpp->var_next-1;
114 numrows = lpp->cst_next-1;
115 numentries = matrix_get_entries(lpp->m);
116 objsen = lpp->opt_type == lpp_minimize ? 1 : -1;
119 obj = obstack_alloc(&obst, numcols * sizeof(*obj));
120 lb = obstack_alloc(&obst, numcols * sizeof(*lb));
121 colname = obstack_alloc(&obst, numcols * sizeof(*colname));
122 rowname = obstack_alloc(&obst, numrows * sizeof(*rowname));
123 vartype = obstack_alloc(&obst, numcols * sizeof(*vartype));
124 //indices = obstack_alloc(&obst, numcols * sizeof(*indices));
125 //startv = obstack_alloc(&obst, numcols * sizeof(*startv));
126 matbeg = obstack_alloc(&obst, numcols * sizeof(*matbeg));
127 matcnt = obstack_alloc(&obst, numcols * sizeof(*matcnt));
128 matind = obstack_alloc(&obst, numentries * sizeof(*matind));
129 matval = obstack_alloc(&obst, numentries * sizeof(*matval));
130 rhs = obstack_alloc(&obst, numrows * sizeof(*rhs));
131 sense = obstack_alloc(&obst, numrows * sizeof(*sense));
135 /* fill the CPLEX matrix*/
136 for (i = 0; i < numcols; ++i) {
137 lpp_name_t *curr_var = lpp->vars[1+i];
139 obj[i] = matrix_get(lpp->m, 0, 1+i);
142 colname[i] = (char*) curr_var->name;
143 vartype[i] = gurobi_var_encoding[curr_var->type.var_type];
146 if (curr_var->value_kind == lpp_value_start) {
147 panic("start values not supported in gurobi yet");
149 startv[sv_cnt++] = curr_var->value;
155 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
158 matind[o] = elem->row-1;
159 matval[o] = elem->val;
165 /* get constraint stuff (right hand side, type, name) */
166 for (i = 0; i < numrows; ++i) {
167 lpp_name_t *curr_cst = lpp->csts[1 + i];
169 rhs[i] = matrix_get(lpp->m, 1 + i, 0);
170 sense[i] = gurobi_cst_encoding[curr_cst->type.cst_type];
171 rowname[i] = (char*) curr_cst->name;
174 error = GRBloadmodel(grb->env, &grb->model, lpp->name, numcols, numrows,
175 objsen, 0, obj, sense, rhs, matbeg, matcnt, matind,
176 matval, lb, NULL, vartype, colname, rowname);
177 check_gurobi_error(grb, error);
178 grb->modelenv = GRBgetenv(grb->model);
180 obstack_free(&obst, NULL);
181 lpp_free_matrix(lpp);
184 static void gurobi_solve(gurobi_t *grb)
186 lpp_t *lpp = grb->lpp;
190 int numcols = lpp->var_next-1;
194 /* Set the time limit appropriately */
195 if(lpp->time_limit_secs > 0.0) {
196 error = GRBsetdblparam(grb->modelenv, GRB_DBL_PAR_TIMELIMIT, lpp->time_limit_secs);
197 check_gurobi_error(grb, error);
202 * If a bound of the objective function is supplied,
203 * set it accordingly, dependign on minimization or maximization.
206 fprintf(stderr, "Warning: gurobi bound not implemented yet\n");
211 error = GRBoptimize(grb->model);
212 check_gurobi_error(grb, error);
214 /* get solution status */
215 error = GRBgetintattr(grb->model, GRB_INT_ATTR_STATUS, &optimstatus);
216 check_gurobi_error(grb, error);
218 switch (optimstatus) {
219 case GRB_OPTIMAL: lpp->sol_state = lpp_optimal; break;
220 case GRB_INFEASIBLE: lpp->sol_state = lpp_infeasible; break;
221 case GRB_INF_OR_UNBD: lpp->sol_state = lpp_inforunb; break;
222 case GRB_UNBOUNDED: lpp->sol_state = lpp_unbounded; break;
223 /* TODO: is this correct? */
224 default: lpp->sol_state = lpp_feasible; break;
227 if (lpp->sol_state >= lpp_feasible) {
228 /* get variable solution values */
229 values = alloca(numcols * sizeof(*values));
230 error = GRBgetdblattrarray(grb->model, GRB_DBL_ATTR_X, 0, numcols,
232 check_gurobi_error(grb, error);
233 for(i=0; i<numcols; ++i) {
234 lpp->vars[1+i]->value = values[i];
235 lpp->vars[1+i]->value_kind = lpp_value_solution;
238 /* Get the value of the objective function. */
239 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_OBJVAL, &lpp->objval);
240 check_gurobi_error(grb, error);
241 error = GRBgetdblattr(grb->model , GRB_DBL_ATTR_OBJBOUND,
244 lpp->best_bound = FP_NAN;
248 /* get some statistics */
249 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_ITERCOUNT, &iterations);
250 check_gurobi_error(grb, error);
251 lpp->iterations = (unsigned) iterations;
253 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_RUNTIME, &lpp->sol_time);
254 check_gurobi_error(grb, error);
257 void lpp_solve_gurobi(lpp_t *lpp)
259 gurobi_t *grb = new_gurobi(lpp);
260 gurobi_construct(grb);