Scippy

SCIP

Solving Constraint Integer Programs

lpi_grb.c
Go to the documentation of this file.
1 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
2 /* */
3 /* This file is part of the program and library */
4 /* SCIP --- Solving Constraint Integer Programs */
5 /* */
6 /* Copyright (C) 2002-2014 Konrad-Zuse-Zentrum */
7 /* fuer Informationstechnik Berlin */
8 /* */
9 /* SCIP is distributed under the terms of the ZIB Academic License. */
10 /* */
11 /* You should have received a copy of the ZIB Academic License */
12 /* along with SCIP; see the file COPYING. If not email to scip@zib.de. */
13 /* */
14 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
15 
16 /**@file lpi_grb.c
17  * @ingroup LPIS
18  * @brief LP interface for Gurobi
19  * @author Marc Pfetsch
20  *
21  * This LPI is beta!
22  *
23  * Several things are missing in the Gurobi interface that make this LPI relatively useless:
24  *
25  * - Gurobi currently does not allow to access the basis inverse.
26  * - Strong branching is supported, but not documented.
27  * - The support of ranged rows is complicated for the user: one has to keep track of the additional
28  * variables, which are added to generate a ranged row. Hence, one would need to adapt the count
29  * of variables and retrieve the information of ranged rows to get the correct answers.
30  *
31  * While the first two issues only influence the performance, the third is critical for some
32  * problems, which contain ranged rows.
33  *
34  * @todo Check whether functions for basis inverses are correct. Which ones are the right ones?
35  *
36  * @todo Check whether solisbasic is correctly used.
37  *
38  * @todo Try quad-precision and concurrent runs.
39  */
40 
41 /*--+----1----+----2----+----3----+----4----+----5----+----6----+----7----+----8----+----9----+----0----+----1----+----2*/
42 
43 #include <assert.h>
44 #include <string.h>
45 
46 #include "gurobi_c.h"
47 #include "lpi/lpi.h"
48 #include "scip/pub_message.h"
49 
50 
51 #define CHECK_ZERO(messagehdlr, x) { int _restat_; \
52  if( (_restat_ = (x)) != 0 ) \
53  { \
54  SCIPmessagePrintWarning((messagehdlr), "Gurobi error %d: %s\n", _restat_, GRBgeterrormsg(grbenv)); \
55  return SCIP_LPERROR; \
56  } \
57  }
58 
59 #if( GRB_VERSION_MAJOR < 4 )
60 #define GRB_METHOD_DUAL GRB_LPMETHOD_DUAL
61 #define GRB_METHOD_PRIMAL GRB_LPMETHOD_PRIMAL
62 #define GRB_INT_PAR_METHOD GRB_INT_PAR_LPMETHOD
63 #endif
64 
65 typedef unsigned int SCIP_SINGLEPACKET; /**< storing single bits in packed format */
66 #define SCIP_SINGLEPACKETSIZE (sizeof(SCIP_SINGLEPACKET)*8) /**< each entry needs one bit of information */
67 typedef unsigned int SCIP_DUALPACKET; /**< storing bit pairs in packed format */
68 #define SCIP_DUALPACKETSIZE (sizeof(SCIP_DUALPACKET)*4) /**< each entry needs two bits of information */
69 
70 typedef SCIP_DUALPACKET COLPACKET; /* each column needs two bits of information (basic/on_lower/on_upper) */
71 #define COLS_PER_PACKET SCIP_DUALPACKETSIZE
72 typedef SCIP_DUALPACKET ROWPACKET; /* each row needs two bit of information (basic/on_lower/on_upper) */
73 #define ROWS_PER_PACKET SCIP_DUALPACKETSIZE
74 
75 
76 /* At several places we need to guarantee to have a factorization of an optimal basis and call the simplex to produce
77  * it. In a numerical perfect world, this should need no iterations. However, due to numerical inaccuracies after
78  * refactorization, it might be necessary to do a few extra pivot steps. */
79 #define GRB_REFACTORMAXITERS 50 /* maximal number of iterations allowed for producing a refactorization of the basis */
80 
81 
82 /* Gurobi parameter lists which can be changed */
83 #define NUMINTPARAM 4
84 
85 static const char* intparam[NUMINTPARAM] =
86 {
87  GRB_INT_PAR_SCALEFLAG,
88  GRB_INT_PAR_PRESOLVE,
89  GRB_INT_PAR_SIMPLEXPRICING,
90  GRB_INT_PAR_OUTPUTFLAG
91 };
92 
93 #define NUMDBLPARAM 6
94 
95 static const char* dblparam[NUMDBLPARAM] =
96 {
97  GRB_DBL_PAR_FEASIBILITYTOL,
98  GRB_DBL_PAR_OPTIMALITYTOL,
99  GRB_DBL_PAR_CUTOFF,
100  GRB_DBL_PAR_TIMELIMIT,
101  GRB_DBL_PAR_ITERATIONLIMIT,
102  GRB_DBL_PAR_MARKOWITZTOL
103 };
104 
105 static const double dblparammin[NUMDBLPARAM] =
106 {
107  +1e-09, /* GRB_DBL_PAR_FEASIBILITYTOL */
108  +1e-09, /* GRB_DBL_PAR_OPTIMALITYTOL */
109  -GRB_INFINITY, /* GRB_DBL_PAR_CUTOFF */
110  0, /* GRB_DBL_PAR_TIMELIMIT */
111  0, /* GRB_DBL_PAR_ITERATIONLIMIT */
112  1e-04 /* GRB_DBL_PAR_MARKOWITZTOL */
113 };
114 
115 /** Gurobi parameter settings */
116 struct GRBParam
117 {
118  int intparval[NUMINTPARAM]; /**< integer parameter values */
119  double dblparval[NUMDBLPARAM]; /**< double parameter values */
120 };
121 typedef struct GRBParam GRBPARAM;
122 
123 
124 /** LP interface */
125 struct SCIP_LPi
126 {
127  GRBmodel* grbmodel; /**< Gurobi model pointer */
128  GRBenv* grbenv; /**< environment corresponding to model */
129  int solstat; /**< solution status of last optimization call */
130  GRBPARAM defparam; /**< default parameter values */
131  GRBPARAM curparam; /**< current parameter values stored in Gurobi LP */
132  GRBPARAM grbparam; /**< current parameter values for this LP */
133  char* senarray; /**< array for storing row senses */
134  SCIP_Real* rhsarray; /**< array for storing rhs values */
135  SCIP_Real* valarray; /**< array for storing coefficient values */
136  int* cstat; /**< array for storing column basis status */
137  int* rstat; /**< array for storing row basis status */
138  int* indarray; /**< array for storing coefficient indices */
139  int sidechgsize; /**< size of senarray */
140  int valsize; /**< size of valarray and indarray */
141  int cstatsize; /**< size of cstat array */
142  int rstatsize; /**< size of rstat array */
143  int iterations; /**< number of iterations used in the last solving call */
144  SCIP_Bool solisbasic; /**< is current LP solution a basic solution? */
145  SCIP_Bool fromscratch; /**< should each solve be performed without previous basis state? */
146  SCIP_PRICING pricing; /**< SCIP pricing setting */
147  SCIP_MESSAGEHDLR* messagehdlr; /**< messagehdlr handler to printing messages, or NULL */
148 };
149 
150 /** LPi state stores basis information */
151 struct SCIP_LPiState
152 {
153  int ncols; /**< number of LP columns */
154  int nrows; /**< number of LP rows */
155  COLPACKET* packcstat; /**< column basis status in compressed form */
156  ROWPACKET* packrstat; /**< row basis status in compressed form */
157 };
158 
159 
160 static GRBenv* grbenv = NULL; /**< Gurobi environment (only needed for initialization) */
161 static int numlp = 0; /**< number of open LP objects */
162 
163 
164 
165 /*
166  * dynamic memory arrays
167  */
168 
169 /** resizes senarray to have at least num entries */
170 static
171 SCIP_RETCODE ensureSidechgMem(
172  SCIP_LPI* lpi, /**< LP interface structure */
173  int num /**< minimal number of entries in array */
174  )
175 {
176  assert(lpi != NULL);
177 
178  if( num > lpi->sidechgsize )
179  {
180  int newsize;
181 
182  newsize = MAX(2*lpi->sidechgsize, num);
183  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->senarray, newsize) );
184  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->rhsarray, newsize) );
185  lpi->sidechgsize = newsize;
186  }
187  assert(num <= lpi->sidechgsize);
188 
189  return SCIP_OKAY;
190 }
191 
192 /** resizes valarray and indarray to have at least num entries */
193 static
194 SCIP_RETCODE ensureValMem(
195  SCIP_LPI* lpi, /**< LP interface structure */
196  int num /**< minimal number of entries in array */
197  )
198 {
199  assert(lpi != NULL);
200 
201  if( num > lpi->valsize )
202  {
203  int newsize;
204 
205  newsize = MAX(2*lpi->valsize, num);
206  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->valarray, newsize) );
207  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->indarray, newsize) );
208  lpi->valsize = newsize;
209  }
210  assert(num <= lpi->valsize);
211 
212  return SCIP_OKAY;
213 }
214 
215 /** resizes cstat array to have at least num entries */
216 static
217 SCIP_RETCODE ensureCstatMem(
218  SCIP_LPI* lpi, /**< LP interface structure */
219  int num /**< minimal number of entries in array */
220  )
221 {
222  assert(lpi != NULL);
223 
224  if( num > lpi->cstatsize )
225  {
226  int newsize;
227 
228  newsize = MAX(2*lpi->cstatsize, num);
229  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->cstat, newsize) );
230  lpi->cstatsize = newsize;
231  }
232  assert(num <= lpi->cstatsize);
233 
234  return SCIP_OKAY;
235 }
236 
237 /** resizes rstat array to have at least num entries */
238 static
239 SCIP_RETCODE ensureRstatMem(
240  SCIP_LPI* lpi, /**< LP interface structure */
241  int num /**< minimal number of entries in array */
242  )
243 {
244  assert(lpi != NULL);
245 
246  if( num > lpi->rstatsize )
247  {
248  int newsize;
249 
250  newsize = MAX(2*lpi->rstatsize, num);
251  SCIP_ALLOC( BMSreallocMemoryArray(&lpi->rstat, newsize) );
252  lpi->rstatsize = newsize;
253  }
254  assert(num <= lpi->rstatsize);
255 
256  return SCIP_OKAY;
257 }
258 
259 /** stores current basis in internal arrays of LPI data structure */
260 static
261 SCIP_RETCODE getBase(
262  SCIP_LPI* lpi, /**< LP interface structure */
263  SCIP_Bool* success /**< whether basis information has successfully been obtained */
264  )
265 {
266  int ncols;
267  int nrows;
268  int res;
269 
270  assert( lpi != NULL );
271  assert( lpi->grbmodel != NULL );
272  assert( lpi->grbenv != NULL );
273 
274  SCIPdebugMessage("getBase()\n");
275  if ( success != NULL )
276  *success = TRUE;
277 
278  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
279  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
280 
281  /* allocate enough memory for storing uncompressed basis information */
282  SCIP_CALL( ensureCstatMem(lpi, ncols) );
283  SCIP_CALL( ensureRstatMem(lpi, nrows) );
284 
285  /* get unpacked basis information from Gurobi */
286  res = GRBgetintattrarray(lpi->grbmodel, GRB_INT_ATTR_VBASIS, 0, ncols, lpi->cstat);
287  if ( res == GRB_ERROR_DATA_NOT_AVAILABLE )
288  {
289  /* if the model is infeasible Gurobi does not currently return basis information */
290  if ( success != NULL )
291  *success = FALSE;
292  return SCIP_OKAY;
293  }
294  else if ( res != 0 )
295  {
296  SCIPerrorMessage("Gurobi error %d: %s\n", res, GRBgeterrormsg(lpi->grbenv));
297  return SCIP_LPERROR;
298  }
299 
300  res = GRBgetintattrarray(lpi->grbmodel, GRB_INT_ATTR_CBASIS, 0, nrows, lpi->rstat);
301  if ( res == GRB_ERROR_DATA_NOT_AVAILABLE )
302  {
303  /* if the model is infeasible Gurobi does not currently return basis information */
304  if ( success != NULL )
305  *success = FALSE;
306  return SCIP_OKAY;
307  }
308  else if ( res != 0 )
309  {
310  SCIPerrorMessage("Gurobi error %d: %s\n", res, GRBgeterrormsg(lpi->grbenv));
311  return SCIP_LPERROR;
312  }
313 
314  return SCIP_OKAY;
315 }
316 
317 /** loads basis stored in internal arrays of LPI data structure into Gurobi */
318 static
319 SCIP_RETCODE setBase(
320  SCIP_LPI* lpi /**< LP interface structure */
321  )
322 {
323  int ncols;
324  int nrows;
325 
326  assert( lpi != NULL );
327  assert( lpi->grbmodel != NULL );
328 
329  SCIPdebugMessage("setBase()\n");
330 
331  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
332  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
333 
334  /* load basis information into Gurobi */
335  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrarray(lpi->grbmodel, GRB_INT_ATTR_VBASIS, 0, ncols, lpi->cstat) );
336  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrarray(lpi->grbmodel, GRB_INT_ATTR_CBASIS, 0, nrows, lpi->rstat) );
337 
338  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
339 
340  return SCIP_OKAY;
341 }
342 
343 
344 
345 
346 /*
347  * LPi state methods
348  */
349 
350 /** returns the number of packets needed to store column packet information */
351 static
352 int colpacketNum(
353  int ncols /**< number of columns to store */
354  )
355 {
356  return (ncols+(int)COLS_PER_PACKET-1)/(int)COLS_PER_PACKET;
357 }
358 
359 /** returns the number of packets needed to store row packet information */
360 static
361 int rowpacketNum(
362  int nrows /**< number of rows to store */
363  )
364 {
365  return (nrows+(int)ROWS_PER_PACKET-1)/(int)ROWS_PER_PACKET;
366 }
367 
368 
369 /* The basis information for Gurobi is negative. So we cannot use the functions in bitencode.h/c. The functions below are a modified copy. */
370 
371 /** encode a negated dual bit vector into packed format */
372 static
373 void SCIPencodeDualBitNeg(
374  const int* inp, /**< unpacked input vector */
375  SCIP_DUALPACKET* out, /**< buffer to store the packed vector */
376  int count /**< number of elements */
377  )
378 {
379  static const SCIP_DUALPACKET mask[SCIP_DUALPACKETSIZE][4] = { /* if the packet size changes, the mask has to be updated */
380  {0x00000000, 0x00000001, 0x00000002, 0x00000003},
381  {0x00000000, 0x00000004, 0x00000008, 0x0000000C},
382  {0x00000000, 0x00000010, 0x00000020, 0x00000030},
383  {0x00000000, 0x00000040, 0x00000080, 0x000000C0},
384  {0x00000000, 0x00000100, 0x00000200, 0x00000300},
385  {0x00000000, 0x00000400, 0x00000800, 0x00000C00},
386  {0x00000000, 0x00001000, 0x00002000, 0x00003000},
387  {0x00000000, 0x00004000, 0x00008000, 0x0000C000},
388  {0x00000000, 0x00010000, 0x00020000, 0x00030000},
389  {0x00000000, 0x00040000, 0x00080000, 0x000C0000},
390  {0x00000000, 0x00100000, 0x00200000, 0x00300000},
391  {0x00000000, 0x00400000, 0x00800000, 0x00C00000},
392  {0x00000000, 0x01000000, 0x02000000, 0x03000000},
393  {0x00000000, 0x04000000, 0x08000000, 0x0C000000},
394  {0x00000000, 0x10000000, 0x20000000, 0x30000000},
395  {0x00000000, 0x40000000, 0x80000000, 0xC0000000}
396  };
397  int i;
398  int rest;
399  int nfull;
400 
401  assert(inp != NULL || count == 0);
402  assert(out != NULL || count == 0);
403  assert(count >= 0);
404  assert(SCIP_DUALPACKETSIZE == 16);
405 
406  rest = count % (int)SCIP_DUALPACKETSIZE;
407  nfull = count - rest;
408 
409  for( i = 0; i < nfull; i += (int)SCIP_DUALPACKETSIZE, inp += (int)SCIP_DUALPACKETSIZE )
410  {
411  assert(inp != NULL);
412  assert(out != NULL);
413 
414 #ifndef NDEBUG
415  {
416  unsigned int j;
417  for( j = 0; j < SCIP_DUALPACKETSIZE; ++j )
418  assert(0 <= -inp[j] && -inp[j] <= 3);
419  }
420 #endif
421  *out++ =
422  mask[0][-inp[0]] | mask[1][-inp[1]] | mask[2][-inp[2]] | mask[3][inp[3]]
423  | mask[4][-inp[4]] | mask[5][-inp[5]] | mask[6][-inp[6]]
424  | mask[7][-inp[7]] | mask[8][-inp[8]] | mask[9][-inp[9]]
425  | mask[10][-inp[10]] | mask[11][-inp[11]] | mask[12][-inp[12]]
426  | mask[13][-inp[13]] | mask[14][-inp[14]] | mask[15][-inp[15]];
427  }
428 
429  if( rest > 0 )
430  {
432 
433  assert(inp != NULL);
434  assert(out != NULL);
435 
436  for( i = 0; i < rest; i++ )
437  m |= mask[i][-inp[i]];
438  *out = m;
439  }
440 }
441 
442 /** decode a packed dual bit vector into negated unpacked format */
443 static
444 void SCIPdecodeDualBitNeg(
445  const SCIP_DUALPACKET* inp, /**< packed input vector */
446  int* out, /**< buffer to store unpacked vector */
447  int count /**< number of elements */
448  )
449 {
450  SCIP_DUALPACKET m;
451  int rest;
452  int nfull;
453  int i;
454 
455  assert(inp != NULL || count == 0);
456  assert(out != NULL || count == 0);
457  assert(count >= 0);
458  assert(SCIP_DUALPACKETSIZE == 16);
459 
460  rest = count % (int)SCIP_DUALPACKETSIZE;
461  nfull = count - rest;
462 
463  for( i = 0; i < nfull; i += (int)SCIP_DUALPACKETSIZE )
464  {
465  assert(inp != NULL);
466  assert(out != NULL);
467 
468  m = *inp++;
469 
470  *out++ = -(int)(m & 3);
471  m >>= 2;
472  *out++ = -(int)(m & 3);
473  m >>= 2;
474  *out++ = -(int)(m & 3);
475  m >>= 2;
476  *out++ = -(int)(m & 3);
477  m >>= 2;
478  *out++ = -(int)(m & 3);
479  m >>= 2;
480  *out++ = -(int)(m & 3);
481  m >>= 2;
482  *out++ = -(int)(m & 3);
483  m >>= 2;
484  *out++ = -(int)(m & 3);
485  m >>= 2;
486  *out++ = -(int)(m & 3);
487  m >>= 2;
488  *out++ = -(int)(m & 3);
489  m >>= 2;
490  *out++ = -(int)(m & 3);
491  m >>= 2;
492  *out++ = -(int)(m & 3);
493  m >>= 2;
494  *out++ = -(int)(m & 3);
495  m >>= 2;
496  *out++ = -(int)(m & 3);
497  m >>= 2;
498  *out++ = -(int)(m & 3);
499  m >>= 2;
500  *out++ = -(int)(m & 3);
501  assert(m >> 2 == 0);
502  }
503 
504  if( rest > 0 )
505  {
506  assert(inp != NULL);
507  assert(out != NULL);
508 
509  m = *inp;
510  for( i = 0; i < rest; i++ )
511  {
512  *out++ = -(int)(m & 3);
513  m >>= 2;
514  }
515  }
516 }
517 
518 /** store row and column basis status in a packed LPi state object */
519 static
520 void lpistatePack(
521  SCIP_LPISTATE* lpistate, /**< pointer to LPi state data */
522  const int* cstat, /**< basis status of columns in unpacked format */
523  const int* rstat /**< basis status of rows in unpacked format */
524  )
525 {
526  assert(lpistate != NULL);
527  assert(lpistate->packcstat != NULL);
528  assert(lpistate->packrstat != NULL);
529 
530  SCIPencodeDualBitNeg(cstat, lpistate->packcstat, lpistate->ncols);
531  SCIPencodeDualBitNeg(rstat, lpistate->packrstat, lpistate->nrows);
532 }
533 
534 /** unpacks row and column basis status from a packed LPi state object */
535 static
536 void lpistateUnpack(
537  const SCIP_LPISTATE* lpistate, /**< pointer to LPi state data */
538  int* cstat, /**< buffer for storing basis status of columns in unpacked format */
539  int* rstat /**< buffer for storing basis status of rows in unpacked format */
540  )
541 {
542  assert(lpistate != NULL);
543  assert(lpistate->packcstat != NULL);
544  assert(lpistate->packrstat != NULL);
545 
546  SCIPdecodeDualBitNeg(lpistate->packcstat, cstat, lpistate->ncols);
547  SCIPdecodeDualBitNeg(lpistate->packrstat, rstat, lpistate->nrows);
548 }
549 
550 /** creates LPi state information object */
551 static
552 SCIP_RETCODE lpistateCreate(
553  SCIP_LPISTATE** lpistate, /**< pointer to LPi state */
554  BMS_BLKMEM* blkmem, /**< block memory */
555  int ncols, /**< number of columns to store */
556  int nrows /**< number of rows to store */
557  )
558 {
559  assert(lpistate != NULL);
560  assert(blkmem != NULL);
561  assert(ncols >= 0);
562  assert(nrows >= 0);
563 
564  SCIP_ALLOC( BMSallocBlockMemory(blkmem, lpistate) );
565  SCIP_ALLOC( BMSallocBlockMemoryArray(blkmem, &(*lpistate)->packcstat, colpacketNum(ncols)) );
566  SCIP_ALLOC( BMSallocBlockMemoryArray(blkmem, &(*lpistate)->packrstat, rowpacketNum(nrows)) );
567 
568  return SCIP_OKAY;
569 }
570 
571 /** frees LPi state information */
572 static
573 void lpistateFree(
574  SCIP_LPISTATE** lpistate, /**< pointer to LPi state information (like basis information) */
575  BMS_BLKMEM* blkmem /**< block memory */
576  )
577 {
578  assert(blkmem != NULL);
579  assert(lpistate != NULL);
580  assert(*lpistate != NULL);
581 
582  BMSfreeBlockMemoryArrayNull(blkmem, &(*lpistate)->packcstat, colpacketNum((*lpistate)->ncols));
583  BMSfreeBlockMemoryArrayNull(blkmem, &(*lpistate)->packrstat, rowpacketNum((*lpistate)->nrows));
584  BMSfreeBlockMemory(blkmem, lpistate);
585 }
586 
587 
588 
589 /*
590  * local methods
591  */
592 
593 /** gets all Gurobi parameters used in LPI */
594 static
595 SCIP_RETCODE getParameterValues(
596  SCIP_LPI* lpi, /**< LP interface structure */
597  GRBPARAM* grbparam /**< Gurobi parameters */
598  )
599 {
600  int i;
601 
602  assert( lpi != NULL );
603  assert( lpi->grbenv != NULL );
604  assert( grbparam != NULL );
605 
606  SCIPdebugMessage("getParameterValues()\n");
607 
608  for( i = 0; i < NUMINTPARAM; ++i )
609  {
610  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, intparam[i], &(grbparam->intparval[i])) );
611  }
612  for( i = 0; i < NUMDBLPARAM; ++i )
613  {
614  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, dblparam[i], &(grbparam->dblparval[i])) );
615  }
616 
617  return SCIP_OKAY;
618 }
619 
620 /** in debug mode, checks validity of Gurobi parameters */
621 static
622 SCIP_RETCODE checkParameterValues(
623  SCIP_LPI* lpi /**< LP interface structure */
624  )
625 {
626 #ifndef NDEBUG
627  GRBPARAM par;
628  int i;
629 
630  SCIP_CALL( getParameterValues(lpi, &par) );
631  for (i = 0; i < NUMINTPARAM; ++i)
632  assert( lpi->curparam.intparval[i] == par.intparval[i] );
633  for (i = 0; i < NUMDBLPARAM; ++i)
634  assert(MAX(lpi->curparam.dblparval[i], dblparammin[i]) == par.dblparval[i]); /*lint !e777*/
635 #endif
636 
637  return SCIP_OKAY;
638 }
639 
640 /** sets all Gurobi parameters used in LPI */
641 static
642 SCIP_RETCODE setParameterValues(
643  SCIP_LPI* lpi, /**< LP interface structure */
644  GRBPARAM* grbparam /**< Gurobi parameters */
645  )
646 {
647  int i;
648 
649  assert( lpi != NULL );
650  assert( lpi->grbenv != NULL );
651  assert( grbparam != NULL );
652 
653  SCIPdebugMessage("setParameterValues()\n");
654 
655  for( i = 0; i < NUMINTPARAM; ++i )
656  {
657  if( lpi->curparam.intparval[i] != grbparam->intparval[i] )
658  {
659  SCIPdebugMessage("setting Gurobi int parameter %s from %d to %d\n",
660  intparam[i], lpi->curparam.intparval[i], grbparam->intparval[i]);
661  lpi->curparam.intparval[i] = grbparam->intparval[i];
662  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, intparam[i], lpi->curparam.intparval[i]) );
663  }
664  }
665  for( i = 0; i < NUMDBLPARAM; ++i )
666  {
667  if( lpi->curparam.dblparval[i] != grbparam->dblparval[i] ) /*lint !e777*/
668  {
669  SCIPdebugMessage("setting Gurobi dbl parameter %s from %g to %g\n",
670  dblparam[i], lpi->curparam.dblparval[i], MAX(grbparam->dblparval[i], dblparammin[i]));
671  lpi->curparam.dblparval[i] = MAX(grbparam->dblparval[i], dblparammin[i]);
672  CHECK_ZERO( lpi->messagehdlr, GRBsetdblparam(lpi->grbenv, dblparam[i], lpi->curparam.dblparval[i]) );
673  }
674  }
675 
676  SCIP_CALL( checkParameterValues(lpi) );
677 
678  return SCIP_OKAY;
679 }
680 
681 /** copies Gurobi parameters from source to dest */
682 static
683 void copyParameterValues(
684  GRBPARAM* dest, /**< destination Gurobi parameters */
685  const GRBPARAM* source /**< original Gurobi parameters */
686  )
687 {
688  int i;
689 
690  for( i = 0; i < NUMINTPARAM; ++i )
691  dest->intparval[i] = source->intparval[i];
692  for( i = 0; i < NUMDBLPARAM; ++i )
693  dest->dblparval[i] = source->dblparval[i];
694 }
695 
696 /** gets a single integer parameter value */
697 static
698 SCIP_RETCODE getIntParam(
699  SCIP_LPI* lpi, /**< LP interface structure */
700  const char* param, /**< parameter name */
701  int* p /**< value of parameter */
702  )
703 {
704  int i;
705 
706  assert( lpi != NULL );
707 
708  for( i = 0; i < NUMINTPARAM; ++i )
709  {
710  if( strcmp(intparam[i], param) == 0 )
711  {
712  *p = lpi->grbparam.intparval[i];
713  return SCIP_OKAY;
714  }
715  }
716 
717  SCIPerrorMessage("unknown Gurobi integer parameter <%s>.\n", param);
718  return SCIP_LPERROR;
719 }
720 
721 /** sets a single integer parameter value */
722 static
723 SCIP_RETCODE setIntParam(
724  SCIP_LPI* lpi, /**< LP interface structure */
725  const char* param, /**< parameter name */
726  int parval /**< value of parameter */
727  )
728 {
729  int i;
730 
731  assert( lpi != NULL );
732 
733  for( i = 0; i < NUMINTPARAM; ++i )
734  {
735  if( strcmp(intparam[i], param) == 0 )
736  {
737  lpi->grbparam.intparval[i] = parval;
738  return SCIP_OKAY;
739  }
740  }
741 
742  SCIPerrorMessage("unknown Gurobi integer parameter <%s>.\n", param);
743  return SCIP_LPERROR;
744 }
745 
746 /** gets a single double parameter value */
747 static
748 SCIP_RETCODE getDblParam(SCIP_LPI* lpi, const char* param, double* p)
749 {
750  int i;
751 
752  assert(lpi != NULL);
753 
754  for( i = 0; i < NUMDBLPARAM; ++i )
755  {
756  if( strcmp(dblparam[i], param) == 0 )
757  {
758  *p = lpi->grbparam.dblparval[i];
759  return SCIP_OKAY;
760  }
761  }
762 
763  SCIPerrorMessage("unknown Gurobi double parameter <%s>.\n", param);
764  return SCIP_LPERROR;
765 }
766 
767 /** sets a single double parameter value */
768 static
769 SCIP_RETCODE setDblParam(
770  SCIP_LPI* lpi, /**< LP interface structure */
771  const char* param, /**< parameter name */
772  double parval /**< value of parameter */
773  )
774 {
775  int i;
776 
777  assert( lpi != NULL );
778 
779  for( i = 0; i < NUMDBLPARAM; ++i )
780  {
781  if( strcmp(dblparam[i], param) == 0 )
782  {
783  lpi->grbparam.dblparval[i] = parval;
784  return SCIP_OKAY;
785  }
786  }
787 
788  SCIPerrorMessage("unknown Gurobi double parameter <%s>.\n", param);
789  return SCIP_LPERROR;
790 }
791 
792 /** marks the current LP to be unsolved */
793 static
794 void invalidateSolution(
795  SCIP_LPI* lpi /**< LP interface structure */
796  )
797 {
798  assert(lpi != NULL);
799  lpi->solstat = -1;
800 }
801 
802 /** converts SCIP's lhs/rhs pairs into Gurobi's sen/rhs */
803 static
804 SCIP_RETCODE convertSides(
805  SCIP_LPI* lpi, /**< LP interface structure */
806  int nrows, /**< number of rows */
807  const SCIP_Real* lhs, /**< left hand side vector */
808  const SCIP_Real* rhs, /**< right hand side vector */
809  int* rngcount /**< number of ranged rows found */
810  )
811 {
812  int i;
813 
814  assert(lpi != NULL);
815  assert(nrows >= 0);
816  assert(lhs != NULL);
817  assert(rhs != NULL);
818 
819  /* convert lhs/rhs into sen/rhs */
820  *rngcount = 0;
821  for( i = 0; i < nrows; ++i )
822  {
823  assert(lhs[i] <= rhs[i]);
824 
825  if( lhs[i] == rhs[i] ) /*lint !e777*/
826  {
827  assert(-GRB_INFINITY < rhs[i] && rhs[i] < GRB_INFINITY);
828  lpi->senarray[i] = GRB_EQUAL;
829  lpi->rhsarray[i] = rhs[i];
830  }
831  else if( lhs[i] <= -GRB_INFINITY )
832  {
833  assert(-GRB_INFINITY < rhs[i] && rhs[i] < GRB_INFINITY);
834  lpi->senarray[i] = GRB_LESS_EQUAL;
835  lpi->rhsarray[i] = rhs[i];
836  }
837  else if( rhs[i] >= GRB_INFINITY )
838  {
839  assert(-GRB_INFINITY < lhs[i] && lhs[i] < GRB_INFINITY);
840  lpi->senarray[i] = GRB_GREATER_EQUAL;
841  lpi->rhsarray[i] = lhs[i];
842  }
843  else
844  {
845  /* Gurobi cannot handle ranged rows */
846  SCIPerrorMessage("Gurobi cannot handle ranged rows.\n");
847  SCIPABORT();
848  return SCIP_LPERROR;
849  /* (*rngcount)++; */
850  }
851  }
852  return SCIP_OKAY;
853 }
854 
855 /** converts Gurobi's sen/rhs pairs into SCIP's lhs/rhs pairs */
856 static
857 SCIP_RETCODE reconvertBothSides(
858  SCIP_LPI* lpi, /**< LP interface structure */
859  int nrows, /**< number of rows */
860  SCIP_Real* lhs, /**< buffer to store the left hand side vector */
861  SCIP_Real* rhs /**< buffer to store the right hand side vector */
862  )
863 {
864  int i;
865 
866  assert(lpi != NULL);
867  assert(nrows >= 0);
868  assert(lhs != NULL);
869  assert(rhs != NULL);
870 
871  for (i = 0; i < nrows; ++i)
872  {
873  switch( lpi->senarray[i] )
874  {
875  case GRB_EQUAL:
876  lhs[i] = lpi->rhsarray[i];
877  rhs[i] = lpi->rhsarray[i];
878  break;
879 
880  case GRB_LESS_EQUAL:
881  lhs[i] = -GRB_INFINITY;
882  rhs[i] = lpi->rhsarray[i];
883  break;
884 
885  case GRB_GREATER_EQUAL:
886  lhs[i] = lpi->rhsarray[i];
887  rhs[i] = GRB_INFINITY;
888  break;
889 
890  default:
891  SCIPerrorMessage("invalid row sense\n");
892  SCIPABORT();
893  return SCIP_LPERROR;
894  }
895  assert(lhs[i] <= rhs[i]);
896  }
897  return SCIP_OKAY;
898 }
899 
900 /** converts Gurobi's sen/rhs pairs into SCIP's lhs/rhs pairs, only storing the left hand side */
901 static
902 SCIP_RETCODE reconvertLhs(
903  SCIP_LPI* lpi, /**< LP interface structure */
904  int nrows, /**< number of rows */
905  SCIP_Real* lhs /**< buffer to store the left hand side vector */
906  )
907 {
908  int i;
909 
910  assert(lpi != NULL);
911  assert(nrows >= 0);
912  assert(lhs != NULL);
913 
914  for (i = 0; i < nrows; ++i)
915  {
916  switch( lpi->senarray[i] )
917  {
918  case GRB_EQUAL:
919  lhs[i] = lpi->rhsarray[i];
920  break;
921 
922  case GRB_LESS_EQUAL:
923  lhs[i] = -GRB_INFINITY;
924  break;
925 
926  case GRB_GREATER_EQUAL:
927  lhs[i] = lpi->rhsarray[i];
928  break;
929 
930  default:
931  SCIPerrorMessage("invalid row sense\n");
932  SCIPABORT();
933  return SCIP_LPERROR;
934  }
935  }
936  return SCIP_OKAY;
937 }
938 
939 /** converts Gurobi's sen/rhs pairs into SCIP's lhs/rhs pairs, only storing the right hand side */
940 static
941 SCIP_RETCODE reconvertRhs(
942  SCIP_LPI* lpi, /**< LP interface structure */
943  int nrows, /**< number of rows */
944  SCIP_Real* rhs /**< buffer to store the right hand side vector */
945  )
946 {
947  int i;
948 
949  assert(lpi != NULL);
950  assert(nrows >= 0);
951  assert(rhs != NULL);
952 
953  for (i = 0; i < nrows; ++i)
954  {
955  switch( lpi->senarray[i] )
956  {
957  case GRB_EQUAL:
958  rhs[i] = lpi->rhsarray[i];
959  break;
960 
961  case GRB_LESS_EQUAL:
962  rhs[i] = lpi->rhsarray[i];
963  break;
964 
965  case GRB_GREATER_EQUAL:
966  rhs[i] = GRB_INFINITY;
967  break;
968 
969  default:
970  SCIPerrorMessage("invalid row sense\n");
971  SCIPABORT();
972  return SCIP_LPERROR;
973  }
974  }
975  return SCIP_OKAY;
976 }
977 
978 /** converts Gurobi's sen/rhs pairs into SCIP's lhs/rhs pairs */
979 static
980 SCIP_RETCODE reconvertSides(
981  SCIP_LPI* lpi, /**< LP interface structure */
982  int nrows, /**< number of rows */
983  SCIP_Real* lhs, /**< buffer to store the left hand side vector, or NULL */
984  SCIP_Real* rhs /**< buffer to store the right hand side vector, or NULL */
985  )
986 {
987  if( lhs != NULL && rhs != NULL )
988  {
989  SCIP_CALL( reconvertBothSides(lpi, nrows, lhs, rhs) );
990  }
991  else if( lhs != NULL )
992  {
993  SCIP_CALL( reconvertLhs(lpi, nrows, lhs) );
994  }
995  else if( rhs != NULL )
996  {
997  SCIP_CALL( reconvertRhs(lpi, nrows, rhs) );
998  }
999  return SCIP_OKAY;
1000 }
1001 
1002 /** after restoring old LP data, need to resolve the LP to be able to retrieve correct information */
1003 static
1004 SCIP_RETCODE restoreLPData(
1005  SCIP_LPI* lpi /**< LP interface structure */
1006  )
1007 {
1008  assert( lpi != NULL );
1009 
1010  /* set dual simplex */
1011  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, GRB_METHOD_DUAL) );
1012  CHECK_ZERO( lpi->messagehdlr, GRBoptimize(lpi->grbmodel) );
1013 
1014 #ifndef NDEBUG
1015  {
1016  double cnt;
1017 
1018  /* modifying the LP, restoring the old LP, and loading the old basis is not enough for Gurobi to be able to return
1019  * the basis -> we have to resolve the LP;
1020  *
1021  * In a numerical perfect world, GRB_REFACTORMAXITERS below should be zero. However, due to numerical inaccuracies
1022  * after refactorization, it might be necessary to do a few extra pivot steps.
1023  */
1024  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
1025  if ( cnt > (double) GRB_REFACTORMAXITERS )
1026  SCIPmessagePrintWarning(lpi->messagehdlr, "Gurobi needed %d iterations to restore optimal basis.\n", (int) cnt);
1027  }
1028 #endif
1029 
1030  return SCIP_OKAY;
1031 }
1032 
1033 
1034 
1035 /*
1036  * LP Interface Methods
1037  */
1038 
1039 
1040 /*
1041  * Miscellaneous Methods
1042  */
1043 
1044 static char grbname[100];
1045 
1046 /**@name Miscellaneous Methods */
1047 /**@{ */
1048 
1049 /** gets name and version of LP solver */
1051  void
1052  )
1053 {
1054  int major;
1055  int minor;
1056  int technical;
1057 
1058  GRBversion(&major, &minor, &technical);
1059  snprintf(grbname, 100, "Gurobi %d.%d.%d", major, minor, technical);
1060  return grbname;
1061 }
1062 
1063 /** gets description of LP solver (developer, webpage, ...) */
1065  void
1066  )
1067 {
1068  return "Linear Programming Solver developed by Gurobi Optimization (www.gurobi.com)";
1069 }
1070 
1071 /** gets pointer for LP solver - use only with great care
1072  *
1073  * Here we return the pointer to the model.
1074  */
1076  SCIP_LPI* lpi /**< pointer to an LP interface structure */
1077  )
1078 {
1079  return (void*) lpi->grbmodel;
1080 }
1081 /**@} */
1082 
1083 
1084 
1085 
1086 /*
1087  * LPI Creation and Destruction Methods
1088  */
1089 
1090 /**@name LPI Creation and Destruction Methods */
1091 /**@{ */
1092 
1093 /** creates an LP problem object */
1095  SCIP_LPI** lpi, /**< pointer to an LP interface structure */
1096  SCIP_MESSAGEHDLR* messagehdlr, /**< message handler to use for printing messages, or NULL */
1097  const char* name, /**< problem name */
1098  SCIP_OBJSEN objsen /**< objective sense */
1099  )
1100 {
1101  assert(sizeof(SCIP_Real) == sizeof(double)); /* Gurobi only works with doubles as floating points */
1102  assert(sizeof(SCIP_Bool) == sizeof(int)); /* Gurobi only works with ints as bools */
1103  assert(lpi != NULL);
1104  assert(numlp >= 0);
1105 
1106  SCIPdebugMessage("SCIPlpiCreate()\n");
1107 
1108  /* create environment
1109  *
1110  * Each problem will get a copy of the original environment. Thus, grbenv is only needed once.
1111  */
1112  if ( grbenv == NULL )
1113  {
1114  /* initialize environment - no log file */
1115  CHECK_ZERO( messagehdlr, GRBloadenv(&grbenv, NULL) );
1116 
1117  /* turn off output for all models */
1118  CHECK_ZERO( messagehdlr, GRBsetintparam(grbenv, GRB_INT_PAR_OUTPUTFLAG, 0) );
1119 
1120  /* turn on that basis information for infeasible and unbounded models is available */
1121  CHECK_ZERO( messagehdlr, GRBsetintparam(grbenv, GRB_INT_PAR_INFUNBDINFO, 1) );
1122  }
1123  assert( grbenv != NULL );
1124 
1125  /* create empty LPI */
1126  SCIP_ALLOC( BMSallocMemory(lpi) );
1127  CHECK_ZERO( messagehdlr, GRBnewmodel(grbenv, &(*lpi)->grbmodel, name, 0, NULL, NULL, NULL, NULL, NULL) );
1128 
1129  /* get local copy of environment */
1130  (*lpi)->grbenv = GRBgetenv((*lpi)->grbmodel);
1131  (*lpi)->senarray = NULL;
1132  (*lpi)->rhsarray = NULL;
1133  (*lpi)->valarray = NULL;
1134  (*lpi)->cstat = NULL;
1135  (*lpi)->rstat = NULL;
1136  (*lpi)->indarray = NULL;
1137  (*lpi)->sidechgsize = 0;
1138  (*lpi)->valsize = 0;
1139  (*lpi)->cstatsize = 0;
1140  (*lpi)->rstatsize = 0;
1141  (*lpi)->iterations = 0;
1142  (*lpi)->solisbasic = FALSE;
1143  (*lpi)->fromscratch = FALSE;
1144  (*lpi)->pricing = SCIP_PRICING_LPIDEFAULT;
1145  (*lpi)->messagehdlr = messagehdlr;
1146  invalidateSolution(*lpi);
1147 
1148  /* get default parameter values */
1149  SCIP_CALL( getParameterValues((*lpi), &((*lpi)->defparam)) );
1150  copyParameterValues(&((*lpi)->curparam), &((*lpi)->defparam));
1151  copyParameterValues(&((*lpi)->grbparam), &((*lpi)->defparam));
1152  ++numlp;
1153 
1154  /* set objective sense */
1155  SCIP_CALL( SCIPlpiChgObjsen(*lpi, objsen) );
1156 
1157  /* set default pricing */
1158  SCIP_CALL( SCIPlpiSetIntpar(*lpi, SCIP_LPPAR_PRICING, (*lpi)->pricing) );
1159 
1160  SCIPmessagePrintWarning(messagehdlr, "The Gurobi LPI is a beta version only - use with care.\n");
1161 
1162  return SCIP_OKAY;
1163 }
1164 
1165 /** deletes an LP problem object */
1167  SCIP_LPI** lpi /**< pointer to an LP interface structure */
1168  )
1169 {
1170  assert(grbenv != NULL);
1171  assert(lpi != NULL);
1172  assert(*lpi != NULL);
1173 
1174  SCIPdebugMessage("SCIPlpiFree()\n");
1175 
1176  /* free model */
1177  CHECK_ZERO( (*lpi)->messagehdlr, GRBfreemodel((*lpi)->grbmodel) );
1178 
1179  /* free memory */
1180  BMSfreeMemoryArrayNull(&(*lpi)->senarray);
1181  BMSfreeMemoryArrayNull(&(*lpi)->rhsarray);
1182  BMSfreeMemoryArrayNull(&(*lpi)->cstat);
1183  BMSfreeMemoryArrayNull(&(*lpi)->rstat);
1184  BMSfreeMemory(lpi);
1185 
1186  /* free environment */
1187  --numlp;
1188  if( numlp == 0 )
1189  {
1190  GRBfreeenv(grbenv);
1191  grbenv = NULL;
1192  }
1193 
1194  return SCIP_OKAY;
1195 }
1196 
1197 /**@} */
1198 
1199 
1200 
1201 
1202 /*
1203  * Modification Methods
1204  */
1205 
1206 /**@name Modification Methods */
1207 /**@{ */
1208 
1209 /** copies LP data with column matrix into LP solver */
1211  SCIP_LPI* lpi, /**< LP interface structure */
1212  SCIP_OBJSEN objsen, /**< objective sense */
1213  int ncols, /**< number of columns */
1214  const SCIP_Real* obj, /**< objective function values of columns */
1215  const SCIP_Real* lb, /**< lower bounds of columns */
1216  const SCIP_Real* ub, /**< upper bounds of columns */
1217  char** colnames, /**< column names, or NULL */
1218  int nrows, /**< number of rows */
1219  const SCIP_Real* lhs, /**< left hand sides of rows */
1220  const SCIP_Real* rhs, /**< right hand sides of rows */
1221  char** rownames, /**< row names, or NULL */
1222  int nnonz, /**< number of nonzero elements in the constraint matrix */
1223  const int* beg, /**< start index of each column in ind- and val-array */
1224  const int* ind, /**< row indices of constraint matrix entries */
1225  const SCIP_Real* val /**< values of constraint matrix entries */
1226  )
1227 {
1228  int* cnt;
1229  int rngcount;
1230  int c;
1231 
1232  assert(lpi != NULL);
1233  assert(lpi->grbmodel != NULL);
1234  assert(lpi->grbenv != NULL);
1235  assert(objsen == SCIP_OBJSEN_MAXIMIZE || objsen == SCIP_OBJSEN_MINIMIZE);
1236 
1237  SCIPdebugMessage("loading LP in column format into Gurobi: %d cols, %d rows\n", ncols, nrows);
1238 
1239  invalidateSolution(lpi);
1240 
1241  SCIP_CALL( ensureSidechgMem(lpi, nrows) );
1242 
1243  /* convert lhs/rhs into sen/rhs/range tuples */
1244  SCIP_CALL( convertSides(lpi, nrows, lhs, rhs, &rngcount) );
1245  assert( rngcount == 0 );
1246 
1247  /* calculate column lengths */
1248  SCIP_ALLOC( BMSallocMemoryArray(&cnt, ncols) );
1249  for( c = 0; c < ncols-1; ++c )
1250  {
1251  cnt[c] = beg[c+1] - beg[c];
1252  assert(cnt[c] >= 0);
1253  }
1254  cnt[ncols-1] = nnonz - beg[ncols-1];
1255  assert(cnt[ncols-1] >= 0);
1256 
1257  /* delete model */
1258  assert( lpi->grbmodel != NULL );
1259  CHECK_ZERO( lpi->messagehdlr, GRBfreemodel(lpi->grbmodel) );
1260 
1261  /* load model - all variables are continuous */
1262  CHECK_ZERO( lpi->messagehdlr, GRBloadmodel(lpi->grbenv, &(lpi->grbmodel), NULL, ncols, nrows, objsen, 0.0, (SCIP_Real*)obj,
1263  lpi->senarray, lpi->rhsarray, (int*)beg, cnt, (int*)ind, (SCIP_Real*)val, (SCIP_Real*)lb, (SCIP_Real*)ub, NULL, colnames, rownames) );
1264  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1265 
1266  /* free temporary memory */
1267  BMSfreeMemoryArray(&cnt);
1268 
1269 #ifndef NDEBUG
1270  {
1271  int temp;
1272 
1273  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &temp) );
1274  assert( temp == ncols);
1275 
1276  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &temp) );
1277  assert( temp == nrows);
1278 
1279  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMNZS, &temp) );
1280  assert( temp == nnonz);
1281  }
1282 #endif
1283 
1284  return SCIP_OKAY;
1285 }
1286 
1287 /** adds columns to the LP */
1289  SCIP_LPI* lpi, /**< LP interface structure */
1290  int ncols, /**< number of columns to be added */
1291  const SCIP_Real* obj, /**< objective function values of new columns */
1292  const SCIP_Real* lb, /**< lower bounds of new columns */
1293  const SCIP_Real* ub, /**< upper bounds of new columns */
1294  char** colnames, /**< column names, or NULL */
1295  int nnonz, /**< number of nonzero elements to be added to the constraint matrix */
1296  const int* beg, /**< start index of each column in ind- and val-array, or NULL if nnonz == 0 */
1297  const int* ind, /**< row indices of constraint matrix entries, or NULL if nnonz == 0 */
1298  const SCIP_Real* val /**< values of constraint matrix entries, or NULL if nnonz == 0 */
1299  )
1300 {
1301  assert(lpi != NULL);
1302  assert(lpi->grbmodel != NULL);
1303 
1304  SCIPdebugMessage("adding %d columns with %d nonzeros to Gurobi\n", ncols, nnonz);
1305 
1306  invalidateSolution(lpi);
1307 
1308  /* add columns - all new variables are continuous */
1309  CHECK_ZERO( lpi->messagehdlr, GRBaddvars(lpi->grbmodel, ncols, nnonz, (int*)beg, (int*)ind, (SCIP_Real*)val, (SCIP_Real*)obj, (SCIP_Real*)lb, (SCIP_Real*)ub, NULL, colnames) )
1310  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1311 
1312  return SCIP_OKAY;
1313 }
1314 
1315 /** deletes all columns in the given range from LP */
1317  SCIP_LPI* lpi, /**< LP interface structure */
1318  int firstcol, /**< first column to be deleted */
1319  int lastcol /**< last column to be deleted */
1320  )
1321 {
1322  int j;
1323  int* which;
1324 
1325  assert(lpi != NULL);
1326  assert(lpi->grbmodel != NULL);
1327 #ifndef NDEBUG
1328  {
1329  int temp;
1330 
1331  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &temp) );
1332  assert(0 <= firstcol && firstcol <= lastcol && lastcol < temp);
1333  }
1334 #endif
1335 
1336  SCIPdebugMessage("deleting %d columns from Gurobi\n", lastcol - firstcol + 1);
1337 
1338  invalidateSolution(lpi);
1339 
1340  /* Gurobi can't delete a range of columns, we have to set up an index array */
1341  SCIP_ALLOC( BMSallocMemoryArray(&which, lastcol-firstcol+1) );;
1342  for( j = firstcol; j <= lastcol; ++j )
1343  which[j - firstcol] = j;
1344 
1345  CHECK_ZERO( lpi->messagehdlr, GRBdelvars(lpi->grbmodel, lastcol-firstcol+1, which) );
1346  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1347 
1348  BMSfreeMemoryArray( &which );
1349 
1350  return SCIP_OKAY;
1351 }
1352 
1353 /** deletes columns from SCIP_LP; the new position of a column must not be greater that its old position */
1355  SCIP_LPI* lpi, /**< LP interface structure */
1356  int* dstat /**< deletion status of columns
1357  * input: 1 if column should be deleted, 0 if not
1358  * output: new position of column, -1 if column was deleted */
1359  )
1360 {
1361  int j, nvars, num;
1362  int* which;
1363 
1364  assert(lpi != NULL);
1365  assert(lpi->grbmodel != NULL);
1366 
1367  SCIPdebugMessage("deleting a column set from Gurobi\n");
1368 
1369  invalidateSolution(lpi);
1370 
1371  /* Gurobi can't delete a range of columns, we have to set up an index array */
1372  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &nvars) );
1373 
1374  SCIP_ALLOC( BMSallocMemoryArray(&which, nvars) );;
1375  num = 0;
1376  for( j = 0; j < nvars; ++j )
1377  {
1378  if( dstat[j] )
1379  which[num++] = j;
1380  }
1381  CHECK_ZERO( lpi->messagehdlr, GRBdelvars(lpi->grbmodel, num, which) );
1382  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1383 
1384  BMSfreeMemoryArray( &which );
1385 
1386  return SCIP_OKAY;
1387 }
1388 
1389 /** adds rows to the LP */
1391  SCIP_LPI* lpi, /**< LP interface structure */
1392  int nrows, /**< number of rows to be added */
1393  const SCIP_Real* lhs, /**< left hand sides of new rows */
1394  const SCIP_Real* rhs, /**< right hand sides of new rows */
1395  char** rownames, /**< row names, or NULL */
1396  int nnonz, /**< number of nonzero elements to be added to the constraint matrix */
1397  const int* beg, /**< start index of each row in ind- and val-array, or NULL if nnonz == 0 */
1398  const int* ind, /**< column indices of constraint matrix entries, or NULL if nnonz == 0 */
1399  const SCIP_Real* val /**< values of constraint matrix entries, or NULL if nnonz == 0 */
1400  )
1401 {
1402  int rngcount;
1403 
1404  assert(lpi != NULL);
1405  assert(lpi->grbmodel != NULL);
1406 
1407  SCIPdebugMessage("adding %d rows with %d nonzeros to Gurobi\n", nrows, nnonz);
1408 
1409  invalidateSolution(lpi);
1410 
1411  SCIP_CALL( ensureSidechgMem(lpi, nrows) );
1412 
1413  /* convert lhs/rhs into sen/rhs/range tuples */
1414  SCIP_CALL( convertSides(lpi, nrows, lhs, rhs, &rngcount) );
1415  assert( rngcount == 0 );
1416 
1417  /* add rows to LP */
1418  CHECK_ZERO( lpi->messagehdlr, GRBaddconstrs(lpi->grbmodel, nrows, nnonz, (int*)beg, (int*)ind, (SCIP_Real*)val, lpi->senarray, lpi->rhsarray, rownames) );
1419  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1420 
1421  return SCIP_OKAY;
1422 }
1423 
1424 /** deletes all rows in the given range from LP */
1426  SCIP_LPI* lpi, /**< LP interface structure */
1427  int firstrow, /**< first row to be deleted */
1428  int lastrow /**< last row to be deleted */
1429  )
1430 {
1431  int i;
1432  int* which;
1433 
1434  assert(lpi != NULL);
1435  assert(lpi->grbmodel != NULL);
1436 #ifndef NDEBUG
1437  {
1438  int nrows;
1439  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
1440  assert(0 <= firstrow && firstrow <= lastrow && lastrow < nrows);
1441  }
1442 #endif
1443 
1444  SCIPdebugMessage("deleting %d rows from Gurobi\n", lastrow - firstrow + 1);
1445 
1446  invalidateSolution(lpi);
1447 
1448  /* Gurobi can't delete a range of rows, we have to set up an index array */
1449  SCIP_ALLOC( BMSallocMemoryArray(&which, lastrow-firstrow+1) );;
1450  for( i = firstrow; i <= lastrow; ++i )
1451  which[i - firstrow] = i;
1452 
1453  CHECK_ZERO( lpi->messagehdlr, GRBdelconstrs(lpi->grbmodel, lastrow-firstrow+1, which) );
1454  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1455 
1456  BMSfreeMemoryArray( &which );
1457 
1458  return SCIP_OKAY;
1459 }
1460 
1461 /** deletes rows from SCIP_LP; the new position of a row must not be greater that its old position */
1463  SCIP_LPI* lpi, /**< LP interface structure */
1464  int* dstat /**< deletion status of rows
1465  * input: 1 if row should be deleted, 0 if not
1466  * output: new position of row, -1 if row was deleted */
1467  )
1468 {
1469  int i, num;
1470  int nrows;
1471  int* which;
1472 
1473  assert(lpi != NULL);
1474  assert(lpi->grbmodel != NULL);
1475 
1476  SCIPdebugMessage("deleting a row set from Gurobi\n");
1477 
1478  invalidateSolution(lpi);
1479 
1480  /* Gurobi can't delete a range of rows, we have to set up an index array */
1481  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
1482  SCIP_ALLOC( BMSallocMemoryArray(&which, nrows) );;
1483  num = 0;
1484  for( i = 0; i < nrows; ++i )
1485  {
1486  if( dstat[i] )
1487  which[num++] = i;
1488  }
1489  CHECK_ZERO( lpi->messagehdlr, GRBdelconstrs(lpi->grbmodel, num, which) );
1490  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1491 
1492  /* update dstat */
1493  num = 0;
1494  for( i = 0; i < nrows; ++i )
1495  {
1496  if( dstat[i] )
1497  {
1498  dstat[i] = -1;
1499  ++num;
1500  }
1501  else
1502  dstat[i] = i - num;
1503  }
1504 
1505  BMSfreeMemoryArray( &which );
1506 
1507  return SCIP_OKAY;
1508 }
1509 
1510 /** clears the whole LP */
1512  SCIP_LPI* lpi /**< LP interface structure */
1513  )
1514 {
1515  assert( lpi != NULL );
1516  assert( lpi->grbmodel != NULL );
1517  assert( lpi->grbenv != NULL );
1518 
1519  SCIPdebugMessage("clearing Gurobi LP\n");
1520 
1521  invalidateSolution(lpi);
1522 
1523  CHECK_ZERO( lpi->messagehdlr, GRBfreemodel(lpi->grbmodel) );
1524  CHECK_ZERO( lpi->messagehdlr, GRBnewmodel(lpi->grbenv, &(lpi->grbmodel), "", 0, NULL, NULL, NULL, NULL, NULL) );
1525  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1526 
1527  return SCIP_OKAY;
1528 }
1529 
1530 /** changes lower and upper bounds of columns */
1532  SCIP_LPI* lpi, /**< LP interface structure */
1533  int ncols, /**< number of columns to change bounds for */
1534  const int* ind, /**< column indices */
1535  const SCIP_Real* lb, /**< values for the new lower bounds */
1536  const SCIP_Real* ub /**< values for the new upper bounds */
1537  )
1538 {
1539  assert(lpi != NULL);
1540  assert(lpi->grbmodel != NULL);
1541 
1542  SCIPdebugMessage("changing %d bounds in Gurobi\n", ncols);
1543 #ifdef SCIP_DEBUG
1544  {
1545  int i;
1546  for( i = 0; i < ncols; ++i )
1547  SCIPdebugPrintf(" col %d: [%g,%g]\n", ind[i], lb[i], ub[i]);
1548  }
1549 #endif
1550 
1551  invalidateSolution(lpi);
1552 
1553  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrlist(lpi->grbmodel, GRB_DBL_ATTR_LB, ncols, (int*)ind, (SCIP_Real*)lb) );
1554  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrlist(lpi->grbmodel, GRB_DBL_ATTR_UB, ncols, (int*)ind, (SCIP_Real*)ub) );
1555 
1556  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1557 
1558  return SCIP_OKAY;
1559 }
1560 
1561 /** changes left and right hand sides of rows */
1563  SCIP_LPI* lpi, /**< LP interface structure */
1564  int nrows, /**< number of rows to change sides for */
1565  const int* ind, /**< row indices */
1566  const SCIP_Real* lhs, /**< new values for left hand sides */
1567  const SCIP_Real* rhs /**< new values for right hand sides */
1568  )
1569 {
1570  int rngcount;
1571 
1572  assert(lpi != NULL);
1573  assert(lpi->grbmodel != NULL);
1574 
1575  SCIPdebugMessage("changing %d sides in Gurobi\n", nrows);
1576 
1577  invalidateSolution(lpi);
1578 
1579  /* convert lhs/rhs into sen/rhs/range tuples */
1580  SCIP_CALL( ensureSidechgMem(lpi, nrows) );
1581  SCIP_CALL( convertSides(lpi, nrows, lhs, rhs, &rngcount) );
1582  assert( rngcount == 0 );
1583 
1584  /* change row sides */
1585  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrlist(lpi->grbmodel, GRB_DBL_ATTR_RHS, nrows, (int*)ind, lpi->rhsarray) );
1586  CHECK_ZERO( lpi->messagehdlr, GRBsetcharattrlist(lpi->grbmodel, GRB_CHAR_ATTR_SENSE, nrows, (int*)ind, lpi->senarray) );
1587 
1588  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1589 
1590  return SCIP_OKAY;
1591 }
1592 
1593 /** changes a single coefficient */
1595  SCIP_LPI* lpi, /**< LP interface structure */
1596  int row, /**< row number of coefficient to change */
1597  int col, /**< column number of coefficient to change */
1598  SCIP_Real newval /**< new value of coefficient */
1599  )
1600 {
1601  assert(lpi != NULL);
1602  assert(lpi->grbmodel != NULL);
1603 
1604  SCIPdebugMessage("changing coefficient row %d, column %d in Gurobi to %g\n", row, col, newval);
1605 
1606  invalidateSolution(lpi);
1607 
1608  CHECK_ZERO( lpi->messagehdlr, GRBchgcoeffs(lpi->grbmodel, 1, &row, &col, &newval) );
1609  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1610 
1611  return SCIP_OKAY;
1612 }
1613 
1614 /** changes the objective sense */
1616  SCIP_LPI* lpi, /**< LP interface structure */
1617  SCIP_OBJSEN objsen /**< new objective sense */
1618  )
1619 {
1620  assert(lpi != NULL);
1621  assert(lpi->grbmodel != NULL);
1622  assert(objsen == SCIP_OBJSEN_MAXIMIZE || objsen == SCIP_OBJSEN_MINIMIZE);
1623 
1624  SCIPdebugMessage("changing objective sense in Gurobi to %d\n", objsen);
1625 
1626  invalidateSolution(lpi);
1627 
1628  /* The objective sense of Gurobi and SCIP are equal */
1629  CHECK_ZERO( lpi->messagehdlr, GRBsetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, objsen) );
1630 
1631  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1632 
1633  return SCIP_OKAY;
1634 }
1635 
1636 /** changes objective values of columns in the LP */
1638  SCIP_LPI* lpi, /**< LP interface structure */
1639  int ncols, /**< number of columns to change objective value for */
1640  int* ind, /**< column indices to change objective value for */
1641  SCIP_Real* obj /**< new objective values for columns */
1642  )
1643 {
1644  assert(lpi != NULL);
1645  assert(lpi->grbmodel != NULL);
1646 
1647  SCIPdebugMessage("changing %d objective values in Gurobi\n", ncols);
1648 
1649  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrlist(lpi->grbmodel, GRB_DBL_ATTR_OBJ, ncols, ind, obj) );
1650 
1651  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
1652 
1653  return SCIP_OKAY;
1654 }
1655 
1656 /** multiplies a row with a non-zero scalar; for negative scalars, the row's sense is switched accordingly */
1658  SCIP_LPI* lpi, /**< LP interface structure */
1659  int row, /**< row number to scale */
1660  SCIP_Real scaleval /**< scaling multiplier */
1661  )
1662 {
1663  SCIP_Real lhs;
1664  SCIP_Real rhs;
1665  int nnonz;
1666  int ncols;
1667  int beg;
1668  int i;
1669 
1670  assert(lpi != NULL);
1671  assert(lpi->grbmodel != NULL);
1672  assert(scaleval != 0.0);
1673 
1674  SCIPdebugMessage("scaling row %d with factor %g in Gurobi\n", row, scaleval);
1675 
1676  invalidateSolution(lpi);
1677 
1678  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
1679  SCIP_CALL( ensureValMem(lpi, ncols) );
1680 
1681  /* get the row */
1682  SCIP_CALL( SCIPlpiGetRows(lpi, row, row, &lhs, &rhs, &nnonz, &beg, lpi->indarray, lpi->valarray) );
1683 
1684  /* scale row coefficients */
1685  for( i = 0; i < nnonz; ++i )
1686  {
1687  SCIP_CALL( SCIPlpiChgCoef(lpi, row, lpi->indarray[i], lpi->valarray[i] * scaleval) );
1688  }
1689 
1690  /* scale row sides */
1691  if( lhs > -GRB_INFINITY )
1692  lhs *= scaleval;
1693  else if( scaleval < 0.0 )
1694  lhs = GRB_INFINITY;
1695  if( rhs < GRB_INFINITY )
1696  rhs *= scaleval;
1697  else if( scaleval < 0.0 )
1698  rhs = -GRB_INFINITY;
1699  if( scaleval > 0.0 )
1700  {
1701  SCIP_CALL( SCIPlpiChgSides(lpi, 1, &row, &lhs, &rhs) );
1702  }
1703  else
1704  {
1705  SCIP_CALL( SCIPlpiChgSides(lpi, 1, &row, &rhs, &lhs) );
1706  }
1707 
1708  return SCIP_OKAY;
1709 }
1710 
1711 /** multiplies a column with a non-zero scalar; the objective value is multiplied with the scalar, and the bounds
1712  * are divided by the scalar; for negative scalars, the column's bounds are switched
1713  */
1715  SCIP_LPI* lpi, /**< LP interface structure */
1716  int col, /**< column number to scale */
1717  SCIP_Real scaleval /**< scaling multiplier */
1718  )
1719 {
1720  SCIP_Real lb;
1721  SCIP_Real ub;
1722  SCIP_Real obj;
1723  int nnonz;
1724  int ncols;
1725  int beg;
1726  int i;
1727 
1728  assert(lpi != NULL);
1729  assert(lpi->grbmodel != NULL);
1730  assert(scaleval != 0.0);
1731 
1732  SCIPdebugMessage("scaling column %d with factor %g in Gurobi\n", col, scaleval);
1733 
1734  invalidateSolution(lpi);
1735 
1736  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
1737  SCIP_CALL( ensureValMem(lpi, ncols) );
1738 
1739  /* get the column */
1740  SCIP_CALL( SCIPlpiGetCols(lpi, col, col, &lb, &ub, &nnonz, &beg, lpi->indarray, lpi->valarray) );
1741 
1742  /* get objective coefficient */
1743  SCIP_CALL( SCIPlpiGetObj(lpi, col, col, &obj) );
1744 
1745  /* scale column coefficients */
1746  for( i = 0; i < nnonz; ++i )
1747  {
1748  SCIP_CALL( SCIPlpiChgCoef(lpi, lpi->indarray[i], col, lpi->valarray[i] * scaleval) );
1749  }
1750 
1751  /* scale objective value */
1752  obj *= scaleval;
1753  SCIP_CALL( SCIPlpiChgObj(lpi, 1, &col, &obj) );
1754 
1755  /* scale column bounds */
1756  if( lb > -GRB_INFINITY )
1757  lb /= scaleval;
1758  else if( scaleval < 0.0 )
1759  lb = GRB_INFINITY;
1760  if( ub < GRB_INFINITY )
1761  ub /= scaleval;
1762  else if( scaleval < 0.0 )
1763  ub = -GRB_INFINITY;
1764  if( scaleval > 0.0 )
1765  {
1766  SCIP_CALL( SCIPlpiChgBounds(lpi, 1, &col, &lb, &ub) );
1767  }
1768  else
1769  {
1770  SCIP_CALL( SCIPlpiChgBounds(lpi, 1, &col, &ub, &lb) );
1771  }
1772 
1773  return SCIP_OKAY;
1774 }
1775 
1776 /**@} */
1777 
1778 
1779 
1780 
1781 /*
1782  * Data Accessing Methods
1783  */
1784 
1785 /**@name Data Accessing Methods */
1786 /**@{ */
1787 
1788 /** gets the number of rows in the LP */
1790  SCIP_LPI* lpi, /**< LP interface structure */
1791  int* nrows /**< pointer to store the number of rows */
1792  )
1793 {
1794  assert(lpi != NULL);
1795  assert(nrows != NULL);
1796 
1797  SCIPdebugMessage("getting number of rows\n");
1798 
1799  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, nrows) );
1800 
1801  return SCIP_OKAY;
1802 }
1803 
1804 /** gets the number of columns in the LP */
1806  SCIP_LPI* lpi, /**< LP interface structure */
1807  int* ncols /**< pointer to store the number of cols */
1808  )
1809 {
1810  assert(lpi != NULL);
1811  assert(ncols != NULL);
1812 
1813  SCIPdebugMessage("getting number of columns\n");
1814 
1815  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, ncols) );
1816 
1817  return SCIP_OKAY;
1818 }
1819 
1820 /** gets the number of nonzero elements in the LP constraint matrix */
1822  SCIP_LPI* lpi, /**< LP interface structure */
1823  int* nnonz /**< pointer to store the number of nonzeros */
1824  )
1825 {
1826  assert(lpi != NULL);
1827  assert(nnonz != NULL);
1828 
1829  SCIPdebugMessage("getting number of non-zeros\n");
1830 
1831  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMNZS, nnonz) );
1832 
1833  return SCIP_OKAY;
1834 }
1835 
1836 /** gets columns from LP problem object; the arrays have to be large enough to store all values
1837  * Either both, lb and ub, have to be NULL, or both have to be non-NULL,
1838  * either nnonz, beg, ind, and val have to be NULL, or all of them have to be non-NULL.
1839  */
1841  SCIP_LPI* lpi, /**< LP interface structure */
1842  int firstcol, /**< first column to get from LP */
1843  int lastcol, /**< last column to get from LP */
1844  SCIP_Real* lb, /**< buffer to store the lower bound vector, or NULL */
1845  SCIP_Real* ub, /**< buffer to store the upper bound vector, or NULL */
1846  int* nnonz, /**< pointer to store the number of nonzero elements returned, or NULL */
1847  int* beg, /**< buffer to store start index of each column in ind- and val-array, or NULL */
1848  int* ind, /**< buffer to store column indices of constraint matrix entries, or NULL */
1849  SCIP_Real* val /**< buffer to store values of constraint matrix entries, or NULL */
1850  )
1851 {
1852  assert(lpi != NULL);
1853  assert(lpi->grbmodel != NULL);
1854 #ifndef NDEBUG
1855  {
1856  int ncols;
1857  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
1858  assert(0 <= firstcol && firstcol <= lastcol && lastcol < ncols);
1859  }
1860 #endif
1861 
1862  SCIPdebugMessage("getting columns %d to %d\n", firstcol, lastcol);
1863 
1864  if( lb != NULL )
1865  {
1866  assert(ub != NULL);
1867 
1868  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_LB, firstcol, lastcol-firstcol+1, lb) );
1869  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_UB, firstcol, lastcol-firstcol+1, ub) );
1870  }
1871  else
1872  assert(ub == NULL);
1873 
1874  if( nnonz != NULL )
1875  {
1876  assert(beg != NULL);
1877  assert(ind != NULL);
1878  assert(val != NULL);
1879 
1880  /* get matrix entries */
1881  CHECK_ZERO( lpi->messagehdlr, GRBgetvars(lpi->grbmodel, nnonz, beg, ind, val, firstcol, lastcol-firstcol+1) )
1882  }
1883  else
1884  {
1885  assert(beg == NULL);
1886  assert(ind == NULL);
1887  assert(val == NULL);
1888  }
1889 
1890  return SCIP_OKAY;
1891 }
1892 
1893 /** gets rows from LP problem object; the arrays have to be large enough to store all values.
1894  * Either both, lhs and rhs, have to be NULL, or both have to be non-NULL,
1895  * either nnonz, beg, ind, and val have to be NULL, or all of them have to be non-NULL.
1896  */
1898  SCIP_LPI* lpi, /**< LP interface structure */
1899  int firstrow, /**< first row to get from LP */
1900  int lastrow, /**< last row to get from LP */
1901  SCIP_Real* lhs, /**< buffer to store left hand side vector, or NULL */
1902  SCIP_Real* rhs, /**< buffer to store right hand side vector, or NULL */
1903  int* nnonz, /**< pointer to store the number of nonzero elements returned, or NULL */
1904  int* beg, /**< buffer to store start index of each row in ind- and val-array, or NULL */
1905  int* ind, /**< buffer to store row indices of constraint matrix entries, or NULL */
1906  SCIP_Real* val /**< buffer to store values of constraint matrix entries, or NULL */
1907  )
1908 {
1909  assert(lpi != NULL);
1910  assert(lpi->grbmodel != NULL);
1911 #ifndef NDEBUG
1912  {
1913  int nrows;
1914  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
1915  assert(0 <= firstrow && firstrow <= lastrow && lastrow < nrows);
1916  }
1917 #endif
1918 
1919  SCIPdebugMessage("getting rows %d to %d\n", firstrow, lastrow);
1920 
1921  if( lhs != NULL || rhs != NULL )
1922  {
1923  /* get row sense and rhs */
1924  SCIP_CALL( ensureSidechgMem(lpi, lastrow - firstrow + 1) );
1925  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_RHS, firstrow, lastrow-firstrow+1, lpi->rhsarray) );
1926  CHECK_ZERO( lpi->messagehdlr, GRBgetcharattrarray(lpi->grbmodel, GRB_CHAR_ATTR_SENSE, firstrow, lastrow-firstrow+1, lpi->senarray) );
1927 
1928  /* convert sen and rhs into lhs/rhs tuples */
1929  SCIP_CALL( reconvertSides(lpi, lastrow - firstrow + 1, lhs, rhs) );
1930  }
1931 
1932  if( nnonz != NULL )
1933  {
1934  assert(beg != NULL);
1935  assert(ind != NULL);
1936  assert(val != NULL);
1937 
1938  /* get matrix entries */
1939  CHECK_ZERO( lpi->messagehdlr, GRBgetconstrs(lpi->grbmodel, nnonz, beg, ind, val, firstrow, lastrow-firstrow+1) );
1940  }
1941  else
1942  {
1943  assert(beg == NULL);
1944  assert(ind == NULL);
1945  assert(val == NULL);
1946  }
1947 
1948  return SCIP_OKAY;
1949 }
1950 
1951 /** gets column names */
1953  SCIP_LPI* lpi, /**< LP interface structure */
1954  int firstcol, /**< first column to get name from LP */
1955  int lastcol, /**< last column to get name from LP */
1956  char** colnames, /**< pointers to column names (of size at least lastcol-firstcol+1) */
1957  char* namestorage, /**< storage for col names */
1958  int namestoragesize, /**< size of namestorage (if 0, storageleft returns the storage needed) */
1959  int* storageleft /**< amount of storage left (if < 0 the namestorage was not big enough) */
1960  )
1961 {
1962  SCIPerrorMessage("SCIPlpiGetColNames() has not been implemented yet.\n");
1963  return SCIP_LPERROR;
1964 }
1965 
1966 /** gets row names */
1968  SCIP_LPI* lpi, /**< LP interface structure */
1969  int firstrow, /**< first row to get name from LP */
1970  int lastrow, /**< last row to get name from LP */
1971  char** rownames, /**< pointers to row names (of size at least lastrow-firstrow+1) */
1972  char* namestorage, /**< storage for row names */
1973  int namestoragesize, /**< size of namestorage (if 0, -storageleft returns the storage needed) */
1974  int* storageleft /**< amount of storage left (if < 0 the namestorage was not big enough) */
1975  )
1976 {
1977  SCIPerrorMessage("SCIPlpiGetRowNames() has not been implemented yet.\n");
1978  return SCIP_LPERROR;
1979 }
1980 
1981 /** gets the objective sense of the LP */
1983  SCIP_LPI* lpi, /**< LP interface structure */
1984  SCIP_OBJSEN* objsen /**< pointer to store objective sense */
1985  )
1986 {
1987  int grbobjsen;
1988 
1989  assert( lpi != NULL );
1990  assert( lpi->grbmodel != NULL );
1991  assert( objsen != NULL );
1992 
1993  SCIPdebugMessage("getting objective sense\n");
1994 
1995  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &grbobjsen) );
1996  assert(grbobjsen == GRB_MINIMIZE || grbobjsen == GRB_MAXIMIZE);
1997 
1998  *objsen = (grbobjsen == GRB_MINIMIZE) ? SCIP_OBJSEN_MINIMIZE : SCIP_OBJSEN_MAXIMIZE;
1999 
2000  return SCIP_OKAY;
2001 }
2002 
2003 /** gets objective coefficients from LP problem object */
2005  SCIP_LPI* lpi, /**< LP interface structure */
2006  int firstcol, /**< first column to get objective coefficient for */
2007  int lastcol, /**< last column to get objective coefficient for */
2008  SCIP_Real* vals /**< array to store objective coefficients */
2009  )
2010 {
2011  assert(lpi != NULL);
2012  assert(lpi->grbmodel != NULL);
2013  assert(firstcol <= lastcol);
2014  assert(vals != NULL);
2015 
2016  SCIPdebugMessage("getting objective values %d to %d\n", firstcol, lastcol);
2017 
2018  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_OBJ, firstcol, lastcol-firstcol+1, vals) );
2019 
2020  return SCIP_OKAY;
2021 }
2022 
2023 /** gets current bounds from LP problem object */
2025  SCIP_LPI* lpi, /**< LP interface structure */
2026  int firstcol, /**< first column to get bounds for */
2027  int lastcol, /**< last column to get bounds for */
2028  SCIP_Real* lbs, /**< array to store lower bound values, or NULL */
2029  SCIP_Real* ubs /**< array to store upper bound values, or NULL */
2030  )
2031 {
2032  assert(lpi != NULL);
2033  assert(lpi->grbmodel != NULL);
2034 #ifndef NDEBUG
2035  {
2036  int ncols;
2037  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
2038  assert(0 <= firstcol && firstcol <= lastcol && lastcol < ncols);
2039  }
2040 #endif
2041 
2042  SCIPdebugMessage("getting bounds %d to %d\n", firstcol, lastcol);
2043 
2044  if( lbs != NULL )
2045  {
2046  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_LB, firstcol, lastcol-firstcol+1, lbs) );
2047  }
2048 
2049  if( ubs != NULL )
2050  {
2051  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_UB, firstcol, lastcol-firstcol+1, ubs) );
2052  }
2053 
2054  return SCIP_OKAY;
2055 }
2056 
2057 /** gets current row sides from LP problem object */
2059  SCIP_LPI* lpi, /**< LP interface structure */
2060  int firstrow, /**< first row to get sides for */
2061  int lastrow, /**< last row to get sides for */
2062  SCIP_Real* lhss, /**< array to store left hand side values, or NULL */
2063  SCIP_Real* rhss /**< array to store right hand side values, or NULL */
2064  )
2065 {
2066  assert(lpi != NULL);
2067  assert(lpi->grbmodel != NULL);
2068  assert(firstrow <= lastrow);
2069 
2070  SCIPdebugMessage("getting row sides %d to %d\n", firstrow, lastrow);
2071 
2072  /* get row sense, rhs, and ranges */
2073  SCIP_CALL( ensureSidechgMem(lpi, lastrow - firstrow + 1) );
2074 
2075  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_RHS, firstrow, lastrow-firstrow+1, lpi->rhsarray) );
2076  CHECK_ZERO( lpi->messagehdlr, GRBgetcharattrarray(lpi->grbmodel, GRB_CHAR_ATTR_SENSE, firstrow, lastrow-firstrow+1, lpi->senarray) );
2077 
2078  /* convert sen and rhs into lhs/rhs tuples */
2079  SCIP_CALL( reconvertSides(lpi, lastrow - firstrow + 1, lhss, rhss) );
2080 
2081  return SCIP_OKAY;
2082 }
2083 
2084 /** gets a single coefficient */
2086  SCIP_LPI* lpi, /**< LP interface structure */
2087  int row, /**< row number of coefficient */
2088  int col, /**< column number of coefficient */
2089  SCIP_Real* val /**< pointer to store the value of the coefficient */
2090  )
2091 {
2092  assert(lpi != NULL);
2093  assert(lpi->grbmodel != NULL);
2094 
2095  SCIPdebugMessage("getting coefficient of row %d col %d\n", row, col);
2096 
2097  CHECK_ZERO( lpi->messagehdlr, GRBgetcoeff(lpi->grbmodel, row, col, val) );
2098 
2099  return SCIP_OKAY;
2100 }
2101 
2102 /**@} */
2103 
2104 
2105 
2106 
2107 /*
2108  * Solving Methods
2109  */
2110 
2111 /**@name Solving Methods */
2112 /**@{ */
2113 
2114 /** calls primal simplex to solve the LP
2115  *
2116  * @todo Check concurrent (GRB_METHOD_CONCURRENT or GRB_METHOD_DETERMINISTIC_CONCURRENT)
2117  */
2119  SCIP_LPI* lpi /**< LP interface structure */
2120  )
2121 {
2122  double cnt;
2123  int retval;
2124  int primalfeasible;
2125  int dualfeasible;
2126 
2127  assert( lpi != NULL );
2128  assert( lpi->grbmodel != NULL );
2129  assert( lpi->grbenv != NULL );
2130 
2131 #ifdef SCIP_DEBUG
2132  {
2133  int ncols, nrows;
2134  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
2135  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
2136  SCIPdebugMessage("calling Gurobi primal simplex: %d cols, %d rows\n", ncols, nrows);
2137  }
2138 #endif
2139 
2140  invalidateSolution(lpi);
2141 
2142  if ( lpi->fromscratch )
2143  {
2144  CHECK_ZERO( lpi->messagehdlr, GRBresetmodel(lpi->grbmodel) );
2145  }
2146 
2147  SCIPdebugMessage("calling GRBoptimize() - primal\n");
2148 
2149  /* set primal simplex */
2150  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2151  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, GRB_METHOD_PRIMAL) );
2152 
2153  retval = GRBoptimize(lpi->grbmodel);
2154  switch( retval )
2155  {
2156  case 0:
2157  break;
2158  case GRB_ERROR_OUT_OF_MEMORY:
2159  return SCIP_NOMEMORY;
2160  default:
2161  return SCIP_LPERROR;
2162  }
2163 
2164  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2165  lpi->iterations = (int) cnt;
2166 
2167  lpi->solisbasic = TRUE;
2168  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2169 
2170  SCIPdebugMessage("Gurobi primal simplex needed %d iterations to gain LP status %d\n", (int) cnt, lpi->solstat);
2171 
2172  /*
2173  CHECK_ZERO( lpi->messagehdlr, CPXsolninfo(lpi->grbenv, lpi->grbmodel, NULL, NULL, &primalfeasible, &dualfeasible) );
2174  SCIPdebugMessage(" -> Gurobi returned solstat=%d, pfeas=%d, dfeas=%d (%d iterations)\n",
2175  lpi->solstat, primalfeasible, dualfeasible, lpi->iterations);
2176  */
2177  primalfeasible = FALSE;
2178  dualfeasible = FALSE;
2179 
2180  if( lpi->solstat == GRB_INF_OR_UNBD
2181  || (lpi->solstat == GRB_INFEASIBLE && !dualfeasible)
2182  || (lpi->solstat == GRB_UNBOUNDED && !primalfeasible) )
2183  {
2184  int presolve;
2185 
2186  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_PRESOLVE, &presolve) );
2187 
2188  if( presolve != GRB_PRESOLVE_OFF )
2189  {
2190  /* maybe the preprocessor solved the problem; but we need a solution, so solve again without preprocessing */
2191  SCIPdebugMessage("presolver may have solved the problem -> calling Gurobi primal simplex again without presolve\n");
2192 
2193  /* switch off preprocessing */
2194  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF) );
2195 
2196  retval = GRBoptimize(lpi->grbmodel);
2197  switch( retval )
2198  {
2199  case 0:
2200  break;
2201  case GRB_ERROR_OUT_OF_MEMORY:
2202  return SCIP_NOMEMORY;
2203  default:
2204  return SCIP_LPERROR;
2205  }
2206 
2207  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2208  lpi->iterations += (int) cnt;
2209  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2210  SCIPdebugMessage(" -> Gurobi returned solstat=%d (%d iterations)\n", lpi->solstat, lpi->iterations);
2211 
2212  /* reset parameters */
2213  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_PRESOLVE, presolve) );
2214  }
2215 
2216  if( lpi->solstat == GRB_INF_OR_UNBD )
2217  {
2218  /* preprocessing was not the problem; issue a warning message and treat LP as infeasible */
2219  SCIPerrorMessage("Gurobi primal simplex returned GRB_INF_OR_UNBD after presolving was turned off\n");
2220  }
2221  }
2222 
2223  return SCIP_OKAY;
2224 }
2225 
2226 /** calls dual simplex to solve the LP
2227  *
2228  * @todo Check concurrent (GRB_METHOD_CONCURRENT or GRB_METHOD_DETERMINISTIC_CONCURRENT)
2229  */
2231  SCIP_LPI* lpi /**< LP interface structure */
2232  )
2233 {
2234  int retval;
2235  double cnt;
2236 
2237  assert( lpi != NULL );
2238  assert( lpi->grbmodel != NULL );
2239  assert( lpi->grbenv != NULL );
2240 
2241 #ifdef SCIP_DEBUG
2242  {
2243  int ncols, nrows;
2244  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
2245  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
2246  SCIPdebugMessage("calling Gurobi dual simplex: %d cols, %d rows\n", ncols, nrows);
2247  }
2248 #endif
2249 
2250  invalidateSolution(lpi);
2251 
2252  if ( lpi->fromscratch )
2253  {
2254  CHECK_ZERO( lpi->messagehdlr, GRBresetmodel(lpi->grbmodel) );
2255  }
2256 
2257  SCIPdebugMessage("calling GRBoptimize() - dual\n");
2258 
2259  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2260 
2261  /* set dual simplex */
2262  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, GRB_METHOD_DUAL) );
2263 
2264  retval = GRBoptimize(lpi->grbmodel);
2265  switch( retval )
2266  {
2267  case 0:
2268  break;
2269  case GRB_ERROR_OUT_OF_MEMORY:
2270  return SCIP_NOMEMORY;
2271  default:
2272  return SCIP_LPERROR;
2273  }
2274 
2275  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2276  lpi->iterations = (int) cnt;
2277 
2278  lpi->solisbasic = TRUE;
2279  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2280 
2281  SCIPdebugMessage("Gurobi dual simplex needed %d iterations to gain LP status %d\n", (int) cnt, lpi->solstat);
2282 
2283  /*
2284  SCIPdebugMessage(" -> Gurobi returned solstat=%d, pfeas=%d, dfeas=%d (%d iterations)\n",
2285  lpi->solstat, primalfeasible, dualfeasible, lpi->iterations);
2286  */
2287 
2288  if( lpi->solstat == GRB_INF_OR_UNBD )
2289  {
2290  int presolve;
2291  CHECK_ZERO( lpi->messagehdlr, getIntParam(lpi, GRB_INT_PAR_PRESOLVE, &presolve) );
2292 
2293  if( presolve != GRB_PRESOLVE_OFF )
2294  {
2295  /* maybe the preprocessor solved the problem; but we need a solution, so solve again without preprocessing */
2296  SCIPdebugMessage("presolver may have solved the problem -> calling Gurobi dual simplex again without presolve\n");
2297 
2298  /* switch off preprocessing */
2299  CHECK_ZERO( lpi->messagehdlr, setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF) );
2300  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2301 
2302  retval = GRBoptimize(lpi->grbmodel);
2303  switch( retval )
2304  {
2305  case 0:
2306  break;
2307  case GRB_ERROR_OUT_OF_MEMORY:
2308  return SCIP_NOMEMORY;
2309  default:
2310  return SCIP_LPERROR;
2311  }
2312 
2313  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2314  lpi->iterations += (int) cnt;
2315  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2316  SCIPdebugMessage(" -> Gurobi returned solstat=%d (%d iterations)\n", lpi->solstat, lpi->iterations);
2317 
2318  /* switch on preprocessing again */
2319  CHECK_ZERO( lpi->messagehdlr, setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_AUTO) );
2320  }
2321 
2322  if( lpi->solstat == GRB_INF_OR_UNBD )
2323  {
2324  /* preprocessing was not the problem; issue a warning message and treat LP as infeasible */
2325  SCIPerrorMessage("Gurobi dual simplex returned GRB_INF_OR_UNBD after presolving was turned off\n");
2326  }
2327  }
2328 
2329  return SCIP_OKAY;
2330 }
2331 
2332 /** calls barrier or interior point algorithm to solve the LP with crossover to simplex basis */
2334  SCIP_LPI* lpi, /**< LP interface structure */
2335  SCIP_Bool crossover /**< perform crossover */
2336  )
2337 {
2338  int retval;
2339  double cnt;
2340 
2341  assert( lpi != NULL );
2342  assert( lpi->grbmodel != NULL );
2343  assert( lpi->grbenv != NULL );
2344 
2345 #ifdef SCIP_DEBUG
2346  {
2347  int ncols, nrows;
2348  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
2349  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
2350  SCIPdebugMessage("calling Gurobi barrier: %d cols, %d rows\n", ncols, nrows);
2351  }
2352 #endif
2353 
2354  invalidateSolution(lpi);
2355 
2356  if ( lpi->fromscratch )
2357  {
2358  CHECK_ZERO( lpi->messagehdlr, GRBresetmodel(lpi->grbmodel) );
2359  }
2360 
2361  SCIPdebugMessage("calling GRBoptimize() - barrier\n");
2362 
2363  /* set barrier */
2364  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2365 
2366  if( crossover )
2367  {
2368  /* turn on crossover to automatic setting (-1) */
2369  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_CROSSOVER, -1) );
2370  }
2371  else
2372  {
2373  /* turn off crossover */
2374  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_CROSSOVER, 0) );
2375  }
2376 
2377  CHECK_ZERO( lpi->messagehdlr, GRBsetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, GRB_METHOD_BARRIER) );
2378 
2379  retval = GRBoptimize(lpi->grbmodel);
2380  switch( retval )
2381  {
2382  case 0:
2383  break;
2384  case GRB_ERROR_OUT_OF_MEMORY:
2385  return SCIP_NOMEMORY;
2386  default:
2387  return SCIP_LPERROR;
2388  }
2389 
2390  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2391  lpi->iterations = (int) cnt;
2392 
2393  lpi->solisbasic = crossover;
2394  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2395 
2396  SCIPdebugMessage("Gurobi barrier needed %d iterations to gain LP status %d\n", (int) cnt, lpi->solstat);
2397 
2398  /*
2399  SCIPdebugMessage(" -> Gurobi returned solstat=%d, pfeas=%d, dfeas=%d (%d iterations)\n",
2400  lpi->solstat, primalfeasible, dualfeasible, lpi->iterations);
2401  */
2402 
2403  if( lpi->solstat == GRB_INF_OR_UNBD )
2404  {
2405  int presolve;
2406  CHECK_ZERO( lpi->messagehdlr, getIntParam(lpi, GRB_INT_PAR_PRESOLVE, &presolve) );
2407 
2408  if( presolve != GRB_PRESOLVE_OFF )
2409  {
2410  /* maybe the preprocessor solved the problem; but we need a solution, so solve again without preprocessing */
2411  SCIPdebugMessage("presolver may have solved the problem -> calling Gurobi barrier again without presolve\n");
2412 
2413  /* switch off preprocessing */
2414  CHECK_ZERO( lpi->messagehdlr, setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF) );
2415  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2416 
2417  retval = GRBoptimize(lpi->grbmodel);
2418  switch( retval )
2419  {
2420  case 0:
2421  break;
2422  case GRB_ERROR_OUT_OF_MEMORY:
2423  return SCIP_NOMEMORY;
2424  default:
2425  return SCIP_LPERROR;
2426  }
2427 
2428  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_ITERCOUNT, &cnt) );
2429  lpi->iterations += (int) cnt;
2430  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &lpi->solstat) );
2431  SCIPdebugMessage(" -> Gurobi returned solstat=%d (%d iterations)\n", lpi->solstat, lpi->iterations);
2432 
2433  /* switch on preprocessing again */
2434  CHECK_ZERO( lpi->messagehdlr, setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_AUTO) );
2435  }
2436 
2437  if( lpi->solstat == GRB_INF_OR_UNBD )
2438  {
2439  /* preprocessing was not the problem; issue a warning message and treat LP as infeasible */
2440  SCIPerrorMessage("Gurobi dual simplex returned GRB_INF_OR_UNBD after presolving was turned off\n");
2441  }
2442  }
2443  return SCIP_OKAY;
2444 }
2445 
2446 /** start strong branching - call before any strong branching */
2448  SCIP_LPI* lpi /**< LP interface structure */
2449  )
2450 {
2451  /* currently do nothing */
2452  return SCIP_OKAY;
2453 }
2454 
2455 /** end strong branching - call after any strong branching */
2457  SCIP_LPI* lpi /**< LP interface structure */
2458  )
2459 {
2460  /* currently do nothing */
2461  return SCIP_OKAY;
2462 }
2463 
2464 /** performs strong branching iterations on one candidate */
2465 static
2466 SCIP_RETCODE lpiStrongbranch(
2467  SCIP_LPI* lpi, /**< LP interface structure */
2468  int col, /**< column to apply strong branching on */
2469  SCIP_Real psol, /**< current primal solution value of column */
2470  int itlim, /**< iteration limit for strong branchings */
2471  SCIP_Real* down, /**< stores dual bound after branching column down */
2472  SCIP_Real* up, /**< stores dual bound after branching column up */
2473  SCIP_Bool* downvalid, /**< stores whether the returned down value is a valid dual bound;
2474  * otherwise, it can only be used as an estimate value */
2475  SCIP_Bool* upvalid, /**< stores whether the returned up value is a valid dual bound;
2476  * otherwise, it can only be used as an estimate value */
2477  int* iter /**< stores total number of strong branching iterations, or -1; may be NULL */
2478  )
2479 {
2480  SCIP_Real oldlb;
2481  SCIP_Real oldub;
2482  SCIP_Real newlb;
2483  SCIP_Real newub;
2484  SCIP_Real olditlim;
2485  SCIP_Bool error;
2486  SCIP_Bool success;
2487  int objsen;
2488  int it;
2489 
2490  assert( lpi != NULL );
2491  assert( lpi->grbmodel != NULL );
2492  assert( lpi->grbenv != NULL );
2493  assert( down != NULL );
2494  assert( up != NULL );
2495  assert( downvalid != NULL );
2496  assert( upvalid != NULL );
2497 
2498  SCIPdebugMessage("performing strong branching on variable %d (%d iterations)\n", col, itlim);
2499 
2500  SCIP_CALL( setParameterValues(lpi, &(lpi->grbparam)) );
2501 
2502  error = FALSE;
2503  *downvalid = FALSE;
2504  *upvalid = FALSE;
2505  if( iter != NULL )
2506  *iter = 0;
2507 
2508  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &objsen) );
2509 
2510  /* save current LP basis and bounds*/
2511  SCIP_CALL( getBase(lpi, &success) );
2512  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_LB, col, &oldlb) );
2513  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_UB, col, &oldub) );
2514 
2515  if ( lpi->fromscratch )
2516  {
2517  CHECK_ZERO( lpi->messagehdlr, GRBresetmodel(lpi->grbmodel) );
2518  }
2519 
2520  /* save old iteration limit and set iteration limit to strong branching limit */
2521  if( itlim > INT_MAX )
2522  itlim = INT_MAX;
2523 
2524  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_ITERATIONLIMIT, &olditlim) );
2525  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_ITERATIONLIMIT, (double) itlim) );
2526 
2527  /* down branch */
2528  newub = EPSCEIL(psol-1.0, 1e-06);
2529  if( newub >= oldlb - 0.5 )
2530  {
2531  SCIPdebugMessage("strong branching down (%g) on x%d (%g) with %d iterations\n", newub, col, psol, itlim);
2532 
2533  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_UB, col, newub) );
2534 
2535  SCIP_CALL( SCIPlpiSolveDual(lpi) );
2536  /* when iteration limit was reached the objective value is not computed */
2537  if( SCIPlpiIsOptimal(lpi) ) /*|| SCIPlpiIsIterlimExc(lpi) ) */
2538  {
2539  SCIP_CALL( SCIPlpiGetObjval(lpi, down) );
2540  *downvalid = TRUE;
2541  }
2542  else if( SCIPlpiIsPrimalInfeasible(lpi) || SCIPlpiIsObjlimExc(lpi) )
2543  {
2544  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_CUTOFF, down) );
2545  }
2546  else if( !SCIPlpiIsIterlimExc(lpi) )
2547  error = TRUE;
2548 
2549  if( iter != NULL )
2550  {
2551  SCIP_CALL( SCIPlpiGetIterations(lpi, &it) );
2552  *iter += it;
2553  }
2554  SCIPdebugMessage(" -> down (x%d <= %g): %g\n", col, newub, *down);
2555 
2556  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_UB, col, oldub) );
2557  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
2558 #ifdef SCIP_DEBUG
2559  {
2560  double b;
2561  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_UB, col, &b) );
2562  assert( b == oldub );
2563  }
2564 #endif
2565 
2566  if ( success )
2567  {
2568  SCIP_CALL( setBase(lpi) );
2569  }
2570  }
2571  else
2572  {
2573  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_CUTOFF, down) );
2574  *downvalid = TRUE;
2575  }
2576 
2577  /* up branch */
2578  if( !error )
2579  {
2580  newlb = EPSFLOOR(psol+1.0, 1e-06);
2581  if( newlb <= oldub + 0.5 )
2582  {
2583  SCIPdebugMessage("strong branching up (%g) on x%d (%g) with %d iterations\n", newlb, col, psol, itlim);
2584 
2585  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_LB, col, newlb) );
2586 
2587  SCIP_CALL( SCIPlpiSolveDual(lpi) );
2588  /* when iteration limit was reached the objective value is not computed */
2589  if( SCIPlpiIsOptimal(lpi) ) /*|| SCIPlpiIsIterlimExc(lpi) ) */
2590  {
2591  SCIP_CALL( SCIPlpiGetObjval(lpi, up) );
2592  *upvalid = TRUE;
2593  }
2594  else if( SCIPlpiIsPrimalInfeasible(lpi) || SCIPlpiIsObjlimExc(lpi) )
2595  {
2596  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_CUTOFF, up) );
2597  }
2598  else if( !SCIPlpiIsIterlimExc(lpi) )
2599  error = TRUE;
2600 
2601  if( iter != NULL )
2602  {
2603  SCIP_CALL( SCIPlpiGetIterations(lpi, &it) );
2604  *iter += it;
2605  }
2606  SCIPdebugMessage(" -> up (x%d >= %g): %g\n", col, newlb, *up);
2607 
2608  CHECK_ZERO( lpi->messagehdlr, GRBsetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_LB, col, oldlb) );
2609  CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) );
2610 #ifdef SCIP_DEBUG
2611  {
2612  double b;
2613  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrelement(lpi->grbmodel, GRB_DBL_ATTR_LB, col, &b) );
2614  assert( b == oldlb );
2615  }
2616 #endif
2617 
2618  if ( success )
2619  {
2620  SCIP_CALL( setBase(lpi) );
2621  }
2622  }
2623  else
2624  {
2625  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_CUTOFF, up) );
2626  *upvalid = TRUE;
2627  }
2628  }
2629 
2630  /* reset iteration limit */
2631  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_ITERATIONLIMIT, olditlim) );
2632  /* CHECK_ZERO( lpi->messagehdlr, GRBupdatemodel(lpi->grbmodel) ); */
2633 
2634  if( error )
2635  {
2636  SCIPerrorMessage("LP error in strong branching.\n");
2637  return SCIP_LPERROR;
2638  }
2639 
2640  return SCIP_OKAY;
2641 }
2642 
2643 /** performs strong branching iterations on one @b fractional candidate */
2645  SCIP_LPI* lpi, /**< LP interface structure */
2646  int col, /**< column to apply strong branching on */
2647  SCIP_Real psol, /**< fractional current primal solution value of column */
2648  int itlim, /**< iteration limit for strong branchings */
2649  SCIP_Real* down, /**< stores dual bound after branching column down */
2650  SCIP_Real* up, /**< stores dual bound after branching column up */
2651  SCIP_Bool* downvalid, /**< stores whether the returned down value is a valid dual bound;
2652  * otherwise, it can only be used as an estimate value */
2653  SCIP_Bool* upvalid, /**< stores whether the returned up value is a valid dual bound;
2654  * otherwise, it can only be used as an estimate value */
2655  int* iter /**< stores total number of strong branching iterations, or -1; may be NULL */
2656  )
2657 {
2658  /* pass call on to lpiStrongbranch() */
2659  SCIP_CALL( lpiStrongbranch(lpi, col, psol, itlim, down, up, downvalid, upvalid, iter) );
2660 
2661  return SCIP_OKAY;
2662 }
2663 
2664 /** performs strong branching iterations on given @b fractional candidates */
2666  SCIP_LPI* lpi, /**< LP interface structure */
2667  int* cols, /**< columns to apply strong branching on */
2668  int ncols, /**< number of columns */
2669  SCIP_Real* psols, /**< fractional current primal solution values of columns */
2670  int itlim, /**< iteration limit for strong branchings */
2671  SCIP_Real* down, /**< stores dual bounds after branching columns down */
2672  SCIP_Real* up, /**< stores dual bounds after branching columns up */
2673  SCIP_Bool* downvalid, /**< stores whether the returned down values are valid dual bounds;
2674  * otherwise, they can only be used as an estimate values */
2675  SCIP_Bool* upvalid, /**< stores whether the returned up values are a valid dual bounds;
2676  * otherwise, they can only be used as an estimate values */
2677  int* iter /**< stores total number of strong branching iterations, or -1; may be NULL */
2678  )
2679 {
2680  int j;
2681 
2682  assert( iter != NULL );
2683  assert( cols != NULL );
2684  assert( psols != NULL );
2685  assert( down != NULL );
2686  assert( up != NULL );
2687  assert( downvalid != NULL );
2688  assert( upvalid != NULL );
2689  assert( down != NULL );
2690 
2691  if( iter != NULL )
2692  *iter = 0;
2693 
2694  for( j = 0; j < ncols; ++j )
2695  {
2696  /* pass call on to lpiStrongbranch() */
2697  SCIP_CALL( lpiStrongbranch(lpi, cols[j], psols[j], itlim, &(down[j]), &(up[j]), &(downvalid[j]), &(upvalid[j]), iter) );
2698  }
2699  return SCIP_OKAY;
2700 }
2701 
2702 /** performs strong branching iterations on one candidate with @b integral value */
2704  SCIP_LPI* lpi, /**< LP interface structure */
2705  int col, /**< column to apply strong branching on */
2706  SCIP_Real psol, /**< current integral primal solution value of column */
2707  int itlim, /**< iteration limit for strong branchings */
2708  SCIP_Real* down, /**< stores dual bound after branching column down */
2709  SCIP_Real* up, /**< stores dual bound after branching column up */
2710  SCIP_Bool* downvalid, /**< stores whether the returned down value is a valid dual bound;
2711  * otherwise, it can only be used as an estimate value */
2712  SCIP_Bool* upvalid, /**< stores whether the returned up value is a valid dual bound;
2713  * otherwise, it can only be used as an estimate value */
2714  int* iter /**< stores total number of strong branching iterations, or -1; may be NULL */
2715  )
2716 {
2717  /* pass call on to lpiStrongbranch() */
2718  SCIP_CALL( lpiStrongbranch(lpi, col, psol, itlim, down, up, downvalid, upvalid, iter) );
2719 
2720  return SCIP_OKAY;
2721 }
2722 
2723 /** performs strong branching iterations on given candidates with @b integral values */
2725  SCIP_LPI* lpi, /**< LP interface structure */
2726  int* cols, /**< columns to apply strong branching on */
2727  int ncols, /**< number of columns */
2728  SCIP_Real* psols, /**< current integral primal solution values of columns */
2729  int itlim, /**< iteration limit for strong branchings */
2730  SCIP_Real* down, /**< stores dual bounds after branching columns down */
2731  SCIP_Real* up, /**< stores dual bounds after branching columns up */
2732  SCIP_Bool* downvalid, /**< stores whether the returned down values are valid dual bounds;
2733  * otherwise, they can only be used as an estimate values */
2734  SCIP_Bool* upvalid, /**< stores whether the returned up values are a valid dual bounds;
2735  * otherwise, they can only be used as an estimate values */
2736  int* iter /**< stores total number of strong branching iterations, or -1; may be NULL */
2737  )
2738 {
2739  int j;
2740 
2741  assert( iter != NULL );
2742  assert( cols != NULL );
2743  assert( psols != NULL );
2744  assert( down != NULL );
2745  assert( up != NULL );
2746  assert( downvalid != NULL );
2747  assert( upvalid != NULL );
2748  assert( down != NULL );
2749 
2750  if( iter != NULL )
2751  *iter = 0;
2752 
2753  for( j = 0; j < ncols; ++j )
2754  {
2755  /* pass call on to lpiStrongbranch() */
2756  SCIP_CALL( lpiStrongbranch(lpi, cols[j], psols[j], itlim, &(down[j]), &(up[j]), &(downvalid[j]), &(upvalid[j]), iter) );
2757  }
2758  return SCIP_OKAY;
2759 }
2760 /**@} */
2761 
2762 
2763 
2764 
2765 /*
2766  * Solution Information Methods
2767  */
2768 
2769 /**@name Solution Information Methods */
2770 /**@{ */
2771 
2772 /** returns whether a solve method was called after the last modification of the LP */
2774  SCIP_LPI* lpi /**< LP interface structure */
2775  )
2776 {
2777  assert(lpi != NULL);
2778 
2779  return (lpi->solstat != -1);
2780 }
2781 
2782 /** gets information about primal and dual feasibility of the current LP solution */
2784  SCIP_LPI* lpi, /**< LP interface structure */
2785  SCIP_Bool* primalfeasible, /**< stores primal feasibility status */
2786  SCIP_Bool* dualfeasible /**< stores dual feasibility status */
2787  )
2788 {
2789  int algo;
2790 
2791  assert( lpi != NULL );
2792  assert( lpi->grbmodel != NULL );
2793  assert( lpi->grbenv != NULL );
2794  assert( lpi->solstat >= 1 );
2795 
2796  SCIPdebugMessage("getting solution feasibility\n");
2797 
2798  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, &algo) );
2799 
2800  if( primalfeasible != NULL )
2801  {
2802  *primalfeasible = (lpi->solstat == GRB_OPTIMAL || (lpi->solstat == GRB_UNBOUNDED && algo == GRB_METHOD_PRIMAL));
2803  }
2804 
2805  if( dualfeasible != NULL )
2806  {
2807  *dualfeasible = (lpi->solstat == GRB_OPTIMAL || (lpi->solstat == GRB_INFEASIBLE && algo == GRB_METHOD_DUAL));
2808  }
2809 
2810 
2811 #if 0
2812  /* @todo: check whether this code is needed anymore (this was the first version) */
2813  SCIP_Real viol;
2814  SCIP_Real tol;
2815 
2816  assert( lpi != NULL );
2817  assert( lpi->grbmodel != NULL );
2818  assert( lpi->solstat >= 1 );
2819 
2820  SCIPdebugMessage("getting solution feasibility\n");
2821 
2822  if( primalfeasible != NULL )
2823  {
2824  if(lpi->solstat != GRB_INF_OR_UNBD && lpi->solstat != GRB_INFEASIBLE)
2825  {
2826  /* check whether maximum scaled violation is smaller than feasibility tolerance */
2827  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_CONSTR_SRESIDUAL, &viol) );
2828  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_FEASIBILITYTOL, &tol) );
2829  *primalfeasible = (viol <= tol) ? TRUE : FALSE;
2830  SCIPdebugMessage("primal violation: %g (tol: %g)\n", viol, tol);
2831  }
2832  else
2833  *primalfeasible = FALSE;
2834  }
2835 
2836  if( dualfeasible != NULL )
2837  {
2838  if(lpi->solstat != GRB_UNBOUNDED && lpi->solstat != GRB_INFEASIBLE)
2839  {
2840  /* check whether maximum scaled dual violation is smaller than optimality tolerance */
2841  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_DUAL_SRESIDUAL, &viol) );
2842  CHECK_ZERO( lpi->messagehdlr, GRBgetdblparam(lpi->grbenv, GRB_DBL_PAR_OPTIMALITYTOL, &tol) );
2843  *dualfeasible = (viol <= tol) ? TRUE : FALSE;
2844  SCIPdebugMessage("dual violation: %g (tol: %g)\n", viol, tol);
2845  }
2846  else
2847  *dualfeasible = FALSE;
2848  }
2849 #endif
2850 
2851  return SCIP_OKAY;
2852 }
2853 
2854 /** returns TRUE iff LP is proven to have a primal unbounded ray (but not necessary a primal feasible point);
2855  * this does not necessarily mean, that the solver knows and can return the primal ray
2856  */
2858  SCIP_LPI* lpi /**< LP interface structure */
2859  )
2860 {
2861  assert(lpi != NULL);
2862  assert(lpi->grbmodel != NULL);
2863  assert(lpi->solstat >= 0);
2864 
2865  return (lpi->solstat == GRB_UNBOUNDED);
2866 }
2867 
2868 /** returns TRUE iff LP is proven to have a primal unbounded ray (but not necessary a primal feasible point),
2869  * and the solver knows and can return the primal ray
2870  */
2872  SCIP_LPI* lpi /**< LP interface structure */
2873  )
2874 {
2875  assert(lpi != NULL);
2876  assert(lpi->grbmodel != NULL);
2877  assert(lpi->solstat >= 0);
2878 
2879  return (lpi->solstat == GRB_UNBOUNDED);
2880 }
2881 
2882 /** returns TRUE iff LP is proven to be primal unbounded */
2884  SCIP_LPI* lpi /**< LP interface structure */
2885  )
2886 {
2887  SCIP_Bool primalfeasible;
2888  SCIP_RETCODE retcode;
2889 
2890  assert(lpi != NULL);
2891  assert(lpi->grbmodel != NULL);
2892  assert(lpi->solstat >= 0);
2893 
2894  SCIPdebugMessage("checking for primal unboundedness\n");
2895 
2896  primalfeasible = FALSE; /* to fix compiler warning */
2897  retcode = SCIPlpiGetSolFeasibility(lpi, &primalfeasible, NULL);
2898  if ( retcode != SCIP_OKAY )
2899  return FALSE;
2900 
2901  /* Probably GRB_UNBOUNDED means that the problem has an unbounded ray, but not necessarily that a feasible primal solution exists. */
2902  return (primalfeasible && (lpi->solstat == GRB_UNBOUNDED || lpi->solstat == GRB_INF_OR_UNBD));
2903 }
2904 
2905 /** returns TRUE iff LP is proven to be primal infeasible */
2907  SCIP_LPI* lpi /**< LP interface structure */
2908  )
2909 {
2910  assert(lpi != NULL);
2911  assert(lpi->grbmodel != NULL);
2912  assert(lpi->solstat >= 0);
2913 
2914  SCIPdebugMessage("checking for primal infeasibility\n");
2915 
2916  assert( lpi->solstat != GRB_INF_OR_UNBD );
2917  return (lpi->solstat == GRB_INFEASIBLE);
2918 }
2919 
2920 /** returns TRUE iff LP is proven to be primal feasible */
2922  SCIP_LPI* lpi /**< LP interface structure */
2923  )
2924 {
2925  int algo;
2926 
2927  assert( lpi != NULL );
2928  assert( lpi->grbmodel != NULL );
2929  assert( lpi->grbenv != NULL );
2930  assert( lpi->solstat >= 0 );
2931 
2932  SCIPdebugMessage("checking for primal feasibility\n");
2933 
2934  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, &algo) );
2935 
2936  return (lpi->solstat == GRB_OPTIMAL || (lpi->solstat == GRB_UNBOUNDED && algo == GRB_METHOD_PRIMAL));
2937 }
2938 
2939 /** returns TRUE iff LP is proven to have a dual unbounded ray (but not necessary a dual feasible point);
2940  * this does not necessarily mean, that the solver knows and can return the dual ray
2941  */
2943  SCIP_LPI* lpi /**< LP interface structure */
2944  )
2945 {
2946  assert(lpi != NULL);
2947  assert(lpi->grbmodel != NULL);
2948  assert(lpi->solstat >= 0);
2949 
2950  return (lpi->solstat == GRB_INFEASIBLE); /* ????????? */
2951 }
2952 
2953 /** returns TRUE iff LP is proven to have a dual unbounded ray (but not necessary a dual feasible point),
2954  * and the solver knows and can return the dual ray
2955  */
2957  SCIP_LPI* lpi /**< LP interface structure */
2958  )
2959 {
2960  int algo;
2961 
2962  assert( lpi != NULL );
2963  assert( lpi->grbmodel != NULL );
2964  assert( lpi->grbenv != NULL );
2965  assert( lpi->solstat >= 0 );
2966 
2967  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, &algo) );
2968 
2969  return (lpi->solstat == GRB_INFEASIBLE && algo == GRB_METHOD_DUAL);
2970 }
2971 
2972 /** returns TRUE iff LP is proven to be dual unbounded */
2974  SCIP_LPI* lpi /**< LP interface structure */
2975  )
2976 {
2977  int algo;
2978 
2979  assert( lpi != NULL );
2980  assert( lpi->grbmodel != NULL );
2981  assert( lpi->grbenv != NULL );
2982  assert( lpi->solstat >= 0 );
2983 
2984  SCIPdebugMessage("checking for dual unboundedness\n");
2985 
2986  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, &algo) );
2987 
2988  return (lpi->solstat == GRB_INFEASIBLE && algo == GRB_METHOD_DUAL);
2989 }
2990 
2991 /** returns TRUE iff LP is proven to be dual infeasible */
2993  SCIP_LPI* lpi /**< LP interface structure */
2994  )
2995 {
2996  assert( lpi != NULL );
2997  assert( lpi->grbmodel != NULL );
2998  assert( lpi->solstat >= 0 );
2999 
3000  SCIPdebugMessage("checking for dual infeasibility\n");
3001 
3002  return (lpi->solstat == GRB_UNBOUNDED);
3003 }
3004 
3005 /** returns TRUE iff LP is proven to be dual feasible */
3007  SCIP_LPI* lpi /**< LP interface structure */
3008  )
3009 {
3010  int algo;
3011 
3012  assert( lpi != NULL );
3013  assert( lpi->grbmodel != NULL );
3014  assert( lpi->grbenv != NULL );
3015  assert( lpi->solstat >= 0 );
3016 
3017  SCIPdebugMessage("checking for dual feasibility\n");
3018 
3019  CHECK_ZERO( lpi->messagehdlr, GRBgetintparam(lpi->grbenv, GRB_INT_PAR_METHOD, &algo) );
3020 
3021  return (lpi->solstat == GRB_OPTIMAL || (lpi->solstat == GRB_INFEASIBLE && algo == GRB_METHOD_DUAL));
3022 }
3023 
3024 /** returns TRUE iff LP was solved to optimality */
3026  SCIP_LPI* lpi /**< LP interface structure */
3027  )
3028 {
3029  assert(lpi != NULL);
3030  assert(lpi->grbmodel != NULL);
3031  assert(lpi->solstat >= 0);
3032 
3033  return (lpi->solstat == GRB_OPTIMAL);
3034 }
3035 
3036 /** returns TRUE iff current LP basis is stable */
3038  SCIP_LPI* lpi /**< LP interface structure */
3039  )
3040 {
3041  assert(lpi != NULL);
3042  assert(lpi->grbmodel != NULL);
3043  assert(lpi->solstat >= 0);
3044 
3045  SCIPdebugMessage("checking for stability: Gurobi solstat = %d\n", lpi->solstat);
3046 
3047  return (lpi->solstat != GRB_NUMERIC);
3048 }
3049 
3050 /** returns TRUE iff the objective limit was reached */
3052  SCIP_LPI* lpi /**< LP interface structure */
3053  )
3054 {
3055  assert(lpi != NULL);
3056  assert(lpi->grbmodel != NULL);
3057  assert(lpi->solstat >= 0);
3058 
3059  return (lpi->solstat == GRB_CUTOFF);
3060 }
3061 
3062 /** returns TRUE iff the iteration limit was reached */
3064  SCIP_LPI* lpi /**< LP interface structure */
3065  )
3066 {
3067  assert(lpi != NULL);
3068  assert(lpi->grbmodel != NULL);
3069  assert(lpi->solstat >= 0);
3070 
3071  return (lpi->solstat == GRB_ITERATION_LIMIT);
3072 }
3073 
3074 /** returns TRUE iff the time limit was reached */
3076  SCIP_LPI* lpi /**< LP interface structure */
3077  )
3078 {
3079  assert(lpi != NULL);
3080  assert(lpi->grbmodel != NULL);
3081  assert(lpi->solstat >= 0);
3082 
3083  return (lpi->solstat == GRB_TIME_LIMIT);
3084 }
3085 
3086 /** returns the internal solution status of the solver */
3088  SCIP_LPI* lpi /**< LP interface structure */
3089  )
3090 {
3091  assert(lpi != NULL);
3092  assert(lpi->grbmodel != NULL);
3093 
3094  return lpi->solstat;
3095 }
3096 
3097 /** tries to reset the internal status of the LP solver in order to ignore an instability of the last solving call */
3099  SCIP_LPI* lpi, /**< LP interface structure */
3100  SCIP_Bool* success /**< pointer to store, whether the instability could be ignored */
3101  )
3102 {
3103  assert(lpi != NULL);
3104  assert(lpi->grbmodel != NULL);
3105  assert(success != NULL);
3106 
3107  *success = FALSE;
3108 
3109  return SCIP_OKAY;
3110 }
3111 
3112 /** gets objective value of solution
3113  *
3114  * @note if the solution status is iteration limit reached (GRB_ITERATION_LIMIT), the objective value was not computed
3115  */
3117  SCIP_LPI* lpi, /**< LP interface structure */
3118  SCIP_Real* objval /**< stores the objective value */
3119  )
3120 {
3121  assert(lpi != NULL);
3122  assert(lpi->grbmodel != NULL);
3123 
3124  SCIPdebugMessage("getting solution's objective value\n");
3125 
3126  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_OBJVAL, objval) );
3127 
3128  return SCIP_OKAY;
3129 }
3130 
3131 /** gets primal and dual solution vectors */
3133  SCIP_LPI* lpi, /**< LP interface structure */
3134  SCIP_Real* objval, /**< stores the objective value, may be NULL if not needed */
3135  SCIP_Real* primsol, /**< primal solution vector, may be NULL if not needed */
3136  SCIP_Real* dualsol, /**< dual solution vector, may be NULL if not needed */
3137  SCIP_Real* activity, /**< row activity vector, may be NULL if not needed */
3138  SCIP_Real* redcost /**< reduced cost vector, may be NULL if not needed */
3139  )
3140 {
3141  int ncols;
3142  int nrows;
3143 
3144  assert(lpi != NULL);
3145  assert(lpi->grbmodel != NULL);
3146  assert(lpi->solstat >= 0);
3147 
3148  SCIPdebugMessage("getting solution\n");
3149 
3150  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3151  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3152  assert( ncols >= 0 && nrows >= 0 );
3153 
3154  if( objval != NULL )
3155  {
3156  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_OBJVAL, objval) );
3157  }
3158 
3159  if( primsol != NULL )
3160  {
3161  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_X, 0, ncols, primsol) );
3162  }
3163 
3164  if( dualsol != NULL )
3165  {
3166  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_PI, 0, nrows, dualsol) );
3167  }
3168 
3169  if( activity != NULL )
3170  {
3171  int i;
3172 
3173  /* first get the values of the slack variables */
3174  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_SLACK, 0, nrows, activity) );
3175 
3176  SCIP_CALL( ensureSidechgMem(lpi, nrows) );
3177 
3178  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_RHS, 0, nrows, lpi->rhsarray) );
3179  CHECK_ZERO( lpi->messagehdlr, GRBgetcharattrarray(lpi->grbmodel, GRB_CHAR_ATTR_SENSE, 0, nrows, lpi->senarray) );
3180 
3181  for( i = 0; i < nrows; ++i )
3182  {
3183  switch(lpi->senarray[i])
3184  {
3185  case GRB_LESS_EQUAL:
3186  case GRB_EQUAL:
3187  activity[i] = lpi->rhsarray[i] - activity[i];
3188  break;
3189  case GRB_GREATER_EQUAL:
3190  activity[i] = lpi->rhsarray[i] - activity[i];
3191  break;
3192  default:
3193  SCIPerrorMessage("Unkown sense %c.\n", lpi->senarray[i]);
3194  SCIPABORT();
3195  return SCIP_INVALIDDATA; /*lint !e527*/
3196  }
3197  }
3198  }
3199 
3200  if( redcost != NULL )
3201  {
3202  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_RC, 0, ncols, redcost) );
3203  }
3204 
3205  return SCIP_OKAY;
3206 }
3207 
3208 /** gets primal ray for unbounded LPs */
3210  SCIP_LPI* lpi, /**< LP interface structure */
3211  SCIP_Real* ray /**< primal ray */
3212  )
3213 {
3214  int ncols;
3215 
3216  assert(lpi != NULL);
3217  assert(lpi->grbmodel != NULL);
3218  assert(lpi->solstat >= 0);
3219 
3220  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3221  assert( ncols >= 0 );
3222 
3223  SCIPdebugMessage("calling Gurobi get primal ray: %d cols\n", ncols);
3224 
3225  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_UNBDRAY, 0, ncols, ray) );
3226 
3227  return SCIP_OKAY;
3228 }
3229 
3230 /** gets dual Farkas proof for infeasibility */
3232  SCIP_LPI* lpi, /**< LP interface structure */
3233  SCIP_Real* dualfarkas /**< dual Farkas row multipliers */
3234  )
3235 {
3236  int nrows;
3237 
3238  assert(lpi != NULL);
3239  assert(lpi->grbmodel != NULL);
3240  assert(lpi->solstat >= 0);
3241  assert(dualfarkas != NULL);
3242 
3243  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3244  assert( nrows >= 0 );
3245 
3246  SCIPdebugMessage("calling Gurobi dual Farkas: %d rows\n", nrows);
3247 
3248  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_FARKASDUAL, 0, nrows, dualfarkas) );
3249 
3250  return SCIP_LPERROR;
3251 }
3252 
3253 /** gets the number of LP iterations of the last solve call */
3255  SCIP_LPI* lpi, /**< LP interface structure */
3256  int* iterations /**< pointer to store the number of iterations of the last solve call */
3257  )
3258 {
3259  assert(lpi != NULL);
3260  assert(lpi->grbmodel != NULL);
3261  assert(iterations != NULL);
3262 
3263  *iterations = lpi->iterations;
3264 
3265  return SCIP_OKAY;
3266 }
3267 
3268 /** gets information about the quality of an LP solution
3269  *
3270  * Such information is usually only available, if also a (maybe not optimal) solution is available.
3271  * The LPI should return SCIP_INVALID for @p quality, if the requested quantity is not available.
3272  */
3274  SCIP_LPI* lpi, /**< LP interface structure */
3275  SCIP_LPSOLQUALITY qualityindicator, /**< indicates which quality should be returned */
3276  SCIP_Real* quality /**< pointer to store quality number */
3277  )
3278 {
3279  assert(lpi != NULL);
3280  assert(quality != NULL);
3281 
3282  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattr(lpi->grbmodel, GRB_DBL_ATTR_KAPPA, quality) );
3283 
3284  return SCIP_OKAY;
3285 }
3286 
3287 /**@} */
3288 
3289 
3290 
3291 
3292 /*
3293  * LP Basis Methods
3294  */
3295 
3296 /**@name LP Basis Methods */
3297 /**@{ */
3298 
3299 /** gets current basis status for columns and rows; arrays must be large enough to store the basis status */
3301  SCIP_LPI* lpi, /**< LP interface structure */
3302  int* cstat, /**< array to store column basis status, or NULL */
3303  int* rstat /**< array to store row basis status, or NULL */
3304  )
3305 {
3306  assert(lpi != NULL);
3307  assert(lpi->grbmodel != NULL);
3308 
3309  SCIPdebugMessage("saving Gurobi basis into %p/%p\n", (void*) cstat, (void*) rstat);
3310 
3311  if( rstat != 0 )
3312  {
3313  int i;
3314  int nrows;
3315 
3316  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3317 
3318  for( i = 0; i < nrows; ++i )
3319  {
3320  int stat;
3321  CHECK_ZERO( lpi->messagehdlr, GRBgetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, &stat) );
3322 
3323  switch( stat )
3324  {
3325  case GRB_BASIC:
3326  rstat[i] = SCIP_BASESTAT_BASIC;
3327  break;
3328 
3329  case GRB_NONBASIC_LOWER:
3330  rstat[i] = SCIP_BASESTAT_LOWER;
3331  break;
3332 
3333  case GRB_NONBASIC_UPPER:
3334  rstat[i] = SCIP_BASESTAT_UPPER;
3335  break;
3336 
3337  case GRB_SUPERBASIC:
3338  rstat[i] = SCIP_BASESTAT_ZERO;
3339  break;
3340 
3341  default:
3342  SCIPerrorMessage("invalid basis status %d\n", stat);
3343  SCIPABORT();
3344  return SCIP_INVALIDDATA; /*lint !e527*/
3345  }
3346  }
3347  }
3348 
3349  if( cstat != 0 )
3350  {
3351  int j;
3352  int ncols;
3353 
3354  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3355 
3356  for( j = 0; j < ncols; ++j )
3357  {
3358  int stat;
3359  CHECK_ZERO( lpi->messagehdlr, GRBgetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, &stat) );
3360 
3361  switch( stat )
3362  {
3363  case GRB_BASIC:
3364  cstat[j] = SCIP_BASESTAT_BASIC;
3365  break;
3366 
3367  case GRB_NONBASIC_LOWER:
3368  cstat[j] = SCIP_BASESTAT_LOWER;
3369  break;
3370 
3371  case GRB_NONBASIC_UPPER:
3372  cstat[j] = SCIP_BASESTAT_UPPER;
3373  break;
3374  case GRB_SUPERBASIC:
3375  cstat[j] = SCIP_BASESTAT_ZERO;
3376  break;
3377 
3378  default:
3379  SCIPerrorMessage("invalid basis status %d\n", stat);
3380  SCIPABORT();
3381  return SCIP_INVALIDDATA; /*lint !e527*/
3382  }
3383  }
3384  }
3385 
3386  return SCIP_OKAY;
3387 }
3388 
3389 /** sets current basis status for columns and rows */
3391  SCIP_LPI* lpi, /**< LP interface structure */
3392  int* cstat, /**< array with column basis status */
3393  int* rstat /**< array with row basis status */
3394  )
3395 {
3396  int i, j;
3397  int nrows, ncols;
3398 
3399  assert(lpi != NULL);
3400  assert(lpi->grbmodel != NULL);
3401  assert(cstat != NULL);
3402  assert(rstat != NULL);
3403 
3404  SCIPdebugMessage("loading basis %p/%p into Gurobi\n", (void*) cstat, (void*) rstat);
3405 
3406  invalidateSolution(lpi);
3407 
3408  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3409  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3410 
3411  for( i = 0; i < nrows; ++i )
3412  {
3413  switch( rstat[i] )
3414  {
3415  case SCIP_BASESTAT_BASIC:
3416  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, GRB_BASIC) );
3417  break;
3418 
3419  case SCIP_BASESTAT_LOWER:
3420  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, GRB_NONBASIC_LOWER) );
3421  break;
3422 
3423  case SCIP_BASESTAT_UPPER:
3424  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, GRB_NONBASIC_UPPER) );
3425  break;
3426 
3427  case SCIP_BASESTAT_ZERO:
3428  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, GRB_SUPERBASIC) );
3429  break;
3430 
3431  default:
3432  SCIPerrorMessage("invalid basis status %d\n", rstat[i]);
3433  SCIPABORT();
3434  return SCIP_INVALIDDATA; /*lint !e527*/
3435  }
3436  }
3437 
3438  for( j = 0; j < ncols; ++j )
3439  {
3440  switch( cstat[j] )
3441  {
3442  case SCIP_BASESTAT_BASIC:
3443  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, GRB_BASIC) );
3444  break;
3445 
3446  case SCIP_BASESTAT_LOWER:
3447  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, GRB_NONBASIC_LOWER) );
3448  break;
3449 
3450  case SCIP_BASESTAT_UPPER:
3451  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, GRB_NONBASIC_UPPER) );
3452 
3453  case SCIP_BASESTAT_ZERO:
3454  CHECK_ZERO( lpi->messagehdlr, GRBsetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, GRB_SUPERBASIC) );
3455  break;
3456 
3457  default:
3458  SCIPerrorMessage("invalid basis status %d\n", cstat[j]);
3459  SCIPABORT();
3460  return SCIP_INVALIDDATA; /*lint !e527*/
3461  }
3462  }
3463 
3464  return SCIP_OKAY;
3465 }
3466 
3467 /** returns the indices of the basic columns and rows; basic column n gives value n, basic row m gives value -1-m */
3468 extern
3470  SCIP_LPI* lpi, /**< LP interface structure */
3471  int* bind /**< pointer to store basis indices ready to keep number of rows entries */
3472  )
3473 {
3474  int i;
3475  int nrows;
3476  int ncols;
3477  int* bhead;
3478  int status;
3479 
3480  assert(lpi != NULL);
3481  assert(lpi->grbmodel != NULL);
3482 
3483  SCIPdebugMessage("getting basis information\n");
3484 
3485  /* check whether we have to reoptimize */
3486  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &status) );
3487  if ( status == GRB_LOADED || status == GRB_INTERRUPTED || status == GRB_INPROGRESS )
3488  {
3489  SCIP_CALL_QUIET( restoreLPData(lpi) );
3490  }
3491 
3492  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3493  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3494 
3495 
3496  /* get space for bhead */
3497  SCIP_ALLOC( BMSallocMemoryArray(&bhead, nrows+ncols) );
3498 
3499  /* bet basis indices */
3500  CHECK_ZERO( lpi->messagehdlr, GRBgetBasisHead(lpi->grbmodel, bhead) );
3501 
3502  for (i = 0; i < nrows; ++i)
3503  {
3504  /* entries >= ncols refer to slack variables */
3505  if ( bhead[i] < ncols )
3506  bind[i] = bhead[i];
3507  else
3508  bind[i] = -1 - (bhead[i] - ncols);
3509  }
3510  BMSfreeMemoryArray(&bhead);
3511 
3512 #if 0
3513  /* old implementation */
3514  cnt = 0;
3515  for( i = 0; i < nrows; ++i )
3516  {
3517  int stat;
3518  CHECK_ZERO( lpi->messagehdlr, GRBgetintattrelement(lpi->grbmodel, GRB_INT_ATTR_CBASIS, i, &stat) );
3519 
3520  if( stat == GRB_BASIC )
3521  bind[cnt++] = -1 - i;
3522  }
3523 
3524  for( j = 0; j < ncols; ++j )
3525  {
3526  int stat;
3527  CHECK_ZERO( lpi->messagehdlr, GRBgetintattrelement(lpi->grbmodel, GRB_INT_ATTR_VBASIS, j, &stat) );
3528 
3529  if( stat == GRB_BASIC )
3530  bind[cnt++] = j;
3531  }
3532  assert( cnt == nrows );
3533 #endif
3534 
3535  return SCIP_OKAY;
3536 }
3537 
3538 /** get dense row of inverse basis matrix B^-1 */
3540  SCIP_LPI* lpi, /**< LP interface structure */
3541  int r, /**< row number */
3542  SCIP_Real* coef /**< pointer to store the coefficients of the row */
3543  )
3544 {
3545  SVECTOR x;
3546  SVECTOR b;
3547  int nrows;
3548  double val;
3549  int ind;
3550  int k;
3551  int i;
3552  int status;
3553 
3554  assert(lpi != NULL);
3555  assert(lpi->grbmodel != NULL);
3556 
3557  SCIPdebugMessage("getting binv-row %d\n", r);
3558 
3559  /* check whether we have to reoptimize */
3560  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &status) );
3561  if ( status == GRB_LOADED || status == GRB_INTERRUPTED || status == GRB_INPROGRESS )
3562  {
3563  SCIP_CALL_QUIET( restoreLPData(lpi) );
3564  }
3565 
3566  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3567 
3568  /* set up solution vector */
3569  x.len = 0;
3570  SCIP_ALLOC( BMSallocMemoryArray(&(x.ind), nrows) );
3571  SCIP_ALLOC( BMSallocMemoryArray(&(x.val), nrows) );
3572 
3573  /* set up rhs */
3574  b.len = 1;
3575  ind = r;
3576  val = 1.0;
3577  b.ind = &ind;
3578  b.val = &val;
3579 
3580  /* solve B^T x = e_r, which results in the r-th row of the basis inverse */
3581  CHECK_ZERO( lpi->messagehdlr, GRBBSolve(lpi->grbmodel, &b, &x) );
3582 
3583  /* size should be at most the number of rows */
3584  assert( x.len <= nrows );
3585 
3586  /* copy solution to dense vector */
3587  k = 0;
3588  for (i = 0; i < nrows; ++i)
3589  {
3590  assert( k <= x.len );
3591  if ( k < x.len && (x.ind)[k] == i )
3592  coef[i] = (x.val)[k++];
3593  else
3594  coef[i] = 0.0;
3595  }
3596 
3597  /* free solution space */
3598  BMSfreeMemoryArray(&(x.val));
3599  BMSfreeMemoryArray(&(x.ind));
3600 
3601  return SCIP_OKAY;
3602 }
3603 
3604 /** get dense column of inverse basis matrix B^-1 */
3606  SCIP_LPI* lpi, /**< LP interface structure */
3607  int c, /**< column number of B^-1; this is NOT the number of the column in the LP;
3608  * you have to call SCIPlpiGetBasisInd() to get the array which links the
3609  * B^-1 column numbers to the row and column numbers of the LP!
3610  * c must be between 0 and nrows-1, since the basis has the size
3611  * nrows * nrows */
3612  SCIP_Real* coef /**< pointer to store the coefficients of the column */
3613  )
3614 {
3615  SVECTOR x;
3616  SVECTOR b;
3617  int nrows;
3618  double val;
3619  int ind;
3620  int k;
3621  int i;
3622  int status;
3623 
3624  assert(lpi != NULL);
3625  assert(lpi->grbmodel != NULL);
3626 
3627  SCIPdebugMessage("getting binv-col %d\n", c);
3628 
3629  /* check whether we have to reoptimize */
3630  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &status) );
3631  if ( status == GRB_LOADED || status == GRB_INTERRUPTED || status == GRB_INPROGRESS )
3632  {
3633  SCIP_CALL_QUIET( restoreLPData(lpi) );
3634  }
3635 
3636  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3637 
3638  /* set up solution vector */
3639  x.len = 0;
3640  SCIP_ALLOC( BMSallocMemoryArray(&(x.ind), nrows) );
3641  SCIP_ALLOC( BMSallocMemoryArray(&(x.val), nrows) );
3642 
3643  /* set up rhs */
3644  b.len = 1;
3645  ind = c;
3646  val = 1.0;
3647  b.ind = &ind;
3648  b.val = &val;
3649 
3650  /* solve B x = e_c, which results in the c-th columns of the basis inverse */
3651  CHECK_ZERO( lpi->messagehdlr, GRBFSolve(lpi->grbmodel, &b, &x) );
3652 
3653  /* size should be at most the number of rows */
3654  assert( x.len <= nrows );
3655 
3656  /* copy solution to dense vector */
3657  k = 0;
3658  for (i = 0; i < nrows; ++i)
3659  {
3660  assert( k <= x.len );
3661  if ( k < x.len && (x.ind)[k] == i )
3662  coef[i] = (x.val)[k++];
3663  else
3664  coef[i] = 0.0;
3665  }
3666 
3667  /* free solution space */
3668  BMSfreeMemoryArray(&(x.val));
3669  BMSfreeMemoryArray(&(x.ind));
3670 
3671  return SCIP_OKAY;
3672 }
3673 
3674 /** get dense row of inverse basis matrix times constraint matrix B^-1 * A */
3676  SCIP_LPI* lpi, /**< LP interface structure */
3677  int r, /**< row number */
3678  const SCIP_Real* binvrow, /**< row in (A_B)^-1 from prior call to SCIPlpiGetBInvRow(), or NULL */
3679  SCIP_Real* coef /**< vector to return coefficients */
3680  )
3681 { /*lint --e{715}*/
3682  SVECTOR x;
3683  int ncols;
3684  int nrows;
3685  int k;
3686  int j;
3687  int status;
3688 
3689  assert(lpi != NULL);
3690  assert(lpi->grbmodel != NULL);
3691 
3692  SCIPdebugMessage("getting binv-row %d\n", r);
3693 
3694  /* check whether we have to reoptimize */
3695  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &status) );
3696  if ( status == GRB_LOADED || status == GRB_INTERRUPTED || status == GRB_INPROGRESS )
3697  {
3698  SCIP_CALL_QUIET( restoreLPData(lpi) );
3699  }
3700 
3701  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3702  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3703 
3704  x.len = 0;
3705  SCIP_ALLOC( BMSallocMemoryArray(&(x.ind), ncols + nrows) );
3706  SCIP_ALLOC( BMSallocMemoryArray(&(x.val), ncols + nrows) );
3707 
3708  CHECK_ZERO( lpi->messagehdlr, GRBBinvRowi(lpi->grbmodel, r, &x) );
3709 
3710  /* size should be at most the number of columns plus rows for slack variables */
3711  assert( x.len <= ncols + nrows );
3712 
3713  k = 0;
3714  for (j = 0; j < ncols; ++j)
3715  {
3716  assert( k <= x.len );
3717  if ( k < x.len && (x.ind)[k] == j )
3718  coef[j] = (x.val)[k++];
3719  else
3720  coef[j] = 0.0;
3721  }
3722 
3723  /* free solution space */
3724  BMSfreeMemoryArray(&(x.val));
3725  BMSfreeMemoryArray(&(x.ind));
3726 
3727  return SCIP_OKAY;
3728 }
3729 
3730 /** get dense column of inverse basis matrix times constraint matrix B^-1 * A */
3732  SCIP_LPI* lpi, /**< LP interface structure */
3733  int c, /**< column number */
3734  SCIP_Real* coef /**< vector to return coefficients */
3735  )
3736 { /*lint --e{715}*/
3737  SVECTOR x;
3738  int nrows;
3739  int k;
3740  int j;
3741  int status;
3742 
3743  assert(lpi != NULL);
3744  assert(lpi->grbmodel != NULL);
3745 
3746  SCIPdebugMessage("getting binv-col %d\n", c);
3747 
3748  /* check whether we have to reoptimize */
3749  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_STATUS, &status) );
3750  if ( status == GRB_LOADED || status == GRB_INTERRUPTED || status == GRB_INPROGRESS )
3751  {
3752  SCIP_CALL_QUIET( restoreLPData(lpi) );
3753  }
3754 
3755  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3756 
3757  x.len = 0;
3758  SCIP_ALLOC( BMSallocMemoryArray(&(x.ind), nrows) );
3759  SCIP_ALLOC( BMSallocMemoryArray(&(x.val), nrows) );
3760 
3761  CHECK_ZERO( lpi->messagehdlr, GRBBinvColj(lpi->grbmodel, c, &x) );
3762 
3763  /* size should be at most the number of rows */
3764  assert( x.len <= nrows );
3765 
3766  k = 0;
3767  for (j = 0; j < nrows; ++j)
3768  {
3769  assert( k <= x.len );
3770  if ( k < x.len && (x.ind)[k] == j )
3771  coef[j] = (x.val)[k++];
3772  else
3773  coef[j] = 0.0;
3774  }
3775 
3776  /* free solution space */
3777  BMSfreeMemoryArray(&(x.val));
3778  BMSfreeMemoryArray(&(x.ind));
3779 
3780  return SCIP_OKAY;
3781 }
3782 
3783 /**@} */
3784 
3785 
3786 
3787 
3788 /*
3789  * LP State Methods
3790  */
3791 
3792 /**@name LP State Methods */
3793 /**@{ */
3794 
3795 /** stores LPi state (like basis information) into lpistate object */
3797  SCIP_LPI* lpi, /**< LP interface structure */
3798  BMS_BLKMEM* blkmem, /**< block memory */
3799  SCIP_LPISTATE** lpistate /**< pointer to LPi state information (like basis information) */
3800  )
3801 {
3802  SCIP_Bool success;
3803  int ncols;
3804  int nrows;
3805 
3806  assert(blkmem != NULL);
3807  assert(lpi != NULL);
3808  assert(lpi->grbmodel != NULL);
3809  assert(lpistate != NULL);
3810 
3811  /* if there is no basis information available, no state can be saved */
3812  if( !lpi->solisbasic )
3813  {
3814  *lpistate = NULL;
3815  return SCIP_OKAY;
3816  }
3817 
3818  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3819  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3820  assert(ncols >= 0);
3821  assert(nrows >= 0);
3822 
3823  /* get unpacked basis information from Gurobi */
3824  SCIP_CALL( getBase(lpi, &success) );
3825 
3826  if ( success )
3827  {
3828  /* allocate lpistate data */
3829  SCIP_CALL( lpistateCreate(lpistate, blkmem, ncols, nrows) );
3830  (*lpistate)->ncols = ncols;
3831  (*lpistate)->nrows = nrows;
3832 
3833  SCIPdebugMessage("stored Gurobi LPI state in %p (%d cols, %d rows)\n", (void*) *lpistate, ncols, nrows);
3834 
3835  /* pack LPi state data */
3836  lpistatePack(*lpistate, lpi->cstat, lpi->rstat);
3837  }
3838  else
3839  {
3840  /* In this case no basis information is available. Since SCIP expects the information to work
3841  in any case, we allocate the lpistate, but do not use the packed information. This might
3842  happen if the model is infeasible, since Gurobi currently does not return basis information
3843  in this case. */
3844  SCIP_ALLOC( BMSallocBlockMemory(blkmem, lpistate) );
3845  (*lpistate)->ncols = ncols;
3846  (*lpistate)->nrows = nrows;
3847  (*lpistate)->packrstat = NULL;
3848  (*lpistate)->packcstat = NULL;
3849  }
3850 
3851  return SCIP_OKAY;
3852 }
3853 
3854 /** loads LPi state (like basis information) into solver; note that the LP might have been extended with additional
3855  * columns and rows since the state was stored with SCIPlpiGetState()
3856  */
3858  SCIP_LPI* lpi, /**< LP interface structure */
3859  BMS_BLKMEM* blkmem, /**< block memory */
3860  SCIP_LPISTATE* lpistate /**< LPi state information (like basis information) */
3861  )
3862 {
3863  int ncols;
3864  int nrows;
3865  int i;
3866 
3867  assert(blkmem != NULL);
3868  assert(lpi != NULL);
3869  assert(lpi->grbmodel != NULL);
3870 
3871  /* if there was no basis information available, the LPI state was not stored */
3872  if( lpistate == NULL || lpistate->packrstat == NULL || lpistate->packcstat )
3873  return SCIP_OKAY;
3874 
3875  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMCONSTRS, &nrows) );
3876  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_NUMVARS, &ncols) );
3877  assert(lpistate->ncols <= ncols);
3878  assert(lpistate->nrows <= nrows);
3879 
3880  SCIPdebugMessage("loading LPI state %p (%d cols, %d rows) into Gurobi LP with %d cols and %d rows\n",
3881  (void*) lpistate, lpistate->ncols, lpistate->nrows, ncols, nrows);
3882 
3883  if( lpistate->ncols == 0 || lpistate->nrows == 0 )
3884  return SCIP_OKAY;
3885 
3886  /* allocate enough memory for storing uncompressed basis information */
3887  SCIP_CALL( ensureCstatMem(lpi, ncols) );
3888  SCIP_CALL( ensureRstatMem(lpi, nrows) );
3889 
3890  /* unpack LPi state data */
3891  lpistateUnpack(lpistate, lpi->cstat, lpi->rstat);
3892 
3893  /* extend the basis to the current LP beyond the previously existing columns */
3894  for( i = lpistate->ncols; i < ncols; ++i )
3895  {
3896  SCIP_Real bnd;
3897  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_LB, i, i, &bnd) );
3898  if ( SCIPlpiIsInfinity(lpi, REALABS(bnd)) )
3899  {
3900  /* if lower bound is +/- infinity -> try upper bound */
3901  CHECK_ZERO( lpi->messagehdlr, GRBgetdblattrarray(lpi->grbmodel, GRB_DBL_ATTR_UB, i, i, &bnd) );
3902  if ( SCIPlpiIsInfinity(lpi, REALABS(bnd)) )
3903  lpi->cstat[i] = SCIP_BASESTAT_ZERO; /* variable is free */
3904  else
3905  lpi->cstat[i] = SCIP_BASESTAT_UPPER; /* use finite upper bound */
3906  }
3907  else
3908  lpi->cstat[i] = SCIP_BASESTAT_LOWER; /* use finite lower bound */
3909  }
3910  for( i = lpistate->nrows; i < nrows; ++i )
3911  lpi->rstat[i] = SCIP_BASESTAT_BASIC;
3912 
3913  /* load basis information into Gurobi */
3914  SCIP_CALL( setBase(lpi) );
3915 
3916  return SCIP_OKAY;
3917 }
3918 
3919 /** clears current LPi state (like basis information) of the solver */
3921  SCIP_LPI* lpi /**< LP interface structure */
3922  )
3923 {
3924  assert(lpi != NULL);
3925 
3926  CHECK_ZERO( lpi->messagehdlr, GRBresetmodel(lpi->grbmodel) );
3927 
3928  return SCIP_OKAY;
3929 }
3930 
3931 /** frees LPi state information */
3933  SCIP_LPI* lpi, /**< LP interface structure */
3934  BMS_BLKMEM* blkmem, /**< block memory */
3935  SCIP_LPISTATE** lpistate /**< pointer to LPi state information (like basis information) */
3936  )
3937 {
3938  assert(lpi != NULL);
3939  assert(lpistate != NULL);
3940 
3941  if( *lpistate != NULL )
3942  lpistateFree(lpistate, blkmem);
3943 
3944  return SCIP_OKAY;
3945 }
3946 
3947 /** checks, whether the given LP state contains simplex basis information */
3949  SCIP_LPI* lpi, /**< LP interface structure */
3950  SCIP_LPISTATE* lpistate /**< LP state information (like basis information) */
3951  )
3952 { /*lint --e{715}*/
3953  return (lpistate != NULL);
3954 }
3955 
3956 /** reads LP state (like basis information from a file */
3958  SCIP_LPI* lpi, /**< LP interface structure */
3959  const char* fname /**< file name */
3960  )
3961 {
3962  size_t l;
3963  assert(lpi != NULL);
3964  assert(lpi->grbmodel != NULL);
3965 
3966  SCIPdebugMessage("reading LP state from file <%s>\n", fname);
3967 
3968  /* gurobi reads a basis if the extension is ".bas" */
3969  l = strlen(fname);
3970  if ( l > 4 && fname[l-4] == '.' && fname[l-3] == 'b' && fname[l-2] == 'a' && fname[l-1] == 's' )
3971  {
3972  CHECK_ZERO( lpi->messagehdlr, GRBread(lpi->grbmodel, fname) );
3973  }
3974  else
3975  {
3976  SCIPerrorMessage("To read a basis with gurobi, the extension has to be '.bas'.\n");
3977  return SCIP_LPERROR;
3978  }
3979 
3980  return SCIP_OKAY;
3981 }
3982 
3983 /** writes LP state (like basis information) to a file */
3985  SCIP_LPI* lpi, /**< LP interface structure */
3986  const char* fname /**< file name */
3987  )
3988 {
3989  size_t l;
3990  assert(lpi != NULL);
3991  assert(lpi->grbmodel != NULL);
3992 
3993  SCIPdebugMessage("writing basis state to file <%s>\n", fname);
3994 
3995  /* gurobi writes the basis if the extension is ".bas" */
3996  l = strlen(fname);
3997  if ( l > 4 && fname[l-4] == '.' && fname[l-3] == 'b' && fname[l-2] == 'a' && fname[l-1] == 's' )
3998  {
3999  CHECK_ZERO( lpi->messagehdlr, GRBwrite(lpi->grbmodel, fname) );
4000  }
4001  else
4002  {
4003  char name[SCIP_MAXSTRLEN];
4004 
4005  /* force extension to be ".bas" */
4006  snprintf(name, SCIP_MAXSTRLEN, "%s.bas", fname);
4007  CHECK_ZERO( lpi->messagehdlr, GRBwrite(lpi->grbmodel, fname) );
4008  }
4009 
4010  return SCIP_OKAY;
4011 }
4012 
4013 /**@} */
4014 
4015 
4016 
4017 
4018 /*
4019  * LP Pricing Norms Methods
4020  */
4021 
4022 /**@name LP Pricing Norms Methods */
4023 /**@{ */
4024 
4025 /** stores LPi pricing norms information
4026  * @todo should we store norm information?
4027  */
4029  SCIP_LPI* lpi, /**< LP interface structure */
4030  BMS_BLKMEM* blkmem, /**< block memory */
4031  SCIP_LPINORMS** lpinorms /**< pointer to LPi pricing norms information */
4032  )
4033 {
4034  assert(lpinorms != NULL);
4035 
4036  (*lpinorms) = NULL;
4037 
4038  return SCIP_OKAY;
4039 }
4040 
4041 /** loads LPi pricing norms into solver; note that the LP might have been extended with additional
4042  * columns and rows since the state was stored with SCIPlpiGetNorms()
4043  */
4045  SCIP_LPI* lpi, /**< LP interface structure */
4046  BMS_BLKMEM* blkmem, /**< block memory */
4047  SCIP_LPINORMS* lpinorms /**< LPi pricing norms information */
4048  )
4049 {
4050  assert(lpinorms == NULL);
4051 
4052  /* no work necessary */
4053  return SCIP_OKAY;
4054 }
4055 
4056 /** frees pricing norms information */
4058  SCIP_LPI* lpi, /**< LP interface structure */
4059  BMS_BLKMEM* blkmem, /**< block memory */
4060  SCIP_LPINORMS** lpinorms /**< pointer to LPi pricing norms information */
4061  )
4062 {
4063  assert(lpinorms == NULL);
4064 
4065  /* no work necessary */
4066  return SCIP_OKAY;
4067 }
4068 
4069 /**@} */
4070 
4071 
4072 
4073 
4074 /*
4075  * Parameter Methods
4076  */
4077 
4078 /**@name Parameter Methods */
4079 /**@{ */
4080 
4081 /** gets integer parameter of LP */
4083  SCIP_LPI* lpi, /**< LP interface structure */
4084  SCIP_LPPARAM type, /**< parameter number */
4085  int* ival /**< buffer to store the parameter value */
4086  )
4087 {
4088  int temp;
4089  SCIP_Real dtemp;
4090 
4091  assert(lpi != NULL);
4092  assert(lpi->grbmodel != NULL);
4093  assert(ival != NULL);
4094 
4095  SCIPdebugMessage("getting int parameter %d\n", type);
4096 
4097  switch( type )
4098  {
4100  *ival = (int) lpi->fromscratch;
4101  break;
4102  case SCIP_LPPAR_FASTMIP:
4103  /* maybe set perturbation */
4104  return SCIP_PARAMETERUNKNOWN;
4105  case SCIP_LPPAR_SCALING:
4106  SCIP_CALL( getIntParam(lpi, GRB_INT_PAR_SCALEFLAG, &temp) );
4107  assert( temp == 0 || temp == 1 );
4108  *ival = (temp == 1) ? TRUE : FALSE;
4109  break;
4110  case SCIP_LPPAR_PRESOLVING:
4111  SCIP_CALL( getIntParam(lpi, GRB_INT_PAR_PRESOLVE, &temp) );
4112  assert( temp == GRB_PRESOLVE_AUTO || temp == GRB_PRESOLVE_OFF || temp == GRB_PRESOLVE_CONSERVATIVE || temp == GRB_PRESOLVE_AGGRESSIVE );
4113  *ival = (temp == GRB_PRESOLVE_OFF) ? FALSE : TRUE;
4114  break;
4115  case SCIP_LPPAR_PRICING:
4116  *ival = (int) lpi->pricing;
4117  break;
4118  case SCIP_LPPAR_LPINFO:
4119  SCIP_CALL( getIntParam(lpi, GRB_INT_PAR_OUTPUTFLAG, &temp) );
4120  assert( temp == 0 || temp == 1 );
4121  *ival = (temp == 1) ? TRUE : FALSE;
4122  break;
4123  case SCIP_LPPAR_LPITLIM:
4124  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_ITERATIONLIMIT, &dtemp) );
4125  assert( dtemp >= 0.0 );
4126  if( dtemp >= GRB_INFINITY )
4127  *ival = INT_MAX;
4128  else
4129  *ival = (int) dtemp;
4130  break;
4131  default:
4132  return SCIP_PARAMETERUNKNOWN;
4133  } /*lint !e788*/
4134 
4135  return SCIP_OKAY;
4136 }
4137 
4138 /** sets integer parameter of LP */
4140  SCIP_LPI* lpi, /**< LP interface structure */
4141  SCIP_LPPARAM type, /**< parameter number */
4142  int ival /**< parameter value */
4143  )
4144 {
4145  assert(lpi != NULL);
4146  assert(lpi->grbmodel != NULL);
4147 
4148  SCIPdebugMessage("setting int parameter %d to %d\n", type, ival);
4149 
4150  switch( type )
4151  {
4153  assert(ival == TRUE || ival == FALSE);
4154  lpi->fromscratch = (SCIP_Bool) ival;
4155  break;
4156  case SCIP_LPPAR_FASTMIP:
4157  assert(ival == TRUE || ival == FALSE);
4158  return SCIP_PARAMETERUNKNOWN;
4159  break;
4160  case SCIP_LPPAR_SCALING:
4161  assert(ival == TRUE || ival == FALSE);
4162  if( ival )
4163  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SCALEFLAG, 1) );
4164  else
4165  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SCALEFLAG, 0) );
4166  break;
4167  case SCIP_LPPAR_PRESOLVING:
4168  assert(ival == TRUE || ival == FALSE);
4169  if( ival )
4170  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_AUTO) );
4171  else
4172  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF) );
4173  break;
4174  case SCIP_LPPAR_PRICING:
4175  lpi->pricing = (SCIP_PRICING)ival;
4176  switch( (SCIP_PRICING)ival )
4177  {
4179  case SCIP_PRICING_AUTO:
4180  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_AUTO) );
4181  break;
4182  case SCIP_PRICING_FULL:
4183  /* full does not seem to exist -> use auto */
4184  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_AUTO) );
4185  break;
4186  case SCIP_PRICING_PARTIAL:
4187  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_PARTIAL) );
4188  break;
4189  case SCIP_PRICING_STEEP:
4190  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_STEEPEST_EDGE) );
4191  break;
4193  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_STEEPEST_QUICK) );
4194  break;
4195  case SCIP_PRICING_DEVEX:
4196  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_SIMPLEXPRICING, GRB_SIMPLEXPRICING_DEVEX) );
4197  break;
4198  default:
4199  return SCIP_LPERROR;
4200  }
4201  break;
4202  case SCIP_LPPAR_LPINFO:
4203  assert(ival == TRUE || ival == FALSE);
4204  if( ival )
4205  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_OUTPUTFLAG, 1) );
4206  else
4207  SCIP_CALL( setIntParam(lpi, GRB_INT_PAR_OUTPUTFLAG, 0) );
4208  break;
4209  case SCIP_LPPAR_LPITLIM:
4210  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_ITERATIONLIMIT, (double) ival) );
4211  break;
4212  default:
4213  return SCIP_PARAMETERUNKNOWN;
4214  } /*lint !e788*/
4215 
4216  return SCIP_OKAY;
4217 }
4218 
4219 /** gets floating point parameter of LP */
4221  SCIP_LPI* lpi, /**< LP interface structure */
4222  SCIP_LPPARAM type, /**< parameter number */
4223  SCIP_Real* dval /**< buffer to store the parameter value */
4224  )
4225 {
4226  int objsen;
4227 
4228  assert(lpi != NULL);
4229  assert(lpi->grbmodel != NULL);
4230  assert(dval != NULL);
4231 
4232  SCIPdebugMessage("getting real parameter %d\n", type);
4233 
4234  switch( type )
4235  {
4236  case SCIP_LPPAR_FEASTOL:
4237  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_FEASIBILITYTOL, dval) );
4238  break;
4240  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_OPTIMALITYTOL, dval) );
4241  break;
4243  return SCIP_PARAMETERUNKNOWN;
4244  break;
4245  case SCIP_LPPAR_LOBJLIM:
4246  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &objsen) );
4247  if( objsen == 1 )
4248  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_CUTOFF, dval) );
4249  else
4250  return SCIP_PARAMETERUNKNOWN;
4251  break;
4252  case SCIP_LPPAR_UOBJLIM:
4253  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &objsen) );
4254  if( objsen == 0 )
4255  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_CUTOFF, dval) );
4256  else
4257  return SCIP_PARAMETERUNKNOWN;
4258  break;
4259  case SCIP_LPPAR_LPTILIM:
4260  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_TIMELIMIT, dval) );
4261  break;
4262  case SCIP_LPPAR_MARKOWITZ:
4263  SCIP_CALL( getDblParam(lpi, GRB_DBL_PAR_MARKOWITZTOL, dval) );
4264  break;
4265  default:
4266  return SCIP_PARAMETERUNKNOWN;
4267  } /*lint !e788*/
4268 
4269  return SCIP_OKAY;
4270 }
4271 
4272 /** sets floating point parameter of LP */
4274  SCIP_LPI* lpi, /**< LP interface structure */
4275  SCIP_LPPARAM type, /**< parameter number */
4276  SCIP_Real dval /**< parameter value */
4277  )
4278 {
4279  int objsen;
4280 
4281  assert(lpi != NULL);
4282  assert(lpi->grbmodel != NULL);
4283 
4284  SCIPdebugMessage("setting real parameter %d to %g\n", type, dval);
4285 
4286  switch( type )
4287  {
4288  case SCIP_LPPAR_FEASTOL:
4289  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_FEASIBILITYTOL, dval) );
4290  break;
4292  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_OPTIMALITYTOL, dval) );
4293  break;
4295  return SCIP_PARAMETERUNKNOWN;
4296  break;
4297  case SCIP_LPPAR_LOBJLIM:
4298  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &objsen) );
4299  if( objsen == 1 )
4300  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_CUTOFF, dval) );
4301  break;
4302  case SCIP_LPPAR_UOBJLIM:
4303  CHECK_ZERO( lpi->messagehdlr, GRBgetintattr(lpi->grbmodel, GRB_INT_ATTR_MODELSENSE, &objsen) );
4304  if( objsen == 0 )
4305  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_CUTOFF, dval) );
4306  break;
4307  case SCIP_LPPAR_LPTILIM:
4308  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_TIMELIMIT, dval) );
4309  break;
4310  case SCIP_LPPAR_MARKOWITZ:
4311  SCIP_CALL( setDblParam(lpi, GRB_DBL_PAR_MARKOWITZTOL, dval) );
4312  break;
4313  default:
4314  return SCIP_PARAMETERUNKNOWN;
4315  } /*lint !e788*/
4316 
4317  return SCIP_OKAY;
4318 }
4319 
4320 /**@} */
4321 
4322 
4323 
4324 
4325 /*
4326  * Numerical Methods
4327  */
4328 
4329 /**@name Numerical Methods */
4330 /**@{ */
4331 
4332 /** returns value treated as infinity in the LP solver */
4334  SCIP_LPI* lpi /**< LP interface structure */
4335  )
4336 { /*lint --e{715}*/
4337  return GRB_INFINITY;
4338 }
4339 
4340 /** checks if given value is treated as infinity in the LP solver */
4342  SCIP_LPI* lpi, /**< LP interface structure */
4343  SCIP_Real val /**< value to be checked for infinity */
4344  )
4345 { /*lint --e{715}*/
4346  return (val >= GRB_INFINITY);
4347 }
4348 
4349 /**@} */
4350 
4351 
4352 
4353 
4354 /*
4355  * File Interface Methods
4356  */
4357 
4358 /**@name File Interface Methods */
4359 /**@{ */
4360 
4361 /** reads LP from a file */
4363  SCIP_LPI* lpi, /**< LP interface structure */
4364  const char* fname /**< file name */
4365  )
4366 {
4367  assert(lpi != NULL);
4368  assert(lpi->grbmodel != NULL);
4369 
4370  SCIPdebugMessage("reading LP from file <%s>\n", fname);
4371 
4372  CHECK_ZERO( lpi->messagehdlr, GRBread(lpi->grbmodel, fname) );
4373 
4374  return SCIP_OKAY;
4375 }
4376 
4377 /** writes LP to a file */
4379  SCIP_LPI* lpi, /**< LP interface structure */
4380  const char* fname /**< file name */
4381  )
4382 {
4383  assert(lpi != NULL);
4384  assert(lpi->grbmodel != NULL);
4385 
4386  SCIPdebugMessage("writing LP to file <%s>\n", fname);
4387 
4388  CHECK_ZERO( lpi->messagehdlr, GRBwrite(lpi->grbmodel, fname) );
4389 
4390  return SCIP_OKAY;
4391 }
4392 
4393 /**@} */
4394