| 1 | //----------------------------------------------------------------------------- |
| 2 | // name: OSI Interface for Gurobi |
| 3 | // template: OSI Cplex Interface written by T. Achterberg |
| 4 | // author: Stefan Vigerske |
| 5 | // Humboldt University Berlin |
| 6 | // license: this file may be freely distributed under the terms of EPL |
| 7 | // comments: please scan this file for '???' and 'TODO' and read the comments |
| 8 | //----------------------------------------------------------------------------- |
| 9 | // Copyright (C) 2009 Humboldt University Berlin and others. |
| 10 | // All Rights Reserved. |
| 11 | |
| 12 | // $Id: OsiGrbSolverInterface.cpp 2197 2019-01-06 23:00:34Z unxusr $ |
| 13 | |
| 14 | #include <iostream> |
| 15 | #include <cassert> |
| 16 | #include <string> |
| 17 | #include <numeric> |
| 18 | |
| 19 | #include "CoinPragma.hpp" |
| 20 | #include "CoinError.hpp" |
| 21 | |
| 22 | #include "OsiConfig.h" |
| 23 | #include "OsiGrbSolverInterface.hpp" |
| 24 | #include "OsiCuts.hpp" |
| 25 | #include "OsiRowCut.hpp" |
| 26 | #include "OsiColCut.hpp" |
| 27 | #include "CoinPackedMatrix.hpp" |
| 28 | #include "CoinWarmStartBasis.hpp" |
| 29 | |
| 30 | extern "C" { |
| 31 | #include "gurobi_c.h" |
| 32 | } |
| 33 | |
| 34 | /* A utility definition which allows for easy suppression of unused variable warnings from GCC. |
| 35 | */ |
| 36 | #ifndef UNUSED |
| 37 | #if defined(__GNUC__) |
| 38 | #define UNUSED __attribute__((unused)) |
| 39 | #else |
| 40 | #define UNUSED |
| 41 | #endif |
| 42 | #endif |
| 43 | |
| 44 | //#define DEBUG 1 |
| 45 | |
| 46 | #ifdef DEBUG |
| 47 | #define debugMessage printf("line %4d: ", __LINE__) & printf |
| 48 | #else |
| 49 | #define debugMessage \ |
| 50 | if (false) \ |
| 51 | printf |
| 52 | #endif |
| 53 | |
| 54 | #define GUROBI_CALL(m, x) \ |
| 55 | do { \ |
| 56 | int _retcode; \ |
| 57 | if ((_retcode = (x)) != 0) { \ |
| 58 | char s[1001]; \ |
| 59 | if (OsiGrbSolverInterface::globalenv_) { \ |
| 60 | sprintf(s, "Error <%d> from GUROBI function call: ", _retcode); \ |
| 61 | strncat(s, GRBgeterrormsg(OsiGrbSolverInterface::globalenv_), 1000); \ |
| 62 | } else \ |
| 63 | sprintf(s, "Error <%d> from GUROBI function call (no license?).", _retcode); \ |
| 64 | debugMessage("%s:%d: %s", __FILE__, __LINE__, s); \ |
| 65 | throw CoinError(s, m, "OsiGrbSolverInterface", __FILE__, __LINE__); \ |
| 66 | } \ |
| 67 | } while (false) |
| 68 | |
| 69 | template < bool > |
| 70 | struct CompileTimeAssert; |
| 71 | template <> |
| 72 | struct CompileTimeAssert< true > { |
| 73 | }; |
| 74 | |
| 75 | #if GRB_VERSION_MAJOR < 4 |
| 76 | #define GRB_METHOD_DUAL GRB_LPMETHOD_DUAL |
| 77 | #define GRB_METHOD_PRIMAL GRB_LPMETHOD_PRIMAL |
| 78 | #define GRB_INT_PAR_METHOD GRB_INT_PAR_LPMETHOD |
| 79 | #endif |
| 80 | |
| 81 | //############################################################################# |
| 82 | // A couple of helper functions |
| 83 | //############################################################################# |
| 84 | |
| 85 | inline void freeCacheDouble(double *&ptr) |
| 86 | { |
| 87 | if (ptr != NULL) { |
| 88 | delete[] ptr; |
| 89 | ptr = NULL; |
| 90 | } |
| 91 | } |
| 92 | |
| 93 | inline void freeCacheChar(char *&ptr) |
| 94 | { |
| 95 | if (ptr != NULL) { |
| 96 | delete[] ptr; |
| 97 | ptr = NULL; |
| 98 | } |
| 99 | } |
| 100 | |
| 101 | inline void freeCacheMatrix(CoinPackedMatrix *&ptr) |
| 102 | { |
| 103 | if (ptr != NULL) { |
| 104 | delete ptr; |
| 105 | ptr = NULL; |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | void OsiGrbSolverInterface::switchToLP(void) |
| 110 | { |
| 111 | debugMessage("OsiGrbSolverInterface::switchToLP()\n"); |
| 112 | |
| 113 | if (probtypemip_) { |
| 114 | GRBmodel *lp = getMutableLpPtr(); |
| 115 | int nc = getNumCols(); |
| 116 | |
| 117 | char *contattr = new char[nc]; |
| 118 | CoinFillN(contattr, nc, 'C'); |
| 119 | |
| 120 | GUROBI_CALL("switchToLP", GRBsetcharattrarray(lp, GRB_CHAR_ATTR_VTYPE, 0, nc, contattr)); |
| 121 | |
| 122 | delete[] contattr; |
| 123 | |
| 124 | probtypemip_ = false; |
| 125 | } |
| 126 | } |
| 127 | |
| 128 | void OsiGrbSolverInterface::switchToMIP(void) |
| 129 | { |
| 130 | debugMessage("OsiGrbSolverInterface::switchToMIP()\n"); |
| 131 | |
| 132 | if (!probtypemip_) { |
| 133 | GRBmodel *lp = getMutableLpPtr(); |
| 134 | int nc = getNumCols(); |
| 135 | |
| 136 | assert(coltype_ != NULL); |
| 137 | |
| 138 | GUROBI_CALL("switchToMIP", GRBsetcharattrarray(lp, GRB_CHAR_ATTR_VTYPE, 0, nc, coltype_)); |
| 139 | |
| 140 | probtypemip_ = true; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | void OsiGrbSolverInterface::resizeColSpace(int minsize) |
| 145 | { |
| 146 | debugMessage("OsiGrbSolverInterface::resizeColSpace()\n"); |
| 147 | |
| 148 | if (minsize > colspace_) { |
| 149 | int newcolspace = 2 * colspace_; |
| 150 | if (minsize > newcolspace) |
| 151 | newcolspace = minsize; |
| 152 | char *newcoltype = new char[newcolspace]; |
| 153 | |
| 154 | if (coltype_ != NULL) { |
| 155 | CoinDisjointCopyN(coltype_, colspace_, newcoltype); |
| 156 | delete[] coltype_; |
| 157 | } |
| 158 | coltype_ = newcoltype; |
| 159 | |
| 160 | if (colmap_O2G != NULL) { |
| 161 | int *newcolmap_O2G = new int[newcolspace]; |
| 162 | CoinDisjointCopyN(colmap_O2G, colspace_, newcolmap_O2G); |
| 163 | delete[] colmap_O2G; |
| 164 | colmap_O2G = newcolmap_O2G; |
| 165 | } |
| 166 | |
| 167 | if (colmap_G2O != NULL) { |
| 168 | int *newcolmap_G2O = new int[newcolspace + auxcolspace]; |
| 169 | CoinDisjointCopyN(colmap_G2O, colspace_ + auxcolspace, newcolmap_G2O); |
| 170 | delete[] colmap_G2O; |
| 171 | colmap_G2O = newcolmap_G2O; |
| 172 | } |
| 173 | |
| 174 | colspace_ = newcolspace; |
| 175 | } |
| 176 | assert(minsize == 0 || coltype_ != NULL); |
| 177 | assert(colspace_ >= minsize); |
| 178 | } |
| 179 | |
| 180 | void OsiGrbSolverInterface::freeColSpace() |
| 181 | { |
| 182 | debugMessage("OsiGrbSolverInterface::freeColSpace()\n"); |
| 183 | |
| 184 | if (colspace_ > 0) { |
| 185 | delete[] coltype_; |
| 186 | delete[] colmap_O2G; |
| 187 | delete[] colmap_G2O; |
| 188 | coltype_ = NULL; |
| 189 | colmap_O2G = NULL; |
| 190 | colmap_G2O = NULL; |
| 191 | colspace_ = 0; |
| 192 | auxcolspace = 0; |
| 193 | } |
| 194 | assert(coltype_ == NULL); |
| 195 | assert(colmap_O2G == NULL); |
| 196 | assert(colmap_G2O == NULL); |
| 197 | } |
| 198 | |
| 199 | void OsiGrbSolverInterface::resizeAuxColSpace(int minsize) |
| 200 | { |
| 201 | debugMessage("OsiGrbSolverInterface::resizeAuxColSpace()\n"); |
| 202 | |
| 203 | if (minsize > auxcolspace) { |
| 204 | int newauxcolspace = 2 * auxcolspace; |
| 205 | if (minsize > newauxcolspace) |
| 206 | newauxcolspace = minsize; |
| 207 | |
| 208 | if (colmap_G2O != NULL) { |
| 209 | int *newcolmap_G2O = new int[colspace_ + newauxcolspace]; |
| 210 | CoinDisjointCopyN(colmap_G2O, colspace_ + auxcolspace, newcolmap_G2O); |
| 211 | delete[] colmap_G2O; |
| 212 | colmap_G2O = newcolmap_G2O; |
| 213 | } |
| 214 | |
| 215 | auxcolspace = newauxcolspace; |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /// resizes auxcolind vector to current number of rows and inits values to -1 |
| 220 | void OsiGrbSolverInterface::resizeAuxColIndSpace() |
| 221 | { |
| 222 | debugMessage("OsiGrbSolverInterface::resizeAuxColIndSpace()\n"); |
| 223 | |
| 224 | int numrows = getNumRows(); |
| 225 | if (auxcolindspace < numrows) { |
| 226 | int newspace = 2 * auxcolindspace; |
| 227 | if (numrows > newspace) |
| 228 | newspace = numrows; |
| 229 | |
| 230 | int *newauxcolind = new int[newspace]; |
| 231 | |
| 232 | if (auxcolindspace > 0) |
| 233 | CoinDisjointCopyN(auxcolind, auxcolindspace, newauxcolind); |
| 234 | for (int i = auxcolindspace; i < newspace; ++i) |
| 235 | newauxcolind[i] = -1; |
| 236 | |
| 237 | delete[] auxcolind; |
| 238 | auxcolind = newauxcolind; |
| 239 | auxcolindspace = newspace; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | //############################################################################# |
| 244 | // Solve methods |
| 245 | //############################################################################# |
| 246 | |
| 247 | void OsiGrbSolverInterface::initialSolve() |
| 248 | { |
| 249 | debugMessage("OsiGrbSolverInterface::initialSolve()\n"); |
| 250 | bool takeHint; |
| 251 | OsiHintStrength strength; |
| 252 | int prevalgorithm = -1; |
| 253 | |
| 254 | switchToLP(); |
| 255 | |
| 256 | GRBmodel *lp = getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS); |
| 257 | |
| 258 | /* set whether dual or primal, if hint has been given */ |
| 259 | getHintParam(OsiDoDualInInitial, takeHint, strength); |
| 260 | if (strength != OsiHintIgnore) { |
| 261 | GUROBI_CALL("initialSolve", GRBgetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, &prevalgorithm)); |
| 262 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, takeHint ? GRB_METHOD_DUAL : GRB_METHOD_PRIMAL)); |
| 263 | } |
| 264 | |
| 265 | /* set whether presolve or not */ |
| 266 | int presolve = GRB_PRESOLVE_AUTO; |
| 267 | getHintParam(OsiDoPresolveInInitial, takeHint, strength); |
| 268 | if (strength != OsiHintIgnore) |
| 269 | presolve = takeHint ? GRB_PRESOLVE_AUTO : GRB_PRESOLVE_OFF; |
| 270 | |
| 271 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, presolve)); |
| 272 | |
| 273 | /* set whether output or not */ |
| 274 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_OUTPUTFLAG, (messageHandler()->logLevel() > 0))); |
| 275 | |
| 276 | /* optimize */ |
| 277 | GUROBI_CALL("initialSolve", GRBoptimize(lp)); |
| 278 | |
| 279 | /* reoptimize without presolve if status unclear */ |
| 280 | int stat; |
| 281 | GUROBI_CALL("initialSolve", GRBgetintattr(lp, GRB_INT_ATTR_STATUS, &stat)); |
| 282 | |
| 283 | if (stat == GRB_INF_OR_UNBD && presolve != GRB_PRESOLVE_OFF) { |
| 284 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF)); |
| 285 | GUROBI_CALL("initialSolve", GRBoptimize(lp)); |
| 286 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, presolve)); |
| 287 | } |
| 288 | |
| 289 | /* reset method parameter, if changed */ |
| 290 | if (prevalgorithm != -1) |
| 291 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, prevalgorithm)); |
| 292 | } |
| 293 | |
| 294 | //----------------------------------------------------------------------------- |
| 295 | void OsiGrbSolverInterface::resolve() |
| 296 | { |
| 297 | debugMessage("OsiGrbSolverInterface::resolve()\n"); |
| 298 | bool takeHint; |
| 299 | OsiHintStrength strength; |
| 300 | int prevalgorithm = -1; |
| 301 | |
| 302 | switchToLP(); |
| 303 | |
| 304 | GRBmodel *lp = getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS); |
| 305 | |
| 306 | /* set whether primal or dual */ |
| 307 | getHintParam(OsiDoDualInResolve, takeHint, strength); |
| 308 | if (strength != OsiHintIgnore) { |
| 309 | GUROBI_CALL("resolve", GRBgetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, &prevalgorithm)); |
| 310 | GUROBI_CALL("resolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, takeHint ? GRB_METHOD_DUAL : GRB_METHOD_PRIMAL)); |
| 311 | } |
| 312 | |
| 313 | /* set whether presolve or not */ |
| 314 | int presolve = GRB_PRESOLVE_OFF; |
| 315 | getHintParam(OsiDoPresolveInResolve, takeHint, strength); |
| 316 | if (strength != OsiHintIgnore) |
| 317 | presolve = takeHint ? GRB_PRESOLVE_AUTO : GRB_PRESOLVE_OFF; |
| 318 | |
| 319 | GUROBI_CALL("resolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, presolve)); |
| 320 | |
| 321 | /* set whether output or not */ |
| 322 | GUROBI_CALL("resolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_OUTPUTFLAG, (messageHandler()->logLevel() > 0))); |
| 323 | |
| 324 | /* optimize */ |
| 325 | GUROBI_CALL("resolve", GRBoptimize(lp)); |
| 326 | |
| 327 | /* reoptimize if status unclear */ |
| 328 | int stat; |
| 329 | GUROBI_CALL("resolve", GRBgetintattr(lp, GRB_INT_ATTR_STATUS, &stat)); |
| 330 | |
| 331 | if (stat == GRB_INF_OR_UNBD && presolve != GRB_PRESOLVE_OFF) { |
| 332 | GUROBI_CALL("resolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, GRB_PRESOLVE_OFF)); |
| 333 | GUROBI_CALL("resolve", GRBoptimize(lp)); |
| 334 | GUROBI_CALL("resolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_PRESOLVE, presolve)); |
| 335 | } |
| 336 | |
| 337 | /* reset method parameter, if changed */ |
| 338 | if (prevalgorithm != -1) |
| 339 | GUROBI_CALL("initialSolve", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_METHOD, prevalgorithm)); |
| 340 | } |
| 341 | |
| 342 | //----------------------------------------------------------------------------- |
| 343 | void OsiGrbSolverInterface::branchAndBound() |
| 344 | { |
| 345 | debugMessage("OsiGrbSolverInterface::branchAndBound()\n"); |
| 346 | |
| 347 | switchToMIP(); |
| 348 | |
| 349 | if (colsol_ != NULL && domipstart) { |
| 350 | int *discridx = new int[getNumIntegers()]; |
| 351 | double *discrval = new double[getNumIntegers()]; |
| 352 | |
| 353 | int j = 0; |
| 354 | for (int i = 0; i < getNumCols() && j < getNumIntegers(); ++i) { |
| 355 | if (!isInteger(i) && !isBinary(i)) |
| 356 | continue; |
| 357 | discridx[j] = i; |
| 358 | discrval[j] = colsol_[i]; |
| 359 | ++j; |
| 360 | } |
| 361 | assert(j == getNumIntegers()); |
| 362 | |
| 363 | GUROBI_CALL("branchAndBound", GRBsetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_START, getNumIntegers(), discridx, discrval)); |
| 364 | |
| 365 | delete[] discridx; |
| 366 | delete[] discrval; |
| 367 | } |
| 368 | |
| 369 | GRBmodel *lp = getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS); |
| 370 | |
| 371 | GUROBI_CALL("branchAndBound", GRBsetintparam(GRBgetenv(lp), GRB_INT_PAR_OUTPUTFLAG, (messageHandler()->logLevel() > 0))); |
| 372 | |
| 373 | GUROBI_CALL("branchAndBound", GRBoptimize(lp)); |
| 374 | } |
| 375 | |
| 376 | //############################################################################# |
| 377 | // Parameter related methods |
| 378 | //############################################################################# |
| 379 | |
| 380 | bool OsiGrbSolverInterface::setIntParam(OsiIntParam key, int value) |
| 381 | { |
| 382 | debugMessage("OsiGrbSolverInterface::setIntParam(%d, %d)\n", key, value); |
| 383 | |
| 384 | switch (key) { |
| 385 | case OsiMaxNumIteration: |
| 386 | if (GRBsetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_ITERATIONLIMIT, (double)value) != 0) { |
| 387 | *messageHandler() << "Warning: Setting GUROBI iteration limit to" << value << "failed." << CoinMessageEol; |
| 388 | if (OsiGrbSolverInterface::globalenv_) |
| 389 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 390 | return false; |
| 391 | } |
| 392 | return true; |
| 393 | |
| 394 | case OsiMaxNumIterationHotStart: |
| 395 | if (value >= 0) { |
| 396 | hotStartMaxIteration_ = value; |
| 397 | return true; |
| 398 | } |
| 399 | return false; |
| 400 | |
| 401 | case OsiNameDiscipline: |
| 402 | if (value < 0 || value > 3) |
| 403 | return false; |
| 404 | nameDisc_ = value; |
| 405 | return true; |
| 406 | |
| 407 | case OsiLastIntParam: |
| 408 | default: |
| 409 | return false; |
| 410 | } |
| 411 | } |
| 412 | |
| 413 | //----------------------------------------------------------------------------- |
| 414 | |
| 415 | bool OsiGrbSolverInterface::setDblParam(OsiDblParam key, double value) |
| 416 | { |
| 417 | debugMessage("OsiGrbSolverInterface::setDblParam(%d, %g)\n", key, value); |
| 418 | |
| 419 | switch (key) { |
| 420 | // Gurobi does not have primal or dual objective limits |
| 421 | // case OsiDualObjectiveLimit: |
| 422 | // break; |
| 423 | // case OsiPrimalObjectiveLimit: |
| 424 | // break; |
| 425 | case OsiDualTolerance: |
| 426 | if (GRBsetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_OPTIMALITYTOL, value) != 0) { |
| 427 | *messageHandler() << "Warning: Setting GUROBI dual (i.e., optimality) tolerance to" << value << "failed." << CoinMessageEol; |
| 428 | if (OsiGrbSolverInterface::globalenv_) |
| 429 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 430 | return false; |
| 431 | } |
| 432 | return true; |
| 433 | case OsiPrimalTolerance: |
| 434 | if (GRBsetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_FEASIBILITYTOL, value) != 0) { |
| 435 | *messageHandler() << "Warning: Setting GUROBI primal (i.e., feasiblity) tolerance to" << value << "failed." << CoinMessageEol; |
| 436 | if (OsiGrbSolverInterface::globalenv_) |
| 437 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 438 | return false; |
| 439 | } |
| 440 | return true; |
| 441 | case OsiObjOffset: |
| 442 | return OsiSolverInterface::setDblParam(key, value); |
| 443 | case OsiLastDblParam: |
| 444 | default: |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | //----------------------------------------------------------------------------- |
| 450 | |
| 451 | bool OsiGrbSolverInterface::setStrParam(OsiStrParam key, const std::string &value) |
| 452 | { |
| 453 | debugMessage("OsiGrbSolverInterface::setStrParam(%d, %s)\n", key, value.c_str()); |
| 454 | |
| 455 | switch (key) { |
| 456 | case OsiProbName: |
| 457 | GUROBI_CALL("setStrParam", GRBsetstrattr(getLpPtr(), GRB_STR_ATTR_MODELNAME, const_cast< char * >(value.c_str()))); |
| 458 | return true; |
| 459 | |
| 460 | case OsiSolverName: |
| 461 | case OsiLastStrParam: |
| 462 | default: |
| 463 | return false; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | // Set a hint parameter |
| 468 | bool OsiGrbSolverInterface::setHintParam(OsiHintParam key, bool yesNo, OsiHintStrength strength, void *otherInfo) |
| 469 | { |
| 470 | debugMessage("OsiGrbSolverInterface::setHintParam(%d, %d)\n", key, yesNo); |
| 471 | |
| 472 | if (key == OsiDoScale) { |
| 473 | GUROBI_CALL("setHintParam", GRBsetintparam(GRBgetenv(getMutableLpPtr()), GRB_INT_PAR_SCALEFLAG, yesNo)); |
| 474 | OsiSolverInterface::setHintParam(key, yesNo, strength, otherInfo); |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | return OsiSolverInterface::setHintParam(key, yesNo, strength, otherInfo); |
| 479 | } |
| 480 | |
| 481 | //----------------------------------------------------------------------------- |
| 482 | |
| 483 | bool OsiGrbSolverInterface::getIntParam(OsiIntParam key, int &value) const |
| 484 | { |
| 485 | debugMessage("OsiGrbSolverInterface::getIntParam(%d)\n", key); |
| 486 | |
| 487 | switch (key) { |
| 488 | case OsiMaxNumIteration: |
| 489 | double dblval; |
| 490 | GUROBI_CALL("getIntParam", GRBupdatemodel(getMutableLpPtr())); |
| 491 | GUROBI_CALL("getIntParam", GRBgetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_ITERATIONLIMIT, &dblval)); |
| 492 | value = (int)dblval; |
| 493 | return true; |
| 494 | |
| 495 | case OsiMaxNumIterationHotStart: |
| 496 | value = hotStartMaxIteration_; |
| 497 | return true; |
| 498 | |
| 499 | case OsiNameDiscipline: |
| 500 | value = nameDisc_; |
| 501 | return true; |
| 502 | |
| 503 | case OsiLastIntParam: |
| 504 | default: |
| 505 | return false; |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | //----------------------------------------------------------------------------- |
| 510 | |
| 511 | bool OsiGrbSolverInterface::getDblParam(OsiDblParam key, double &value) const |
| 512 | { |
| 513 | debugMessage("OsiGrbSolverInterface::getDblParam(%d)\n", key); |
| 514 | |
| 515 | GUROBI_CALL("getDblParam", GRBupdatemodel(getMutableLpPtr())); |
| 516 | |
| 517 | switch (key) { |
| 518 | // Gurobi does not have primal or dual objective limits |
| 519 | // case OsiDualObjectiveLimit: |
| 520 | // break; |
| 521 | // case OsiPrimalObjectiveLimit: |
| 522 | // break; |
| 523 | case OsiDualTolerance: |
| 524 | GUROBI_CALL("getDblParam", GRBgetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_OPTIMALITYTOL, &value)); |
| 525 | return true; |
| 526 | case OsiPrimalTolerance: |
| 527 | GUROBI_CALL("getDblParam", GRBgetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_FEASIBILITYTOL, &value)); |
| 528 | return true; |
| 529 | case OsiObjOffset: |
| 530 | return OsiSolverInterface::getDblParam(key, value); |
| 531 | case OsiLastDblParam: |
| 532 | default: |
| 533 | return false; |
| 534 | } |
| 535 | } |
| 536 | |
| 537 | //----------------------------------------------------------------------------- |
| 538 | |
| 539 | bool OsiGrbSolverInterface::getStrParam(OsiStrParam key, std::string &value) const |
| 540 | { |
| 541 | debugMessage("OsiGrbSolverInterface::getStrParam(%d)\n", key); |
| 542 | |
| 543 | switch (key) { |
| 544 | case OsiProbName: { |
| 545 | char *name = NULL; |
| 546 | GUROBI_CALL("getStrParam", GRBgetstrattr(getMutableLpPtr(), GRB_STR_ATTR_MODELNAME, &name)); |
| 547 | assert(name != NULL); |
| 548 | value = name; |
| 549 | return true; |
| 550 | } |
| 551 | |
| 552 | case OsiSolverName: |
| 553 | value = "gurobi"; |
| 554 | return true; |
| 555 | |
| 556 | case OsiLastStrParam: |
| 557 | default: |
| 558 | return false; |
| 559 | } |
| 560 | } |
| 561 | |
| 562 | // Get a hint parameter |
| 563 | bool OsiGrbSolverInterface::getHintParam(OsiHintParam key, bool &yesNo, OsiHintStrength &strength, void *&otherInformation) const |
| 564 | { |
| 565 | debugMessage("OsiGrbSolverInterface::getHintParam[1](%d)\n", key); |
| 566 | if (key == OsiDoScale) { |
| 567 | OsiSolverInterface::getHintParam(key, yesNo, strength, otherInformation); |
| 568 | GUROBI_CALL("getHintParam", GRBupdatemodel(getMutableLpPtr())); |
| 569 | int value; |
| 570 | GUROBI_CALL("getHintParam", GRBgetintparam(GRBgetenv(getMutableLpPtr()), GRB_INT_PAR_SCALEFLAG, &value)); |
| 571 | yesNo = value; |
| 572 | return true; |
| 573 | } |
| 574 | |
| 575 | return OsiSolverInterface::getHintParam(key, yesNo, strength, otherInformation); |
| 576 | } |
| 577 | |
| 578 | // Get a hint parameter |
| 579 | bool OsiGrbSolverInterface::getHintParam(OsiHintParam key, bool &yesNo, OsiHintStrength &strength) const |
| 580 | { |
| 581 | debugMessage("OsiGrbSolverInterface::getHintParam[2](%d)\n", key); |
| 582 | if (key == OsiDoScale) { |
| 583 | OsiSolverInterface::getHintParam(key, yesNo, strength); |
| 584 | GUROBI_CALL("getHintParam", GRBupdatemodel(getMutableLpPtr())); |
| 585 | int value; |
| 586 | GUROBI_CALL("getHintParam", GRBgetintparam(GRBgetenv(getMutableLpPtr()), GRB_INT_PAR_SCALEFLAG, &value)); |
| 587 | yesNo = value; |
| 588 | return true; |
| 589 | } |
| 590 | |
| 591 | return OsiSolverInterface::getHintParam(key, yesNo, strength); |
| 592 | } |
| 593 | |
| 594 | // Get a hint parameter |
| 595 | bool OsiGrbSolverInterface::getHintParam(OsiHintParam key, bool &yesNo) const |
| 596 | { |
| 597 | debugMessage("OsiGrbSolverInterface::getHintParam[3](%d)\n", key); |
| 598 | if (key == OsiDoScale) { |
| 599 | OsiSolverInterface::getHintParam(key, yesNo); |
| 600 | GUROBI_CALL("getHintParam", GRBupdatemodel(getMutableLpPtr())); |
| 601 | int value; |
| 602 | GUROBI_CALL("getHintParam", GRBgetintparam(GRBgetenv(getMutableLpPtr()), GRB_INT_PAR_SCALEFLAG, &value)); |
| 603 | yesNo = value; |
| 604 | return true; |
| 605 | } |
| 606 | |
| 607 | return OsiSolverInterface::getHintParam(key, yesNo); |
| 608 | } |
| 609 | |
| 610 | //############################################################################# |
| 611 | // Methods returning info on how the solution process terminated |
| 612 | //############################################################################# |
| 613 | |
| 614 | bool OsiGrbSolverInterface::isAbandoned() const |
| 615 | { |
| 616 | debugMessage("OsiGrbSolverInterface::isAbandoned()\n"); |
| 617 | |
| 618 | GUROBI_CALL("isAbandoned", GRBupdatemodel(getMutableLpPtr())); |
| 619 | |
| 620 | int stat; |
| 621 | GUROBI_CALL("isAbandoned", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &stat)); |
| 622 | |
| 623 | return ( |
| 624 | stat == GRB_LOADED || stat == GRB_NUMERIC || stat == GRB_INTERRUPTED); |
| 625 | } |
| 626 | |
| 627 | bool OsiGrbSolverInterface::isProvenOptimal() const |
| 628 | { |
| 629 | debugMessage("OsiGrbSolverInterface::isProvenOptimal()\n"); |
| 630 | |
| 631 | GUROBI_CALL("isProvenOptimal", GRBupdatemodel(getMutableLpPtr())); |
| 632 | |
| 633 | int stat; |
| 634 | GUROBI_CALL("isProvenOptimal", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &stat)); |
| 635 | |
| 636 | return (stat == GRB_OPTIMAL); |
| 637 | } |
| 638 | |
| 639 | bool OsiGrbSolverInterface::isProvenPrimalInfeasible() const |
| 640 | { |
| 641 | debugMessage("OsiGrbSolverInterface::isProvenPrimalInfeasible()\n"); |
| 642 | |
| 643 | GUROBI_CALL("isProvenPrimalInfeasible", GRBupdatemodel(getMutableLpPtr())); |
| 644 | |
| 645 | int stat; |
| 646 | GUROBI_CALL("isProvenPrimalInfeasible", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &stat)); |
| 647 | |
| 648 | return (stat == GRB_INFEASIBLE); |
| 649 | } |
| 650 | |
| 651 | bool OsiGrbSolverInterface::isProvenDualInfeasible() const |
| 652 | { |
| 653 | debugMessage("OsiGrbSolverInterface::isProvenDualInfeasible()\n"); |
| 654 | |
| 655 | GUROBI_CALL("isProvenDualInfeasible", GRBupdatemodel(getMutableLpPtr())); |
| 656 | |
| 657 | int stat; |
| 658 | GUROBI_CALL("isProvenDualInfeasible", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &stat)); |
| 659 | |
| 660 | return (stat == GRB_UNBOUNDED); |
| 661 | } |
| 662 | |
| 663 | bool OsiGrbSolverInterface::isPrimalObjectiveLimitReached() const |
| 664 | { |
| 665 | debugMessage("OsiGrbSolverInterface::isPrimalObjectiveLimitReached()\n"); |
| 666 | |
| 667 | return false; |
| 668 | } |
| 669 | |
| 670 | bool OsiGrbSolverInterface::isDualObjectiveLimitReached() const |
| 671 | { |
| 672 | debugMessage("OsiGrbSolverInterface::isDualObjectiveLimitReached()\n"); |
| 673 | |
| 674 | return false; |
| 675 | } |
| 676 | |
| 677 | bool OsiGrbSolverInterface::isIterationLimitReached() const |
| 678 | { |
| 679 | debugMessage("OsiGrbSolverInterface::isIterationLimitReached()\n"); |
| 680 | |
| 681 | GUROBI_CALL("isIterationLimitReached", GRBupdatemodel(getMutableLpPtr())); |
| 682 | |
| 683 | int stat; |
| 684 | GUROBI_CALL("isIterationLimitReached", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &stat)); |
| 685 | |
| 686 | return (stat == GRB_ITERATION_LIMIT); |
| 687 | } |
| 688 | |
| 689 | //############################################################################# |
| 690 | // WarmStart related methods |
| 691 | //############################################################################# |
| 692 | |
| 693 | CoinWarmStart *OsiGrbSolverInterface::getEmptyWarmStart() const |
| 694 | { |
| 695 | return (dynamic_cast< CoinWarmStart * >(new CoinWarmStartBasis())); |
| 696 | } |
| 697 | |
| 698 | // below we assume that getBasisStatus uses the same values as the warm start enum, i.e. 0: free, 1: basic, 2: upper, 3: lower |
| 699 | static CompileTimeAssert< CoinWarmStartBasis::isFree == 0 > UNUSED change_In_Enum_CoinWarmStartBasis_Status_free_breaks_this_function; |
| 700 | static CompileTimeAssert< CoinWarmStartBasis::basic == 1 > UNUSED change_In_Enum_CoinWarmStartBasis_Status_basic_breaks_this_function; |
| 701 | static CompileTimeAssert< CoinWarmStartBasis::atUpperBound == 2 > UNUSED change_In_Enum_CoinWarmStartBasis_Status_upper_breaks_this_function; |
| 702 | static CompileTimeAssert< CoinWarmStartBasis::atLowerBound == 3 > UNUSED change_In_Enum_CoinWarmStartBasis_Status_lower_breaks_this_function; |
| 703 | |
| 704 | CoinWarmStart *OsiGrbSolverInterface::getWarmStart() const |
| 705 | { |
| 706 | debugMessage("OsiGrbSolverInterface::getWarmStart()\n"); |
| 707 | |
| 708 | assert(!probtypemip_); |
| 709 | |
| 710 | if (!basisIsAvailable()) |
| 711 | return NULL; |
| 712 | |
| 713 | CoinWarmStartBasis *ws = NULL; |
| 714 | int numcols = getNumCols(); |
| 715 | int numrows = getNumRows(); |
| 716 | int *cstat = new int[numcols]; |
| 717 | int *rstat = new int[numrows]; |
| 718 | int i; |
| 719 | |
| 720 | #if 1 |
| 721 | getBasisStatus(cstat, rstat); |
| 722 | |
| 723 | ws = new CoinWarmStartBasis; |
| 724 | ws->setSize(numcols, numrows); |
| 725 | |
| 726 | for (i = 0; i < numrows; ++i) |
| 727 | ws->setArtifStatus(i, CoinWarmStartBasis::Status(rstat[i])); |
| 728 | for (i = 0; i < numcols; ++i) |
| 729 | ws->setStructStatus(i, CoinWarmStartBasis::Status(cstat[i])); |
| 730 | |
| 731 | #else |
| 732 | GUROBI_CALL("getWarmStart", GRBupdatemodel(getMutableLpPtr())); |
| 733 | |
| 734 | GUROBI_CALL("getWarmStart", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, 0, numcols, cstat)); |
| 735 | GUROBI_CALL("getWarmStart", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_CBASIS, 0, numrows, rstat)); |
| 736 | |
| 737 | ws = new CoinWarmStartBasis; |
| 738 | ws->setSize(numcols, numrows); |
| 739 | |
| 740 | char sense; |
| 741 | for (i = 0; i < numrows; ++i) { |
| 742 | switch (rstat[i]) { |
| 743 | case GRB_BASIC: |
| 744 | ws->setArtifStatus(i, CoinWarmStartBasis::basic); |
| 745 | break; |
| 746 | case GRB_NONBASIC_LOWER: |
| 747 | case GRB_NONBASIC_UPPER: |
| 748 | GUROBI_CALL("getWarmStart", GRBgetcharattrelement(getMutableLpPtr(), GRB_CHAR_ATTR_SENSE, i, &sense)); |
| 749 | ws->setArtifStatus(i, (sense == '>' ? CoinWarmStartBasis::atUpperBound : CoinWarmStartBasis::atLowerBound)); |
| 750 | break; |
| 751 | default: // unknown row status |
| 752 | delete ws; |
| 753 | delete[] rstat; |
| 754 | delete[] cstat; |
| 755 | return NULL; |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | for (i = 0; i < numcols; ++i) { |
| 760 | switch (cstat[i]) { |
| 761 | case GRB_BASIC: |
| 762 | ws->setStructStatus(i, CoinWarmStartBasis::basic); |
| 763 | break; |
| 764 | case GRB_NONBASIC_LOWER: |
| 765 | ws->setStructStatus(i, CoinWarmStartBasis::atLowerBound); |
| 766 | break; |
| 767 | case GRB_NONBASIC_UPPER: |
| 768 | ws->setStructStatus(i, CoinWarmStartBasis::atUpperBound); |
| 769 | break; |
| 770 | case GRB_SUPERBASIC: |
| 771 | ws->setStructStatus(i, CoinWarmStartBasis::isFree); |
| 772 | break; |
| 773 | default: // unknown column status |
| 774 | delete[] rstat; |
| 775 | delete[] cstat; |
| 776 | delete ws; |
| 777 | return NULL; |
| 778 | } |
| 779 | } |
| 780 | #endif |
| 781 | |
| 782 | delete[] cstat; |
| 783 | delete[] rstat; |
| 784 | |
| 785 | return ws; |
| 786 | } |
| 787 | |
| 788 | //----------------------------------------------------------------------------- |
| 789 | |
| 790 | bool OsiGrbSolverInterface::setWarmStart(const CoinWarmStart *warmstart) |
| 791 | { |
| 792 | debugMessage("OsiGrbSolverInterface::setWarmStart(%p)\n", (void *)warmstart); |
| 793 | |
| 794 | const CoinWarmStartBasis *ws = dynamic_cast< const CoinWarmStartBasis * >(warmstart); |
| 795 | int numcols, numrows, i, oi; |
| 796 | int *stat; |
| 797 | |
| 798 | if (!ws) |
| 799 | return false; |
| 800 | |
| 801 | numcols = ws->getNumStructural(); |
| 802 | numrows = ws->getNumArtificial(); |
| 803 | |
| 804 | if (numcols != getNumCols() || numrows != getNumRows()) |
| 805 | return false; |
| 806 | |
| 807 | switchToLP(); |
| 808 | |
| 809 | stat = new int[numcols + nauxcols > numrows ? numcols + nauxcols : numrows]; |
| 810 | for (i = 0; i < numrows; ++i) { |
| 811 | switch (ws->getArtifStatus(i)) { |
| 812 | case CoinWarmStartBasis::basic: |
| 813 | stat[i] = GRB_BASIC; |
| 814 | break; |
| 815 | case CoinWarmStartBasis::atLowerBound: |
| 816 | case CoinWarmStartBasis::atUpperBound: |
| 817 | stat[i] = GRB_NONBASIC_LOWER; |
| 818 | break; |
| 819 | case CoinWarmStartBasis::isFree: |
| 820 | stat[i] = GRB_SUPERBASIC; |
| 821 | break; |
| 822 | default: // unknown row status |
| 823 | delete[] stat; |
| 824 | return false; |
| 825 | } |
| 826 | } |
| 827 | |
| 828 | GUROBI_CALL("setWarmStart", GRBsetintattrarray(getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS), GRB_INT_ATTR_CBASIS, 0, numrows, stat)); |
| 829 | |
| 830 | for (i = 0; i < numcols + nauxcols; ++i) { |
| 831 | oi = colmap_G2O ? colmap_G2O[i] : i; |
| 832 | if (oi >= 0) { /* normal variable */ |
| 833 | switch (ws->getStructStatus(oi)) { |
| 834 | case CoinWarmStartBasis::basic: |
| 835 | stat[i] = GRB_BASIC; |
| 836 | break; |
| 837 | case CoinWarmStartBasis::atLowerBound: |
| 838 | stat[i] = GRB_NONBASIC_LOWER; |
| 839 | break; |
| 840 | case CoinWarmStartBasis::atUpperBound: |
| 841 | stat[i] = GRB_NONBASIC_UPPER; |
| 842 | break; |
| 843 | case CoinWarmStartBasis::isFree: |
| 844 | stat[i] = GRB_SUPERBASIC; |
| 845 | break; |
| 846 | default: // unknown col status |
| 847 | delete[] stat; |
| 848 | return false; |
| 849 | } |
| 850 | } else { /* auxiliary variable, derive status from status of corresponding ranged row */ |
| 851 | switch (ws->getArtifStatus(-oi - 1)) { |
| 852 | case CoinWarmStartBasis::basic: |
| 853 | stat[i] = GRB_BASIC; |
| 854 | break; |
| 855 | case CoinWarmStartBasis::atLowerBound: |
| 856 | stat[i] = GRB_NONBASIC_LOWER; |
| 857 | break; |
| 858 | case CoinWarmStartBasis::atUpperBound: |
| 859 | stat[i] = GRB_NONBASIC_UPPER; |
| 860 | break; |
| 861 | case CoinWarmStartBasis::isFree: |
| 862 | stat[i] = GRB_SUPERBASIC; |
| 863 | break; |
| 864 | default: // unknown col status |
| 865 | delete[] stat; |
| 866 | return false; |
| 867 | } |
| 868 | } |
| 869 | } |
| 870 | |
| 871 | GUROBI_CALL("setWarmStart", GRBsetintattrarray(getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS), GRB_INT_ATTR_VBASIS, 0, numcols + nauxcols, stat)); |
| 872 | |
| 873 | delete[] stat; |
| 874 | return true; |
| 875 | } |
| 876 | |
| 877 | //############################################################################# |
| 878 | // Hotstart related methods (primarily used in strong branching) |
| 879 | //############################################################################# |
| 880 | |
| 881 | void OsiGrbSolverInterface::markHotStart() |
| 882 | { |
| 883 | debugMessage("OsiGrbSolverInterface::markHotStart()\n"); |
| 884 | |
| 885 | int numcols, numrows; |
| 886 | |
| 887 | assert(!probtypemip_); |
| 888 | |
| 889 | numcols = getNumCols() + nauxcols; |
| 890 | numrows = getNumRows(); |
| 891 | if (numcols > hotStartCStatSize_) { |
| 892 | delete[] hotStartCStat_; |
| 893 | hotStartCStatSize_ = static_cast< int >(1.2 * static_cast< double >(numcols + nauxcols)); // get some extra space for future hot starts |
| 894 | hotStartCStat_ = new int[hotStartCStatSize_]; |
| 895 | } |
| 896 | if (numrows > hotStartRStatSize_) { |
| 897 | delete[] hotStartRStat_; |
| 898 | hotStartRStatSize_ = static_cast< int >(1.2 * static_cast< double >(numrows)); // get some extra space for future hot starts |
| 899 | hotStartRStat_ = new int[hotStartRStatSize_]; |
| 900 | } |
| 901 | |
| 902 | GUROBI_CALL("markHotStart", GRBupdatemodel(getMutableLpPtr())); |
| 903 | |
| 904 | GUROBI_CALL("markHotStart", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, 0, numcols + nauxcols, hotStartCStat_)); |
| 905 | GUROBI_CALL("markHotStart", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_CBASIS, 0, numrows, hotStartRStat_)); |
| 906 | } |
| 907 | |
| 908 | void OsiGrbSolverInterface::solveFromHotStart() |
| 909 | { |
| 910 | debugMessage("OsiGrbSolverInterface::solveFromHotStart()\n"); |
| 911 | |
| 912 | double maxiter; |
| 913 | |
| 914 | switchToLP(); |
| 915 | |
| 916 | assert(getNumCols() <= hotStartCStatSize_); |
| 917 | assert(getNumRows() <= hotStartRStatSize_); |
| 918 | |
| 919 | GUROBI_CALL("solveFromHotStart", GRBupdatemodel(getMutableLpPtr())); |
| 920 | |
| 921 | GUROBI_CALL("solveFromHotStart", GRBsetintattrarray(getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS), GRB_INT_ATTR_CBASIS, 0, getNumRows(), hotStartRStat_)); |
| 922 | GUROBI_CALL("solveFromHotStart", GRBsetintattrarray(getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS), GRB_INT_ATTR_VBASIS, 0, getNumCols() + nauxcols, hotStartCStat_)); |
| 923 | |
| 924 | GUROBI_CALL("solveFromHotStart", GRBgetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_ITERATIONLIMIT, &maxiter)); |
| 925 | GUROBI_CALL("solveFromHotStart", GRBsetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_ITERATIONLIMIT, (double)hotStartMaxIteration_)); |
| 926 | |
| 927 | resolve(); |
| 928 | |
| 929 | GUROBI_CALL("solveFromHotStart", GRBsetdblparam(GRBgetenv(getMutableLpPtr()), GRB_DBL_PAR_ITERATIONLIMIT, maxiter)); |
| 930 | } |
| 931 | |
| 932 | void OsiGrbSolverInterface::unmarkHotStart() |
| 933 | { |
| 934 | debugMessage("OsiGrbSolverInterface::unmarkHotStart()\n"); |
| 935 | |
| 936 | // ??? be lazy with deallocating memory and do nothing here, deallocate memory in the destructor |
| 937 | } |
| 938 | |
| 939 | //############################################################################# |
| 940 | // Problem information methods (original data) |
| 941 | //############################################################################# |
| 942 | |
| 943 | //------------------------------------------------------------------ |
| 944 | // Get number of rows, columns, elements, ... |
| 945 | //------------------------------------------------------------------ |
| 946 | int OsiGrbSolverInterface::getNumCols() const |
| 947 | { |
| 948 | debugMessage("OsiGrbSolverInterface::getNumCols()\n"); |
| 949 | |
| 950 | int numcols; |
| 951 | |
| 952 | GUROBI_CALL("getNumCols", GRBupdatemodel(getMutableLpPtr())); |
| 953 | |
| 954 | GUROBI_CALL("getNumCols", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_NUMVARS, &numcols)); |
| 955 | |
| 956 | numcols -= nauxcols; |
| 957 | |
| 958 | return numcols; |
| 959 | } |
| 960 | |
| 961 | int OsiGrbSolverInterface::getNumRows() const |
| 962 | { |
| 963 | debugMessage("OsiGrbSolverInterface::getNumRows()\n"); |
| 964 | |
| 965 | int numrows; |
| 966 | |
| 967 | GUROBI_CALL("getNumRows", GRBupdatemodel(getMutableLpPtr())); |
| 968 | |
| 969 | GUROBI_CALL("getNumRows", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_NUMCONSTRS, &numrows)); |
| 970 | |
| 971 | return numrows; |
| 972 | } |
| 973 | |
| 974 | int OsiGrbSolverInterface::getNumElements() const |
| 975 | { |
| 976 | debugMessage("OsiGrbSolverInterface::getNumElements()\n"); |
| 977 | |
| 978 | int numnz; |
| 979 | |
| 980 | GUROBI_CALL("getNumElements", GRBupdatemodel(getMutableLpPtr())); |
| 981 | |
| 982 | GUROBI_CALL("getNumElements", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_NUMNZS, &numnz)); |
| 983 | |
| 984 | /* each auxiliary column contributes one nonzero element to exactly one row */ |
| 985 | numnz -= nauxcols; |
| 986 | |
| 987 | return numnz; |
| 988 | } |
| 989 | |
| 990 | //------------------------------------------------------------------ |
| 991 | // Get pointer to rim vectors |
| 992 | //------------------------------------------------------------------ |
| 993 | |
| 994 | const double *OsiGrbSolverInterface::getColLower() const |
| 995 | { |
| 996 | debugMessage("OsiGrbSolverInterface::getColLower()\n"); |
| 997 | |
| 998 | if (collower_ == NULL) { |
| 999 | int ncols = getNumCols(); |
| 1000 | if (ncols > 0) { |
| 1001 | collower_ = new double[ncols]; |
| 1002 | GUROBI_CALL("getColLower", GRBupdatemodel(getMutableLpPtr())); |
| 1003 | |
| 1004 | if (nauxcols) |
| 1005 | GUROBI_CALL("getColLower", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_LB, ncols, colmap_O2G, collower_)); |
| 1006 | else |
| 1007 | GUROBI_CALL("getColLower", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_LB, 0, ncols, collower_)); |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | return collower_; |
| 1012 | } |
| 1013 | |
| 1014 | //------------------------------------------------------------------ |
| 1015 | const double *OsiGrbSolverInterface::getColUpper() const |
| 1016 | { |
| 1017 | debugMessage("OsiGrbSolverInterface::getColUpper()\n"); |
| 1018 | |
| 1019 | if (colupper_ == NULL) { |
| 1020 | int ncols = getNumCols(); |
| 1021 | if (ncols > 0) { |
| 1022 | colupper_ = new double[ncols]; |
| 1023 | GUROBI_CALL("getColUpper", GRBupdatemodel(getMutableLpPtr())); |
| 1024 | |
| 1025 | if (nauxcols) |
| 1026 | GUROBI_CALL("getColUpper", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_UB, ncols, colmap_O2G, colupper_)); |
| 1027 | else |
| 1028 | GUROBI_CALL("getColUpper", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_UB, 0, ncols, colupper_)); |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | return colupper_; |
| 1033 | } |
| 1034 | |
| 1035 | //------------------------------------------------------------------ |
| 1036 | const char *OsiGrbSolverInterface::getRowSense() const |
| 1037 | { |
| 1038 | debugMessage("OsiGrbSolverInterface::getRowSense()\n"); |
| 1039 | |
| 1040 | if (rowsense_ == NULL) { |
| 1041 | int nrows = getNumRows(); |
| 1042 | if (nrows > 0) { |
| 1043 | rowsense_ = new char[nrows]; |
| 1044 | GUROBI_CALL("getRowSense", GRBupdatemodel(getMutableLpPtr())); |
| 1045 | |
| 1046 | GUROBI_CALL("getRowSense", GRBgetcharattrarray(getMutableLpPtr(), GRB_CHAR_ATTR_SENSE, 0, nrows, rowsense_)); |
| 1047 | |
| 1048 | for (int i = 0; i < nrows; ++i) { |
| 1049 | if (auxcolind && auxcolind[i] >= 0) { |
| 1050 | assert(rowsense_[i] == GRB_EQUAL); |
| 1051 | rowsense_[i] = 'R'; |
| 1052 | continue; |
| 1053 | } |
| 1054 | switch (rowsense_[i]) { |
| 1055 | case GRB_LESS_EQUAL: |
| 1056 | rowsense_[i] = 'L'; |
| 1057 | break; |
| 1058 | case GRB_GREATER_EQUAL: |
| 1059 | rowsense_[i] = 'G'; |
| 1060 | break; |
| 1061 | case GRB_EQUAL: |
| 1062 | rowsense_[i] = 'E'; |
| 1063 | break; |
| 1064 | } |
| 1065 | } |
| 1066 | } |
| 1067 | } |
| 1068 | |
| 1069 | return rowsense_; |
| 1070 | } |
| 1071 | |
| 1072 | //------------------------------------------------------------------ |
| 1073 | const double *OsiGrbSolverInterface::getRightHandSide() const |
| 1074 | { |
| 1075 | debugMessage("OsiGrbSolverInterface::getRightHandSide()\n"); |
| 1076 | |
| 1077 | if (rhs_ == NULL) { |
| 1078 | int nrows = getNumRows(); |
| 1079 | if (nrows > 0) { |
| 1080 | rhs_ = new double[nrows]; |
| 1081 | GUROBI_CALL("getRightHandSide", GRBupdatemodel(getMutableLpPtr())); |
| 1082 | |
| 1083 | GUROBI_CALL("getRightHandSide", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_RHS, 0, nrows, rhs_)); |
| 1084 | |
| 1085 | /* for ranged rows, we give the rhs of the aux. variable used to represent the range of this row as row rhs */ |
| 1086 | if (nauxcols) |
| 1087 | for (int i = 0; i < nrows; ++i) |
| 1088 | if (auxcolind[i] >= 0) |
| 1089 | GUROBI_CALL("getRightHandSide", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_UB, auxcolind[i], &rhs_[i])); |
| 1090 | } |
| 1091 | } |
| 1092 | |
| 1093 | return rhs_; |
| 1094 | } |
| 1095 | |
| 1096 | //------------------------------------------------------------------ |
| 1097 | const double *OsiGrbSolverInterface::getRowRange() const |
| 1098 | { |
| 1099 | debugMessage("OsiGrbSolverInterface::getRowRange()\n"); |
| 1100 | |
| 1101 | if (rowrange_ == NULL) { |
| 1102 | int nrows = getNumRows(); |
| 1103 | if (nrows > 0) |
| 1104 | rowrange_ = CoinCopyOfArrayOrZero((double *)NULL, nrows); |
| 1105 | |
| 1106 | if (nauxcols) { |
| 1107 | double auxlb, auxub; |
| 1108 | for (int i = 0; i < nrows; ++i) |
| 1109 | if (auxcolind[i] >= 0) { |
| 1110 | GUROBI_CALL("getRowRange", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_LB, auxcolind[i], &auxlb)); |
| 1111 | GUROBI_CALL("getRowRange", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_UB, auxcolind[i], &auxub)); |
| 1112 | rowrange_[i] = auxub - auxlb; |
| 1113 | } |
| 1114 | } |
| 1115 | } |
| 1116 | |
| 1117 | return rowrange_; |
| 1118 | } |
| 1119 | |
| 1120 | //------------------------------------------------------------------ |
| 1121 | const double *OsiGrbSolverInterface::getRowLower() const |
| 1122 | { |
| 1123 | debugMessage("OsiGrbSolverInterface::getRowLower()\n"); |
| 1124 | |
| 1125 | if (rowlower_ == NULL) { |
| 1126 | int nrows = getNumRows(); |
| 1127 | if (nrows > 0) { |
| 1128 | const char *rowsense = getRowSense(); |
| 1129 | |
| 1130 | rowlower_ = new double[nrows]; |
| 1131 | |
| 1132 | double rhs; |
| 1133 | double dum1; |
| 1134 | for (int i = 0; i < nrows; ++i) { |
| 1135 | if (auxcolind && auxcolind[i] >= 0) { |
| 1136 | assert(rowsense[i] == 'R'); |
| 1137 | GUROBI_CALL("getRowLower", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_LB, auxcolind[i], &rowlower_[i])); |
| 1138 | } else { |
| 1139 | GUROBI_CALL("getRowLower", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_RHS, i, &rhs)); |
| 1140 | convertSenseToBound(rowsense[i], rhs, 0.0, rowlower_[i], dum1); |
| 1141 | } |
| 1142 | } |
| 1143 | } |
| 1144 | } |
| 1145 | |
| 1146 | return rowlower_; |
| 1147 | } |
| 1148 | |
| 1149 | //------------------------------------------------------------------ |
| 1150 | const double *OsiGrbSolverInterface::getRowUpper() const |
| 1151 | { |
| 1152 | debugMessage("OsiGrbSolverInterface::getRowUpper()\n"); |
| 1153 | |
| 1154 | if (rowupper_ == NULL) { |
| 1155 | int nrows = getNumRows(); |
| 1156 | if (nrows > 0) { |
| 1157 | const char *rowsense = getRowSense(); |
| 1158 | |
| 1159 | rowupper_ = new double[nrows]; |
| 1160 | |
| 1161 | double rhs; |
| 1162 | double dum1; |
| 1163 | for (int i = 0; i < nrows; ++i) { |
| 1164 | if (auxcolind && auxcolind[i] >= 0) { |
| 1165 | assert(rowsense[i] == 'R'); |
| 1166 | GUROBI_CALL("getRowUpper", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_UB, auxcolind[i], &rowupper_[i])); |
| 1167 | } else { |
| 1168 | GUROBI_CALL("getRowUpper", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_RHS, i, &rhs)); |
| 1169 | convertSenseToBound(rowsense[i], rhs, 0.0, dum1, rowupper_[i]); |
| 1170 | } |
| 1171 | } |
| 1172 | } |
| 1173 | } |
| 1174 | |
| 1175 | return rowupper_; |
| 1176 | } |
| 1177 | |
| 1178 | //------------------------------------------------------------------ |
| 1179 | const double *OsiGrbSolverInterface::getObjCoefficients() const |
| 1180 | { |
| 1181 | debugMessage("OsiGrbSolverInterface::getObjCoefficients()\n"); |
| 1182 | |
| 1183 | if (obj_ == NULL) { |
| 1184 | int ncols = getNumCols(); |
| 1185 | if (ncols > 0) { |
| 1186 | obj_ = new double[ncols]; |
| 1187 | GUROBI_CALL("getObjCoefficients", GRBupdatemodel(getMutableLpPtr())); |
| 1188 | |
| 1189 | if (nauxcols) |
| 1190 | GUROBI_CALL("getObjCoefficients", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_OBJ, ncols, colmap_O2G, obj_)); |
| 1191 | else |
| 1192 | GUROBI_CALL("getObjCoefficients", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_OBJ, 0, ncols, obj_)); |
| 1193 | } |
| 1194 | } |
| 1195 | |
| 1196 | return obj_; |
| 1197 | } |
| 1198 | |
| 1199 | //------------------------------------------------------------------ |
| 1200 | double OsiGrbSolverInterface::getObjSense() const |
| 1201 | { |
| 1202 | debugMessage("OsiGrbSolverInterface::getObjSense()\n"); |
| 1203 | |
| 1204 | int sense; |
| 1205 | GUROBI_CALL("getObjSense", GRBupdatemodel(getMutableLpPtr())); |
| 1206 | |
| 1207 | GUROBI_CALL("getObjSense", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_MODELSENSE, &sense)); |
| 1208 | |
| 1209 | return (double)sense; |
| 1210 | } |
| 1211 | |
| 1212 | //------------------------------------------------------------------ |
| 1213 | // Return information on integrality |
| 1214 | //------------------------------------------------------------------ |
| 1215 | |
| 1216 | bool OsiGrbSolverInterface::isContinuous(int colNumber) const |
| 1217 | { |
| 1218 | debugMessage("OsiGrbSolverInterface::isContinuous(%d)\n", colNumber); |
| 1219 | |
| 1220 | return getCtype()[colNumber] == 'C'; |
| 1221 | } |
| 1222 | |
| 1223 | //------------------------------------------------------------------ |
| 1224 | // Row and column copies of the matrix ... |
| 1225 | //------------------------------------------------------------------ |
| 1226 | |
| 1227 | const CoinPackedMatrix *OsiGrbSolverInterface::getMatrixByRow() const |
| 1228 | { |
| 1229 | debugMessage("OsiGrbSolverInterface::getMatrixByRow()\n"); |
| 1230 | |
| 1231 | if (matrixByRow_ == NULL) { |
| 1232 | int nrows = getNumRows(); |
| 1233 | int ncols = getNumCols(); |
| 1234 | |
| 1235 | if (nrows == 0) { |
| 1236 | matrixByRow_ = new CoinPackedMatrix(); |
| 1237 | matrixByRow_->setDimensions(0, ncols); |
| 1238 | return matrixByRow_; |
| 1239 | } |
| 1240 | |
| 1241 | int nelems; |
| 1242 | int *starts = new int[nrows + 1]; |
| 1243 | int *len = new int[nrows]; |
| 1244 | |
| 1245 | GUROBI_CALL("getMatrixByRow", GRBupdatemodel(getMutableLpPtr())); |
| 1246 | |
| 1247 | GUROBI_CALL("getMatrixByRow", GRBgetconstrs(getMutableLpPtr(), &nelems, NULL, NULL, NULL, 0, nrows)); |
| 1248 | |
| 1249 | assert(nelems == getNumElements() + nauxcols); |
| 1250 | int *indices = new int[nelems]; |
| 1251 | double *elements = new double[nelems]; |
| 1252 | |
| 1253 | GUROBI_CALL("getMatrixByRow", GRBgetconstrs(getMutableLpPtr(), &nelems, starts, indices, elements, 0, nrows)); |
| 1254 | |
| 1255 | matrixByRow_ = new CoinPackedMatrix(); |
| 1256 | |
| 1257 | // Should be able to pass null for length of packed matrix, |
| 1258 | // assignMatrix does not seem to allow (even though documentation say it is possible to do this). |
| 1259 | // For now compute the length. |
| 1260 | starts[nrows] = nelems; |
| 1261 | for (int i = 0; i < nrows; ++i) |
| 1262 | len[i] = starts[i + 1] - starts[i]; |
| 1263 | |
| 1264 | matrixByRow_->assignMatrix(false /* not column ordered */, |
| 1265 | ncols + nauxcols, nrows, nelems, |
| 1266 | elements, indices, starts, len); |
| 1267 | |
| 1268 | if (nauxcols) { |
| 1269 | // delete auxiliary columns from matrix |
| 1270 | int *auxcols = new int[nauxcols]; |
| 1271 | int j = 0; |
| 1272 | for (int i = 0; i < ncols + nauxcols; ++i) |
| 1273 | if (colmap_G2O[i] < 0) |
| 1274 | auxcols[j++] = i; |
| 1275 | assert(j == nauxcols); |
| 1276 | |
| 1277 | matrixByRow_->deleteCols(nauxcols, auxcols); |
| 1278 | |
| 1279 | delete[] auxcols; |
| 1280 | |
| 1281 | assert(matrixByRow_->getNumElements() == getNumElements()); |
| 1282 | assert(matrixByRow_->getMinorDim() <= ncols); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | return matrixByRow_; |
| 1287 | } |
| 1288 | |
| 1289 | //------------------------------------------------------------------ |
| 1290 | |
| 1291 | const CoinPackedMatrix *OsiGrbSolverInterface::getMatrixByCol() const |
| 1292 | { |
| 1293 | debugMessage("OsiGrbSolverInterface::getMatrixByCol()\n"); |
| 1294 | |
| 1295 | if (matrixByCol_ == NULL) { |
| 1296 | int nrows = getNumRows(); |
| 1297 | int ncols = getNumCols(); |
| 1298 | |
| 1299 | matrixByCol_ = new CoinPackedMatrix(); |
| 1300 | |
| 1301 | if (ncols > 0) { |
| 1302 | int nelems; |
| 1303 | int *starts = new int[ncols + nauxcols + 1]; |
| 1304 | int *len = new int[ncols + nauxcols]; |
| 1305 | |
| 1306 | GUROBI_CALL("getMatrixByCol", GRBupdatemodel(getMutableLpPtr())); |
| 1307 | |
| 1308 | GUROBI_CALL("getMatrixByCol", GRBgetvars(getMutableLpPtr(), &nelems, NULL, NULL, NULL, 0, ncols + nauxcols)); |
| 1309 | |
| 1310 | int *indices = new int[nelems]; |
| 1311 | double *elements = new double[nelems]; |
| 1312 | |
| 1313 | GUROBI_CALL("getMatrixByCol", GRBgetvars(getMutableLpPtr(), &nelems, starts, indices, elements, 0, ncols + nauxcols)); |
| 1314 | |
| 1315 | // Should be able to pass null for length of packed matrix, |
| 1316 | // assignMatrix does not seem to allow (even though documentation say it is possible to do this). |
| 1317 | // For now compute the length. |
| 1318 | starts[ncols + nauxcols] = nelems; |
| 1319 | for (int i = 0; i < ncols + nauxcols; i++) |
| 1320 | len[i] = starts[i + 1] - starts[i]; |
| 1321 | |
| 1322 | matrixByCol_->assignMatrix(true /* column ordered */, |
| 1323 | nrows, ncols + nauxcols, nelems, |
| 1324 | elements, indices, starts, len); |
| 1325 | |
| 1326 | assert(matrixByCol_->getNumCols() == ncols + nauxcols); |
| 1327 | assert(matrixByCol_->getNumRows() == nrows); |
| 1328 | |
| 1329 | if (nauxcols) { |
| 1330 | // delete auxiliary columns from matrix |
| 1331 | int *auxcols = new int[nauxcols]; |
| 1332 | int j = 0; |
| 1333 | for (int i = 0; i < ncols + nauxcols; ++i) |
| 1334 | if (colmap_G2O[i] < 0) |
| 1335 | auxcols[j++] = i; |
| 1336 | assert(j == nauxcols); |
| 1337 | |
| 1338 | matrixByCol_->deleteCols(nauxcols, auxcols); |
| 1339 | |
| 1340 | delete[] auxcols; |
| 1341 | |
| 1342 | assert(matrixByCol_->getNumElements() == getNumElements()); |
| 1343 | assert(matrixByCol_->getMajorDim() <= ncols); |
| 1344 | assert(matrixByCol_->getMinorDim() <= nrows); |
| 1345 | } |
| 1346 | } else |
| 1347 | matrixByCol_->setDimensions(nrows, ncols); |
| 1348 | } |
| 1349 | |
| 1350 | return matrixByCol_; |
| 1351 | } |
| 1352 | |
| 1353 | //------------------------------------------------------------------ |
| 1354 | // Get solver's value for infinity |
| 1355 | //------------------------------------------------------------------ |
| 1356 | double OsiGrbSolverInterface::getInfinity() const |
| 1357 | { |
| 1358 | debugMessage("OsiGrbSolverInterface::getInfinity()\n"); |
| 1359 | |
| 1360 | return GRB_INFINITY; |
| 1361 | } |
| 1362 | |
| 1363 | //############################################################################# |
| 1364 | // Problem information methods (results) |
| 1365 | //############################################################################# |
| 1366 | |
| 1367 | // *FIXME*: what should be done if a certain vector doesn't exist??? |
| 1368 | |
| 1369 | const double *OsiGrbSolverInterface::getColSolution() const |
| 1370 | { |
| 1371 | debugMessage("OsiGrbSolverInterface::getColSolution()\n"); |
| 1372 | |
| 1373 | if (colsol_ == NULL) { |
| 1374 | int ncols = getNumCols(); |
| 1375 | if (ncols > 0) { |
| 1376 | colsol_ = new double[ncols]; |
| 1377 | |
| 1378 | GUROBI_CALL("getColSolution", GRBupdatemodel(getMutableLpPtr())); |
| 1379 | |
| 1380 | if (GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_X, 0, colsol_) == 0) { // if a solution is available, get it |
| 1381 | if (nauxcols) |
| 1382 | GUROBI_CALL("getColSolution", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_X, ncols, colmap_O2G, colsol_)); |
| 1383 | else |
| 1384 | GUROBI_CALL("getColSolution", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_X, 0, ncols, colsol_)); |
| 1385 | } else { |
| 1386 | *messageHandler() << "Warning: OsiGrbSolverInterface::getColSolution() called, but no solution available! Returning lower bounds, but they may be at -infinity. Be aware!" << CoinMessageEol; |
| 1387 | if (OsiGrbSolverInterface::globalenv_) |
| 1388 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 1389 | if (nauxcols) |
| 1390 | GUROBI_CALL("getColSolution", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_LB, ncols, colmap_O2G, colsol_)); |
| 1391 | else |
| 1392 | GUROBI_CALL("getColSolution", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_LB, 0, ncols, colsol_)); |
| 1393 | } |
| 1394 | } |
| 1395 | } |
| 1396 | |
| 1397 | return colsol_; |
| 1398 | } |
| 1399 | |
| 1400 | //------------------------------------------------------------------ |
| 1401 | const double *OsiGrbSolverInterface::getRowPrice() const |
| 1402 | { |
| 1403 | debugMessage("OsiGrbSolverInterface::getRowPrice()\n"); |
| 1404 | |
| 1405 | if (rowsol_ == NULL) { |
| 1406 | int nrows = getNumRows(); |
| 1407 | if (nrows > 0) { |
| 1408 | rowsol_ = new double[nrows]; |
| 1409 | |
| 1410 | GUROBI_CALL("getRowPrice", GRBupdatemodel(getMutableLpPtr())); |
| 1411 | |
| 1412 | if (GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_PI, 0, rowsol_) == 0) { |
| 1413 | GUROBI_CALL("getRowPrice", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_PI, 0, nrows, rowsol_)); |
| 1414 | } else { |
| 1415 | *messageHandler() << "Warning: OsiGrbSolverInterface::getRowPrice() called, but no solution available! Returning 0.0. Be aware!" << CoinMessageEol; |
| 1416 | if (OsiGrbSolverInterface::globalenv_) |
| 1417 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 1418 | CoinZeroN(rowsol_, nrows); |
| 1419 | } |
| 1420 | |
| 1421 | //??? what is the dual value for a ranged row? |
| 1422 | } |
| 1423 | } |
| 1424 | |
| 1425 | return rowsol_; |
| 1426 | } |
| 1427 | |
| 1428 | //------------------------------------------------------------------ |
| 1429 | const double *OsiGrbSolverInterface::getReducedCost() const |
| 1430 | { |
| 1431 | debugMessage("OsiGrbSolverInterface::getReducedCost()\n"); |
| 1432 | |
| 1433 | if (redcost_ == NULL) { |
| 1434 | int ncols = getNumCols(); |
| 1435 | if (ncols > 0) { |
| 1436 | redcost_ = new double[ncols]; |
| 1437 | |
| 1438 | GUROBI_CALL("getReducedCost", GRBupdatemodel(getMutableLpPtr())); |
| 1439 | |
| 1440 | if (GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_RC, 0, redcost_) == 0) { // if reduced costs are available, get them |
| 1441 | if (nauxcols) |
| 1442 | GUROBI_CALL("getReducedCost", GRBgetdblattrlist(getMutableLpPtr(), GRB_DBL_ATTR_RC, ncols, colmap_O2G, redcost_)); |
| 1443 | else |
| 1444 | GUROBI_CALL("getReducedCost", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_RC, 0, ncols, redcost_)); |
| 1445 | } else { |
| 1446 | *messageHandler() << "Warning: OsiGrbSolverInterface::getReducedCost() called, but no solution available! Returning 0.0. Be aware!" << CoinMessageEol; |
| 1447 | if (OsiGrbSolverInterface::globalenv_) |
| 1448 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 1449 | CoinZeroN(redcost_, ncols); |
| 1450 | } |
| 1451 | } |
| 1452 | } |
| 1453 | |
| 1454 | return redcost_; |
| 1455 | } |
| 1456 | |
| 1457 | //------------------------------------------------------------------ |
| 1458 | const double *OsiGrbSolverInterface::getRowActivity() const |
| 1459 | { |
| 1460 | debugMessage("OsiGrbSolverInterface::getRowActivity()\n"); |
| 1461 | |
| 1462 | if (rowact_ == NULL) { |
| 1463 | int nrows = getNumRows(); |
| 1464 | if (nrows > 0) { |
| 1465 | rowact_ = new double[nrows]; |
| 1466 | |
| 1467 | GUROBI_CALL("getRowActivity", GRBupdatemodel(getMutableLpPtr())); |
| 1468 | |
| 1469 | if (GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_SLACK, 0, rowact_) == 0) { |
| 1470 | GUROBI_CALL("getRowActivity", GRBgetdblattrarray(getMutableLpPtr(), GRB_DBL_ATTR_SLACK, 0, nrows, rowact_)); |
| 1471 | |
| 1472 | for (int r = 0; r < nrows; ++r) { |
| 1473 | if (nauxcols && auxcolind[r] >= 0) { |
| 1474 | GUROBI_CALL("getRowActivity", GRBgetdblattrelement(getMutableLpPtr(), GRB_DBL_ATTR_X, auxcolind[r], &rowact_[r])); |
| 1475 | } else |
| 1476 | rowact_[r] = getRightHandSide()[r] - rowact_[r]; |
| 1477 | } |
| 1478 | } else { |
| 1479 | *messageHandler() << "Warning: OsiGrbSolverInterface::getRowActivity() called, but no solution available! Returning 0.0. Be aware!" << CoinMessageEol; |
| 1480 | if (OsiGrbSolverInterface::globalenv_) |
| 1481 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 1482 | CoinZeroN(rowact_, nrows); |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | return rowact_; |
| 1487 | } |
| 1488 | |
| 1489 | //------------------------------------------------------------------ |
| 1490 | double OsiGrbSolverInterface::getObjValue() const |
| 1491 | { |
| 1492 | debugMessage("OsiGrbSolverInterface::getObjValue()\n"); |
| 1493 | |
| 1494 | double objval = 0.0; |
| 1495 | |
| 1496 | GUROBI_CALL("getObjValue", GRBupdatemodel(getMutableLpPtr())); |
| 1497 | |
| 1498 | if (GRBgetdblattr(getMutableLpPtr(), GRB_DBL_ATTR_OBJVAL, &objval) == 0) { |
| 1499 | // Adjust objective function value by constant term in objective function |
| 1500 | double objOffset; |
| 1501 | getDblParam(OsiObjOffset, objOffset); |
| 1502 | objval = objval - objOffset; |
| 1503 | } else { |
| 1504 | *messageHandler() << "Warning: OsiGrbSolverInterface::getObjValue() called, but probably no solution available! Returning 0.0. Be aware!" << CoinMessageEol; |
| 1505 | if (OsiGrbSolverInterface::globalenv_) |
| 1506 | *messageHandler() << "\t GUROBI error message: " << GRBgeterrormsg(OsiGrbSolverInterface::globalenv_) << CoinMessageEol; |
| 1507 | } |
| 1508 | |
| 1509 | return objval; |
| 1510 | } |
| 1511 | |
| 1512 | //------------------------------------------------------------------ |
| 1513 | int OsiGrbSolverInterface::getIterationCount() const |
| 1514 | { |
| 1515 | debugMessage("OsiGrbSolverInterface::getIterationCount()\n"); |
| 1516 | |
| 1517 | double itercnt; |
| 1518 | |
| 1519 | GUROBI_CALL("getIterationCount", GRBupdatemodel(getMutableLpPtr())); |
| 1520 | |
| 1521 | GUROBI_CALL("getIterationCount", GRBgetdblattr(getMutableLpPtr(), GRB_DBL_ATTR_ITERCOUNT, &itercnt)); |
| 1522 | |
| 1523 | return (int)itercnt; |
| 1524 | } |
| 1525 | |
| 1526 | //------------------------------------------------------------------ |
| 1527 | std::vector< double * > OsiGrbSolverInterface::getDualRays(int maxNumRays, |
| 1528 | bool fullRay) const |
| 1529 | { |
| 1530 | debugMessage("OsiGrbSolverInterface::getDualRays(%d,%s)\n", maxNumRays, |
| 1531 | fullRay ? "true" : "false"); |
| 1532 | |
| 1533 | if (fullRay == true) { |
| 1534 | throw CoinError("Full dual rays not yet implemented.", "getDualRays", |
| 1535 | "OsiGrbSolverInterface"); |
| 1536 | } |
| 1537 | |
| 1538 | OsiGrbSolverInterface solver(*this); |
| 1539 | |
| 1540 | const int numcols = getNumCols(); |
| 1541 | const int numrows = getNumRows(); |
| 1542 | int *index = new int[CoinMax(numcols, numrows)]; |
| 1543 | int i; |
| 1544 | for (i = CoinMax(numcols, numrows) - 1; i >= 0; --i) { |
| 1545 | index[i] = i; |
| 1546 | } |
| 1547 | double *obj = new double[CoinMax(numcols, 2 * numrows)]; |
| 1548 | CoinFillN(obj, numcols, 0.0); |
| 1549 | solver.setObjCoeffSet(index, index + numcols, obj); |
| 1550 | |
| 1551 | double *clb = new double[2 * numrows]; |
| 1552 | double *cub = new double[2 * numrows]; |
| 1553 | |
| 1554 | const double plusone = 1.0; |
| 1555 | const double minusone = -1.0; |
| 1556 | const char *sense = getRowSense(); |
| 1557 | |
| 1558 | const CoinPackedVectorBase **cols = new const CoinPackedVectorBase *[numrows]; |
| 1559 | int newcols = 0; |
| 1560 | for (i = 0; i < numrows; ++i) { |
| 1561 | switch (sense[i]) { |
| 1562 | case 'L': |
| 1563 | cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &minusone, false); |
| 1564 | break; |
| 1565 | case 'G': |
| 1566 | cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &plusone, false); |
| 1567 | break; |
| 1568 | case 'R': |
| 1569 | cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &minusone, false); |
| 1570 | cols[newcols++] = new CoinShallowPackedVector(1, &index[i], &plusone, false); |
| 1571 | break; |
| 1572 | case 'N': |
| 1573 | case 'E': |
| 1574 | break; |
| 1575 | } |
| 1576 | } |
| 1577 | |
| 1578 | CoinFillN(obj, newcols, 1.0); |
| 1579 | CoinFillN(clb, newcols, 0.0); |
| 1580 | CoinFillN(cub, newcols, getInfinity()); |
| 1581 | |
| 1582 | solver.addCols(newcols, cols, clb, cub, obj); |
| 1583 | delete[] index; |
| 1584 | delete[] cols; |
| 1585 | delete[] clb; |
| 1586 | delete[] cub; |
| 1587 | delete[] obj; |
| 1588 | |
| 1589 | solver.setObjSense(1.0); // minimize |
| 1590 | solver.initialSolve(); |
| 1591 | |
| 1592 | const double *solverpi = solver.getRowPrice(); |
| 1593 | double *pi = new double[numrows]; |
| 1594 | for (i = numrows - 1; i >= 0; --i) { |
| 1595 | pi[i] = -solverpi[i]; |
| 1596 | } |
| 1597 | return std::vector< double * >(1, pi); |
| 1598 | } |
| 1599 | |
| 1600 | //------------------------------------------------------------------ |
| 1601 | std::vector< double * > OsiGrbSolverInterface::getPrimalRays(int maxNumRays) const |
| 1602 | { |
| 1603 | debugMessage("OsiGrbSolverInterface::getPrimalRays(%d)\n", maxNumRays); |
| 1604 | |
| 1605 | return std::vector< double * >(); |
| 1606 | } |
| 1607 | |
| 1608 | //############################################################################# |
| 1609 | // Problem modifying methods (rim vectors) |
| 1610 | //############################################################################# |
| 1611 | |
| 1612 | void OsiGrbSolverInterface::setObjCoeff(int elementIndex, double elementValue) |
| 1613 | { |
| 1614 | debugMessage("OsiGrbSolverInterface::setObjCoeff(%d, %g)\n", elementIndex, elementValue); |
| 1615 | |
| 1616 | GUROBI_CALL("setObjCoeff", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_OBJ, nauxcols ? colmap_O2G[elementIndex] : elementIndex, elementValue)); |
| 1617 | |
| 1618 | if (obj_ != NULL) |
| 1619 | obj_[elementIndex] = elementValue; |
| 1620 | } |
| 1621 | |
| 1622 | //----------------------------------------------------------------------------- |
| 1623 | |
| 1624 | void OsiGrbSolverInterface::setObjCoeffSet(const int *indexFirst, |
| 1625 | const int *indexLast, |
| 1626 | const double *coeffList) |
| 1627 | { |
| 1628 | debugMessage("OsiGrbSolverInterface::setObjCoeffSet(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)coeffList); |
| 1629 | |
| 1630 | const int cnt = (int)(indexLast - indexFirst); |
| 1631 | |
| 1632 | if (cnt == 0) |
| 1633 | return; |
| 1634 | if (cnt == 1) { |
| 1635 | setObjCoeff(*indexFirst, *coeffList); |
| 1636 | return; |
| 1637 | } |
| 1638 | assert(cnt > 1); |
| 1639 | |
| 1640 | if (nauxcols) { |
| 1641 | int *indices = new int[cnt]; |
| 1642 | for (int i = 0; i < cnt; ++i) |
| 1643 | indices[i] = colmap_O2G[indexFirst[i]]; |
| 1644 | GUROBI_CALL("setObjCoeffSet", GRBsetdblattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_OBJ, cnt, indices, const_cast< double * >(coeffList))); |
| 1645 | delete[] indices; |
| 1646 | } else { |
| 1647 | GUROBI_CALL("setObjCoeffSet", GRBsetdblattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_OBJ, cnt, const_cast< int * >(indexFirst), const_cast< double * >(coeffList))); |
| 1648 | } |
| 1649 | |
| 1650 | if (obj_ != NULL) |
| 1651 | for (int i = 0; i < cnt; ++i) |
| 1652 | obj_[indexFirst[i]] = coeffList[i]; |
| 1653 | } |
| 1654 | |
| 1655 | //----------------------------------------------------------------------------- |
| 1656 | void OsiGrbSolverInterface::setColLower(int elementIndex, double elementValue) |
| 1657 | { |
| 1658 | debugMessage("OsiGrbSolverInterface::setColLower(%d, %g)\n", elementIndex, elementValue); |
| 1659 | |
| 1660 | GUROBI_CALL("setColLower", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_LB, nauxcols ? colmap_O2G[elementIndex] : elementIndex, elementValue)); |
| 1661 | |
| 1662 | if (collower_ != NULL) |
| 1663 | collower_[elementIndex] = elementValue; |
| 1664 | } |
| 1665 | |
| 1666 | //----------------------------------------------------------------------------- |
| 1667 | void OsiGrbSolverInterface::setColUpper(int elementIndex, double elementValue) |
| 1668 | { |
| 1669 | debugMessage("OsiGrbSolverInterface::setColUpper(%d, %g)\n", elementIndex, elementValue); |
| 1670 | |
| 1671 | GUROBI_CALL("setColUpper", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_UB, nauxcols ? colmap_O2G[elementIndex] : elementIndex, elementValue)); |
| 1672 | |
| 1673 | if (colupper_ != NULL) |
| 1674 | colupper_[elementIndex] = elementValue; |
| 1675 | } |
| 1676 | |
| 1677 | //----------------------------------------------------------------------------- |
| 1678 | void OsiGrbSolverInterface::setColBounds(int elementIndex, double lower, double upper) |
| 1679 | { |
| 1680 | debugMessage("OsiGrbSolverInterface::setColBounds(%d, %g, %g)\n", elementIndex, lower, upper); |
| 1681 | |
| 1682 | setColLower(elementIndex, lower); |
| 1683 | setColUpper(elementIndex, upper); |
| 1684 | } |
| 1685 | |
| 1686 | //----------------------------------------------------------------------------- |
| 1687 | void OsiGrbSolverInterface::setColSetBounds(const int *indexFirst, |
| 1688 | const int *indexLast, |
| 1689 | const double *boundList) |
| 1690 | { |
| 1691 | debugMessage("OsiGrbSolverInterface::setColSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList); |
| 1692 | |
| 1693 | const int cnt = (int)(indexLast - indexFirst); |
| 1694 | if (cnt == 0) |
| 1695 | return; |
| 1696 | if (cnt == 1) { |
| 1697 | setColLower(*indexFirst, boundList[0]); |
| 1698 | setColUpper(*indexFirst, boundList[1]); |
| 1699 | return; |
| 1700 | } |
| 1701 | assert(cnt > 1); |
| 1702 | |
| 1703 | double *lbList = new double[cnt]; |
| 1704 | double *ubList = new double[cnt]; |
| 1705 | int *indices = nauxcols ? new int[cnt] : NULL; |
| 1706 | |
| 1707 | for (int i = 0; i < cnt; ++i) { |
| 1708 | lbList[i] = boundList[2 * i]; |
| 1709 | ubList[i] = boundList[2 * i + 1]; |
| 1710 | if (indices) |
| 1711 | indices[i] = colmap_O2G[indexFirst[i]]; |
| 1712 | |
| 1713 | if (collower_ != NULL) |
| 1714 | collower_[indexFirst[i]] = boundList[2 * i]; |
| 1715 | if (colupper_ != NULL) |
| 1716 | colupper_[indexFirst[i]] = boundList[2 * i + 1]; |
| 1717 | } |
| 1718 | |
| 1719 | GUROBI_CALL("setColSetBounds", GRBsetdblattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_LB, cnt, indices ? indices : const_cast< int * >(indexFirst), lbList)); |
| 1720 | GUROBI_CALL("setColSetBounds", GRBsetdblattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_UB, cnt, indices ? indices : const_cast< int * >(indexFirst), ubList)); |
| 1721 | |
| 1722 | delete[] lbList; |
| 1723 | delete[] ubList; |
| 1724 | delete[] indices; |
| 1725 | } |
| 1726 | |
| 1727 | //----------------------------------------------------------------------------- |
| 1728 | void OsiGrbSolverInterface::setRowLower(int i, double elementValue) |
| 1729 | { |
| 1730 | debugMessage("OsiGrbSolverInterface::setRowLower(%d, %g)\n", i, elementValue); |
| 1731 | |
| 1732 | double rhs = getRightHandSide()[i]; |
| 1733 | double range = getRowRange()[i]; |
| 1734 | char sense = getRowSense()[i]; |
| 1735 | double lower = 0, upper = 0; |
| 1736 | |
| 1737 | convertSenseToBound(sense, rhs, range, lower, upper); |
| 1738 | if (lower != elementValue) { |
| 1739 | convertBoundToSense(elementValue, upper, sense, rhs, range); |
| 1740 | setRowType(i, sense, rhs, range); |
| 1741 | } |
| 1742 | } |
| 1743 | |
| 1744 | //----------------------------------------------------------------------------- |
| 1745 | void OsiGrbSolverInterface::setRowUpper(int i, double elementValue) |
| 1746 | { |
| 1747 | debugMessage("OsiGrbSolverInterface::setRowUpper(%d, %g)\n", i, elementValue); |
| 1748 | |
| 1749 | double rhs = getRightHandSide()[i]; |
| 1750 | double range = getRowRange()[i]; |
| 1751 | char sense = getRowSense()[i]; |
| 1752 | double lower = 0, upper = 0; |
| 1753 | |
| 1754 | convertSenseToBound(sense, rhs, range, lower, upper); |
| 1755 | if (upper != elementValue) { |
| 1756 | convertBoundToSense(lower, elementValue, sense, rhs, range); |
| 1757 | setRowType(i, sense, rhs, range); |
| 1758 | } |
| 1759 | } |
| 1760 | |
| 1761 | //----------------------------------------------------------------------------- |
| 1762 | void OsiGrbSolverInterface::setRowBounds(int elementIndex, double lower, double upper) |
| 1763 | { |
| 1764 | debugMessage("OsiGrbSolverInterface::setRowBounds(%d, %g, %g)\n", elementIndex, lower, upper); |
| 1765 | |
| 1766 | double rhs, range; |
| 1767 | char sense; |
| 1768 | |
| 1769 | convertBoundToSense(lower, upper, sense, rhs, range); |
| 1770 | |
| 1771 | setRowType(elementIndex, sense, rhs, range); |
| 1772 | } |
| 1773 | |
| 1774 | //----------------------------------------------------------------------------- |
| 1775 | void OsiGrbSolverInterface::setRowType(int i, char sense, double rightHandSide, double range) |
| 1776 | { |
| 1777 | debugMessage("OsiGrbSolverInterface::setRowType(%d, %c, %g, %g)\n", i, sense, rightHandSide, range); |
| 1778 | |
| 1779 | GUROBI_CALL("setRowType", GRBupdatemodel(getMutableLpPtr())); |
| 1780 | |
| 1781 | if (nauxcols && auxcolind[i] >= 0) { // so far, row i is a ranged row |
| 1782 | switch (sense) { |
| 1783 | case 'R': |
| 1784 | // row i was ranged row and remains a ranged row |
| 1785 | assert(range > 0); |
| 1786 | GUROBI_CALL("setRowType", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_LB, auxcolind[i], rightHandSide - range)); |
| 1787 | GUROBI_CALL("setRowType", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_UB, auxcolind[i], rightHandSide)); |
| 1788 | break; |
| 1789 | case 'N': |
| 1790 | case 'L': |
| 1791 | case 'G': |
| 1792 | case 'E': |
| 1793 | convertToNormalRow(i, sense, rightHandSide); |
| 1794 | break; |
| 1795 | default: |
| 1796 | std::cerr << "Unknown row sense: " << sense << std::endl; |
| 1797 | exit(-1); |
| 1798 | } |
| 1799 | } else if (sense == 'R') { |
| 1800 | convertToRangedRow(i, rightHandSide, range); |
| 1801 | } else { |
| 1802 | char grbsense; |
| 1803 | switch (sense) { |
| 1804 | case 'N': |
| 1805 | grbsense = GRB_LESS_EQUAL; |
| 1806 | rightHandSide = getInfinity(); |
| 1807 | break; |
| 1808 | |
| 1809 | case 'L': |
| 1810 | grbsense = GRB_LESS_EQUAL; |
| 1811 | break; |
| 1812 | |
| 1813 | case 'G': |
| 1814 | grbsense = GRB_GREATER_EQUAL; |
| 1815 | break; |
| 1816 | |
| 1817 | case 'E': |
| 1818 | grbsense = GRB_EQUAL; |
| 1819 | break; |
| 1820 | |
| 1821 | default: |
| 1822 | std::cerr << "Unknown row sense: " << sense << std::endl; |
| 1823 | exit(-1); |
| 1824 | } |
| 1825 | GUROBI_CALL("setRowType", GRBsetcharattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_CHAR_ATTR_SENSE, i, grbsense)); |
| 1826 | GUROBI_CALL("setRowType", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_RHS, i, rightHandSide)); |
| 1827 | } |
| 1828 | |
| 1829 | if (rowsense_ != NULL) |
| 1830 | rowsense_[i] = sense; |
| 1831 | |
| 1832 | if (rhs_ != NULL) |
| 1833 | rhs_[i] = rightHandSide; |
| 1834 | |
| 1835 | if (rowlower_ != NULL || rowupper_ != NULL) { |
| 1836 | double dummy; |
| 1837 | convertSenseToBound(sense, rightHandSide, range, |
| 1838 | rowlower_ ? rowlower_[i] : dummy, |
| 1839 | rowupper_ ? rowupper_[i] : dummy); |
| 1840 | } |
| 1841 | } |
| 1842 | |
| 1843 | //----------------------------------------------------------------------------- |
| 1844 | void OsiGrbSolverInterface::setRowSetBounds(const int *indexFirst, |
| 1845 | const int *indexLast, |
| 1846 | const double *boundList) |
| 1847 | { |
| 1848 | debugMessage("OsiGrbSolverInterface::setRowSetBounds(%p, %p, %p)\n", (void *)indexFirst, (void *)indexLast, (void *)boundList); |
| 1849 | |
| 1850 | const long int cnt = indexLast - indexFirst; |
| 1851 | if (cnt <= 0) |
| 1852 | return; |
| 1853 | |
| 1854 | char *sense = new char[cnt]; |
| 1855 | double *rhs = new double[cnt]; |
| 1856 | double *range = new double[cnt]; |
| 1857 | for (int i = 0; i < cnt; ++i) |
| 1858 | convertBoundToSense(boundList[2 * i], boundList[2 * i + 1], sense[i], rhs[i], range[i]); |
| 1859 | setRowSetTypes(indexFirst, indexLast, sense, rhs, range); |
| 1860 | |
| 1861 | delete[] range; |
| 1862 | delete[] rhs; |
| 1863 | delete[] sense; |
| 1864 | } |
| 1865 | |
| 1866 | //----------------------------------------------------------------------------- |
| 1867 | void OsiGrbSolverInterface::setRowSetTypes(const int *indexFirst, |
| 1868 | const int *indexLast, |
| 1869 | const char *senseList, |
| 1870 | const double *rhsList, |
| 1871 | const double *rangeList) |
| 1872 | { |
| 1873 | debugMessage("OsiGrbSolverInterface::setRowSetTypes(%p, %p, %p, %p, %p)\n", |
| 1874 | (void *)indexFirst, (void *)indexLast, (void *)senseList, (void *)rhsList, (void *)rangeList); |
| 1875 | |
| 1876 | const int cnt = (int)(indexLast - indexFirst); |
| 1877 | if (cnt == 0) |
| 1878 | return; |
| 1879 | if (cnt == 1) { |
| 1880 | setRowType(*indexFirst, *senseList, *rhsList, *rangeList); |
| 1881 | return; |
| 1882 | } |
| 1883 | assert(cnt > 0); |
| 1884 | |
| 1885 | GUROBI_CALL("setRowSetTypes", GRBupdatemodel(getMutableLpPtr())); |
| 1886 | |
| 1887 | char *grbsense = new char[cnt]; |
| 1888 | double *rhs = new double[cnt]; |
| 1889 | for (int i = 0; i < cnt; ++i) { |
| 1890 | rhs[i] = rhsList[i]; |
| 1891 | if (nauxcols && auxcolind[indexFirst[i]] >= 0) { // so far, row is a ranged row |
| 1892 | switch (senseList[i]) { |
| 1893 | case 'R': |
| 1894 | // row i was ranged row and remains a ranged row |
| 1895 | assert(rangeList[i] > 0); |
| 1896 | GUROBI_CALL("setRowSetTypes", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_LB, auxcolind[indexFirst[i]], rhsList[i] - rangeList[i])); |
| 1897 | GUROBI_CALL("setRowSetTypes", GRBsetdblattrelement(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), GRB_DBL_ATTR_UB, auxcolind[indexFirst[i]], rhsList[i])); |
| 1898 | grbsense[i] = GRB_EQUAL; |
| 1899 | rhs[i] = 0.0; |
| 1900 | break; |
| 1901 | case 'N': |
| 1902 | convertToNormalRow(indexFirst[i], senseList[i], rhsList[i]); |
| 1903 | grbsense[i] = GRB_LESS_EQUAL; |
| 1904 | rhs[i] = getInfinity(); |
| 1905 | break; |
| 1906 | case 'L': |
| 1907 | convertToNormalRow(indexFirst[i], senseList[i], rhsList[i]); |
| 1908 | grbsense[i] = GRB_LESS_EQUAL; |
| 1909 | break; |
| 1910 | case 'G': |
| 1911 | convertToNormalRow(indexFirst[i], senseList[i], rhsList[i]); |
| 1912 | grbsense[i] = GRB_GREATER_EQUAL; |
| 1913 | break; |
| 1914 | case 'E': |
| 1915 | convertToNormalRow(indexFirst[i], senseList[i], rhsList[i]); |
| 1916 | grbsense[i] = GRB_EQUAL; |
| 1917 | break; |
| 1918 | default: |
| 1919 | std::cerr << "Unknown row sense: " << senseList[i] << std::endl; |
| 1920 | exit(-1); |
| 1921 | } |
| 1922 | } else if (senseList[i] == 'R') { |
| 1923 | convertToRangedRow(indexFirst[i], rhsList[i], rangeList[i]); |
| 1924 | grbsense[i] = GRB_EQUAL; |
| 1925 | rhs[i] = 0.0; |
| 1926 | } else { |
| 1927 | switch (senseList[i]) { |
| 1928 | case 'N': |
| 1929 | grbsense[i] = GRB_LESS_EQUAL; |
| 1930 | rhs[i] = getInfinity(); |
| 1931 | break; |
| 1932 | |
| 1933 | case 'L': |
| 1934 | grbsense[i] = GRB_LESS_EQUAL; |
| 1935 | break; |
| 1936 | |
| 1937 | case 'G': |
| 1938 | grbsense[i] = GRB_GREATER_EQUAL; |
| 1939 | break; |
| 1940 | |
| 1941 | case 'E': |
| 1942 | grbsense[i] = GRB_EQUAL; |
| 1943 | break; |
| 1944 | |
| 1945 | default: |
| 1946 | std::cerr << "Unknown row sense: " << senseList[i] << std::endl; |
| 1947 | exit(-1); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | if (rowsense_ != NULL) |
| 1952 | rowsense_[indexFirst[i]] = senseList[i]; |
| 1953 | |
| 1954 | if (rhs_ != NULL) |
| 1955 | rhs_[indexFirst[i]] = senseList[i] == 'N' ? getInfinity() : rhsList[i]; |
| 1956 | } |
| 1957 | |
| 1958 | GUROBI_CALL("setRowSetTypes", GRBsetcharattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), GRB_CHAR_ATTR_SENSE, cnt, const_cast< int * >(indexFirst), grbsense)); |
| 1959 | GUROBI_CALL("setRowSetTypes", GRBsetdblattrlist(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), GRB_DBL_ATTR_RHS, cnt, const_cast< int * >(indexFirst), rhs)); |
| 1960 | |
| 1961 | delete[] rhs; |
| 1962 | delete[] grbsense; |
| 1963 | } |
| 1964 | |
| 1965 | //############################################################################# |
| 1966 | void OsiGrbSolverInterface::setContinuous(int index) |
| 1967 | { |
| 1968 | debugMessage("OsiGrbSolverInterface::setContinuous(%d)\n", index); |
| 1969 | |
| 1970 | assert(coltype_ != NULL); |
| 1971 | assert(colspace_ >= getNumCols()); |
| 1972 | |
| 1973 | coltype_[index] = GRB_CONTINUOUS; |
| 1974 | |
| 1975 | if (probtypemip_) { |
| 1976 | GUROBI_CALL("setContinuous", GRBupdatemodel(getMutableLpPtr())); |
| 1977 | GUROBI_CALL("setContinuous", GRBsetcharattrelement(getMutableLpPtr(), GRB_CHAR_ATTR_VTYPE, nauxcols ? colmap_O2G[index] : index, GRB_CONTINUOUS)); |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | //----------------------------------------------------------------------------- |
| 1982 | void OsiGrbSolverInterface::setInteger(int index) |
| 1983 | { |
| 1984 | debugMessage("OsiGrbSolverInterface::setInteger(%d)\n", index); |
| 1985 | |
| 1986 | assert(coltype_ != NULL); |
| 1987 | assert(colspace_ >= getNumCols()); |
| 1988 | |
| 1989 | if (getColLower()[index] == 0.0 && getColUpper()[index] == 1.0) |
| 1990 | coltype_[index] = GRB_BINARY; |
| 1991 | else |
| 1992 | coltype_[index] = GRB_INTEGER; |
| 1993 | |
| 1994 | if (probtypemip_) { |
| 1995 | GUROBI_CALL("setInteger", GRBupdatemodel(getMutableLpPtr())); |
| 1996 | GUROBI_CALL("setInteger", GRBsetcharattrelement(getMutableLpPtr(), GRB_CHAR_ATTR_VTYPE, nauxcols ? colmap_O2G[index] : index, coltype_[index])); |
| 1997 | } |
| 1998 | } |
| 1999 | |
| 2000 | //----------------------------------------------------------------------------- |
| 2001 | void OsiGrbSolverInterface::setContinuous(const int *indices, int len) |
| 2002 | { |
| 2003 | debugMessage("OsiGrbSolverInterface::setContinuous(%p, %d)\n", (void *)indices, len); |
| 2004 | |
| 2005 | for (int i = 0; i < len; ++i) |
| 2006 | setContinuous(indices[i]); |
| 2007 | } |
| 2008 | |
| 2009 | //----------------------------------------------------------------------------- |
| 2010 | void OsiGrbSolverInterface::setInteger(const int *indices, int len) |
| 2011 | { |
| 2012 | debugMessage("OsiGrbSolverInterface::setInteger(%p, %d)\n", (void *)indices, len); |
| 2013 | |
| 2014 | for (int i = 0; i < len; ++i) |
| 2015 | setInteger(indices[i]); |
| 2016 | } |
| 2017 | |
| 2018 | //############################################################################# |
| 2019 | |
| 2020 | void OsiGrbSolverInterface::setRowName(int ndx, std::string name) |
| 2021 | { |
| 2022 | debugMessage("OsiGrbSolverInterface::setRowName\n"); |
| 2023 | |
| 2024 | if (ndx < 0 || ndx >= getNumRows()) |
| 2025 | return; |
| 2026 | |
| 2027 | int nameDiscipline; |
| 2028 | getIntParam(OsiNameDiscipline, nameDiscipline); |
| 2029 | if (nameDiscipline == 0) |
| 2030 | return; |
| 2031 | |
| 2032 | OsiSolverInterface::setRowName(ndx, name); |
| 2033 | |
| 2034 | GUROBI_CALL("setRowName", GRBsetstrattrelement(getLpPtr(), GRB_STR_ATTR_CONSTRNAME, ndx, const_cast< char * >(name.c_str()))); |
| 2035 | } |
| 2036 | |
| 2037 | void OsiGrbSolverInterface::setColName(int ndx, std::string name) |
| 2038 | { |
| 2039 | debugMessage("OsiGrbSolverInterface::setColName\n"); |
| 2040 | |
| 2041 | if (ndx < 0 || ndx >= getNumCols()) |
| 2042 | return; |
| 2043 | |
| 2044 | int nameDiscipline; |
| 2045 | getIntParam(OsiNameDiscipline, nameDiscipline); |
| 2046 | if (nameDiscipline == 0) |
| 2047 | return; |
| 2048 | |
| 2049 | OsiSolverInterface::setColName(ndx, name); |
| 2050 | |
| 2051 | GUROBI_CALL("setColName", GRBsetstrattrelement(getLpPtr(), GRB_STR_ATTR_VARNAME, nauxcols ? colmap_O2G[ndx] : ndx, const_cast< char * >(name.c_str()))); |
| 2052 | } |
| 2053 | |
| 2054 | //############################################################################# |
| 2055 | |
| 2056 | void OsiGrbSolverInterface::setObjSense(double s) |
| 2057 | { |
| 2058 | debugMessage("OsiGrbSolverInterface::setObjSense(%g)\n", s); |
| 2059 | |
| 2060 | GUROBI_CALL("setObjSense", GRBsetintattr(getLpPtr(OsiGrbSolverInterface::FREECACHED_RESULTS), GRB_INT_ATTR_MODELSENSE, (int)s)); |
| 2061 | } |
| 2062 | |
| 2063 | //----------------------------------------------------------------------------- |
| 2064 | |
| 2065 | void OsiGrbSolverInterface::setColSolution(const double *cs) |
| 2066 | { |
| 2067 | debugMessage("OsiGrbSolverInterface::setColSolution(%p)\n", (void *)cs); |
| 2068 | |
| 2069 | int nc = getNumCols(); |
| 2070 | |
| 2071 | if (cs == NULL) |
| 2072 | freeCachedResults(); |
| 2073 | else if (nc > 0) { |
| 2074 | // If colsol isn't allocated, then allocate it |
| 2075 | if (colsol_ == NULL) |
| 2076 | colsol_ = new double[nc]; |
| 2077 | |
| 2078 | // Copy in new col solution. |
| 2079 | CoinDisjointCopyN(cs, nc, colsol_); |
| 2080 | |
| 2081 | //*messageHandler() << "OsiGrb::setColSolution: Gurobi does not allow setting the column solution. Command is ignored." << CoinMessageEol; |
| 2082 | } |
| 2083 | } |
| 2084 | |
| 2085 | //----------------------------------------------------------------------------- |
| 2086 | |
| 2087 | void OsiGrbSolverInterface::setRowPrice(const double *rs) |
| 2088 | { |
| 2089 | debugMessage("OsiGrbSolverInterface::setRowPrice(%p)\n", (void *)rs); |
| 2090 | |
| 2091 | int nr = getNumRows(); |
| 2092 | |
| 2093 | if (rs == NULL) |
| 2094 | freeCachedResults(); |
| 2095 | else if (nr > 0) { |
| 2096 | // If rowsol isn't allocated, then allocate it |
| 2097 | if (rowsol_ == NULL) |
| 2098 | rowsol_ = new double[nr]; |
| 2099 | |
| 2100 | // Copy in new row solution. |
| 2101 | CoinDisjointCopyN(rs, nr, rowsol_); |
| 2102 | |
| 2103 | *messageHandler() << "OsiGrb::setRowPrice: Gurobi does not allow setting the row price. Command is ignored." << CoinMessageEol; |
| 2104 | } |
| 2105 | } |
| 2106 | |
| 2107 | //############################################################################# |
| 2108 | // Problem modifying methods (matrix) |
| 2109 | //############################################################################# |
| 2110 | void OsiGrbSolverInterface::addCol(const CoinPackedVectorBase &vec, |
| 2111 | const double collb, const double colub, |
| 2112 | const double obj) |
| 2113 | { |
| 2114 | debugMessage("OsiGrbSolverInterface::addCol(%p, %g, %g, %g)\n", (void *)&vec, collb, colub, obj); |
| 2115 | |
| 2116 | int nc = getNumCols(); |
| 2117 | assert(colspace_ >= nc); |
| 2118 | |
| 2119 | resizeColSpace(nc + 1); |
| 2120 | coltype_[nc] = GRB_CONTINUOUS; |
| 2121 | |
| 2122 | GUROBI_CALL("addCol", GRBupdatemodel(getMutableLpPtr())); |
| 2123 | |
| 2124 | GUROBI_CALL("addCol", GRBaddvar(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), vec.getNumElements(), const_cast< int * >(vec.getIndices()), const_cast< double * >(vec.getElements()), obj, collb, colub, coltype_[nc], NULL)); |
| 2125 | |
| 2126 | if (nauxcols) { |
| 2127 | colmap_O2G[nc] = nc + nauxcols; |
| 2128 | colmap_G2O[nc + nauxcols] = nc; |
| 2129 | } |
| 2130 | } |
| 2131 | |
| 2132 | //----------------------------------------------------------------------------- |
| 2133 | void OsiGrbSolverInterface::addCols(const int numcols, |
| 2134 | const CoinPackedVectorBase *const *cols, |
| 2135 | const double *collb, const double *colub, |
| 2136 | const double *obj) |
| 2137 | { |
| 2138 | debugMessage("OsiGrbSolverInterface::addCols(%d, %p, %p, %p, %p)\n", numcols, (void *)cols, (void *)collb, (void *)colub, (void *)obj); |
| 2139 | |
| 2140 | int nc = getNumCols(); |
| 2141 | assert(colspace_ >= nc); |
| 2142 | |
| 2143 | resizeColSpace(nc + numcols); |
| 2144 | CoinFillN(&coltype_[nc], numcols, GRB_CONTINUOUS); |
| 2145 | |
| 2146 | int i; |
| 2147 | int nz = 0; |
| 2148 | for (i = 0; i < numcols; ++i) |
| 2149 | nz += cols[i]->getNumElements(); |
| 2150 | |
| 2151 | int *index = new int[nz]; |
| 2152 | double *elem = new double[nz]; |
| 2153 | int *start = new int[numcols + 1]; |
| 2154 | |
| 2155 | nz = 0; |
| 2156 | start[0] = 0; |
| 2157 | for (i = 0; i < numcols; ++i) { |
| 2158 | const CoinPackedVectorBase *col = cols[i]; |
| 2159 | const int len = col->getNumElements(); |
| 2160 | CoinDisjointCopyN(col->getIndices(), len, index + nz); |
| 2161 | CoinDisjointCopyN(col->getElements(), len, elem + nz); |
| 2162 | nz += len; |
| 2163 | start[i + 1] = nz; |
| 2164 | } |
| 2165 | |
| 2166 | GUROBI_CALL("addCols", GRBupdatemodel(getMutableLpPtr())); |
| 2167 | |
| 2168 | GUROBI_CALL("addCols", GRBaddvars(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), numcols, nz, start, index, elem, const_cast< double * >(obj), const_cast< double * >(collb), const_cast< double * >(colub), NULL, NULL)); |
| 2169 | |
| 2170 | delete[] start; |
| 2171 | delete[] elem; |
| 2172 | delete[] index; |
| 2173 | |
| 2174 | if (nauxcols) |
| 2175 | for (i = 0; i < numcols; ++i) { |
| 2176 | colmap_O2G[nc + i] = nc + i + nauxcols; |
| 2177 | colmap_G2O[nc + i + nauxcols] = nc + i; |
| 2178 | } |
| 2179 | } |
| 2180 | |
| 2181 | static int intcompare(const void *p1, const void *p2) |
| 2182 | { |
| 2183 | return (*(const int *)p1) - (*(const int *)p2); |
| 2184 | } |
| 2185 | |
| 2186 | //----------------------------------------------------------------------------- |
| 2187 | void OsiGrbSolverInterface::deleteCols(const int num, const int *columnIndices) |
| 2188 | { |
| 2189 | debugMessage("OsiGrbSolverInterface::deleteCols(%d, %p)\n", num, (void *)columnIndices); |
| 2190 | |
| 2191 | if (num == 0) |
| 2192 | return; |
| 2193 | |
| 2194 | GUROBI_CALL("deleteCols", GRBupdatemodel(getMutableLpPtr())); |
| 2195 | |
| 2196 | int *ind = NULL; |
| 2197 | |
| 2198 | if (nauxcols) { |
| 2199 | int nc = getNumCols(); |
| 2200 | |
| 2201 | ind = new int[num]; |
| 2202 | |
| 2203 | // translate into gurobi indices and sort |
| 2204 | for (int i = 0; i < num; ++i) |
| 2205 | ind[i] = colmap_O2G[columnIndices[i]]; |
| 2206 | qsort((void *)ind, num, sizeof(int), intcompare); |
| 2207 | |
| 2208 | // delete indices in gurobi |
| 2209 | GUROBI_CALL("deleteCols", GRBdelvars(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), num, ind)); |
| 2210 | |
| 2211 | nc -= num; |
| 2212 | |
| 2213 | // update colmap_G2O and auxcolind |
| 2214 | int offset = 0; |
| 2215 | int j = 0; |
| 2216 | for (int i = ind[0]; i < nc + nauxcols; ++i) { |
| 2217 | if (j < num && i == ind[j]) { // variable i has been deleted, increase offset by 1 |
| 2218 | assert(colmap_G2O[i] >= 0); |
| 2219 | ++offset; |
| 2220 | while (j < num && i == ind[j]) |
| 2221 | ++j; |
| 2222 | } |
| 2223 | // variable from position i+offset has moved to position i |
| 2224 | if (colmap_G2O[i + offset] >= 0) { // if variable at (hithero) position i+offset was a regular variable, then it is now stored at position i |
| 2225 | // substract offset since variable indices also changed in Osi |
| 2226 | colmap_G2O[i] = colmap_G2O[i + offset] - offset; |
| 2227 | assert(colmap_G2O[i] >= 0); |
| 2228 | // update also colmap_O2G |
| 2229 | colmap_O2G[colmap_G2O[i]] = i; |
| 2230 | } else { // if variable at (hithero) position i+offset was an auxiliary variable, then the corresponding row index is now stored at position i |
| 2231 | int rngrowidx = -colmap_G2O[i + offset] - 1; |
| 2232 | assert(auxcolind[rngrowidx] == i + offset); |
| 2233 | colmap_G2O[i] = colmap_G2O[i + offset]; |
| 2234 | // update also auxcolind to point to position i now |
| 2235 | auxcolind[rngrowidx] = i; |
| 2236 | } |
| 2237 | } |
| 2238 | } else { |
| 2239 | GUROBI_CALL("deleteCols", GRBdelvars(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), num, const_cast< int * >(columnIndices))); |
| 2240 | } |
| 2241 | |
| 2242 | #ifndef NDEBUG |
| 2243 | if (nauxcols) { |
| 2244 | int nc = getNumCols(); |
| 2245 | int nr = getNumRows(); |
| 2246 | for (int i = 0; i < nc + nauxcols; ++i) { |
| 2247 | assert(i >= nc || colmap_G2O[colmap_O2G[i]] == i); |
| 2248 | assert(colmap_G2O[i] < 0 || colmap_O2G[colmap_G2O[i]] == i); |
| 2249 | assert(colmap_G2O[i] >= 0 || -colmap_G2O[i] - 1 < nr); |
| 2250 | assert(colmap_G2O[i] >= 0 || auxcolind[-colmap_G2O[i] - 1] == i); |
| 2251 | } |
| 2252 | } |
| 2253 | #endif |
| 2254 | |
| 2255 | if (!getColNames().empty() || coltype_ != NULL) { |
| 2256 | if (ind == NULL) |
| 2257 | ind = new int[num]; |
| 2258 | |
| 2259 | memcpy(ind, columnIndices, num * sizeof(int)); |
| 2260 | qsort((void *)ind, num, sizeof(int), intcompare); |
| 2261 | |
| 2262 | if (!getColNames().empty()) |
| 2263 | for (int i = num - 1; i >= 0; --i) |
| 2264 | deleteColNames(ind[i], 1); |
| 2265 | |
| 2266 | if (coltype_ != NULL) { |
| 2267 | int offset = 0; |
| 2268 | for (int i = 0; i <= getNumCols(); ++i) { |
| 2269 | // variable i+offset was deleted |
| 2270 | if (offset < num && ind[offset] == i + offset) |
| 2271 | ++offset; |
| 2272 | |
| 2273 | // move column type from position i+offset to i |
| 2274 | coltype_[i] = coltype_[i + offset]; |
| 2275 | } |
| 2276 | } |
| 2277 | } |
| 2278 | |
| 2279 | delete[] ind; |
| 2280 | } |
| 2281 | |
| 2282 | //----------------------------------------------------------------------------- |
| 2283 | void OsiGrbSolverInterface::addRow(const CoinPackedVectorBase &vec, |
| 2284 | const double rowlb, const double rowub) |
| 2285 | { |
| 2286 | debugMessage("OsiGrbSolverInterface::addRow(%p, %g, %g)\n", (void *)&vec, rowlb, rowub); |
| 2287 | |
| 2288 | char sense; |
| 2289 | double rhs, range; |
| 2290 | |
| 2291 | convertBoundToSense(rowlb, rowub, sense, rhs, range); |
| 2292 | |
| 2293 | addRow(vec, sense, rhs, range); |
| 2294 | } |
| 2295 | |
| 2296 | //----------------------------------------------------------------------------- |
| 2297 | void OsiGrbSolverInterface::addRow(const CoinPackedVectorBase &vec, |
| 2298 | const char rowsen, const double rowrhs, |
| 2299 | const double rowrng) |
| 2300 | { |
| 2301 | debugMessage("OsiGrbSolverInterface::addRow(%p, %c, %g, %g)\n", (void *)&vec, rowsen, rowrhs, rowrng); |
| 2302 | |
| 2303 | char grbsense; |
| 2304 | double rhs = rowrhs; |
| 2305 | |
| 2306 | switch (rowsen) { |
| 2307 | case 'R': |
| 2308 | /* ranged rows are added as equality rows first, and then an auxiliary variable is added */ |
| 2309 | assert(rowrng > 0.0); |
| 2310 | grbsense = GRB_EQUAL; |
| 2311 | rhs = 0.0; |
| 2312 | break; |
| 2313 | |
| 2314 | case 'N': |
| 2315 | grbsense = GRB_LESS_EQUAL; |
| 2316 | rhs = getInfinity(); |
| 2317 | break; |
| 2318 | |
| 2319 | case 'L': |
| 2320 | grbsense = GRB_LESS_EQUAL; |
| 2321 | break; |
| 2322 | |
| 2323 | case 'G': |
| 2324 | grbsense = GRB_GREATER_EQUAL; |
| 2325 | break; |
| 2326 | |
| 2327 | case 'E': |
| 2328 | grbsense = GRB_EQUAL; |
| 2329 | break; |
| 2330 | |
| 2331 | default: |
| 2332 | std::cerr << "Unknown row sense: " << rowsen << std::endl; |
| 2333 | exit(-1); |
| 2334 | } |
| 2335 | |
| 2336 | GUROBI_CALL("addRow", GRBupdatemodel(getMutableLpPtr())); |
| 2337 | |
| 2338 | GUROBI_CALL("addRow", GRBaddconstr(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_COLUMN), vec.getNumElements(), const_cast< int * >(vec.getIndices()), const_cast< double * >(vec.getElements()), grbsense, rhs, NULL)); |
| 2339 | |
| 2340 | if (rowsen == 'R') |
| 2341 | convertToRangedRow(getNumRows() - 1, rowrhs, rowrng); |
| 2342 | else if (nauxcols) |
| 2343 | resizeAuxColIndSpace(); |
| 2344 | } |
| 2345 | |
| 2346 | //----------------------------------------------------------------------------- |
| 2347 | void OsiGrbSolverInterface::addRows(const int numrows, |
| 2348 | const CoinPackedVectorBase *const *rows, |
| 2349 | const double *rowlb, const double *rowub) |
| 2350 | { |
| 2351 | debugMessage("OsiGrbSolverInterface::addRows(%d, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowlb, (void *)rowub); |
| 2352 | |
| 2353 | int i; |
| 2354 | int nz = 0; |
| 2355 | for (i = 0; i < numrows; ++i) |
| 2356 | nz += rows[i]->getNumElements(); |
| 2357 | |
| 2358 | int *index = new int[nz]; |
| 2359 | double *elem = new double[nz]; |
| 2360 | int *start = new int[numrows + 1]; |
| 2361 | char *grbsense = new char[numrows]; |
| 2362 | double *rhs = new double[numrows]; |
| 2363 | double range; |
| 2364 | |
| 2365 | bool haverangedrows = false; |
| 2366 | |
| 2367 | nz = 0; |
| 2368 | start[0] = 0; |
| 2369 | for (i = 0; i < numrows; ++i) { |
| 2370 | const CoinPackedVectorBase *row = rows[i]; |
| 2371 | const int len = row->getNumElements(); |
| 2372 | CoinDisjointCopyN(row->getIndices(), len, index + nz); |
| 2373 | CoinDisjointCopyN(row->getElements(), len, elem + nz); |
| 2374 | nz += len; |
| 2375 | start[i + 1] = nz; |
| 2376 | |
| 2377 | convertBoundToSense(rowlb[i], rowub[i], grbsense[i], rhs[i], range); |
| 2378 | if (range || grbsense[i] == 'R') { |
| 2379 | grbsense[i] = 'E'; |
| 2380 | rhs[i] = 0.0; |
| 2381 | haverangedrows = true; |
| 2382 | } |
| 2383 | |
| 2384 | switch (grbsense[i]) { |
| 2385 | case 'N': |
| 2386 | grbsense[i] = GRB_LESS_EQUAL; |
| 2387 | rhs[i] = getInfinity(); |
| 2388 | break; |
| 2389 | case 'L': |
| 2390 | grbsense[i] = GRB_LESS_EQUAL; |
| 2391 | break; |
| 2392 | case 'G': |
| 2393 | grbsense[i] = GRB_GREATER_EQUAL; |
| 2394 | break; |
| 2395 | case 'E': |
| 2396 | grbsense[i] = GRB_EQUAL; |
| 2397 | break; |
| 2398 | } |
| 2399 | } |
| 2400 | |
| 2401 | GUROBI_CALL("addRows", GRBupdatemodel(getMutableLpPtr())); |
| 2402 | |
| 2403 | GUROBI_CALL("addRows", GRBaddconstrs(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), numrows, nz, start, index, elem, grbsense, rhs, NULL)); |
| 2404 | |
| 2405 | delete[] start; |
| 2406 | delete[] elem; |
| 2407 | delete[] index; |
| 2408 | delete[] grbsense; |
| 2409 | delete[] rhs; |
| 2410 | |
| 2411 | if (haverangedrows) { |
| 2412 | int nr = getNumRows() - numrows; |
| 2413 | for (i = 0; i < numrows; ++i) |
| 2414 | if (rowlb[i] > getInfinity() && rowub[i] < getInfinity() && rowub[i] - rowlb[i] > 0.0) |
| 2415 | convertToRangedRow(nr + i, rowub[i], rowub[i] - rowlb[i]); |
| 2416 | } else if (nauxcols) |
| 2417 | resizeAuxColIndSpace(); |
| 2418 | } |
| 2419 | |
| 2420 | //----------------------------------------------------------------------------- |
| 2421 | void OsiGrbSolverInterface::addRows(const int numrows, |
| 2422 | const CoinPackedVectorBase *const *rows, |
| 2423 | const char *rowsen, const double *rowrhs, |
| 2424 | const double *rowrng) |
| 2425 | { |
| 2426 | debugMessage("OsiGrbSolverInterface::addRows(%d, %p, %p, %p, %p)\n", numrows, (void *)rows, (void *)rowsen, (void *)rowrhs, (void *)rowrng); |
| 2427 | |
| 2428 | int i; |
| 2429 | int nz = 0; |
| 2430 | for (i = 0; i < numrows; ++i) |
| 2431 | nz += rows[i]->getNumElements(); |
| 2432 | |
| 2433 | int *index = new int[nz]; |
| 2434 | double *elem = new double[nz]; |
| 2435 | int *start = new int[numrows + 1]; |
| 2436 | char *grbsense = new char[numrows]; |
| 2437 | double *rhs = new double[numrows]; |
| 2438 | bool haverangedrows = false; |
| 2439 | |
| 2440 | nz = 0; |
| 2441 | start[0] = 0; |
| 2442 | for (i = 0; i < numrows; ++i) { |
| 2443 | const CoinPackedVectorBase *row = rows[i]; |
| 2444 | const int len = row->getNumElements(); |
| 2445 | CoinDisjointCopyN(row->getIndices(), len, index + nz); |
| 2446 | CoinDisjointCopyN(row->getElements(), len, elem + nz); |
| 2447 | nz += len; |
| 2448 | start[i + 1] = nz; |
| 2449 | |
| 2450 | rhs[i] = rowrhs[i]; |
| 2451 | |
| 2452 | switch (rowsen[i]) { |
| 2453 | case 'R': |
| 2454 | assert(rowrng[i] > 0.0); |
| 2455 | grbsense[i] = GRB_EQUAL; |
| 2456 | rhs[i] = 0.0; |
| 2457 | haverangedrows = true; |
| 2458 | break; |
| 2459 | case 'N': |
| 2460 | grbsense[i] = GRB_LESS_EQUAL; |
| 2461 | rhs[i] = getInfinity(); |
| 2462 | break; |
| 2463 | case 'L': |
| 2464 | grbsense[i] = GRB_LESS_EQUAL; |
| 2465 | break; |
| 2466 | case 'G': |
| 2467 | grbsense[i] = GRB_GREATER_EQUAL; |
| 2468 | break; |
| 2469 | case 'E': |
| 2470 | grbsense[i] = GRB_EQUAL; |
| 2471 | break; |
| 2472 | } |
| 2473 | } |
| 2474 | |
| 2475 | GUROBI_CALL("addRows", GRBupdatemodel(getMutableLpPtr())); |
| 2476 | |
| 2477 | GUROBI_CALL("addRows", GRBaddconstrs(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), numrows, nz, start, index, elem, grbsense, rhs, NULL)); |
| 2478 | |
| 2479 | delete[] start; |
| 2480 | delete[] elem; |
| 2481 | delete[] index; |
| 2482 | delete[] grbsense; |
| 2483 | delete[] rhs; |
| 2484 | |
| 2485 | if (haverangedrows) { |
| 2486 | int nr = getNumRows() - numrows; |
| 2487 | for (i = 0; i < numrows; ++i) |
| 2488 | if (rowsen[i] == 'R') |
| 2489 | convertToRangedRow(nr + i, rowrhs[i], rowrng[i]); |
| 2490 | } else if (nauxcols) |
| 2491 | resizeAuxColIndSpace(); |
| 2492 | } |
| 2493 | |
| 2494 | //----------------------------------------------------------------------------- |
| 2495 | void OsiGrbSolverInterface::deleteRows(const int num, const int *rowIndices) |
| 2496 | { |
| 2497 | debugMessage("OsiGrbSolverInterface::deleteRows(%d, %p)\n", num, (void *)rowIndices); |
| 2498 | |
| 2499 | if (nauxcols) { // check if a ranged row should be deleted; if so, then convert it into a normal row first |
| 2500 | for (int i = 0; i < num; ++i) { |
| 2501 | if (auxcolind[rowIndices[i]] >= 0) |
| 2502 | convertToNormalRow(rowIndices[i], 'E', 0.0); |
| 2503 | } |
| 2504 | } |
| 2505 | |
| 2506 | GUROBI_CALL("deleteRows", GRBdelconstrs(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_COLUMN), num, const_cast< int * >(rowIndices))); |
| 2507 | |
| 2508 | if (nauxcols == 0 && getRowNames().empty()) |
| 2509 | return; |
| 2510 | |
| 2511 | int *ind = CoinCopyOfArray(rowIndices, num); |
| 2512 | qsort((void *)ind, num, sizeof(int), intcompare); |
| 2513 | |
| 2514 | if (nauxcols) { |
| 2515 | int nr = getNumRows(); |
| 2516 | |
| 2517 | int offset = 0; |
| 2518 | int j = 0; |
| 2519 | for (int i = 0; i < nr; ++i) { |
| 2520 | if (j < num && i == ind[j]) { |
| 2521 | ++offset; |
| 2522 | while (j < num && i == ind[j]) |
| 2523 | ++j; |
| 2524 | } |
| 2525 | auxcolind[i] = auxcolind[i + offset]; |
| 2526 | if (auxcolind[i] >= 0) |
| 2527 | colmap_G2O[auxcolind[i]] = -i - 1; |
| 2528 | } |
| 2529 | } |
| 2530 | |
| 2531 | #ifndef NDEBUG |
| 2532 | if (nauxcols) { |
| 2533 | int nc = getNumCols(); |
| 2534 | int nr = getNumRows(); |
| 2535 | for (int i = 0; i < nc + nauxcols; ++i) { |
| 2536 | assert(i >= nc || colmap_G2O[colmap_O2G[i]] == i); |
| 2537 | assert(colmap_G2O[i] < 0 || colmap_O2G[colmap_G2O[i]] == i); |
| 2538 | assert(colmap_G2O[i] >= 0 || -colmap_G2O[i] - 1 < nr); |
| 2539 | assert(colmap_G2O[i] >= 0 || auxcolind[-colmap_G2O[i] - 1] == i); |
| 2540 | } |
| 2541 | } |
| 2542 | #endif |
| 2543 | |
| 2544 | if (!getRowNames().empty()) |
| 2545 | for (int i = num - 1; i >= 0; --i) |
| 2546 | deleteRowNames(ind[i], 1); |
| 2547 | |
| 2548 | delete[] ind; |
| 2549 | } |
| 2550 | |
| 2551 | //############################################################################# |
| 2552 | // Methods to input a problem |
| 2553 | //############################################################################# |
| 2554 | |
| 2555 | void OsiGrbSolverInterface::loadProblem(const CoinPackedMatrix &matrix, |
| 2556 | const double *collb, const double *colub, |
| 2557 | const double *obj, |
| 2558 | const double *rowlb, const double *rowub) |
| 2559 | { |
| 2560 | debugMessage("OsiGrbSolverInterface::loadProblem(1)(%p, %p, %p, %p, %p, %p)\n", (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowlb, (void *)rowub); |
| 2561 | |
| 2562 | const double inf = getInfinity(); |
| 2563 | |
| 2564 | int nrows = matrix.getNumRows(); |
| 2565 | char *rowSense = new char[nrows]; |
| 2566 | double *rowRhs = new double[nrows]; |
| 2567 | double *rowRange = new double[nrows]; |
| 2568 | |
| 2569 | int i; |
| 2570 | for (i = nrows - 1; i >= 0; --i) { |
| 2571 | const double lower = rowlb ? rowlb[i] : -inf; |
| 2572 | const double upper = rowub ? rowub[i] : inf; |
| 2573 | convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]); |
| 2574 | } |
| 2575 | |
| 2576 | loadProblem(matrix, collb, colub, obj, rowSense, rowRhs, rowRange); |
| 2577 | delete[] rowSense; |
| 2578 | delete[] rowRhs; |
| 2579 | delete[] rowRange; |
| 2580 | } |
| 2581 | |
| 2582 | //----------------------------------------------------------------------------- |
| 2583 | |
| 2584 | void OsiGrbSolverInterface::assignProblem(CoinPackedMatrix *&matrix, |
| 2585 | double *&collb, double *&colub, |
| 2586 | double *&obj, |
| 2587 | double *&rowlb, double *&rowub) |
| 2588 | { |
| 2589 | debugMessage("OsiGrbSolverInterface::assignProblem()\n"); |
| 2590 | |
| 2591 | loadProblem(*matrix, collb, colub, obj, rowlb, rowub); |
| 2592 | delete matrix; |
| 2593 | matrix = 0; |
| 2594 | delete[] collb; |
| 2595 | collb = 0; |
| 2596 | delete[] colub; |
| 2597 | colub = 0; |
| 2598 | delete[] obj; |
| 2599 | obj = 0; |
| 2600 | delete[] rowlb; |
| 2601 | rowlb = 0; |
| 2602 | delete[] rowub; |
| 2603 | rowub = 0; |
| 2604 | } |
| 2605 | |
| 2606 | //----------------------------------------------------------------------------- |
| 2607 | |
| 2608 | void OsiGrbSolverInterface::loadProblem(const CoinPackedMatrix &matrix, |
| 2609 | const double *collb, const double *colub, |
| 2610 | const double *obj, |
| 2611 | const char *rowsen, const double *rowrhs, |
| 2612 | const double *rowrng) |
| 2613 | { |
| 2614 | debugMessage("OsiGrbSolverInterface::loadProblem(2)(%p, %p, %p, %p, %p, %p, %p)\n", |
| 2615 | (void *)&matrix, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng); |
| 2616 | |
| 2617 | int nc = matrix.getNumCols(); |
| 2618 | int nr = matrix.getNumRows(); |
| 2619 | int i; |
| 2620 | |
| 2621 | char *myrowsen = new char[nr]; |
| 2622 | double *myrowrhs = new double[nr]; |
| 2623 | bool haverangedrows = false; |
| 2624 | |
| 2625 | for (i = 0; i < nr; ++i) { |
| 2626 | if (rowrhs) |
| 2627 | myrowrhs[i] = rowrhs[i]; |
| 2628 | else |
| 2629 | myrowrhs[i] = 0.0; |
| 2630 | |
| 2631 | if (rowsen) |
| 2632 | switch (rowsen[i]) { |
| 2633 | case 'R': |
| 2634 | assert(rowrng && rowrng[i] > 0.0); |
| 2635 | myrowsen[i] = GRB_EQUAL; |
| 2636 | myrowrhs[i] = 0.0; |
| 2637 | haverangedrows = true; |
| 2638 | break; |
| 2639 | |
| 2640 | case 'N': |
| 2641 | myrowsen[i] = GRB_LESS_EQUAL; |
| 2642 | myrowrhs[i] = getInfinity(); |
| 2643 | break; |
| 2644 | |
| 2645 | case 'L': |
| 2646 | myrowsen[i] = GRB_LESS_EQUAL; |
| 2647 | break; |
| 2648 | |
| 2649 | case 'G': |
| 2650 | myrowsen[i] = GRB_GREATER_EQUAL; |
| 2651 | break; |
| 2652 | |
| 2653 | case 'E': |
| 2654 | myrowsen[i] = GRB_EQUAL; |
| 2655 | break; |
| 2656 | } |
| 2657 | else |
| 2658 | myrowsen[i] = 'G'; |
| 2659 | } |
| 2660 | |
| 2661 | // Set column values to defaults if NULL pointer passed |
| 2662 | double *clb; |
| 2663 | double *cub; |
| 2664 | double *ob; |
| 2665 | if (collb != NULL) |
| 2666 | clb = const_cast< double * >(collb); |
| 2667 | else { |
| 2668 | clb = new double[nc]; |
| 2669 | CoinFillN(clb, nc, 0.0); |
| 2670 | } |
| 2671 | |
| 2672 | if (colub != NULL) |
| 2673 | cub = const_cast< double * >(colub); |
| 2674 | else { |
| 2675 | cub = new double[nc]; |
| 2676 | CoinFillN(cub, nc, getInfinity()); |
| 2677 | } |
| 2678 | |
| 2679 | if (obj != NULL) |
| 2680 | ob = const_cast< double * >(obj); |
| 2681 | else { |
| 2682 | ob = new double[nc]; |
| 2683 | CoinFillN(ob, nc, 0.0); |
| 2684 | } |
| 2685 | |
| 2686 | bool freeMatrixRequired = false; |
| 2687 | CoinPackedMatrix *m = NULL; |
| 2688 | |
| 2689 | if (!matrix.isColOrdered()) { |
| 2690 | m = new CoinPackedMatrix(); |
| 2691 | m->reverseOrderedCopyOf(matrix); |
| 2692 | freeMatrixRequired = true; |
| 2693 | } else |
| 2694 | m = const_cast< CoinPackedMatrix * >(&matrix); |
| 2695 | |
| 2696 | // up to GUROBI 2.0.1, GUROBI may give an "Index is out of range" error if the constraint matrix has uninitalized "gaps" |
| 2697 | #if (GRB_VERSION_MAJOR < 2) || (GRB_VERSION_MAJOR == 2 && GRB_VERSION_MINOR == 0 && GRB_VERSION_TECHNICAL <= 1) |
| 2698 | if (m->hasGaps()) { |
| 2699 | if (freeMatrixRequired) { |
| 2700 | m->removeGaps(); |
| 2701 | } else { |
| 2702 | m = new CoinPackedMatrix(matrix); |
| 2703 | if (m->hasGaps()) |
| 2704 | m->removeGaps(); |
| 2705 | freeMatrixRequired = true; |
| 2706 | } |
| 2707 | } |
| 2708 | #endif |
| 2709 | |
| 2710 | assert(nc == m->getNumCols()); |
| 2711 | assert(nr == m->getNumRows()); |
| 2712 | assert(m->isColOrdered()); |
| 2713 | |
| 2714 | int modelsense; |
| 2715 | GUROBI_CALL("loadProblem", GRBupdatemodel(getMutableLpPtr())); |
| 2716 | |
| 2717 | GUROBI_CALL("loadProblem", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_MODELSENSE, &modelsense)); |
| 2718 | |
| 2719 | std::string pn; |
| 2720 | getStrParam(OsiProbName, pn); |
| 2721 | |
| 2722 | gutsOfDestructor(); // kill old LP, if any |
| 2723 | |
| 2724 | GUROBI_CALL("loadProblem", GRBloadmodel(getEnvironmentPtr(), &lp_, const_cast< char * >(pn.c_str()), nc, nr, modelsense, 0.0, ob, const_cast< char * >(myrowsen), myrowrhs, const_cast< int * >(m->getVectorStarts()), const_cast< int * >(m->getVectorLengths()), const_cast< int * >(m->getIndices()), const_cast< double * >(m->getElements()), const_cast< double * >(clb), const_cast< double * >(cub), NULL, NULL, NULL)); |
| 2725 | |
| 2726 | // GUROBI up to version 2.0.1 may return a scaled LP after GRBoptimize when requesting it via GRBgetvars |
| 2727 | #if (GRB_VERSION_MAJOR < 2) || (GRB_VERSION_MAJOR == 2 && GRB_VERSION_MINOR == 0 && GRB_VERSION_TECHNICAL <= 1) |
| 2728 | setHintParam(OsiDoScale, false); |
| 2729 | #endif |
| 2730 | |
| 2731 | delete[] myrowsen; |
| 2732 | delete[] myrowrhs; |
| 2733 | if (collb == NULL) |
| 2734 | delete[] clb; |
| 2735 | if (colub == NULL) |
| 2736 | delete[] cub; |
| 2737 | if (obj == NULL) |
| 2738 | delete[] ob; |
| 2739 | if (freeMatrixRequired) |
| 2740 | delete m; |
| 2741 | |
| 2742 | resizeColSpace(nc); |
| 2743 | CoinFillN(coltype_, nc, GRB_CONTINUOUS); |
| 2744 | |
| 2745 | if (haverangedrows) { |
| 2746 | assert(rowrhs); |
| 2747 | assert(rowrng); |
| 2748 | assert(rowsen); |
| 2749 | for (i = 0; i < nr; ++i) |
| 2750 | if (rowsen[i] == 'R') |
| 2751 | convertToRangedRow(i, rowrhs[i], rowrng[i]); |
| 2752 | } |
| 2753 | } |
| 2754 | |
| 2755 | //----------------------------------------------------------------------------- |
| 2756 | |
| 2757 | void OsiGrbSolverInterface::assignProblem(CoinPackedMatrix *&matrix, |
| 2758 | double *&collb, double *&colub, |
| 2759 | double *&obj, |
| 2760 | char *&rowsen, double *&rowrhs, |
| 2761 | double *&rowrng) |
| 2762 | { |
| 2763 | debugMessage("OsiGrbSolverInterface::assignProblem()\n"); |
| 2764 | |
| 2765 | loadProblem(*matrix, collb, colub, obj, rowsen, rowrhs, rowrng); |
| 2766 | delete matrix; |
| 2767 | matrix = NULL; |
| 2768 | delete[] collb; |
| 2769 | collb = NULL; |
| 2770 | delete[] colub; |
| 2771 | colub = NULL; |
| 2772 | delete[] obj; |
| 2773 | obj = NULL; |
| 2774 | delete[] rowsen; |
| 2775 | rowsen = NULL; |
| 2776 | delete[] rowrhs; |
| 2777 | rowrhs = NULL; |
| 2778 | delete[] rowrng; |
| 2779 | rowrng = NULL; |
| 2780 | } |
| 2781 | |
| 2782 | //----------------------------------------------------------------------------- |
| 2783 | |
| 2784 | void OsiGrbSolverInterface::loadProblem(const int numcols, const int numrows, |
| 2785 | const int *start, const int *index, |
| 2786 | const double *value, |
| 2787 | const double *collb, const double *colub, |
| 2788 | const double *obj, |
| 2789 | const double *rowlb, const double *rowub) |
| 2790 | { |
| 2791 | debugMessage("OsiGrbSolverInterface::loadProblem(3)()\n"); |
| 2792 | |
| 2793 | const double inf = getInfinity(); |
| 2794 | |
| 2795 | char *rowSense = new char[numrows]; |
| 2796 | double *rowRhs = new double[numrows]; |
| 2797 | double *rowRange = new double[numrows]; |
| 2798 | |
| 2799 | for (int i = numrows - 1; i >= 0; --i) { |
| 2800 | const double lower = rowlb ? rowlb[i] : -inf; |
| 2801 | const double upper = rowub ? rowub[i] : inf; |
| 2802 | convertBoundToSense(lower, upper, rowSense[i], rowRhs[i], rowRange[i]); |
| 2803 | } |
| 2804 | |
| 2805 | loadProblem(numcols, numrows, start, index, value, collb, colub, obj, rowSense, rowRhs, rowRange); |
| 2806 | |
| 2807 | delete[] rowSense; |
| 2808 | delete[] rowRhs; |
| 2809 | delete[] rowRange; |
| 2810 | } |
| 2811 | |
| 2812 | //----------------------------------------------------------------------------- |
| 2813 | |
| 2814 | void OsiGrbSolverInterface::loadProblem(const int numcols, const int numrows, |
| 2815 | const int *start, const int *index, |
| 2816 | const double *value, |
| 2817 | const double *collb, const double *colub, |
| 2818 | const double *obj, |
| 2819 | const char *rowsen, const double *rowrhs, |
| 2820 | const double *rowrng) |
| 2821 | { |
| 2822 | debugMessage("OsiGrbSolverInterface::loadProblem(4)(%d, %d, %p, %p, %p, %p, %p, %p, %p, %p, %p)\n", |
| 2823 | numcols, numrows, (void *)start, (void *)index, (void *)value, (void *)collb, (void *)colub, (void *)obj, (void *)rowsen, (void *)rowrhs, (void *)rowrng); |
| 2824 | |
| 2825 | int nc = numcols; |
| 2826 | int nr = numrows; |
| 2827 | int i; |
| 2828 | |
| 2829 | char *myrowsen = new char[nr]; |
| 2830 | double *myrowrhs = new double[nr]; |
| 2831 | bool haverangedrows = false; |
| 2832 | |
| 2833 | for (i = 0; i < nr; i++) { |
| 2834 | if (rowrhs) |
| 2835 | myrowrhs[i] = rowrhs[i]; |
| 2836 | else |
| 2837 | myrowrhs[i] = 0.0; |
| 2838 | |
| 2839 | if (rowsen) |
| 2840 | switch (rowsen[i]) { |
| 2841 | case 'R': |
| 2842 | assert(rowrng && rowrng[i] > 0.0); |
| 2843 | myrowsen[i] = GRB_EQUAL; |
| 2844 | myrowrhs[i] = 0.0; |
| 2845 | haverangedrows = true; |
| 2846 | break; |
| 2847 | |
| 2848 | case 'N': |
| 2849 | myrowsen[i] = GRB_LESS_EQUAL; |
| 2850 | myrowrhs[i] = getInfinity(); |
| 2851 | break; |
| 2852 | |
| 2853 | case 'L': |
| 2854 | myrowsen[i] = GRB_LESS_EQUAL; |
| 2855 | break; |
| 2856 | |
| 2857 | case 'G': |
| 2858 | myrowsen[i] = GRB_GREATER_EQUAL; |
| 2859 | break; |
| 2860 | |
| 2861 | case 'E': |
| 2862 | myrowsen[i] = GRB_EQUAL; |
| 2863 | break; |
| 2864 | } |
| 2865 | else |
| 2866 | myrowsen[i] = 'G'; |
| 2867 | } |
| 2868 | |
| 2869 | // Set column values to defaults if NULL pointer passed |
| 2870 | double *clb; |
| 2871 | double *cub; |
| 2872 | double *ob; |
| 2873 | if (collb != NULL) |
| 2874 | clb = const_cast< double * >(collb); |
| 2875 | else { |
| 2876 | clb = new double[nc]; |
| 2877 | CoinFillN(clb, nc, 0.0); |
| 2878 | } |
| 2879 | |
| 2880 | if (colub != NULL) |
| 2881 | cub = const_cast< double * >(colub); |
| 2882 | else { |
| 2883 | cub = new double[nc]; |
| 2884 | CoinFillN(cub, nc, getInfinity()); |
| 2885 | } |
| 2886 | |
| 2887 | if (obj != NULL) |
| 2888 | ob = const_cast< double * >(obj); |
| 2889 | else { |
| 2890 | ob = new double[nc]; |
| 2891 | CoinFillN(ob, nc, 0.0); |
| 2892 | } |
| 2893 | |
| 2894 | int *len = new int[nc]; |
| 2895 | for (i = 0; i < nc; ++i) |
| 2896 | len[i] = start[i + 1] - start[i]; |
| 2897 | |
| 2898 | GUROBI_CALL("loadProblem", GRBupdatemodel(getMutableLpPtr())); |
| 2899 | |
| 2900 | int modelsense; |
| 2901 | GUROBI_CALL("loadProblem", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_MODELSENSE, &modelsense)); |
| 2902 | |
| 2903 | std::string pn; |
| 2904 | getStrParam(OsiProbName, pn); |
| 2905 | |
| 2906 | gutsOfDestructor(); // kill old LP, if any |
| 2907 | |
| 2908 | GUROBI_CALL("loadProblem", GRBloadmodel(getEnvironmentPtr(), &lp_, const_cast< char * >(pn.c_str()), nc, nr, modelsense, 0.0, ob, myrowsen, myrowrhs, const_cast< int * >(start), len, const_cast< int * >(index), const_cast< double * >(value), const_cast< double * >(clb), const_cast< double * >(cub), NULL, NULL, NULL)); |
| 2909 | |
| 2910 | // GUROBI up to version 2.0.1 may return a scaled LP after GRBoptimize when requesting it via GRBgetvars |
| 2911 | #if (GRB_VERSION_MAJOR < 2) || (GRB_VERSION_MAJOR == 2 && GRB_VERSION_MINOR == 0 && GRB_VERSION_TECHNICAL <= 1) |
| 2912 | setHintParam(OsiDoScale, false); |
| 2913 | #endif |
| 2914 | |
| 2915 | delete[] myrowsen; |
| 2916 | delete[] myrowrhs; |
| 2917 | if (collb == NULL) |
| 2918 | delete[] clb; |
| 2919 | if (colub == NULL) |
| 2920 | delete[] cub; |
| 2921 | if (obj == NULL) |
| 2922 | delete[] ob; |
| 2923 | delete[] len; |
| 2924 | |
| 2925 | resizeColSpace(nc); |
| 2926 | CoinFillN(coltype_, nc, GRB_CONTINUOUS); |
| 2927 | |
| 2928 | if (haverangedrows) { |
| 2929 | assert(rowrhs); |
| 2930 | assert(rowrng); |
| 2931 | assert(rowsen); |
| 2932 | for (i = 0; i < nr; ++i) |
| 2933 | if (rowsen[i] == 'R') |
| 2934 | convertToRangedRow(i, rowrhs[i], rowrng[i]); |
| 2935 | } |
| 2936 | } |
| 2937 | |
| 2938 | //----------------------------------------------------------------------------- |
| 2939 | // Read mps files |
| 2940 | //----------------------------------------------------------------------------- |
| 2941 | int OsiGrbSolverInterface::readMps(const char *filename, |
| 2942 | const char *extension) |
| 2943 | { |
| 2944 | debugMessage("OsiGrbSolverInterface::readMps(%s, %s)\n", filename, extension); |
| 2945 | |
| 2946 | // just call base class method |
| 2947 | return OsiSolverInterface::readMps(filename, extension); |
| 2948 | } |
| 2949 | |
| 2950 | //----------------------------------------------------------------------------- |
| 2951 | // Write mps files |
| 2952 | //----------------------------------------------------------------------------- |
| 2953 | void OsiGrbSolverInterface::writeMps(const char *filename, |
| 2954 | const char *extension, |
| 2955 | double objSense) const |
| 2956 | { |
| 2957 | debugMessage("OsiGrbSolverInterface::writeMps(%s, %s, %g)\n", filename, extension, objSense); |
| 2958 | |
| 2959 | std::string f(filename); |
| 2960 | std::string e(extension); |
| 2961 | std::string fullname = f + "." + e; |
| 2962 | |
| 2963 | //TODO give row and column names? |
| 2964 | writeMpsNative(fullname.c_str(), NULL, NULL); |
| 2965 | |
| 2966 | // do not call Gurobi's MPS writer, because |
| 2967 | // 1. it only writes mps if the extension is mps |
| 2968 | // 2. the instance in Gurobi may have extra columns due to reformulated ranged rows, that may be confusing |
| 2969 | // GUROBI_CALL( "writeMps", GRBwrite(getMutableLpPtr(), const_cast<char*>(fullname.c_str())) ); |
| 2970 | } |
| 2971 | |
| 2972 | //############################################################################# |
| 2973 | // CPX specific public interfaces |
| 2974 | //############################################################################# |
| 2975 | |
| 2976 | GRBenv *OsiGrbSolverInterface::getEnvironmentPtr() const |
| 2977 | { |
| 2978 | assert(localenv_ != NULL || globalenv_ != NULL); |
| 2979 | return localenv_ ? localenv_ : globalenv_; |
| 2980 | } |
| 2981 | |
| 2982 | GRBmodel *OsiGrbSolverInterface::getLpPtr(int keepCached) |
| 2983 | { |
| 2984 | freeCachedData(keepCached); |
| 2985 | return getMutableLpPtr(); |
| 2986 | } |
| 2987 | |
| 2988 | /// Return whether the current Gurobi environment runs in demo mode. |
| 2989 | bool OsiGrbSolverInterface::isDemoLicense() const |
| 2990 | { |
| 2991 | debugMessage("OsiGrbSolverInterface::isDemoLicense()\n"); |
| 2992 | |
| 2993 | GRBenv *env = getEnvironmentPtr(); |
| 2994 | |
| 2995 | GRBmodel *testlp; |
| 2996 | |
| 2997 | // a Gurobi demo license allows to solve only models with up to 500 variables |
| 2998 | // thus, let's try to create and solve a model with 1000 variables |
| 2999 | GUROBI_CALL("isDemoLicense", GRBnewmodel(env, &testlp, "licensetest", 1000, NULL, NULL, NULL, NULL, NULL)); |
| 3000 | // GUROBI_CALL( "resolve", GRBsetintparam(GRBgetenv(testlp), GRB_INT_PAR_PRESOLVE, presolve) ); |
| 3001 | |
| 3002 | int rc = GRBoptimize(testlp); |
| 3003 | |
| 3004 | if (rc == GRB_ERROR_SIZE_LIMIT_EXCEEDED) |
| 3005 | return true; |
| 3006 | |
| 3007 | GUROBI_CALL("isDemoLicense", rc); |
| 3008 | |
| 3009 | GRBfreemodel(testlp); |
| 3010 | |
| 3011 | return false; |
| 3012 | } |
| 3013 | |
| 3014 | //----------------------------------------------------------------------------- |
| 3015 | |
| 3016 | const char *OsiGrbSolverInterface::getCtype() const |
| 3017 | { |
| 3018 | debugMessage("OsiGrbSolverInterface::getCtype()\n"); |
| 3019 | |
| 3020 | return coltype_; |
| 3021 | } |
| 3022 | |
| 3023 | //############################################################################# |
| 3024 | // Static instance counter methods |
| 3025 | //############################################################################# |
| 3026 | |
| 3027 | void OsiGrbSolverInterface::incrementInstanceCounter() |
| 3028 | { |
| 3029 | if (numInstances_ == 0 && !globalenv_) { |
| 3030 | GUROBI_CALL("incrementInstanceCounter", GRBloadenv(&globalenv_, NULL)); |
| 3031 | assert(globalenv_ != NULL); |
| 3032 | globalenv_is_ours = true; |
| 3033 | } |
| 3034 | numInstances_++; |
| 3035 | } |
| 3036 | |
| 3037 | //----------------------------------------------------------------------------- |
| 3038 | |
| 3039 | void OsiGrbSolverInterface::decrementInstanceCounter() |
| 3040 | { |
| 3041 | assert(numInstances_ != 0); |
| 3042 | assert(globalenv_ != NULL); |
| 3043 | numInstances_--; |
| 3044 | if (numInstances_ == 0 && globalenv_is_ours) { |
| 3045 | GRBfreeenv(globalenv_); |
| 3046 | globalenv_ = NULL; |
| 3047 | } |
| 3048 | } |
| 3049 | |
| 3050 | //----------------------------------------------------------------------------- |
| 3051 | |
| 3052 | unsigned int OsiGrbSolverInterface::getNumInstances() |
| 3053 | { |
| 3054 | return numInstances_; |
| 3055 | } |
| 3056 | |
| 3057 | void OsiGrbSolverInterface::setEnvironment(GRBenv *globalenv) |
| 3058 | { |
| 3059 | if (numInstances_) { |
| 3060 | assert(globalenv_); |
| 3061 | throw CoinError("Cannot set global GUROBI environment, since some OsiGrb instance is still using it.", "setEnvironment", "OsiGrbSolverInterface", __FILE__, __LINE__); |
| 3062 | } |
| 3063 | |
| 3064 | assert(!globalenv_ || !globalenv_is_ours); |
| 3065 | |
| 3066 | globalenv_ = globalenv; |
| 3067 | globalenv_is_ours = false; |
| 3068 | } |
| 3069 | |
| 3070 | //############################################################################# |
| 3071 | // Constructors, destructors clone and assignment |
| 3072 | //############################################################################# |
| 3073 | |
| 3074 | //------------------------------------------------------------------- |
| 3075 | // Default Constructor |
| 3076 | //------------------------------------------------------------------- |
| 3077 | OsiGrbSolverInterface::OsiGrbSolverInterface(bool use_local_env) |
| 3078 | : OsiSolverInterface() |
| 3079 | , localenv_(NULL) |
| 3080 | , lp_(NULL) |
| 3081 | , hotStartCStat_(NULL) |
| 3082 | , hotStartCStatSize_(0) |
| 3083 | , hotStartRStat_(NULL) |
| 3084 | , hotStartRStatSize_(0) |
| 3085 | , hotStartMaxIteration_(1000000) |
| 3086 | , // ??? default iteration limit for strong branching is large |
| 3087 | nameDisc_(0) |
| 3088 | , obj_(NULL) |
| 3089 | , collower_(NULL) |
| 3090 | , colupper_(NULL) |
| 3091 | , rowsense_(NULL) |
| 3092 | , rhs_(NULL) |
| 3093 | , rowrange_(NULL) |
| 3094 | , rowlower_(NULL) |
| 3095 | , rowupper_(NULL) |
| 3096 | , colsol_(NULL) |
| 3097 | , rowsol_(NULL) |
| 3098 | , redcost_(NULL) |
| 3099 | , rowact_(NULL) |
| 3100 | , matrixByRow_(NULL) |
| 3101 | , matrixByCol_(NULL) |
| 3102 | , probtypemip_(false) |
| 3103 | , domipstart(false) |
| 3104 | , colspace_(0) |
| 3105 | , coltype_(NULL) |
| 3106 | , nauxcols(0) |
| 3107 | , auxcolspace(0) |
| 3108 | , colmap_O2G(NULL) |
| 3109 | , colmap_G2O(NULL) |
| 3110 | , auxcolindspace(0) |
| 3111 | , auxcolind(NULL) |
| 3112 | { |
| 3113 | debugMessage("OsiGrbSolverInterface::OsiGrbSolverInterface()\n"); |
| 3114 | |
| 3115 | if (use_local_env) { |
| 3116 | GUROBI_CALL("OsiGrbSolverInterface", GRBloadenv(&localenv_, NULL)); |
| 3117 | assert(localenv_ != NULL); |
| 3118 | } else |
| 3119 | incrementInstanceCounter(); |
| 3120 | |
| 3121 | gutsOfConstructor(); |
| 3122 | |
| 3123 | // change Osi default to Gurobi default |
| 3124 | setHintParam(OsiDoDualInInitial, true, OsiHintTry); |
| 3125 | } |
| 3126 | |
| 3127 | OsiGrbSolverInterface::OsiGrbSolverInterface(GRBenv *localgrbenv) |
| 3128 | : OsiSolverInterface() |
| 3129 | , localenv_(localgrbenv) |
| 3130 | , lp_(NULL) |
| 3131 | , hotStartCStat_(NULL) |
| 3132 | , hotStartCStatSize_(0) |
| 3133 | , hotStartRStat_(NULL) |
| 3134 | , hotStartRStatSize_(0) |
| 3135 | , hotStartMaxIteration_(1000000) |
| 3136 | , // ??? default iteration limit for strong branching is large |
| 3137 | nameDisc_(0) |
| 3138 | , obj_(NULL) |
| 3139 | , collower_(NULL) |
| 3140 | , colupper_(NULL) |
| 3141 | , rowsense_(NULL) |
| 3142 | , rhs_(NULL) |
| 3143 | , rowrange_(NULL) |
| 3144 | , rowlower_(NULL) |
| 3145 | , rowupper_(NULL) |
| 3146 | , colsol_(NULL) |
| 3147 | , rowsol_(NULL) |
| 3148 | , redcost_(NULL) |
| 3149 | , rowact_(NULL) |
| 3150 | , matrixByRow_(NULL) |
| 3151 | , matrixByCol_(NULL) |
| 3152 | , probtypemip_(false) |
| 3153 | , domipstart(false) |
| 3154 | , colspace_(0) |
| 3155 | , coltype_(NULL) |
| 3156 | , nauxcols(0) |
| 3157 | , auxcolspace(0) |
| 3158 | , colmap_O2G(NULL) |
| 3159 | , colmap_G2O(NULL) |
| 3160 | , auxcolindspace(0) |
| 3161 | , auxcolind(NULL) |
| 3162 | { |
| 3163 | debugMessage("OsiGrbSolverInterface::OsiGrbSolverInterface()\n"); |
| 3164 | |
| 3165 | if (localenv_ == NULL) { // if user called this constructor with NULL pointer, we assume that he meant that a local environment should be created |
| 3166 | GUROBI_CALL("OsiGrbSolverInterface", GRBloadenv(&localenv_, NULL)); |
| 3167 | assert(localenv_ != NULL); |
| 3168 | } |
| 3169 | |
| 3170 | gutsOfConstructor(); |
| 3171 | |
| 3172 | // change Osi default to Gurobi default |
| 3173 | setHintParam(OsiDoDualInInitial, true, OsiHintTry); |
| 3174 | } |
| 3175 | |
| 3176 | //---------------------------------------------------------------- |
| 3177 | // Clone |
| 3178 | //---------------------------------------------------------------- |
| 3179 | OsiSolverInterface *OsiGrbSolverInterface::clone(bool copyData) const |
| 3180 | { |
| 3181 | debugMessage("OsiGrbSolverInterface::clone(%d)\n", copyData); |
| 3182 | |
| 3183 | return (new OsiGrbSolverInterface(*this)); |
| 3184 | } |
| 3185 | |
| 3186 | //------------------------------------------------------------------- |
| 3187 | // Copy constructor |
| 3188 | //------------------------------------------------------------------- |
| 3189 | OsiGrbSolverInterface::OsiGrbSolverInterface(const OsiGrbSolverInterface &source) |
| 3190 | : OsiSolverInterface(source) |
| 3191 | , localenv_(NULL) |
| 3192 | , lp_(NULL) |
| 3193 | , hotStartCStat_(NULL) |
| 3194 | , hotStartCStatSize_(0) |
| 3195 | , hotStartRStat_(NULL) |
| 3196 | , hotStartRStatSize_(0) |
| 3197 | , hotStartMaxIteration_(source.hotStartMaxIteration_) |
| 3198 | , nameDisc_(source.nameDisc_) |
| 3199 | , obj_(NULL) |
| 3200 | , collower_(NULL) |
| 3201 | , colupper_(NULL) |
| 3202 | , rowsense_(NULL) |
| 3203 | , rhs_(NULL) |
| 3204 | , rowrange_(NULL) |
| 3205 | , rowlower_(NULL) |
| 3206 | , rowupper_(NULL) |
| 3207 | , colsol_(NULL) |
| 3208 | , rowsol_(NULL) |
| 3209 | , redcost_(NULL) |
| 3210 | , rowact_(NULL) |
| 3211 | , matrixByRow_(NULL) |
| 3212 | , matrixByCol_(NULL) |
| 3213 | , probtypemip_(false) |
| 3214 | , domipstart(false) |
| 3215 | , colspace_(0) |
| 3216 | , coltype_(NULL) |
| 3217 | , nauxcols(0) |
| 3218 | , auxcolspace(0) |
| 3219 | , colmap_O2G(NULL) |
| 3220 | , colmap_G2O(NULL) |
| 3221 | , auxcolindspace(0) |
| 3222 | , auxcolind(NULL) |
| 3223 | { |
| 3224 | debugMessage("OsiGrbSolverInterface::OsiGrbSolverInterface(%p)\n", (void *)&source); |
| 3225 | |
| 3226 | if (source.localenv_) { |
| 3227 | GUROBI_CALL("OsiGrbSolverInterface", GRBloadenv(&localenv_, NULL)); |
| 3228 | assert(localenv_ != NULL); |
| 3229 | } else |
| 3230 | incrementInstanceCounter(); |
| 3231 | |
| 3232 | gutsOfConstructor(); |
| 3233 | gutsOfCopy(source); |
| 3234 | } |
| 3235 | |
| 3236 | //------------------------------------------------------------------- |
| 3237 | // Destructor |
| 3238 | //------------------------------------------------------------------- |
| 3239 | OsiGrbSolverInterface::~OsiGrbSolverInterface() |
| 3240 | { |
| 3241 | debugMessage("OsiGrbSolverInterface::~OsiGrbSolverInterface()\n"); |
| 3242 | |
| 3243 | gutsOfDestructor(); |
| 3244 | if (localenv_) { |
| 3245 | GRBfreeenv(localenv_); |
| 3246 | } else |
| 3247 | decrementInstanceCounter(); |
| 3248 | } |
| 3249 | |
| 3250 | //---------------------------------------------------------------- |
| 3251 | // Assignment operator |
| 3252 | //------------------------------------------------------------------- |
| 3253 | OsiGrbSolverInterface &OsiGrbSolverInterface::operator=(const OsiGrbSolverInterface &rhs) |
| 3254 | { |
| 3255 | debugMessage("OsiGrbSolverInterface::operator=(%p)\n", (void *)&rhs); |
| 3256 | |
| 3257 | if (this != &rhs) { |
| 3258 | OsiSolverInterface::operator=(rhs); |
| 3259 | gutsOfDestructor(); |
| 3260 | gutsOfConstructor(); |
| 3261 | if (rhs.lp_ != NULL) |
| 3262 | gutsOfCopy(rhs); |
| 3263 | } |
| 3264 | |
| 3265 | return *this; |
| 3266 | } |
| 3267 | |
| 3268 | //############################################################################# |
| 3269 | // Applying cuts |
| 3270 | //############################################################################# |
| 3271 | |
| 3272 | OsiSolverInterface::ApplyCutsReturnCode OsiGrbSolverInterface::applyCuts(const OsiCuts &cs, |
| 3273 | double effectivenessLb) |
| 3274 | { |
| 3275 | debugMessage("OsiGrbSolverInterface::applyCuts(%p)\n", (void *)&cs); |
| 3276 | |
| 3277 | OsiSolverInterface::ApplyCutsReturnCode retVal; |
| 3278 | int i; |
| 3279 | |
| 3280 | // Loop once for each column cut |
| 3281 | for (i = 0; i < cs.sizeColCuts(); i++) { |
| 3282 | if (cs.colCut(i).effectiveness() < effectivenessLb) { |
| 3283 | retVal.incrementIneffective(); |
| 3284 | continue; |
| 3285 | } |
| 3286 | if (!cs.colCut(i).consistent()) { |
| 3287 | retVal.incrementInternallyInconsistent(); |
| 3288 | continue; |
| 3289 | } |
| 3290 | if (!cs.colCut(i).consistent(*this)) { |
| 3291 | retVal.incrementExternallyInconsistent(); |
| 3292 | continue; |
| 3293 | } |
| 3294 | if (cs.colCut(i).infeasible(*this)) { |
| 3295 | retVal.incrementInfeasible(); |
| 3296 | continue; |
| 3297 | } |
| 3298 | applyColCut(cs.colCut(i)); |
| 3299 | retVal.incrementApplied(); |
| 3300 | } |
| 3301 | |
| 3302 | // Loop once for each row cut |
| 3303 | int nToApply = 0; |
| 3304 | size_t space = 0; |
| 3305 | |
| 3306 | for (i = 0; i < cs.sizeRowCuts(); i++) { |
| 3307 | if (cs.rowCut(i).effectiveness() < effectivenessLb) { |
| 3308 | retVal.incrementIneffective(); |
| 3309 | continue; |
| 3310 | } |
| 3311 | if (!cs.rowCut(i).consistent()) { |
| 3312 | retVal.incrementInternallyInconsistent(); |
| 3313 | continue; |
| 3314 | } |
| 3315 | if (!cs.rowCut(i).consistent(*this)) { |
| 3316 | retVal.incrementExternallyInconsistent(); |
| 3317 | continue; |
| 3318 | } |
| 3319 | if (cs.rowCut(i).infeasible(*this)) { |
| 3320 | retVal.incrementInfeasible(); |
| 3321 | continue; |
| 3322 | } |
| 3323 | nToApply++; |
| 3324 | space += cs.rowCut(i).row().getNumElements(); |
| 3325 | |
| 3326 | retVal.incrementApplied(); |
| 3327 | } |
| 3328 | |
| 3329 | int base_row = getNumRows(); |
| 3330 | int base_col = getNumCols() + nauxcols; |
| 3331 | |
| 3332 | //now apply row cuts jointly |
| 3333 | int *start = new int[nToApply + 1]; |
| 3334 | int *indices = new int[space]; |
| 3335 | double *values = new double[space]; |
| 3336 | double *lower = new double[nToApply]; |
| 3337 | double *upper = new double[nToApply]; |
| 3338 | |
| 3339 | int cur = 0; |
| 3340 | int r = 0; |
| 3341 | |
| 3342 | int nTrulyRanged = nToApply; |
| 3343 | for (i = 0; i < cs.sizeRowCuts(); i++) { |
| 3344 | |
| 3345 | if (cs.rowCut(i).effectiveness() < effectivenessLb) { |
| 3346 | continue; |
| 3347 | } |
| 3348 | if (!cs.rowCut(i).consistent()) { |
| 3349 | continue; |
| 3350 | } |
| 3351 | if (!cs.rowCut(i).consistent(*this)) { |
| 3352 | continue; |
| 3353 | } |
| 3354 | if (cs.rowCut(i).infeasible(*this)) { |
| 3355 | continue; |
| 3356 | } |
| 3357 | |
| 3358 | start[r] = cur; |
| 3359 | |
| 3360 | const OsiRowCut &rowCut = cs.rowCut(i); |
| 3361 | |
| 3362 | lower[r] = rowCut.lb(); |
| 3363 | upper[r] = rowCut.ub(); |
| 3364 | |
| 3365 | std::cerr << "range " << lower[r] << " - " << upper[r] << std::endl; |
| 3366 | |
| 3367 | if (lower[r] <= -getInfinity()) { |
| 3368 | lower[r] = -GRB_INFINITY; |
| 3369 | nTrulyRanged--; |
| 3370 | } else if (upper[r] >= getInfinity()) { |
| 3371 | upper[r] = GRB_INFINITY; |
| 3372 | nTrulyRanged--; |
| 3373 | } |
| 3374 | |
| 3375 | for (int k = 0; k < rowCut.row().getNumElements(); k++) { |
| 3376 | values[cur + k] = rowCut.row().getElements()[k]; |
| 3377 | indices[cur + k] = rowCut.row().getIndices()[k]; |
| 3378 | } |
| 3379 | |
| 3380 | r++; |
| 3381 | cur += rowCut.row().getNumElements(); |
| 3382 | } |
| 3383 | start[nToApply] = cur; |
| 3384 | |
| 3385 | int nPrevVars = getNumCols(); |
| 3386 | |
| 3387 | GUROBI_CALL("applyRowCuts", GRBaddrangeconstrs(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_COLUMN), nToApply, static_cast< int >(space), start, indices, values, lower, upper, NULL)); |
| 3388 | |
| 3389 | GUROBI_CALL("applyRowCuts", GRBupdatemodel(getMutableLpPtr())); |
| 3390 | |
| 3391 | // store correspondence between ranged rows and new variables added by Gurobi |
| 3392 | int nNewVars = getNumCols() - nPrevVars; |
| 3393 | |
| 3394 | if (nNewVars != nTrulyRanged) { |
| 3395 | |
| 3396 | std::cerr << "ERROR in applying cuts for Gurobi: " << __FILE__ << " : " |
| 3397 | << "line " |
| 3398 | << __LINE__ << " . Exiting" << std::endl; |
| 3399 | exit(-1); |
| 3400 | } |
| 3401 | |
| 3402 | if (nTrulyRanged > 0) { |
| 3403 | if (nauxcols == 0) { |
| 3404 | // this is the first ranged row |
| 3405 | assert(colmap_O2G == NULL); |
| 3406 | assert(colmap_G2O == NULL); |
| 3407 | |
| 3408 | assert(colspace_ >= getNumCols()); |
| 3409 | |
| 3410 | auxcolspace = nTrulyRanged; |
| 3411 | colmap_G2O = new int[colspace_ + auxcolspace]; |
| 3412 | CoinIotaN(colmap_G2O, colspace_ + auxcolspace, 0); |
| 3413 | |
| 3414 | colmap_O2G = CoinCopyOfArray(colmap_G2O, colspace_); |
| 3415 | } else { |
| 3416 | assert(colmap_O2G != NULL); |
| 3417 | assert(colmap_G2O != NULL); |
| 3418 | resizeAuxColSpace(nauxcols + nTrulyRanged); |
| 3419 | } |
| 3420 | |
| 3421 | nauxcols += nTrulyRanged; |
| 3422 | |
| 3423 | resizeAuxColIndSpace(); |
| 3424 | |
| 3425 | int next = 0; |
| 3426 | for (int k = 0; k < nToApply; k++) { |
| 3427 | |
| 3428 | if (lower[k] > -GRB_INFINITY && upper[k] < GRB_INFINITY) { |
| 3429 | auxcolind[base_row + next] = base_col + next; |
| 3430 | colmap_G2O[auxcolind[base_row + next]] = -(base_row + next) - 1; |
| 3431 | next++; |
| 3432 | } |
| 3433 | } |
| 3434 | } |
| 3435 | |
| 3436 | delete[] start; |
| 3437 | delete[] indices; |
| 3438 | delete[] values; |
| 3439 | delete[] lower; |
| 3440 | delete[] upper; |
| 3441 | |
| 3442 | return retVal; |
| 3443 | } |
| 3444 | |
| 3445 | void OsiGrbSolverInterface::applyColCut(const OsiColCut &cc) |
| 3446 | { |
| 3447 | debugMessage("OsiGrbSolverInterface::applyColCut(%p)\n", (void *)&cc); |
| 3448 | |
| 3449 | const double *gurobiColLB = getColLower(); |
| 3450 | const double *gurobiColUB = getColUpper(); |
| 3451 | const CoinPackedVector &lbs = cc.lbs(); |
| 3452 | const CoinPackedVector &ubs = cc.ubs(); |
| 3453 | int i; |
| 3454 | |
| 3455 | for (i = 0; i < lbs.getNumElements(); ++i) |
| 3456 | if (lbs.getElements()[i] > gurobiColLB[lbs.getIndices()[i]]) |
| 3457 | setColLower(lbs.getIndices()[i], lbs.getElements()[i]); |
| 3458 | for (i = 0; i < ubs.getNumElements(); ++i) |
| 3459 | if (ubs.getElements()[i] < gurobiColUB[ubs.getIndices()[i]]) |
| 3460 | setColUpper(ubs.getIndices()[i], ubs.getElements()[i]); |
| 3461 | } |
| 3462 | |
| 3463 | //----------------------------------------------------------------------------- |
| 3464 | |
| 3465 | void OsiGrbSolverInterface::applyRowCut(const OsiRowCut &rowCut) |
| 3466 | { |
| 3467 | debugMessage("OsiGrbSolverInterface::applyRowCut(%p)\n", (void *)&rowCut); |
| 3468 | |
| 3469 | double rhs = 0.0; |
| 3470 | char sns; |
| 3471 | double lb = rowCut.lb(); |
| 3472 | double ub = rowCut.ub(); |
| 3473 | bool isrange = false; |
| 3474 | if (lb <= -getInfinity() && ub >= getInfinity()) // free constraint |
| 3475 | { |
| 3476 | rhs = getInfinity(); |
| 3477 | sns = GRB_LESS_EQUAL; |
| 3478 | } else if (lb <= -getInfinity()) // <= constraint |
| 3479 | { |
| 3480 | rhs = ub; |
| 3481 | sns = GRB_LESS_EQUAL; |
| 3482 | } else if (ub >= getInfinity()) // >= constraint |
| 3483 | { |
| 3484 | rhs = lb; |
| 3485 | sns = GRB_GREATER_EQUAL; |
| 3486 | } else if (ub == lb) // = constraint |
| 3487 | { |
| 3488 | rhs = ub; |
| 3489 | sns = GRB_EQUAL; |
| 3490 | } else // range constraint |
| 3491 | { |
| 3492 | rhs = 0.0; |
| 3493 | sns = GRB_EQUAL; |
| 3494 | isrange = true; |
| 3495 | } |
| 3496 | |
| 3497 | GUROBI_CALL("applyRowCut", GRBaddconstr(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_COLUMN), rowCut.row().getNumElements(), const_cast< int * >(rowCut.row().getIndices()), const_cast< double * >(rowCut.row().getElements()), sns, rhs, NULL)); |
| 3498 | |
| 3499 | if (isrange) { |
| 3500 | convertToRangedRow(getNumRows() - 1, ub, ub - lb); |
| 3501 | } else if (nauxcols) |
| 3502 | resizeAuxColIndSpace(); |
| 3503 | } |
| 3504 | |
| 3505 | //############################################################################# |
| 3506 | // Private methods (non-static and static) and static data |
| 3507 | //############################################################################# |
| 3508 | |
| 3509 | //------------------------------------------------------------------ |
| 3510 | // Static data |
| 3511 | //------------------------------------------------------------------ |
| 3512 | GRBenv *OsiGrbSolverInterface::globalenv_ = NULL; |
| 3513 | bool OsiGrbSolverInterface::globalenv_is_ours = true; |
| 3514 | unsigned int OsiGrbSolverInterface::numInstances_ = 0; |
| 3515 | |
| 3516 | //------------------------------------------------------------------- |
| 3517 | // Get pointer to GRBmodel*. |
| 3518 | // const methods should use getMutableLpPtr(). |
| 3519 | // non-const methods should use getLpPtr(). |
| 3520 | //------------------------------------------------------------------- |
| 3521 | GRBmodel *OsiGrbSolverInterface::getMutableLpPtr() const |
| 3522 | { |
| 3523 | if (lp_ == NULL) { |
| 3524 | assert(getEnvironmentPtr() != NULL); |
| 3525 | |
| 3526 | GUROBI_CALL("getMutableLpPtr", GRBnewmodel(getEnvironmentPtr(), &lp_, "OsiGrb_problem", 0, NULL, NULL, NULL, NULL, NULL)); |
| 3527 | assert(lp_ != NULL); |
| 3528 | |
| 3529 | // GUROBI up to version 2.0.1 may return a scaled LP after GRBoptimize when requesting it via GRBgetvars |
| 3530 | #if (GRB_VERSION_MAJOR < 2) || (GRB_VERSION_MAJOR == 2 && GRB_VERSION_MINOR == 0 && GRB_VERSION_TECHNICAL <= 1) |
| 3531 | GUROBI_CALL("getMutableLpPtr", GRBsetintparam(GRBgetenv(lp_), GRB_INT_PAR_SCALEFLAG, 0)); |
| 3532 | #endif |
| 3533 | } |
| 3534 | return lp_; |
| 3535 | } |
| 3536 | |
| 3537 | //------------------------------------------------------------------- |
| 3538 | |
| 3539 | void OsiGrbSolverInterface::gutsOfCopy(const OsiGrbSolverInterface &source) |
| 3540 | { |
| 3541 | // Set Rim and constraints |
| 3542 | const double *obj = source.getObjCoefficients(); |
| 3543 | const double *rhs = source.getRightHandSide(); |
| 3544 | const char *sense = source.getRowSense(); |
| 3545 | const CoinPackedMatrix *cols = source.getMatrixByCol(); |
| 3546 | const double *lb = source.getColLower(); |
| 3547 | const double *ub = source.getColUpper(); |
| 3548 | loadProblem(*cols, lb, ub, obj, sense, rhs, source.getRowRange()); |
| 3549 | |
| 3550 | // Set Objective Sense |
| 3551 | setObjSense(source.getObjSense()); |
| 3552 | |
| 3553 | // Set Objective Constant |
| 3554 | double dblParam; |
| 3555 | source.getDblParam(OsiObjOffset, dblParam); |
| 3556 | setDblParam(OsiObjOffset, dblParam); |
| 3557 | |
| 3558 | // Set MIP information |
| 3559 | resizeColSpace(source.colspace_); |
| 3560 | CoinDisjointCopyN(source.coltype_, source.colspace_, coltype_); |
| 3561 | |
| 3562 | // Set Problem name |
| 3563 | std::string strParam; |
| 3564 | source.getStrParam(OsiProbName, strParam); |
| 3565 | setStrParam(OsiProbName, strParam); |
| 3566 | |
| 3567 | // Set Solution - not supported by Gurobi |
| 3568 | // setColSolution(source.getColSolution()); |
| 3569 | // setRowPrice(source.getRowPrice()); |
| 3570 | } |
| 3571 | |
| 3572 | //------------------------------------------------------------------- |
| 3573 | void OsiGrbSolverInterface::gutsOfConstructor() |
| 3574 | { |
| 3575 | } |
| 3576 | |
| 3577 | //------------------------------------------------------------------- |
| 3578 | void OsiGrbSolverInterface::gutsOfDestructor() |
| 3579 | { |
| 3580 | debugMessage("OsiGrbSolverInterface::gutsOfDestructor()\n"); |
| 3581 | |
| 3582 | if (lp_ != NULL) { |
| 3583 | GUROBI_CALL("gutsOfDestructor", GRBfreemodel(lp_)); |
| 3584 | lp_ = NULL; |
| 3585 | freeAllMemory(); |
| 3586 | } |
| 3587 | assert(lp_ == NULL); |
| 3588 | assert(obj_ == NULL); |
| 3589 | assert(collower_ == NULL); |
| 3590 | assert(colupper_ == NULL); |
| 3591 | assert(rowsense_ == NULL); |
| 3592 | assert(rhs_ == NULL); |
| 3593 | assert(rowrange_ == NULL); |
| 3594 | assert(rowlower_ == NULL); |
| 3595 | assert(rowupper_ == NULL); |
| 3596 | assert(colsol_ == NULL); |
| 3597 | assert(rowsol_ == NULL); |
| 3598 | assert(redcost_ == NULL); |
| 3599 | assert(rowact_ == NULL); |
| 3600 | assert(matrixByRow_ == NULL); |
| 3601 | assert(matrixByCol_ == NULL); |
| 3602 | assert(coltype_ == NULL); |
| 3603 | assert(colspace_ == 0); |
| 3604 | assert(auxcolspace == 0); |
| 3605 | assert(auxcolindspace == 0); |
| 3606 | } |
| 3607 | |
| 3608 | //------------------------------------------------------------------- |
| 3609 | /// free cached vectors |
| 3610 | |
| 3611 | void OsiGrbSolverInterface::freeCachedColRim() |
| 3612 | { |
| 3613 | freeCacheDouble(obj_); |
| 3614 | freeCacheDouble(collower_); |
| 3615 | freeCacheDouble(colupper_); |
| 3616 | assert(obj_ == NULL); |
| 3617 | assert(collower_ == NULL); |
| 3618 | assert(colupper_ == NULL); |
| 3619 | } |
| 3620 | |
| 3621 | void OsiGrbSolverInterface::freeCachedRowRim() |
| 3622 | { |
| 3623 | freeCacheChar(rowsense_); |
| 3624 | freeCacheDouble(rhs_); |
| 3625 | freeCacheDouble(rowrange_); |
| 3626 | freeCacheDouble(rowlower_); |
| 3627 | freeCacheDouble(rowupper_); |
| 3628 | assert(rowsense_ == NULL); |
| 3629 | assert(rhs_ == NULL); |
| 3630 | assert(rowrange_ == NULL); |
| 3631 | assert(rowlower_ == NULL); |
| 3632 | assert(rowupper_ == NULL); |
| 3633 | } |
| 3634 | |
| 3635 | void OsiGrbSolverInterface::freeCachedMatrix() |
| 3636 | { |
| 3637 | freeCacheMatrix(matrixByRow_); |
| 3638 | freeCacheMatrix(matrixByCol_); |
| 3639 | assert(matrixByRow_ == NULL); |
| 3640 | assert(matrixByCol_ == NULL); |
| 3641 | } |
| 3642 | |
| 3643 | void OsiGrbSolverInterface::freeCachedResults() |
| 3644 | { |
| 3645 | freeCacheDouble(colsol_); |
| 3646 | freeCacheDouble(rowsol_); |
| 3647 | freeCacheDouble(redcost_); |
| 3648 | freeCacheDouble(rowact_); |
| 3649 | assert(colsol_ == NULL); |
| 3650 | assert(rowsol_ == NULL); |
| 3651 | assert(redcost_ == NULL); |
| 3652 | assert(rowact_ == NULL); |
| 3653 | } |
| 3654 | |
| 3655 | void OsiGrbSolverInterface::freeCachedData(int keepCached) |
| 3656 | { |
| 3657 | if (!(keepCached & OsiGrbSolverInterface::KEEPCACHED_COLUMN)) |
| 3658 | freeCachedColRim(); |
| 3659 | if (!(keepCached & OsiGrbSolverInterface::KEEPCACHED_ROW)) |
| 3660 | freeCachedRowRim(); |
| 3661 | if (!(keepCached & OsiGrbSolverInterface::KEEPCACHED_MATRIX)) |
| 3662 | freeCachedMatrix(); |
| 3663 | if (!(keepCached & OsiGrbSolverInterface::KEEPCACHED_RESULTS)) |
| 3664 | freeCachedResults(); |
| 3665 | } |
| 3666 | |
| 3667 | void OsiGrbSolverInterface::freeAllMemory() |
| 3668 | { |
| 3669 | freeCachedData(); |
| 3670 | if (hotStartCStat_ != NULL) |
| 3671 | delete[] hotStartCStat_; |
| 3672 | if (hotStartRStat_ != NULL) |
| 3673 | delete[] hotStartRStat_; |
| 3674 | hotStartCStat_ = NULL; |
| 3675 | hotStartCStatSize_ = 0; |
| 3676 | hotStartRStat_ = NULL; |
| 3677 | hotStartRStatSize_ = 0; |
| 3678 | freeColSpace(); |
| 3679 | delete[] auxcolind; |
| 3680 | auxcolind = NULL; |
| 3681 | auxcolindspace = 0; |
| 3682 | } |
| 3683 | |
| 3684 | /// converts a normal row into a ranged row by adding an auxiliary variable |
| 3685 | void OsiGrbSolverInterface::convertToRangedRow(int rowidx, double rhs, double range) |
| 3686 | { |
| 3687 | debugMessage("OsiGrbSolverInterface::convertToRangedRow()\n"); |
| 3688 | |
| 3689 | assert(rowidx >= 0); |
| 3690 | assert(rowidx < getNumRows()); |
| 3691 | assert(rhs > -getInfinity()); |
| 3692 | assert(rhs < getInfinity()); |
| 3693 | assert(range > 0.0); |
| 3694 | assert(range < getInfinity()); |
| 3695 | |
| 3696 | if (nauxcols == 0) { // row rowidx is the first ranged row |
| 3697 | assert(colmap_O2G == NULL); |
| 3698 | assert(colmap_G2O == NULL); |
| 3699 | |
| 3700 | assert(colspace_ >= getNumCols()); |
| 3701 | |
| 3702 | auxcolspace = 1; |
| 3703 | colmap_G2O = new int[colspace_ + auxcolspace]; |
| 3704 | CoinIotaN(colmap_G2O, colspace_ + auxcolspace, 0); |
| 3705 | |
| 3706 | colmap_O2G = CoinCopyOfArray(colmap_G2O, colspace_); |
| 3707 | } else { |
| 3708 | assert(colmap_O2G != NULL); |
| 3709 | assert(colmap_G2O != NULL); |
| 3710 | resizeAuxColSpace(nauxcols + 1); |
| 3711 | } |
| 3712 | |
| 3713 | resizeAuxColIndSpace(); |
| 3714 | assert(auxcolind[rowidx] == -1); /* not a ranged row yet */ |
| 3715 | |
| 3716 | GUROBI_CALL("convertToRangedRow", GRBupdatemodel(getMutableLpPtr())); |
| 3717 | |
| 3718 | double minusone = -1.0; |
| 3719 | GUROBI_CALL("convertToRangedRow", GRBaddvar(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_PROBLEM), 1, &rowidx, &minusone, 0.0, rhs - range, rhs, GRB_CONTINUOUS, NULL)); |
| 3720 | |
| 3721 | auxcolind[rowidx] = getNumCols() + nauxcols - 1; |
| 3722 | colmap_G2O[auxcolind[rowidx]] = -rowidx - 1; |
| 3723 | |
| 3724 | ++nauxcols; |
| 3725 | |
| 3726 | #ifndef NDEBUG |
| 3727 | if (nauxcols) { |
| 3728 | int nc = getNumCols(); |
| 3729 | int nr = getNumRows(); |
| 3730 | for (int i = 0; i < nc + nauxcols; ++i) { |
| 3731 | assert(i >= nc || colmap_G2O[colmap_O2G[i]] == i); |
| 3732 | assert(colmap_G2O[i] < 0 || colmap_O2G[colmap_G2O[i]] == i); |
| 3733 | assert(colmap_G2O[i] >= 0 || -colmap_G2O[i] - 1 < nr); |
| 3734 | assert(colmap_G2O[i] >= 0 || auxcolind[-colmap_G2O[i] - 1] == i); |
| 3735 | } |
| 3736 | } |
| 3737 | #endif |
| 3738 | } |
| 3739 | |
| 3740 | /// converts a ranged row into a normal row by removing its auxiliary variable |
| 3741 | void OsiGrbSolverInterface::convertToNormalRow(int rowidx, char sense, double rhs) |
| 3742 | { |
| 3743 | debugMessage("OsiGrbSolverInterface::convertToNormalRow()\n"); |
| 3744 | |
| 3745 | assert(rowidx >= 0); |
| 3746 | assert(rowidx < getNumRows()); |
| 3747 | |
| 3748 | int auxvar = auxcolind[rowidx]; |
| 3749 | |
| 3750 | assert(nauxcols); |
| 3751 | assert(auxvar >= 0); |
| 3752 | assert(colmap_G2O[auxvar] == -rowidx - 1); |
| 3753 | |
| 3754 | GUROBI_CALL("convertToNormalRow", GRBupdatemodel(getMutableLpPtr())); |
| 3755 | |
| 3756 | /* row rowidx should be an ordinary equality row with rhs == 0 now */ |
| 3757 | GUROBI_CALL("convertToNormalRow", GRBdelvars(getLpPtr(OsiGrbSolverInterface::KEEPCACHED_ROW), 1, &auxcolind[rowidx])); |
| 3758 | |
| 3759 | auxcolind[rowidx] = -1; |
| 3760 | |
| 3761 | if (nauxcols > 1) { |
| 3762 | int nc = getNumCols(); |
| 3763 | for (int i = auxvar; i < nc; ++i) { |
| 3764 | if (colmap_O2G[i] > auxvar) |
| 3765 | --colmap_O2G[i]; |
| 3766 | colmap_G2O[i] = colmap_G2O[i + 1]; |
| 3767 | } |
| 3768 | for (int i = nc; i < nc + nauxcols; ++i) |
| 3769 | colmap_G2O[i] = colmap_G2O[i + 1]; |
| 3770 | |
| 3771 | int nr = getNumRows(); |
| 3772 | for (int i = 0; i < nr; ++i) |
| 3773 | if (auxcolind[i] > auxvar) |
| 3774 | --auxcolind[i]; |
| 3775 | --nauxcols; |
| 3776 | } else { // no ranged rows left |
| 3777 | delete[] colmap_O2G; |
| 3778 | delete[] colmap_G2O; |
| 3779 | delete[] auxcolind; |
| 3780 | auxcolspace = 0; |
| 3781 | auxcolindspace = 0; |
| 3782 | nauxcols = 0; |
| 3783 | } |
| 3784 | |
| 3785 | setRowType(rowidx, sense, rhs, 0.0); |
| 3786 | |
| 3787 | #ifndef NDEBUG |
| 3788 | if (nauxcols) { |
| 3789 | int nc = getNumCols(); |
| 3790 | int nr = getNumRows(); |
| 3791 | for (int i = 0; i < nc + nauxcols; ++i) { |
| 3792 | assert(i >= nc || colmap_G2O[colmap_O2G[i]] == i); |
| 3793 | assert(colmap_G2O[i] < 0 || colmap_O2G[colmap_G2O[i]] == i); |
| 3794 | assert(colmap_G2O[i] >= 0 || -colmap_G2O[i] - 1 < nr); |
| 3795 | assert(colmap_G2O[i] >= 0 || auxcolind[-colmap_G2O[i] - 1] == i); |
| 3796 | } |
| 3797 | } |
| 3798 | #endif |
| 3799 | } |
| 3800 | |
| 3801 | //############################################################################# |
| 3802 | // Resets as if default constructor |
| 3803 | void OsiGrbSolverInterface::reset() |
| 3804 | { |
| 3805 | setInitialData(); // clear base class |
| 3806 | gutsOfDestructor(); |
| 3807 | } |
| 3808 | |
| 3809 | /**********************************************************************/ |
| 3810 | /* Returns 1 if can just do getBInv etc |
| 3811 | 2 if has all OsiSimplex methods |
| 3812 | and 0 if it has none */ |
| 3813 | int OsiGrbSolverInterface::canDoSimplexInterface() const |
| 3814 | { |
| 3815 | return 0; |
| 3816 | } |
| 3817 | |
| 3818 | /**********************************************************************/ |
| 3819 | bool OsiGrbSolverInterface::basisIsAvailable() const |
| 3820 | { |
| 3821 | if (getNumCols() == 0) |
| 3822 | return true; |
| 3823 | |
| 3824 | GUROBI_CALL("basisIsAvailable", GRBupdatemodel(getMutableLpPtr())); |
| 3825 | |
| 3826 | int status; |
| 3827 | GUROBI_CALL("basisIsAvailable", GRBgetintattr(getMutableLpPtr(), GRB_INT_ATTR_STATUS, &status)); |
| 3828 | |
| 3829 | if (status == GRB_LOADED || status == GRB_INFEASIBLE || status == GRB_INF_OR_UNBD || status == GRB_UNBOUNDED) |
| 3830 | return false; |
| 3831 | |
| 3832 | int dum; |
| 3833 | return GRBgetintattrelement(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, 0, &dum) == 0; |
| 3834 | } |
| 3835 | |
| 3836 | /* Osi return codes: |
| 3837 | 0: free |
| 3838 | 1: basic |
| 3839 | 2: upper |
| 3840 | 3: lower |
| 3841 | */ |
| 3842 | void OsiGrbSolverInterface::getBasisStatus(int *cstat, int *rstat) const |
| 3843 | { |
| 3844 | |
| 3845 | int numcols = getNumCols(); |
| 3846 | int numrows = getNumRows(); |
| 3847 | |
| 3848 | GUROBI_CALL("getBasisStatus", GRBupdatemodel(getMutableLpPtr())); |
| 3849 | |
| 3850 | if (nauxcols) |
| 3851 | GUROBI_CALL("getBasisStatus", GRBgetintattrlist(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, numcols, colmap_O2G, cstat)); |
| 3852 | else |
| 3853 | GUROBI_CALL("getBasisStatus", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, 0, numcols, cstat)); |
| 3854 | |
| 3855 | for (int i = 0; i < numcols; ++i) |
| 3856 | switch (cstat[i]) { |
| 3857 | case GRB_BASIC: |
| 3858 | cstat[i] = 1; |
| 3859 | break; |
| 3860 | case GRB_NONBASIC_LOWER: |
| 3861 | cstat[i] = 3; |
| 3862 | break; |
| 3863 | case GRB_NONBASIC_UPPER: |
| 3864 | cstat[i] = 2; |
| 3865 | break; |
| 3866 | case GRB_SUPERBASIC: |
| 3867 | cstat[i] = 0; |
| 3868 | break; |
| 3869 | } |
| 3870 | |
| 3871 | GUROBI_CALL("getBasisStatus", GRBgetintattrarray(getMutableLpPtr(), GRB_INT_ATTR_CBASIS, 0, numrows, rstat)); |
| 3872 | |
| 3873 | char sense; |
| 3874 | for (int i = 0; i < numrows; ++i) { |
| 3875 | if (nauxcols && auxcolind[i] >= 0) { /* if the row is a ranged row, then we take the basis status from the corresponding auxiliary column |
| 3876 | is this correct??? */ |
| 3877 | int auxcolstat; |
| 3878 | GUROBI_CALL("getBasisStatus", GRBgetintattrelement(getMutableLpPtr(), GRB_INT_ATTR_VBASIS, auxcolind[i], &auxcolstat)); |
| 3879 | switch (auxcolstat) { |
| 3880 | case GRB_BASIC: |
| 3881 | rstat[i] = 1; |
| 3882 | break; |
| 3883 | case GRB_NONBASIC_LOWER: |
| 3884 | rstat[i] = 3; |
| 3885 | break; |
| 3886 | case GRB_NONBASIC_UPPER: |
| 3887 | rstat[i] = 2; |
| 3888 | break; |
| 3889 | case GRB_SUPERBASIC: |
| 3890 | rstat[i] = 0; |
| 3891 | break; |
| 3892 | } |
| 3893 | } else { |
| 3894 | switch (rstat[i]) { |
| 3895 | case GRB_SUPERBASIC: |
| 3896 | rstat[i] = 0; |
| 3897 | break; |
| 3898 | case GRB_BASIC: |
| 3899 | rstat[i] = 1; |
| 3900 | break; |
| 3901 | case GRB_NONBASIC_LOWER: |
| 3902 | case GRB_NONBASIC_UPPER: |
| 3903 | GUROBI_CALL("getBasisStatus", GRBgetcharattrelement(getMutableLpPtr(), GRB_CHAR_ATTR_SENSE, i, &sense)); |
| 3904 | rstat[i] = (sense == '>' ? 2 : 3); |
| 3905 | break; |
| 3906 | } |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 3912 | */ |