| 1 | // Copyright (C) 2001, International Business Machines |
| 2 | // Corporation and others. All Rights Reserved. |
| 3 | // This file is licensed under the terms of Eclipse Public License (EPL). |
| 4 | |
| 5 | // this is a copy of OsiVolSolverInterface (trunk rev. 1466) renamed to OsiTestSolverInterface |
| 6 | |
| 7 | #include "CoinPragma.hpp" |
| 8 | |
| 9 | #include <cstdlib> |
| 10 | #include <cmath> |
| 11 | |
| 12 | #include "CoinHelperFunctions.hpp" |
| 13 | #include "CoinMpsIO.hpp" |
| 14 | #include "OsiTestSolverInterface.hpp" |
| 15 | |
| 16 | //############################################################################# |
| 17 | |
| 18 | void |
| 19 | OsiTestSolverInterface::initFromRlbRub(const int rownum, |
| 20 | const double* rowlb, |
| 21 | const double* rowub) |
| 22 | { |
| 23 | if (maxNumrows_ > 0) { |
| 24 | rowRimAllocator_(); |
| 25 | if (rowub) { |
| 26 | CoinDisjointCopyN(rowub, rownum, rowupper_); |
| 27 | } else { |
| 28 | CoinFillN(rowupper_, rownum, getInfinity()); |
| 29 | } |
| 30 | if (rowlb) { |
| 31 | CoinDisjointCopyN(rowlb, rownum, rowlower_); |
| 32 | } else { |
| 33 | CoinFillN(rowlower_, rownum, -getInfinity()); |
| 34 | } |
| 35 | // Set the initial dual solution |
| 36 | CoinFillN(rowprice_, rownum, 0.0); |
| 37 | convertBoundsToSenses_(); |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | //############################################################################# |
| 42 | |
| 43 | void |
| 44 | OsiTestSolverInterface::initFromRhsSenseRange(const int rownum, |
| 45 | const char* rowsen, |
| 46 | const double* rowrhs, |
| 47 | const double* rowrng) |
| 48 | { |
| 49 | if (maxNumrows_ > 0) { |
| 50 | rowRimAllocator_(); |
| 51 | if (rowsen) { |
| 52 | CoinDisjointCopyN(rowsen, rownum, rowsense_); |
| 53 | } else { |
| 54 | CoinFillN(rowsense_, rownum, 'G'); |
| 55 | } |
| 56 | if (rowrhs) { |
| 57 | CoinDisjointCopyN(rowrhs, rownum, rhs_); |
| 58 | } else { |
| 59 | CoinFillN(rhs_, rownum, 0.0); |
| 60 | } |
| 61 | if (rowrng) { |
| 62 | CoinDisjointCopyN(rowrng, rownum, rowrange_); |
| 63 | } else { |
| 64 | CoinFillN(rowrange_, rownum, 0.0); |
| 65 | } |
| 66 | // Set the initial dual solution |
| 67 | CoinFillN(rowprice_, rownum, 0.0); |
| 68 | convertSensesToBounds_(); |
| 69 | } |
| 70 | } |
| 71 | //############################################################################# |
| 72 | |
| 73 | void |
| 74 | OsiTestSolverInterface::initFromClbCubObj(const int colnum, |
| 75 | const double* collb, |
| 76 | const double* colub, |
| 77 | const double* obj) |
| 78 | { |
| 79 | if (maxNumcols_ > 0) { |
| 80 | colRimAllocator_(); |
| 81 | if (colub) { |
| 82 | CoinDisjointCopyN(colub, colnum, colupper_); |
| 83 | } else { |
| 84 | CoinFillN(colupper_, colnum, getInfinity()); |
| 85 | } |
| 86 | if (collb) { |
| 87 | CoinDisjointCopyN(collb, colnum, collower_); |
| 88 | } else { |
| 89 | CoinFillN(collower_, colnum, 0.0); |
| 90 | } |
| 91 | CoinFillN(continuous_,colnum,true); |
| 92 | if (obj) { |
| 93 | CoinDisjointCopyN(obj, colnum, objcoeffs_); |
| 94 | } else { |
| 95 | CoinFillN(objcoeffs_, colnum, 0.0); |
| 96 | } |
| 97 | int c; |
| 98 | for ( c=0; c<colnum; c++ ) { |
| 99 | if ( fabs(collower_[c]) < fabs(colupper_[c]) ) { |
| 100 | colsol_[c] = collower_[c]; |
| 101 | } |
| 102 | else { |
| 103 | colsol_[c] = colupper_[c]; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | //############################################################################# |
| 110 | // Problem input methods |
| 111 | //############################################################################# |
| 112 | |
| 113 | void |
| 114 | OsiTestSolverInterface::loadProblem(const CoinPackedMatrix& matrix, |
| 115 | const double* collb, const double* colub, |
| 116 | const double* obj, |
| 117 | const double* rowlb, const double* rowub) |
| 118 | { |
| 119 | gutsOfDestructor_(); |
| 120 | const int rownum = matrix.getNumRows(); |
| 121 | const int colnum = matrix.getNumCols(); |
| 122 | |
| 123 | if (matrix.isColOrdered()) { |
| 124 | colMatrix_.setExtraGap(matrix.getExtraGap()); |
| 125 | colMatrix_.setExtraMajor(matrix.getExtraMajor()); |
| 126 | colMatrix_ = matrix; |
| 127 | colMatrixCurrent_ = true; |
| 128 | rowMatrixCurrent_ = false; |
| 129 | maxNumcols_ = colMatrix_.getMaxMajorDim(); |
| 130 | maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) * |
| 131 | colMatrix_.getMinorDim()); |
| 132 | } else { |
| 133 | rowMatrix_.setExtraGap(matrix.getExtraGap()); |
| 134 | rowMatrix_.setExtraMajor(matrix.getExtraMajor()); |
| 135 | rowMatrix_ = matrix; |
| 136 | rowMatrixCurrent_ = true; |
| 137 | colMatrixCurrent_ = false; |
| 138 | maxNumcols_ = static_cast<int>((1+rowMatrix_.getExtraGap()) * |
| 139 | rowMatrix_.getMinorDim()); |
| 140 | maxNumrows_ = rowMatrix_.getMaxMajorDim(); |
| 141 | } |
| 142 | |
| 143 | initFromRlbRub(rownum, rowlb, rowub); |
| 144 | initFromClbCubObj(colnum, collb, colub, obj); |
| 145 | } |
| 146 | |
| 147 | //----------------------------------------------------------------------- |
| 148 | |
| 149 | void |
| 150 | OsiTestSolverInterface::assignProblem(CoinPackedMatrix*& matrix, |
| 151 | double*& collb, double*& colub, |
| 152 | double*& obj, |
| 153 | double*& rowlb, double*& rowub) |
| 154 | { |
| 155 | gutsOfDestructor_(); |
| 156 | const int rownum = matrix->getNumRows(); |
| 157 | const int colnum = matrix->getNumCols(); |
| 158 | maxNumcols_ = colnum; |
| 159 | maxNumrows_ = rownum; |
| 160 | |
| 161 | if (matrix->isColOrdered()) { |
| 162 | colMatrix_.swap(*matrix); |
| 163 | colMatrixCurrent_ = true; |
| 164 | rowMatrixCurrent_ = false; |
| 165 | } else { |
| 166 | rowMatrix_.swap(*matrix); |
| 167 | rowMatrixCurrent_ = true; |
| 168 | colMatrixCurrent_ = false; |
| 169 | } |
| 170 | delete matrix; matrix = 0; |
| 171 | |
| 172 | rowupper_ = rowub; rowub = 0; |
| 173 | rowlower_ = rowlb; rowlb = 0; |
| 174 | colupper_ = colub; colub = 0; |
| 175 | collower_ = collb; collb = 0; |
| 176 | objcoeffs_ = obj; obj = 0; |
| 177 | |
| 178 | if (maxNumrows_ > 0) { |
| 179 | if (!rowupper_) { |
| 180 | rowupper_ = new double[maxNumrows_]; |
| 181 | CoinFillN(rowupper_, rownum, getInfinity()); |
| 182 | } |
| 183 | if (!rowlower_) { |
| 184 | rowlower_ = new double[maxNumrows_]; |
| 185 | CoinFillN(rowlower_, rownum, -getInfinity()); |
| 186 | } |
| 187 | rowsense_ = new char[maxNumrows_]; |
| 188 | rhs_ = new double[maxNumrows_]; |
| 189 | rowrange_ = new double[maxNumrows_]; |
| 190 | rowprice_ = new double[maxNumrows_]; |
| 191 | lhs_ = new double[maxNumrows_]; |
| 192 | // Set the initial dual solution |
| 193 | CoinFillN(rowprice_, rownum, 0.0); |
| 194 | convertBoundsToSenses_(); |
| 195 | } |
| 196 | if (maxNumcols_ > 0) { |
| 197 | if (!colupper_) { |
| 198 | colupper_ = new double[maxNumcols_]; |
| 199 | CoinFillN(colupper_, colnum, getInfinity()); |
| 200 | } |
| 201 | if (!collower_) { |
| 202 | collower_ = new double[maxNumcols_]; |
| 203 | CoinFillN(collower_, colnum, -getInfinity()); |
| 204 | } |
| 205 | if (!objcoeffs_) { |
| 206 | objcoeffs_ = new double[maxNumcols_]; |
| 207 | CoinFillN(objcoeffs_, colnum, -getInfinity()); |
| 208 | } |
| 209 | |
| 210 | colsol_ = new double[maxNumcols_]; |
| 211 | int c; |
| 212 | for ( c=0; c<colnum; c++ ) { |
| 213 | if ( fabs(collower_[c]) < fabs(colupper_[c]) ) { |
| 214 | colsol_[c] = collower_[c]; |
| 215 | } |
| 216 | else { |
| 217 | colsol_[c] = colupper_[c]; |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | rc_ = new double[maxNumcols_]; |
| 222 | continuous_ = new bool[maxNumcols_]; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | //----------------------------------------------------------------------- |
| 227 | |
| 228 | void |
| 229 | OsiTestSolverInterface::loadProblem(const CoinPackedMatrix& matrix, |
| 230 | const double* collb, const double* colub, |
| 231 | const double* obj, |
| 232 | const char* rowsen, const double* rowrhs, |
| 233 | const double* rowrng) |
| 234 | { |
| 235 | gutsOfDestructor_(); |
| 236 | const int rownum = matrix.getNumRows(); |
| 237 | const int colnum = matrix.getNumCols(); |
| 238 | |
| 239 | if (matrix.isColOrdered()) { |
| 240 | colMatrix_ = matrix; |
| 241 | colMatrixCurrent_ = true; |
| 242 | rowMatrixCurrent_ = false; |
| 243 | maxNumcols_ = colMatrix_.getMaxMajorDim(); |
| 244 | maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) * |
| 245 | colMatrix_.getMinorDim()); |
| 246 | } else { |
| 247 | rowMatrix_ = matrix; |
| 248 | rowMatrixCurrent_ = true; |
| 249 | colMatrixCurrent_ = false; |
| 250 | maxNumcols_ = static_cast<int>((1+rowMatrix_.getExtraGap()) * |
| 251 | rowMatrix_.getMinorDim()); |
| 252 | maxNumrows_ = rowMatrix_.getMaxMajorDim(); |
| 253 | } |
| 254 | |
| 255 | initFromRhsSenseRange(rownum, rowsen, rowrhs, rowrng); |
| 256 | initFromClbCubObj(colnum, collb, colub, obj); |
| 257 | } |
| 258 | |
| 259 | //----------------------------------------------------------------------- |
| 260 | |
| 261 | void |
| 262 | OsiTestSolverInterface::assignProblem(CoinPackedMatrix*& matrix, |
| 263 | double*& collb, double*& colub, |
| 264 | double*& obj, |
| 265 | char*& rowsen, double*& rowrhs, |
| 266 | double*& rowrng) |
| 267 | { |
| 268 | gutsOfDestructor_(); |
| 269 | const int rownum = matrix->getNumRows(); |
| 270 | const int colnum = matrix->getNumCols(); |
| 271 | maxNumcols_ = colnum; |
| 272 | maxNumrows_ = rownum; |
| 273 | |
| 274 | if (matrix->isColOrdered()) { |
| 275 | colMatrix_.swap(*matrix); |
| 276 | colMatrixCurrent_ = true; |
| 277 | rowMatrixCurrent_ = false; |
| 278 | } else { |
| 279 | rowMatrix_.swap(*matrix); |
| 280 | rowMatrixCurrent_ = true; |
| 281 | colMatrixCurrent_ = false; |
| 282 | } |
| 283 | delete matrix; matrix = 0; |
| 284 | |
| 285 | rowsense_ = rowsen; rowsen = 0; |
| 286 | rhs_ = rowrhs; rowrhs = 0; |
| 287 | rowrange_ = rowrng; rowrng = 0; |
| 288 | colupper_ = colub; colub = 0; |
| 289 | collower_ = collb; collb = 0; |
| 290 | objcoeffs_ = obj; obj = 0; |
| 291 | |
| 292 | if (maxNumrows_ > 0) { |
| 293 | if (!rowsense_) { |
| 294 | rowsense_ = new char[maxNumrows_]; |
| 295 | CoinFillN(rowsense_, rownum, 'G'); |
| 296 | } |
| 297 | if (!rhs_) { |
| 298 | rhs_ = new double[maxNumrows_]; |
| 299 | CoinFillN(rhs_, rownum, 0.0); |
| 300 | } |
| 301 | if (!rowrange_) { |
| 302 | rowrange_ = new double[maxNumrows_]; |
| 303 | CoinFillN(rowrange_, rownum, 0.0); |
| 304 | } |
| 305 | rowlower_ = new double[maxNumrows_]; |
| 306 | rowupper_ = new double[maxNumrows_]; |
| 307 | rowprice_ = new double[maxNumrows_]; |
| 308 | lhs_ = new double[maxNumrows_]; |
| 309 | // Set the initial dual solution |
| 310 | CoinFillN(rowprice_, rownum, 0.0); |
| 311 | convertSensesToBounds_(); |
| 312 | } |
| 313 | if (maxNumcols_ > 0) { |
| 314 | if (!colupper_) { |
| 315 | colupper_ = new double[maxNumcols_]; |
| 316 | CoinFillN(colupper_, colnum, getInfinity()); |
| 317 | } |
| 318 | if (!collower_) { |
| 319 | collower_ = new double[maxNumcols_]; |
| 320 | CoinFillN(collower_, colnum, -getInfinity()); |
| 321 | } |
| 322 | if (!objcoeffs_) { |
| 323 | objcoeffs_ = new double[maxNumcols_]; |
| 324 | CoinFillN(objcoeffs_, colnum, -getInfinity()); |
| 325 | } |
| 326 | |
| 327 | colsol_ = new double[maxNumcols_]; |
| 328 | int c; |
| 329 | for ( c=0; c<colnum; c++ ) { |
| 330 | if ( fabs(collower_[c]) < fabs(colupper_[c]) ) { |
| 331 | colsol_[c] = collower_[c]; |
| 332 | } |
| 333 | else { |
| 334 | colsol_[c] = colupper_[c]; |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | rc_ = new double[maxNumcols_]; |
| 339 | continuous_ = new bool[maxNumcols_]; |
| 340 | } |
| 341 | } |
| 342 | |
| 343 | //----------------------------------------------------------------------- |
| 344 | |
| 345 | void |
| 346 | OsiTestSolverInterface::loadProblem(const int numcols, const int numrows, |
| 347 | const int* start, const int* index, |
| 348 | const double* value, |
| 349 | const double* collb, const double* colub, |
| 350 | const double* obj, |
| 351 | const double* rowlb, const double* rowub) |
| 352 | { |
| 353 | gutsOfDestructor_(); |
| 354 | |
| 355 | colMatrix_.copyOf(true, numrows, numcols, start[numcols], |
| 356 | value, index, start, 0); |
| 357 | colMatrixCurrent_ = true; |
| 358 | rowMatrixCurrent_ = false; |
| 359 | maxNumcols_ = colMatrix_.getMaxMajorDim(); |
| 360 | maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) * |
| 361 | colMatrix_.getMinorDim()); |
| 362 | |
| 363 | initFromRlbRub(numrows, rowlb, rowub); |
| 364 | initFromClbCubObj(numcols, collb, colub, obj); |
| 365 | } |
| 366 | |
| 367 | //----------------------------------------------------------------------- |
| 368 | |
| 369 | void |
| 370 | OsiTestSolverInterface::loadProblem(const int numcols, const int numrows, |
| 371 | const int* start, const int* index, |
| 372 | const double* value, |
| 373 | const double* collb, const double* colub, |
| 374 | const double* obj, |
| 375 | const char* rowsen, const double* rowrhs, |
| 376 | const double* rowrng) |
| 377 | { |
| 378 | gutsOfDestructor_(); |
| 379 | |
| 380 | colMatrix_.copyOf(true, numrows, numcols, start[numcols], |
| 381 | value, index, start, 0); |
| 382 | colMatrixCurrent_ = true; |
| 383 | rowMatrixCurrent_ = false; |
| 384 | maxNumcols_ = colMatrix_.getMaxMajorDim(); |
| 385 | maxNumrows_ = static_cast<int>((1+colMatrix_.getExtraGap()) * |
| 386 | colMatrix_.getMinorDim()); |
| 387 | |
| 388 | initFromRhsSenseRange(numrows, rowsen, rowrhs, rowrng); |
| 389 | initFromClbCubObj(numcols, collb, colub, obj); |
| 390 | } |
| 391 | |
| 392 | //----------------------------------------------------------------------- |
| 393 | |
| 394 | |
| 395 | int |
| 396 | OsiTestSolverInterface::readMps(const char *filename, const char *extension) |
| 397 | { |
| 398 | CoinMpsIO reader; |
| 399 | reader.setInfinity(getInfinity()); |
| 400 | int retVal = reader.readMps(filename, extension); |
| 401 | if (retVal == 0) { |
| 402 | loadProblem(*reader.getMatrixByCol(), |
| 403 | reader.getColLower(),reader.getColUpper(), |
| 404 | reader.getObjCoefficients(), |
| 405 | reader.getRowLower(),reader.getRowUpper()); |
| 406 | int nc = getNumCols(); |
| 407 | assert (continuous_); |
| 408 | CoinFillN(continuous_, nc, true); |
| 409 | } |
| 410 | return retVal; |
| 411 | } |
| 412 | |
| 413 | |
| 414 | //----------------------------------------------------------------------- |
| 415 | |
| 416 | void |
| 417 | OsiTestSolverInterface::writeMps(const char *filename, |
| 418 | const char *extension, |
| 419 | double /*objSense*/) const |
| 420 | { |
| 421 | CoinMpsIO writer; |
| 422 | writer.setMpsData(*getMatrixByCol(), getInfinity(), |
| 423 | getColLower(), getColUpper(), |
| 424 | getObjCoefficients(), |
| 425 | reinterpret_cast<const char *> (NULL) /*integrality*/, |
| 426 | getRowLower(), getRowUpper(), |
| 427 | reinterpret_cast<const char **> (NULL) /*colnam*/, |
| 428 | reinterpret_cast<const char **> (NULL) /*rownam*/); |
| 429 | std::string fname = filename; |
| 430 | if (extension) |
| 431 | { if (extension[0] != '\0' && extension[0] != '.') |
| 432 | fname += "." ; } |
| 433 | fname += extension; |
| 434 | writer.writeMps(fname.c_str()); |
| 435 | } |