| 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 <cstdlib> |
| 6 | //#include <cassert> |
| 7 | //#include <cmath> |
| 8 | |
| 9 | #include "CoinPragma.hpp" |
| 10 | |
| 11 | #include "OsiUnitTests.hpp" |
| 12 | |
| 13 | #include "OsiColCut.hpp" |
| 14 | |
| 15 | //-------------------------------------------------------------------------- |
| 16 | void OsiColCutUnitTest(const OsiSolverInterface *baseSiP, const std::string &mpsDir) |
| 17 | { |
| 18 | |
| 19 | // Test default constructor |
| 20 | { |
| 21 | OsiColCut r; |
| 22 | OSIUNITTEST_ASSERT_ERROR(r.lbs_.getIndices() == NULL, {}, "osicolcut", "default constructor"); |
| 23 | OSIUNITTEST_ASSERT_ERROR(r.lbs_.getElements() == NULL, {}, "osicolcut", "default constructor"); |
| 24 | OSIUNITTEST_ASSERT_ERROR(r.lbs_.getNumElements() == 0, {}, "osicolcut", "default constructor"); |
| 25 | OSIUNITTEST_ASSERT_ERROR(r.lbs().getNumElements() == 0, {}, "osicolcut", "default constructor"); |
| 26 | OSIUNITTEST_ASSERT_ERROR(r.ubs_.getIndices() == NULL, {}, "osicolcut", "default constructor"); |
| 27 | OSIUNITTEST_ASSERT_ERROR(r.ubs_.getElements() == NULL, {}, "osicolcut", "default constructor"); |
| 28 | OSIUNITTEST_ASSERT_ERROR(r.ubs_.getNumElements() == 0, {}, "osicolcut", "default constructor"); |
| 29 | OSIUNITTEST_ASSERT_ERROR(r.ubs().getNumElements() == 0, {}, "osicolcut", "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 | const int ne3 = 0; |
| 37 | int *inx3 = NULL; |
| 38 | double *el3 = NULL; |
| 39 | { |
| 40 | OsiColCut r; |
| 41 | |
| 42 | // Test setting/getting bounds |
| 43 | r.setLbs(ne, inx, el); |
| 44 | r.setEffectiveness(222.); |
| 45 | OSIUNITTEST_ASSERT_ERROR(r.lbs().getNumElements() == ne, return, "osicolcut", "setting bounds"); |
| 46 | bool bounds_ok = true; |
| 47 | for (int i = 0; i < ne; i++) { |
| 48 | bounds_ok &= r.lbs().getIndices()[i] == inx[i]; |
| 49 | bounds_ok &= r.lbs().getElements()[i] == el[i]; |
| 50 | } |
| 51 | OSIUNITTEST_ASSERT_ERROR(bounds_ok, {}, "osicolcut", "setting bounds"); |
| 52 | OSIUNITTEST_ASSERT_ERROR(r.effectiveness() == 222.0, {}, "osicolcut", "setting bounds"); |
| 53 | |
| 54 | r.setUbs(ne3, inx3, el3); |
| 55 | OSIUNITTEST_ASSERT_ERROR(r.ubs().getNumElements() == 0, {}, "osicolcut", "setting bounds"); |
| 56 | OSIUNITTEST_ASSERT_ERROR(r.ubs().getIndices() == NULL, {}, "osicolcut", "setting bounds"); |
| 57 | OSIUNITTEST_ASSERT_ERROR(r.ubs().getElements() == NULL, {}, "osicolcut", "setting bounds"); |
| 58 | } |
| 59 | |
| 60 | // Test copy constructor and assignment operator |
| 61 | { |
| 62 | OsiColCut rhs; |
| 63 | { |
| 64 | OsiColCut r; |
| 65 | OsiColCut rC1(r); |
| 66 | OSIUNITTEST_ASSERT_ERROR(rC1.lbs().getNumElements() == r.lbs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 67 | OSIUNITTEST_ASSERT_ERROR(rC1.ubs().getNumElements() == r.ubs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 68 | |
| 69 | r.setLbs(ne, inx, el); |
| 70 | r.setUbs(ne, inx, el); |
| 71 | r.setEffectiveness(121.); |
| 72 | |
| 73 | OSIUNITTEST_ASSERT_ERROR(rC1.lbs().getNumElements() != r.lbs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 74 | OSIUNITTEST_ASSERT_ERROR(rC1.ubs().getNumElements() != r.lbs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 75 | |
| 76 | OsiColCut rC2(r); |
| 77 | OSIUNITTEST_ASSERT_ERROR(rC2.lbs().getNumElements() == r.lbs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 78 | OSIUNITTEST_ASSERT_ERROR(rC2.ubs().getNumElements() == r.ubs().getNumElements(), {}, "osicolcut", "copy constructor"); |
| 79 | OSIUNITTEST_ASSERT_ERROR(rC2.lbs().getNumElements() == ne, return, "osicolcut", "copy constructor"); |
| 80 | OSIUNITTEST_ASSERT_ERROR(rC2.ubs().getNumElements() == ne, return, "osicolcut", "copy constructor"); |
| 81 | bool bounds_ok = true; |
| 82 | for (int i = 0; i < ne; i++) { |
| 83 | bounds_ok &= rC2.lbs().getIndices()[i] == inx[i]; |
| 84 | bounds_ok &= rC2.lbs().getElements()[i] == el[i]; |
| 85 | bounds_ok &= rC2.ubs().getIndices()[i] == inx[i]; |
| 86 | bounds_ok &= rC2.ubs().getElements()[i] == el[i]; |
| 87 | } |
| 88 | OSIUNITTEST_ASSERT_ERROR(bounds_ok, {}, "osicolcut", "copy constructor"); |
| 89 | OSIUNITTEST_ASSERT_ERROR(rC2.effectiveness() == 121.0, {}, "osicolcut", "copy constructor"); |
| 90 | |
| 91 | rhs = rC2; |
| 92 | } |
| 93 | // Test that rhs has correct values even though lhs has gone out of scope |
| 94 | OSIUNITTEST_ASSERT_ERROR(rhs.lbs().getNumElements() == ne, return, "osicolcut", "assignment operator"); |
| 95 | OSIUNITTEST_ASSERT_ERROR(rhs.ubs().getNumElements() == ne, return, "osicolcut", "assignment operator"); |
| 96 | bool bounds_ok = true; |
| 97 | for (int i = 0; i < ne; i++) { |
| 98 | bounds_ok &= rhs.lbs().getIndices()[i] == inx[i]; |
| 99 | bounds_ok &= rhs.lbs().getElements()[i] == el[i]; |
| 100 | bounds_ok &= rhs.ubs().getIndices()[i] == inx[i]; |
| 101 | bounds_ok &= rhs.ubs().getElements()[i] == el[i]; |
| 102 | } |
| 103 | OSIUNITTEST_ASSERT_ERROR(bounds_ok, {}, "osicolcut", "assignment operator"); |
| 104 | OSIUNITTEST_ASSERT_ERROR(rhs.effectiveness() == 121.0, {}, "osicolcut", "assignment operator"); |
| 105 | } |
| 106 | |
| 107 | // Test setting bounds with packed vector and operator== |
| 108 | { |
| 109 | const int ne1 = 4; |
| 110 | int inx1[ne] = { 1, 3, 4, 7 }; |
| 111 | double el1[ne] = { 1.2, 3.4, 5.6, 7.8 }; |
| 112 | const int ne2 = 2; |
| 113 | int inx2[ne2] = { 1, 3 }; |
| 114 | double el2[ne2] = { 1.2, 3.4 }; |
| 115 | CoinPackedVector v1, v2; |
| 116 | v1.setVector(ne1, inx1, el1); |
| 117 | v2.setVector(ne2, inx2, el2); |
| 118 | |
| 119 | OsiColCut c1, c2; |
| 120 | OSIUNITTEST_ASSERT_ERROR(c1 == c2, {}, "osicolcut", "setting bounds with packed vector and operator =="); |
| 121 | OSIUNITTEST_ASSERT_ERROR(!(c1 != c2), {}, "osicolcut", "setting bounds with packed vector and operator !="); |
| 122 | |
| 123 | c1.setLbs(v1); |
| 124 | OSIUNITTEST_ASSERT_ERROR(c1 != c2, {}, "osicolcut", "setting bounds with packed vector and operator !="); |
| 125 | OSIUNITTEST_ASSERT_ERROR(!(c1 == c2), {}, "osicolcut", "setting bounds with packed vector and operator =="); |
| 126 | OSIUNITTEST_ASSERT_ERROR(c1.lbs() == v1, {}, "osicolcut", "setting bounds with packed vector and operator !="); |
| 127 | |
| 128 | c1.setUbs(v2); |
| 129 | OSIUNITTEST_ASSERT_ERROR(c1.ubs() == v2, {}, "osicolcut", "setting bounds with packed vector and operator !="); |
| 130 | c1.setEffectiveness(3.); |
| 131 | OSIUNITTEST_ASSERT_ERROR(c1.effectiveness() == 3.0, {}, "osicolcut", "setting bounds with packed vector and operator !="); |
| 132 | |
| 133 | { |
| 134 | OsiColCut c3(c1); |
| 135 | OSIUNITTEST_ASSERT_ERROR(c3 == c1, {}, "osicolcut", "operator =="); |
| 136 | OSIUNITTEST_ASSERT_ERROR(!(c3 != c1), {}, "osicolcut", "operator !="); |
| 137 | } |
| 138 | { |
| 139 | OsiColCut c3(c1); |
| 140 | c3.setLbs(v2); |
| 141 | OSIUNITTEST_ASSERT_ERROR(c3 != c1, {}, "osicolcut", "operator !="); |
| 142 | OSIUNITTEST_ASSERT_ERROR(!(c3 == c1), {}, "osicolcut", "operator =="); |
| 143 | } |
| 144 | { |
| 145 | OsiColCut c3(c1); |
| 146 | c3.setUbs(v1); |
| 147 | OSIUNITTEST_ASSERT_ERROR(c3 != c1, {}, "osicolcut", "operator !="); |
| 148 | OSIUNITTEST_ASSERT_ERROR(!(c3 == c1), {}, "osicolcut", "operator =="); |
| 149 | } |
| 150 | { |
| 151 | OsiColCut c3(c1); |
| 152 | c3.setEffectiveness(5.); |
| 153 | OSIUNITTEST_ASSERT_ERROR(c3 != c1, {}, "osicolcut", "operator !="); |
| 154 | OSIUNITTEST_ASSERT_ERROR(!(c3 == c1), {}, "osicolcut", "operator =="); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // internal consistency |
| 159 | { |
| 160 | const int ne = 1; |
| 161 | int inx[ne] = { -3 }; |
| 162 | double el[ne] = { 1.2 }; |
| 163 | OsiColCut r; |
| 164 | r.setLbs(ne, inx, el); |
| 165 | OSIUNITTEST_ASSERT_ERROR(!r.consistent(), {}, "osicolcut", "consistent"); |
| 166 | } |
| 167 | { |
| 168 | const int ne = 1; |
| 169 | int inx[ne] = { -3 }; |
| 170 | double el[ne] = { 1.2 }; |
| 171 | OsiColCut r; |
| 172 | r.setUbs(ne, inx, el); |
| 173 | OSIUNITTEST_ASSERT_ERROR(!r.consistent(), {}, "osicolcut", "consistent"); |
| 174 | } |
| 175 | { |
| 176 | const int ne = 1; |
| 177 | int inx[ne] = { 100 }; |
| 178 | double el[ne] = { 1.2 }; |
| 179 | const int ne1 = 2; |
| 180 | int inx1[ne1] = { 50, 100 }; |
| 181 | double el1[ne1] = { 100., 100. }; |
| 182 | OsiColCut r; |
| 183 | r.setUbs(ne, inx, el); |
| 184 | r.setLbs(ne1, inx1, el1); |
| 185 | OSIUNITTEST_ASSERT_ERROR(r.consistent(), {}, "osicolcut", "consistent"); |
| 186 | |
| 187 | OsiSolverInterface *imP = baseSiP->clone(); |
| 188 | assert(imP != NULL); |
| 189 | std::string fn = mpsDir + "exmip1"; |
| 190 | imP->readMps(fn.c_str(), "mps"); |
| 191 | OSIUNITTEST_ASSERT_ERROR(!r.consistent(*imP), {}, "osicolcut", "consistent"); |
| 192 | delete imP; |
| 193 | } |
| 194 | { |
| 195 | const int ne = 1; |
| 196 | int inx[ne] = { 100 }; |
| 197 | double el[ne] = { 1.2 }; |
| 198 | const int ne1 = 2; |
| 199 | int inx1[ne1] = { 50, 100 }; |
| 200 | double el1[ne1] = { 100., 1. }; |
| 201 | OsiColCut r; |
| 202 | r.setUbs(ne, inx, el); |
| 203 | r.setLbs(ne1, inx1, el1); |
| 204 | OSIUNITTEST_ASSERT_ERROR(r.consistent(), {}, "osicolcut", "consistent"); |
| 205 | } |
| 206 | { |
| 207 | // Test consistent(IntegerModel) method. |
| 208 | OsiSolverInterface *imP = baseSiP->clone(); |
| 209 | assert(imP != NULL); |
| 210 | std::string fn = mpsDir + "exmip1"; |
| 211 | imP->readMps(fn.c_str(), "mps"); |
| 212 | |
| 213 | OsiColCut cut; |
| 214 | const int ne = 1; |
| 215 | int inx[ne] = { 20 }; |
| 216 | double el[ne] = { 0.25 }; |
| 217 | cut.setLbs(ne, inx, el); |
| 218 | OSIUNITTEST_ASSERT_ERROR(!cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 219 | |
| 220 | cut.setLbs(0, NULL, NULL); |
| 221 | cut.setUbs(ne, inx, el); |
| 222 | OSIUNITTEST_ASSERT_ERROR(!cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 223 | |
| 224 | inx[0] = 4; |
| 225 | cut.setLbs(ne, inx, el); |
| 226 | cut.setUbs(0, NULL, NULL); |
| 227 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 228 | |
| 229 | el[0] = 4.5; |
| 230 | cut.setLbs(0, NULL, NULL); |
| 231 | cut.setUbs(ne, inx, el); |
| 232 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 233 | |
| 234 | cut.setLbs(ne, inx, el); |
| 235 | cut.setUbs(0, NULL, NULL); |
| 236 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 237 | OSIUNITTEST_ASSERT_ERROR(cut.infeasible(*imP), {}, "osicolcut", "infeasible(IntegerModel)"); |
| 238 | |
| 239 | el[0] = 3.0; |
| 240 | cut.setLbs(ne, inx, el); |
| 241 | cut.setUbs(ne, inx, el); |
| 242 | OSIUNITTEST_ASSERT_ERROR(cut.consistent(*imP), {}, "osicolcut", "consistent(IntegerModel)"); |
| 243 | delete imP; |
| 244 | } |
| 245 | { |
| 246 | //Test infeasible(im) method |
| 247 | // Test consistent(IntegerModel) method. |
| 248 | OsiSolverInterface *imP = baseSiP->clone(); |
| 249 | assert(imP != NULL); |
| 250 | std::string fn = mpsDir + "exmip1"; |
| 251 | imP->readMps(fn.c_str(), "mps"); |
| 252 | |
| 253 | OsiColCut cut; |
| 254 | const int ne = 1; |
| 255 | int inx[ne] = { 4 }; |
| 256 | double el[ne] = { 4.5 }; |
| 257 | cut.setLbs(ne, inx, el); |
| 258 | OSIUNITTEST_ASSERT_ERROR(cut.infeasible(*imP), {}, "osicolcut", "infeasible(IntegerModel)"); |
| 259 | |
| 260 | el[0] = 0.25; |
| 261 | cut.setLbs(0, NULL, NULL); |
| 262 | cut.setUbs(ne, inx, el); |
| 263 | OSIUNITTEST_ASSERT_ERROR(cut.infeasible(*imP), {}, "osicolcut", "infeasible(IntegerModel)"); |
| 264 | |
| 265 | el[0] = 3.0; |
| 266 | cut.setLbs(ne, inx, el); |
| 267 | cut.setUbs(ne, inx, el); |
| 268 | OSIUNITTEST_ASSERT_ERROR(!cut.infeasible(*imP), {}, "osicolcut", "infeasible(IntegerModel)"); |
| 269 | |
| 270 | delete imP; |
| 271 | } |
| 272 | { |
| 273 | //Test violation |
| 274 | |
| 275 | double solution[] = { 1.0 }; |
| 276 | OsiColCut cut; |
| 277 | const int ne = 1; |
| 278 | int inx[ne] = { 0 }; |
| 279 | double el[ne] = { 4.5 }; |
| 280 | cut.setLbs(ne, inx, el); |
| 281 | OSIUNITTEST_ASSERT_ERROR(cut.violated(solution), {}, "osicolcut", "violated"); |
| 282 | |
| 283 | el[0] = 0.25; |
| 284 | cut.setLbs(0, NULL, NULL); |
| 285 | cut.setUbs(ne, inx, el); |
| 286 | OSIUNITTEST_ASSERT_ERROR(cut.violated(solution), {}, "osicolcut", "violated"); |
| 287 | |
| 288 | el[0] = 1.0; |
| 289 | cut.setLbs(ne, inx, el); |
| 290 | cut.setUbs(ne, inx, el); |
| 291 | OSIUNITTEST_ASSERT_ERROR(!cut.violated(solution), {}, "osicolcut", "violated"); |
| 292 | } |
| 293 | } |
| 294 | |
| 295 | /* vi: softtabstop=2 shiftwidth=2 expandtab tabstop=2 |
| 296 | */ |