2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @author Matthias Braun
12 #include "lpp_gurobi.h"
25 #include "sp_matrix.h"
27 static char gurobi_cst_encoding[4] = { 0, GRB_EQUAL, GRB_LESS_EQUAL, GRB_GREATER_EQUAL };
28 static char gurobi_var_encoding[4] = { 0, 0, GRB_CONTINUOUS, GRB_BINARY };
30 typedef struct _gurobi_t {
37 static void check_gurobi_error(gurobi_t *grb, int error)
40 panic("gurobi error: %s", GRBgeterrormsg(grb->env));
44 static gurobi_t *new_gurobi(lpp_t *lpp)
48 gurobi_t *grb = XMALLOCZ(gurobi_t);
50 /* /tmp/firm_gurobi.log is a hack (see below) */
51 error = GRBloadenv(&grb->env, "/tmp/firm_gurobi.log");
52 check_gurobi_error(grb, error);
53 /* Matze: do not set the FILE* for logging output. Because:
54 * a) the function is deprecated
55 * b) gurobi closes the FILE handle when it is done, which leads to
56 * very unexpected effects when you pass stdout or stderr as logging
58 * The only thing gurobi sanely supports is giving a string with a filename
59 * :-( ...so we use /tmp/firm_gurobi.log as a temporary measure...
61 if (lpp->log != stdout && lpp->log != stderr) {
62 error = GRBsetintparam(grb->env, GRB_INT_PAR_OUTPUTFLAG, 0);
63 check_gurobi_error(grb, error);
69 static void free_gurobi(gurobi_t *grb)
71 GRBfreemodel(grb->model);
77 * Build CPLEX data structure from LPP matrix.
78 * @note: The LPP matrix is freed after this step, to save memory.
80 static void gurobi_construct(gurobi_t *grb)
86 int numcols, numrows, numentries;
87 int objsen, *matbeg, *matcnt, *matind;
88 double *obj, *rhs, *matval, *lb;
89 char *sense, *vartype;
90 char **colname, **rowname;
92 lpp_t *lpp = grb->lpp;
95 numcols = lpp->var_next-1;
96 numrows = lpp->cst_next-1;
97 numentries = matrix_get_entries(lpp->m);
98 objsen = lpp->opt_type == lpp_minimize ? 1 : -1;
101 obj = obstack_alloc(&obst, numcols * sizeof(*obj));
102 lb = obstack_alloc(&obst, numcols * sizeof(*lb));
103 colname = obstack_alloc(&obst, numcols * sizeof(*colname));
104 rowname = obstack_alloc(&obst, numrows * sizeof(*rowname));
105 vartype = obstack_alloc(&obst, numcols * sizeof(*vartype));
106 //indices = obstack_alloc(&obst, numcols * sizeof(*indices));
107 //startv = obstack_alloc(&obst, numcols * sizeof(*startv));
108 matbeg = obstack_alloc(&obst, numcols * sizeof(*matbeg));
109 matcnt = obstack_alloc(&obst, numcols * sizeof(*matcnt));
110 matind = obstack_alloc(&obst, numentries * sizeof(*matind));
111 matval = obstack_alloc(&obst, numentries * sizeof(*matval));
112 rhs = obstack_alloc(&obst, numrows * sizeof(*rhs));
113 sense = obstack_alloc(&obst, numrows * sizeof(*sense));
117 /* fill the CPLEX matrix*/
118 for (i = 0; i < numcols; ++i) {
119 lpp_name_t *curr_var = lpp->vars[1+i];
121 obj[i] = matrix_get(lpp->m, 0, 1+i);
124 colname[i] = (char*) curr_var->name;
125 vartype[i] = gurobi_var_encoding[curr_var->type.var_type];
129 matrix_foreach_in_col(lpp->m, 1 + i, elem) {
132 matind[o] = elem->row-1;
133 matval[o] = elem->val;
139 /* get constraint stuff (right hand side, type, name) */
140 for (i = 0; i < numrows; ++i) {
141 lpp_name_t *curr_cst = lpp->csts[1 + i];
143 rhs[i] = matrix_get(lpp->m, 1 + i, 0);
144 sense[i] = gurobi_cst_encoding[curr_cst->type.cst_type];
145 rowname[i] = (char*) curr_cst->name;
148 error = GRBloadmodel(grb->env, &grb->model, lpp->name, numcols, numrows,
149 objsen, 0, obj, sense, rhs, matbeg, matcnt, matind,
150 matval, lb, NULL, vartype, colname, rowname);
151 check_gurobi_error(grb, error);
152 grb->modelenv = GRBgetenv(grb->model);
154 obstack_free(&obst, NULL);
155 lpp_free_matrix(lpp);
158 static void gurobi_solve(gurobi_t *grb)
160 lpp_t *lpp = grb->lpp;
164 int numcols = lpp->var_next-1;
168 /* Set the time limit appropriately */
169 if(lpp->time_limit_secs > 0.0) {
170 error = GRBsetdblparam(grb->modelenv, GRB_DBL_PAR_TIMELIMIT, lpp->time_limit_secs);
171 check_gurobi_error(grb, error);
175 error = GRBoptimize(grb->model);
176 check_gurobi_error(grb, error);
178 /* get solution status */
179 error = GRBgetintattr(grb->model, GRB_INT_ATTR_STATUS, &optimstatus);
180 check_gurobi_error(grb, error);
182 switch (optimstatus) {
183 case GRB_OPTIMAL: lpp->sol_state = lpp_optimal; break;
184 case GRB_INFEASIBLE: lpp->sol_state = lpp_infeasible; break;
185 case GRB_INF_OR_UNBD: lpp->sol_state = lpp_inforunb; break;
186 case GRB_UNBOUNDED: lpp->sol_state = lpp_unbounded; break;
187 /* TODO: is this correct? */
188 default: lpp->sol_state = lpp_feasible; break;
191 if (lpp->sol_state >= lpp_feasible) {
192 /* get variable solution values */
193 values = alloca(numcols * sizeof(*values));
194 error = GRBgetdblattrarray(grb->model, GRB_DBL_ATTR_X, 0, numcols,
196 check_gurobi_error(grb, error);
197 for(i=0; i<numcols; ++i) {
198 lpp->vars[1+i]->value = values[i];
199 lpp->vars[1+i]->value_kind = lpp_value_solution;
202 /* Get the value of the objective function. */
203 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_OBJVAL, &lpp->objval);
204 check_gurobi_error(grb, error);
205 error = GRBgetdblattr(grb->model , GRB_DBL_ATTR_OBJBOUND,
208 lpp->best_bound = FP_NAN;
212 /* get some statistics */
213 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_ITERCOUNT, &iterations);
214 check_gurobi_error(grb, error);
215 lpp->iterations = (unsigned) iterations;
217 error = GRBgetdblattr(grb->model, GRB_DBL_ATTR_RUNTIME, &lpp->sol_time);
218 check_gurobi_error(grb, error);
221 void lpp_solve_gurobi(lpp_t *lpp)
223 gurobi_t *grb = new_gurobi(lpp);
224 gurobi_construct(grb);
231 void lpp_solve_gurobi(lpp_t *lpp)