| 1 | // Copyright (C) 2000, International Business Machines |
| 2 | // Corporation and others. All Rights Reserved. |
| 3 | // This code is licensed under the terms of the Eclipse Public License (EPL). |
| 4 | |
| 5 | #include <cfloat> |
| 6 | |
| 7 | #include "CoinPragma.hpp" |
| 8 | |
| 9 | #include "OsiUnitTests.hpp" |
| 10 | |
| 11 | #include "OsiRowCut.hpp" |
| 12 | #include "CoinFloatEqual.hpp" |
| 13 | |
| 14 | //-------------------------------------------------------------------------- |
| 15 | void OsiRowCutUnitTest(const OsiSolverInterface *baseSiP, |
| 16 | const std::string &mpsDir) |
| 17 | { |
| 18 | |
| 19 | CoinRelFltEq eq; |
| 20 | |
| 21 | // Test default constructor |
| 22 | { |
| 23 | OsiRowCut r; |
| 24 | OSIUNITTEST_ASSERT_ERROR(r.row_.getIndices() == NULL, {}, "osirowcut", "default constructor"); |
| 25 | OSIUNITTEST_ASSERT_ERROR(r.row_.getElements() == NULL, {}, "osirowcut", "default constructor"); |
| 26 | OSIUNITTEST_ASSERT_ERROR(r.row_.getNumElements() == 0, {}, "osirowcut", "default constructor"); |
| 27 | OSIUNITTEST_ASSERT_ERROR(r.lb_ == -COIN_DBL_MAX, {}, "osirowcut", "default constructor"); |
| 28 | OSIUNITTEST_ASSERT_ERROR(r.ub_ == COIN_DBL_MAX, {}, "osirowcut", "default constructor"); |
| 29 | OSIUNITTEST_ASSERT_ERROR(r.effectiveness() == 0.0, {}, "osirowcut", "default constructor"); |
| 30 | } |
| 31 | |
| 32 | // Test set and get methods |
| 33 | const int ne = 4; |
| 34 | int inx[ne] = { 1, 3, 4, 7 }; |
| 35 | double el[ne] = { 1.2, 3.4, 5.6, 7.8 }; |
| 36 | { |
| 37 | OsiRowCut r; |
| 38 | |
| 39 | // Test setting getting bounds |
| 40 | r.setLb(65.432); |
| 41 | r.setUb(123.45); |
| 42 | OSIUNITTEST_ASSERT_ERROR(r.lb() == 65.432, {}, "osirowcut", "set bounds"); |
| 43 | OSIUNITTEST_ASSERT_ERROR(r.ub() == 123.45, {}, "osirowcut", "set bounds"); |
| 44 | |
| 45 | // Test setting/getting of effectiveness,timesUsed,timesTested |
| 46 | r.setEffectiveness(45.); |
| 47 | OSIUNITTEST_ASSERT_ERROR(r.effectiveness() == 45.0, {}, "osirowcut", "set effectivenesss"); |
| 48 | |
| 49 | // Test setting/getting elements with int* & float* vectors |
| 50 | r.setRow(ne, inx, el); |
| 51 | OSIUNITTEST_ASSERT_ERROR(r.row().getNumElements() == ne, return, "osirowcut", "set row"); |
| 52 | bool row_ok = true; |
| 53 | for (int i = 0; i < ne; i++) { |
| 54 | row_ok &= r.row().getIndices()[i] == inx[i]; |
| 55 | row_ok &= r.row().getElements()[i] == el[i]; |
| 56 | } |
| 57 | OSIUNITTEST_ASSERT_ERROR(row_ok, {}, "osirowcut", "set row"); |
| 58 | } |
| 59 | |
| 60 | // Repeat test with ownership constructor |
| 61 | { |
| 62 | int *inxo = new int[ne]; |
| 63 | double *elo = new double[ne]; |
| 64 | double lb = 65.432; |
| 65 | double ub = 123.45; |
| 66 | |
| 67 | int i; |
| 68 | for (i = 0; i < ne; i++) |
| 69 | inxo[i] = inx[i]; |
| 70 | for (i = 0; i < ne; i++) |
| 71 | elo[i] = el[i]; |
| 72 | OsiRowCut r(lb, ub, ne, ne, inxo, elo); |
| 73 | |
| 74 | OSIUNITTEST_ASSERT_ERROR(r.row().getNumElements() == ne, {}, "osirowcut", "ownership constructor"); |
| 75 | OSIUNITTEST_ASSERT_ERROR(r.effectiveness() == 0.0, {}, "osirowcut", "ownership constructor"); |
| 76 | |
| 77 | // Test getting bounds |
| 78 | OSIUNITTEST_ASSERT_ERROR(r.lb() == lb, {}, "osirowcut", "ownership constructor"); |
| 79 | OSIUNITTEST_ASSERT_ERROR(r.ub() == ub, {}, "osirowcut", "ownership constructor"); |
| 80 | |
| 81 | // Test setting/getting of effectiveness |
| 82 | r.setEffectiveness(45.); |
| 83 | OSIUNITTEST_ASSERT_ERROR(r.effectiveness() == 45.0, {}, "osirowcut", "ownership constructor"); |
| 84 | |
| 85 | // Test getting elements with int* & float* vectors |
| 86 | OSIUNITTEST_ASSERT_ERROR(r.row().getNumElements() == ne, return, "osirowcut", "ownership constructor"); |
| 87 | bool row_ok = true; |
| 88 | for (int i = 0; i < ne; i++) { |
| 89 | row_ok &= r.row().getIndices()[i] == inx[i]; |
| 90 | row_ok &= r.row().getElements()[i] == el[i]; |
| 91 | } |
| 92 | OSIUNITTEST_ASSERT_ERROR(row_ok, {}, "osirowcut", "ownership constructor"); |
| 93 | } |
| 94 | |
| 95 | // Test sense, rhs, range |
| 96 | { |
| 97 | { OsiRowCut r; |
| 98 | OSIUNITTEST_ASSERT_ERROR(r.sense() == 'N', {}, "osirowcut", "sense, rhs, range"); |
| 99 | OSIUNITTEST_ASSERT_ERROR(r.rhs() == 0.0, {}, "osirowcut", "sense, rhs, range"); |
| 100 | OSIUNITTEST_ASSERT_ERROR(r.range() == 0.0, {}, "osirowcut", "sense, rhs, range"); |
| 101 | } |
| 102 | { |
| 103 | OsiRowCut r; |
| 104 | r.setLb(65.432); |
| 105 | OSIUNITTEST_ASSERT_ERROR(r.sense() == 'G', {}, "osirowcut", "sense, rhs, range"); |
| 106 | OSIUNITTEST_ASSERT_ERROR(r.rhs() == 65.432, {}, "osirowcut", "sense, rhs, range"); |
| 107 | OSIUNITTEST_ASSERT_ERROR(r.range() == 0.0, {}, "osirowcut", "sense, rhs, range"); |
| 108 | } |
| 109 | { |
| 110 | OsiRowCut r; |
| 111 | r.setLb(65.432); |
| 112 | r.setUb(65.432); |
| 113 | OSIUNITTEST_ASSERT_ERROR(r.sense() == 'E', {}, "osirowcut", "sense, rhs, range"); |
| 114 | OSIUNITTEST_ASSERT_ERROR(r.rhs() == 65.432, {}, "osirowcut", "sense, rhs, range"); |
| 115 | OSIUNITTEST_ASSERT_ERROR(r.range() == 0.0, {}, "osirowcut", "sense, rhs, range"); |
| 116 | } |
| 117 | { |
| 118 | OsiRowCut r; |
| 119 | r.setUb(123.45); |
| 120 | OSIUNITTEST_ASSERT_ERROR(r.sense() == 'L', {}, "osirowcut", "sense, rhs, range"); |
| 121 | OSIUNITTEST_ASSERT_ERROR(r.rhs() == 123.45, {}, "osirowcut", "sense, rhs, range"); |
| 122 | OSIUNITTEST_ASSERT_ERROR(r.range() == 0.0, {}, "osirowcut", "sense, rhs, range"); |
| 123 | } |
| 124 | { |
| 125 | OsiRowCut r; |
| 126 | r.setLb(65.432); |
| 127 | r.setUb(123.45); |
| 128 | OSIUNITTEST_ASSERT_ERROR(r.sense() == 'R', {}, "osirowcut", "sense, rhs, range"); |
| 129 | OSIUNITTEST_ASSERT_ERROR(r.rhs() == 123.45, {}, "osirowcut", "sense, rhs, range"); |
| 130 | OSIUNITTEST_ASSERT_ERROR(eq(r.range(), 123.45 - 65.432), {}, "osirowcut", "sense, rhs, range"); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // Test copy constructor and assignment operator |
| 135 | { |
| 136 | OsiRowCut rhs; |
| 137 | { |
| 138 | OsiRowCut r; |
| 139 | OsiRowCut rC1(r); |
| 140 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getNumElements() == r.row().getNumElements(), {}, "osirowcut", "copy constructor"); |
| 141 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getIndices() == r.row().getIndices(), {}, "osirowcut", "copy constructor"); |
| 142 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getElements() == r.row().getElements(), {}, "osirowcut", "copy constructor"); |
| 143 | OSIUNITTEST_ASSERT_ERROR(rC1.lb() == r.lb(), {}, "osirowcut", "copy constructor"); |
| 144 | OSIUNITTEST_ASSERT_ERROR(rC1.ub() == r.ub(), {}, "osirowcut", "copy constructor"); |
| 145 | |
| 146 | r.setLb(65.432); |
| 147 | r.setUb(123.45); |
| 148 | r.setRow(ne, inx, el); |
| 149 | r.setEffectiveness(123.); |
| 150 | |
| 151 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getNumElements() != r.row().getNumElements(), {}, "osirowcut", "modify copy"); |
| 152 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getIndices() != r.row().getIndices(), {}, "osirowcut", "modify copy"); |
| 153 | OSIUNITTEST_ASSERT_ERROR(rC1.row().getElements() != r.row().getElements(), {}, "osirowcut", "modify copy"); |
| 154 | OSIUNITTEST_ASSERT_ERROR(rC1.lb() != r.lb(), {}, "osirowcut", "modify copy"); |
| 155 | OSIUNITTEST_ASSERT_ERROR(rC1.ub() != r.ub(), {}, "osirowcut", "modify copy"); |
| 156 | OSIUNITTEST_ASSERT_ERROR(rC1.effectiveness() != r.effectiveness(), {}, "osirowcut", "modify copy"); |
| 157 | |
| 158 | OsiRowCut rC2(r); |
| 159 | OSIUNITTEST_ASSERT_ERROR(rC2.row().getNumElements() == r.row().getNumElements(), return, "osirowcut", "copy constructor"); |
| 160 | bool row_ok = true; |
| 161 | for (int i = 0; i < r.row().getNumElements(); i++) { |
| 162 | row_ok &= rC2.row().getIndices()[i] == r.row().getIndices()[i]; |
| 163 | row_ok &= rC2.row().getElements()[i] == r.row().getElements()[i]; |
| 164 | } |
| 165 | OSIUNITTEST_ASSERT_ERROR(row_ok, {}, "osirowcut", "copy constructor"); |
| 166 | OSIUNITTEST_ASSERT_ERROR(rC2.lb() == r.lb(), {}, "osirowcut", "copy constructor"); |
| 167 | OSIUNITTEST_ASSERT_ERROR(rC2.ub() == r.ub(), {}, "osirowcut", "copy constructor"); |
| 168 | OSIUNITTEST_ASSERT_ERROR(rC2.effectiveness() == r.effectiveness(), {}, "osirowcut", "copy constructor"); |
| 169 | |
| 170 | rhs = rC2; |
| 171 | } |
| 172 | // Test that rhs has correct values even though lhs has gone out of scope |
| 173 | OSIUNITTEST_ASSERT_ERROR(rhs.row().getNumElements() == ne, return, "osirowcut", "assignment operator"); |
| 174 | bool row_ok = true; |
| 175 | for (int i = 0; i < ne; i++) { |
| 176 | row_ok &= rhs.row().getIndices()[i] == inx[i]; |
| 177 | row_ok &= rhs.row().getElements()[i] == el[i]; |
| 178 | } |
| 179 | OSIUNITTEST_ASSERT_ERROR(row_ok, {}, "osirowcut", "assignment operator"); |
| 180 | OSIUNITTEST_ASSERT_ERROR(rhs.effectiveness() == 123.0, {}, "osirowcut", "assignment operator"); |
| 181 | OSIUNITTEST_ASSERT_ERROR(rhs.lb() == 65.432, {}, "osirowcut", "assignment operator"); |
| 182 | OSIUNITTEST_ASSERT_ERROR(rhs.ub() == 123.45, {}, "osirowcut", "assignment operator"); |
| 183 | } |
| 184 | |
| 185 | // Test setting row with packed vector |
| 186 | { |
| 187 | CoinPackedVector r; |
| 188 | r.setVector(ne, inx, el); |
| 189 | |
| 190 | OsiRowCut rc; |
| 191 | OSIUNITTEST_ASSERT_ERROR(rc.row() != r, {}, "osirowcut", "setting row with packed vector"); |
| 192 | rc.setRow(r); |
| 193 | OSIUNITTEST_ASSERT_ERROR(rc.row() == r, {}, "osirowcut", "setting row with packed vector"); |
| 194 | } |
| 195 | |
| 196 | // Test operator== |
| 197 | { |
| 198 | CoinPackedVector r; |
| 199 | r.setVector(ne, inx, el); |
| 200 | |
| 201 | OsiRowCut rc; |
| 202 | rc.setRow(r); |
| 203 | rc.setEffectiveness(2.); |
| 204 | rc.setLb(3.0); |
| 205 | rc.setUb(4.0); |
| 206 | |
| 207 | { |
| 208 | OsiRowCut c(rc); |
| 209 | OSIUNITTEST_ASSERT_ERROR(c == rc, {}, "osirowcut", "operator =="); |
| 210 | OSIUNITTEST_ASSERT_ERROR(!(c != rc), {}, "osirowcut", "operator !="); |
| 211 | OSIUNITTEST_ASSERT_ERROR(!(c < rc), {}, "osirowcut", "operator <"); |
| 212 | OSIUNITTEST_ASSERT_ERROR(!(rc < c), {}, "osirowcut", "operator <"); |
| 213 | OSIUNITTEST_ASSERT_ERROR(!(c > rc), {}, "osirowcut", "operator >"); |
| 214 | OSIUNITTEST_ASSERT_ERROR(!(rc > c), {}, "osirowcut", "operator >"); |
| 215 | } |
| 216 | { |
| 217 | OsiRowCut c(rc); |
| 218 | const int ne1 = 4; |
| 219 | int inx1[ne] = { 1, 3, 4, 7 }; |
| 220 | double el1[ne] = { 1.2, 3.4, 5.6, 7.9 }; |
| 221 | c.setRow(ne1, inx1, el1); |
| 222 | OSIUNITTEST_ASSERT_ERROR(!(c == rc), {}, "osirowcut", "operator =="); |
| 223 | OSIUNITTEST_ASSERT_ERROR(c != rc, {}, "osirowcut", "operator !="); |
| 224 | OSIUNITTEST_ASSERT_ERROR(!(rc < c), {}, "osirowcut", "operator <"); |
| 225 | } |
| 226 | { |
| 227 | OsiRowCut c(rc); |
| 228 | c.setEffectiveness(3.); |
| 229 | OSIUNITTEST_ASSERT_ERROR(!(c == rc), {}, "osirowcut", "operator =="); |
| 230 | OSIUNITTEST_ASSERT_ERROR(c != rc, {}, "osirowcut", "operator !="); |
| 231 | OSIUNITTEST_ASSERT_ERROR(!(c < rc), {}, "osirowcut", "operator <"); |
| 232 | OSIUNITTEST_ASSERT_ERROR((rc < c), {}, "osirowcut", "operator <"); |
| 233 | OSIUNITTEST_ASSERT_ERROR((c > rc), {}, "osirowcut", "operator >"); |
| 234 | OSIUNITTEST_ASSERT_ERROR(!(rc > c), {}, "osirowcut", "operator >"); |
| 235 | } |
| 236 | { |
| 237 | OsiRowCut c(rc); |
| 238 | c.setLb(4.0); |
| 239 | OSIUNITTEST_ASSERT_ERROR(!(c == rc), {}, "osirowcut", "operator =="); |
| 240 | OSIUNITTEST_ASSERT_ERROR(c != rc, {}, "osirowcut", "operator !="); |
| 241 | } |
| 242 | { |
| 243 | OsiRowCut c(rc); |
| 244 | c.setUb(5.0); |
| 245 | OSIUNITTEST_ASSERT_ERROR(!(c == rc), {}, "osirowcut", "operator =="); |
| 246 | OSIUNITTEST_ASSERT_ERROR(c != rc, {}, "osirowcut", "operator !="); |
| 247 | } |
| 248 | } |
| 249 | #ifndef COIN_NOTEST_DUPLICATE |
| 250 | { |
| 251 | // Test consistent |
| 252 | OsiSolverInterface *imP = baseSiP->clone(); |
| 253 | assert(imP != NULL); |
| 254 | std::string fn = mpsDir + "exmip1"; |
| 255 | OSIUNITTEST_ASSERT_ERROR(imP->readMps(fn.c_str(), "mps") == 0, {}, "osirowcut", "read MPS"); |
| 256 | |
| 257 | OsiRowCut c; |
| 258 | const int ne = 3; |
| 259 | int inx[ne] = { -1, 5, 4 }; |
| 260 | double el[ne] = { 1., 1., 1. }; |
| 261 | c.setRow(ne, inx, el); |
| 262 | OSIUNITTEST_ASSERT_ERROR(!c.consistent(), {}, "osirowcut", "consistent"); |
| 263 | |
| 264 | inx[0] = 5; |
| 265 | #if 0 |
| 266 | c.setRow(ne,inx,el); |
| 267 | OSIUNITTEST_ASSERT_ERROR(!c.consistent(), {}, "osirowcut", "consistent"); |
| 268 | #else |
| 269 | bool errorThrown = false; |
| 270 | try { |
| 271 | c.setRow(ne, inx, el); |
| 272 | } catch (CoinError e) { |
| 273 | errorThrown = true; |
| 274 | } |
| 275 | OSIUNITTEST_ASSERT_ERROR(errorThrown == true, {}, "osirowcut", "duplicate entries"); |
| 276 | #endif |
| 277 | |
| 278 | inx[0] = 3; |
| 279 | c.setRow(ne, inx, el); |
| 280 | OSIUNITTEST_ASSERT_ERROR(c.consistent(), {}, "osirowcut", "consistent"); |
| 281 | |
| 282 | c.setLb(5.); |
| 283 | c.setUb(5.); |
| 284 | OSIUNITTEST_ASSERT_ERROR(c.consistent(), {}, "osirowcut", "consistent"); |
| 285 | |
| 286 | c.setLb(5.5); |
| 287 | OSIUNITTEST_ASSERT_ERROR(c.consistent(), {}, "osirowcut", "consistent"); |
| 288 | OSIUNITTEST_ASSERT_ERROR(c.infeasible(*imP), {}, "osirowcut", "infeasible"); |
| 289 | delete imP; |
| 290 | } |
| 291 | #endif |
| 292 | { |
| 293 | // Test consistent(IntegerModel) method. |
| 294 | OsiSolverInterface *imP = baseSiP->clone(); |
| 295 | assert(imP != NULL); |
| 296 | std::string fn = mpsDir + "exmip1"; |
| 297 | OSIUNITTEST_ASSERT_ERROR(imP->readMps(fn.c_str(), "mps") == 0, {}, "osirowcut", "read MPS"); |
| 298 | |
| 299 | OsiRowCut cut; |
| 300 | const int ne = 3; |
| 301 | int inx[ne] = { 3, 5, 4 }; |
| 302 | double el[ne] = { 1., 1., 1. }; |
| 303 | cut.setRow(ne, inx, el); |
| 304 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(), {}, "osirowcut", "consistent"); |
| 305 | |
| 306 | inx[0] = 7; |
| 307 | cut.setRow(ne, inx, el); |
| 308 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(*imP), {}, "osirowcut", "consistent(IntegerModel)"); |
| 309 | |
| 310 | inx[0] = 8; |
| 311 | cut.setRow(ne, inx, el); |
| 312 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(), {}, "osirowcut", "consistent(IntegerModel)"); |
| 313 | OSIUNITTEST_ASSERT_ERROR(!cut.consistent(*imP), {}, "osirowcut", "consistent(IntegerModel)"); |
| 314 | delete imP; |
| 315 | } |
| 316 | { |
| 317 | //Test infeasible(im) method |
| 318 | OsiSolverInterface *imP = baseSiP->clone(); |
| 319 | assert(imP != NULL); |
| 320 | std::string fn = mpsDir + "exmip1"; |
| 321 | OSIUNITTEST_ASSERT_ERROR(imP->readMps(fn.c_str(), "mps") == 0, {}, "osirowcut", "read MPS"); |
| 322 | |
| 323 | OsiRowCut cut; |
| 324 | const int ne = 3; |
| 325 | int inx[ne] = { 3, 5, 4 }; |
| 326 | double el[ne] = { 1., 1., 1. }; |
| 327 | cut.setRow(ne, inx, el); |
| 328 | OSIUNITTEST_ASSERT_ERROR(!cut.infeasible(*imP), {}, "osirowcut", "infeasible(IntegerModel)"); |
| 329 | |
| 330 | OsiRowCut c1; |
| 331 | OSIUNITTEST_ASSERT_ERROR(!c1.infeasible(*imP), {}, "osirowcut", "infeasible(IntegerModel)"); |
| 332 | OSIUNITTEST_ASSERT_ERROR(c1.consistent(), {}, "osirowcut", "consistent"); |
| 333 | OSIUNITTEST_ASSERT_ERROR(c1.consistent(*imP), {}, "osirowcut", "consistent(IntegerModel)"); |
| 334 | delete imP; |
| 335 | } |
| 336 | { |
| 337 | //Test violation |
| 338 | |
| 339 | double solution[] = { 1.0, 1.0, 1.0, 2.0, 2.0, 2.0 }; |
| 340 | OsiRowCut cut; |
| 341 | const int ne = 3; |
| 342 | int inx[ne] = { 3, 5, 4 }; |
| 343 | double el[ne] = { 1., 1., 1. }; |
| 344 | cut.setRow(ne, inx, el); |
| 345 | cut.setUb(5.); |
| 346 | OSIUNITTEST_ASSERT_ERROR(cut.violated(solution), {}, "osirowcut", "violation"); |
| 347 | |
| 348 | cut.setLb(5.); |
| 349 | cut.setUb(10.); |
| 350 | OSIUNITTEST_ASSERT_ERROR(!cut.violated(solution), {}, "osirowcut", "violation"); |
| 351 | |
| 352 | cut.setLb(6.1); |
| 353 | OSIUNITTEST_ASSERT_ERROR(cut.violated(solution), {}, "osirowcut", "violation"); |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 358 | */ |