| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
package net.sourceforge.combean.test.mathprog.grooml; |
| 19 | |
|
| 20 | |
|
| 21 | |
import groovy.util.GroovyTestCase |
| 22 | |
|
| 23 | |
import net.sourceforge.combean.except.GModelException; |
| 24 | |
import net.sourceforge.combean.mathprog.grooml.GroomlInterpreter; |
| 25 | |
import net.sourceforge.combean.mathprog.grooml.GSet; |
| 26 | |
import net.sourceforge.combean.mathprog.grooml.GExpression; |
| 27 | |
import net.sourceforge.combean.mathprog.grooml.GDynamicExpression; |
| 28 | |
import net.sourceforge.combean.mathprog.grooml.GRows; |
| 29 | |
import net.sourceforge.combean.mathprog.grooml.GVariables; |
| 30 | |
import net.sourceforge.combean.mathprog.grooml.GListSet; |
| 31 | |
|
| 32 | |
|
| 33 | |
class TestGroomlInterpreter extends GroovyTestCase { |
| 34 | |
|
| 35 | |
private GroomlInterpreter builder; |
| 36 | |
|
| 37 | |
void setUp() { |
| 38 | 87 | this.builder = new GroomlInterpreter(); |
| 39 | |
} |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
void testSetCommandOnRange() { |
| 44 | 3 | GSet s = builder.set(1..2); |
| 45 | 3 | auxCheckElemsInSet([1, 2], s); |
| 46 | |
} |
| 47 | |
|
| 48 | |
void testSetProductOperatorOnRange() { |
| 49 | 3 | GSet s = builder.set{ (1..2) * ["a", "b"] }; |
| 50 | 3 | auxCheckElemsInSet([[1,"a"], [1,"b"], [2,"a"], [2,"b"]], s); |
| 51 | |
} |
| 52 | |
|
| 53 | |
void testSetProductOperatorOnList() { |
| 54 | 3 | GSet s = builder.set{ ["a", "b"] * (1..2) }; |
| 55 | 3 | auxCheckElemsInSet([["a",1], ["a",2], ["b",1], ["b",2]], s); |
| 56 | |
} |
| 57 | |
|
| 58 | |
void testSetProductOperatorOnMap() { |
| 59 | 3 | GSet s = builder.set{ [i:1..2] * [j:3..4] }; |
| 60 | 3 | auxCheckElemsInSet([[1,3], [1,4], [2,3], [2,4]], s); |
| 61 | |
|
| 62 | 3 | builder.exec{ s = [i:1..2] * [j:2..3] }; |
| 63 | 3 | auxCheckElemsInSet([[1,2], [1,3], [2,2], [2,3]], s); |
| 64 | |
} |
| 65 | |
|
| 66 | |
void testSetProductWithDynamics() { |
| 67 | 3 | GSet s = builder.set{ bind(i:1..2) * {1..i} }; |
| 68 | 3 | auxCheckElemsInSet([[1,1], [2,1], [2,2]], s); |
| 69 | |
|
| 70 | 3 | s = builder.set{ [i:1..2] * {1..i} }; |
| 71 | 3 | auxCheckElemsInSet([[1,1], [2,1], [2,2]], s); |
| 72 | |
|
| 73 | 3 | builder.exec{ s = [i:1..2] * [j:2..3] * {i..j} }; |
| 74 | 3 | auxCheckElemsInSet([[1,2,1],[1,2,2],[1,3,1],[1,3,2],[1,3,3], |
| 75 | |
[2,2,2],[2,3,2],[2,3,3]], s); |
| 76 | |
} |
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
void testExpr() { |
| 81 | 3 | GExpression e = builder.expr("2 x[1]"); |
| 82 | 3 | assertEquals("2.0 x[1]", e.termsToString([:])); |
| 83 | |
} |
| 84 | |
|
| 85 | |
void testExprOnClosure() { |
| 86 | 3 | GExpression e = builder.expr{ "2 x[$i]" }; |
| 87 | 3 | assertTrue(e instanceof GDynamicExpression); |
| 88 | 3 | builder.i = 1; |
| 89 | 3 | assertEquals("2.0 x[1]", e.termsToString(this.builder)); |
| 90 | |
} |
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
void testPlusOperatorOnExpression() { |
| 95 | 3 | GExpression e1 = builder.expr("1.0 x[1]"); |
| 96 | 3 | GExpression e = builder.expr { |
| 97 | 3 | e1 + { "2.0 x[$i]" } |
| 98 | |
} |
| 99 | 3 | builder.i = 2; |
| 100 | 3 | auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e); |
| 101 | |
} |
| 102 | |
|
| 103 | |
void testPlusOperatorOnStrings() { |
| 104 | 3 | GExpression e = builder.expr { |
| 105 | 3 | "1.0 x[1]" + expr("2.0 x[2]") |
| 106 | |
} |
| 107 | 3 | auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e); |
| 108 | |
} |
| 109 | |
|
| 110 | |
void testPlusOperatorOnClosures() { |
| 111 | 3 | GExpression e = builder.expr { |
| 112 | 3 | expr { "1.0 x[$i]" } + { "2.0 x[$j]" } |
| 113 | |
} |
| 114 | 3 | builder.i = 1; |
| 115 | 3 | builder.j = 2; |
| 116 | 3 | auxCheckTermsInExpression(["1.0 x[1]", "2.0 x[2]"], e); |
| 117 | |
} |
| 118 | |
|
| 119 | |
|
| 120 | |
|
| 121 | |
void testSumWithStringExpression() { |
| 122 | 3 | GExpression expr = builder.sum(i:1..2) { "x[$i]" }; |
| 123 | |
|
| 124 | 3 | String terms = "" |
| 125 | 3 | expr.evaluate([:]).each{ terms += "$it, " }; |
| 126 | 3 | assertEquals("1.0 x[1], 1.0 x[2], ", terms); |
| 127 | |
} |
| 128 | |
|
| 129 | |
void testSumWithTwoIndices() { |
| 130 | 3 | GExpression expr = builder.sum(i:1..2, j:3..4) { "${j-i} x[$i,$j]" }; |
| 131 | |
|
| 132 | 3 | final expectedTerms = ["1.0 x[2,3]", |
| 133 | |
"2.0 x[1,3]", |
| 134 | |
"2.0 x[2,4]", |
| 135 | |
"3.0 x[1,4]"]; |
| 136 | 3 | auxCheckTermsInExpression(expectedTerms, expr); |
| 137 | |
|
| 138 | |
} |
| 139 | |
|
| 140 | |
void testNestedSums() { |
| 141 | 3 | GExpression expr = null; |
| 142 | 3 | builder.exec{ |
| 143 | 3 | expr = sum(i:1..2, sum(j:3..4) { "${j-i} x[$i,$j]" }) |
| 144 | |
} |
| 145 | |
|
| 146 | 3 | final expectedTerms = ["1.0 x[2,3]", |
| 147 | |
"2.0 x[1,3]", |
| 148 | |
"2.0 x[2,4]", |
| 149 | |
"3.0 x[1,4]"]; |
| 150 | 3 | auxCheckTermsInExpression(expectedTerms, expr); |
| 151 | |
} |
| 152 | |
|
| 153 | |
void testSumWithDoubleIndex() { |
| 154 | 3 | GExpression expr = null; |
| 155 | |
|
| 156 | 3 | builder.exec{ |
| 157 | 3 | expr = sum(["i", "j"]:(1..2)*(3..4)) { "${j-i} x[$i,$j]" } |
| 158 | |
} |
| 159 | |
|
| 160 | 3 | final expectedTerms = ["1.0 x[2,3]", |
| 161 | |
"2.0 x[1,3]", |
| 162 | |
"2.0 x[2,4]", |
| 163 | |
"3.0 x[1,4]"]; |
| 164 | 3 | auxCheckTermsInExpression(expectedTerms, expr); |
| 165 | |
} |
| 166 | |
|
| 167 | |
|
| 168 | |
|
| 169 | |
void testSingleRow() { |
| 170 | 3 | builder.load{ |
| 171 | 3 | vars("x", 10..12) { 1.0 }; |
| 172 | |
} |
| 173 | 3 | GRows r = builder.row("foo") { |
| 174 | 12 | sum(i:[10,12]) { "$i x[$i]" } | 10.0 |
| 175 | |
} |
| 176 | 3 | println r.toString(); |
| 177 | 3 | assertEquals("rows 'foo' : [row { numIter 2 expr "+ |
| 178 | 3 | "{ 10.0 x[10] + 12.0 x[12] } = 10.0 'foo[0]', ]", r.toString()); |
| 179 | |
} |
| 180 | |
|
| 181 | |
|
| 182 | |
|
| 183 | |
void testCreationOfVariables() { |
| 184 | 3 | GVariables vars = builder.vars("bar", 1..2) { [it/2, -it..it] }; |
| 185 | 3 | assertEquals("vars 'bar' : [0.5 | [-1.0; 1.0], " + |
| 186 | 3 | "1.0 | [-2.0; 2.0]]", vars.toString()) |
| 187 | |
} |
| 188 | |
|
| 189 | |
void testCreationOfTermsWithSingleVariablesInEnv() { |
| 190 | 3 | builder.exec{ |
| 191 | 3 | var("x") { 1.0 } |
| 192 | 3 | var("y") { 1.0 } |
| 193 | 3 | GExpression e = x |
| 194 | 3 | auxCheckTermsInExpression(["1.0 x[]"], e) |
| 195 | 3 | e = 3 * x |
| 196 | 3 | auxCheckTermsInExpression(["3.0 x[]"], e); |
| 197 | 3 | e = -x + 2*y |
| 198 | 3 | auxCheckTermsInExpression(["-1.0 x[]", "2.0 y[]"], e); |
| 199 | |
} |
| 200 | |
} |
| 201 | |
|
| 202 | |
void testAccessToVariablesInEnvironmentWithIndexOperator() { |
| 203 | 3 | builder.exec{ |
| 204 | 3 | vars("x", 2..4) { 1.0 } |
| 205 | 3 | vars("y", (1..2)*(3..4)) { 1.0 } |
| 206 | 3 | GExpression e = x[2] |
| 207 | 3 | auxCheckTermsInExpression(["1.0 x[2]"], e) |
| 208 | 3 | e = 4 * x[3] |
| 209 | 3 | auxCheckTermsInExpression(["4.0 x[3]"], e); |
| 210 | 3 | e = -x[4] |
| 211 | 3 | auxCheckTermsInExpression(["-1.0 x[4]"], e); |
| 212 | 3 | e = y[1,3]*2 |
| 213 | 3 | auxCheckTermsInExpression(["2.0 y[1,3]"], e); |
| 214 | 3 | e = x["foo"] |
| 215 | 3 | auxCheckTermsInExpression(["1.0 x[foo]"], e); |
| 216 | 3 | e = x["foo", "bar"] |
| 217 | 3 | auxCheckTermsInExpression(["1.0 x[foo,bar]"], e); |
| 218 | |
} |
| 219 | |
} |
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
void testModelWithSingleRow() { |
| 224 | 3 | builder.load{ |
| 225 | 3 | vars("x", 1..2) { 1.0 }; |
| 226 | 3 | row("foo") { |
| 227 | 3 | sum(i:1..2) { "$i x[$i]" } | 10 |
| 228 | |
} |
| 229 | |
} |
| 230 | 3 | assertEquals(""" |
| 231 | |
vars x[1.0, 1.0] |
| 232 | |
foo: |
| 233 | |
1.0 x[1] + 2.0 x[2] = 10.0 |
| 234 | 3 | """, builder.getModel().toSummaryString()); |
| 235 | |
} |
| 236 | |
|
| 237 | |
void testSingleRowWithStringIndices() { |
| 238 | 3 | builder.load{ |
| 239 | 3 | def idxSet = ["bla", "fasel"]; |
| 240 | 3 | vars("x", [it:idxSet]) { 1.0 }; |
| 241 | 3 | row("foo") { |
| 242 | 3 | sum(i:idxSet) { "2.0 x[$i]" } | 10 |
| 243 | |
} |
| 244 | |
} |
| 245 | 3 | assertEquals(""" |
| 246 | |
vars x[1.0, 1.0] |
| 247 | |
foo: |
| 248 | |
2.0 x[bla] + 2.0 x[fasel] = 10.0 |
| 249 | 3 | """, builder.getModel().toSummaryString()); |
| 250 | |
} |
| 251 | |
|
| 252 | |
void testModelWithDoubleIndexedVariableAndSingleRow() { |
| 253 | 3 | builder.load{ |
| 254 | 3 | vars("x", [i:(1..2)] * [j:(3..4)] ) { i + j }; |
| 255 | 3 | row("foo") { |
| 256 | 3 | sum(i:1..2) { "x[$i,3]" } | 1 |
| 257 | |
} |
| 258 | |
} |
| 259 | 3 | assertEquals(""" |
| 260 | |
vars x[4.0, 5.0, 5.0, 6.0] |
| 261 | |
foo: |
| 262 | |
1.0 x[1,3] + 1.0 x[2,3] = 1.0 |
| 263 | 3 | """, builder.getModel().toSummaryString()); |
| 264 | |
} |
| 265 | |
|
| 266 | |
|
| 267 | |
void testModelWithDoubleIndexedVariableAndMultipleRows() { |
| 268 | 3 | builder.load{ |
| 269 | 3 | vars("x", (1..2)*(3..4)) { 1.0 }; |
| 270 | 3 | rows("foo", [i:1..2]) { |
| 271 | 6 | [ sum([j:3..4]) { "x[$i,$j]" }, "= 1.0" ] |
| 272 | |
} |
| 273 | |
} |
| 274 | 3 | assertEquals(""" |
| 275 | |
vars x[1.0, 1.0, 1.0, 1.0] |
| 276 | |
foo: |
| 277 | |
1.0 x[1,3] + 1.0 x[1,4] = 1.0 |
| 278 | |
1.0 x[2,3] + 1.0 x[2,4] = 1.0 |
| 279 | 3 | """, builder.getModel().toSummaryString()); |
| 280 | |
} |
| 281 | |
|
| 282 | |
void testRowsWithOperatorBasedRowDefinition() { |
| 283 | 3 | builder.load{ |
| 284 | 3 | vars("x", (1..2)*(3..4)) { 1.0 }; |
| 285 | 3 | rows("foo", [i:1..2]) { |
| 286 | 6 | sum([j:3..4]) { "x[$i,$j]" } << 1.0 |
| 287 | |
} |
| 288 | 3 | rows("bar", [i:1..2]) { |
| 289 | 6 | sum([j:3..4]) { "x[$i,$j]" } >> -1 |
| 290 | |
} |
| 291 | 3 | rows("baz", [i:1..2]) { |
| 292 | 6 | sum([j:3..4]) { "x[$i,$j]" } | 1.0 |
| 293 | |
} |
| 294 | |
} |
| 295 | 3 | assertEquals(""" |
| 296 | |
vars x[1.0, 1.0, 1.0, 1.0] |
| 297 | |
foo: |
| 298 | |
1.0 x[1,3] + 1.0 x[1,4] <= 1.0 |
| 299 | |
1.0 x[2,3] + 1.0 x[2,4] <= 1.0 |
| 300 | |
bar: |
| 301 | |
1.0 x[1,3] + 1.0 x[1,4] >= -1.0 |
| 302 | |
1.0 x[2,3] + 1.0 x[2,4] >= -1.0 |
| 303 | |
baz: |
| 304 | |
1.0 x[1,3] + 1.0 x[1,4] = 1.0 |
| 305 | |
1.0 x[2,3] + 1.0 x[2,4] = 1.0 |
| 306 | 3 | """, builder.getModel().toSummaryString()); |
| 307 | |
} |
| 308 | |
|
| 309 | |
private int calculate(Number i) { |
| 310 | 12 | return i+1; |
| 311 | |
} |
| 312 | |
|
| 313 | |
private static final double num = 10.0; |
| 314 | |
|
| 315 | |
void testModelWithAccessToExternalDefinitions() { |
| 316 | 3 | def coeff = [1: 2.0, 2: 4.0]; |
| 317 | 3 | builder.load{ |
| 318 | 6 | vars("x", 1..2) { coeff[it] }; |
| 319 | 3 | row("foo") { |
| 320 | 3 | sum(i:1..2) { "${calculate(coeff[i])} x[$i]" } | num |
| 321 | |
} |
| 322 | |
} |
| 323 | 3 | assertEquals(""" |
| 324 | |
vars x[2.0, 4.0] |
| 325 | |
foo: |
| 326 | |
3.0 x[1] + 5.0 x[2] = 10.0 |
| 327 | 3 | """, builder.getModel().toSummaryString()); |
| 328 | |
} |
| 329 | |
|
| 330 | |
/* Errors during parsing/execution of model */ |
| 331 | |
|
| 332 | |
void testDoubleDefinedVariable() { |
| 333 | 3 | shouldFail (GModelException) { |
| 334 | 3 | builder.load{ |
| 335 | 3 | vars("x", 1..2) { 1.0 }; |
| 336 | 3 | vars("x", 3..4) { 1.0 }; |
| 337 | 0 | row("foo") { "x[1]" | 10 } |
| 338 | |
} |
| 339 | |
} |
| 340 | |
} |
| 341 | |
|
| 342 | |
void testDoubleDefinedRow() { |
| 343 | 3 | shouldFail (GModelException) { |
| 344 | 3 | builder.load{ |
| 345 | 3 | vars("x", 1..2) { 1.0 }; |
| 346 | 3 | row("foo") { "x[1]" | 10 } |
| 347 | 3 | row("foo") { "x[2]" | 10 } |
| 348 | |
} |
| 349 | |
} |
| 350 | |
} |
| 351 | |
|
| 352 | |
void testAccessToNonExistingProperty() { |
| 353 | 3 | shouldFail (GModelException) { |
| 354 | 3 | builder.load{ |
| 355 | 3 | vars("x", 1..2) { 1.0 }; |
| 356 | 3 | row("foo") { |
| 357 | 3 | sum(i:1..2) { "$i x[$j]" } | 10 |
| 358 | |
} |
| 359 | 3 | solve() |
| 360 | |
} |
| 361 | |
} |
| 362 | |
} |
| 363 | |
|
| 364 | |
void testAccessToNonExistingVariableIndex() { |
| 365 | 3 | def msg = shouldFail (GModelException) { |
| 366 | 3 | builder.load{ |
| 367 | 3 | vars("x", 1..2) { 1.0 }; |
| 368 | 3 | row("foo") { |
| 369 | 9 | sum(i:1..3) { "$i x[$i]" } | 10 |
| 370 | |
} |
| 371 | 3 | solve() |
| 372 | |
} |
| 373 | |
} |
| 374 | 3 | assertEquals("Trying to access undefined index '3' of variable 'x'", msg); |
| 375 | |
} |
| 376 | |
|
| 377 | |
/** |
| 378 | |
* Test solving models |
| 379 | |
*/ |
| 380 | |
void testSolveModelAndAccessSolution() { |
| 381 | 3 | builder.load { |
| 382 | 3 | max() |
| 383 | 3 | var("y") { 1.0 } |
| 384 | 3 | vars("x", 1..2) { [it, 0..1] } |
| 385 | 3 | row("foo") { |
| 386 | 9 | sum([i:1..2]) { x[i] } + y << 1 |
| 387 | |
} |
| 388 | |
|
| 389 | |
// run the LP-solver |
| 390 | 3 | solve() |
| 391 | |
|
| 392 | 3 | assertEquals("{1=0.0, 2=1.0}", x().toString()); |
| 393 | 3 | assertEquals(0.0, x(1)); |
| 394 | 3 | assertEquals(1.0, x(2)); |
| 395 | 3 | assertEquals(0.0, y()); |
| 396 | |
} |
| 397 | |
|
| 398 | |
} |
| 399 | |
|
| 400 | |
/* Helper functions */ |
| 401 | |
|
| 402 | |
private void auxCheckTermsInExpression(List expectedTerms, GExpression expr) { |
| 403 | 45 | def terms = expr.evaluate(this.builder).collect{ it.toString() }; |
| 404 | 45 | assertEquals(expectedTerms.size(), terms.size()); |
| 405 | 45 | terms.sort(); |
| 406 | 45 | expectedTerms.sort(); |
| 407 | 45 | assertArrayEquals(expectedTerms.toArray(), terms.toArray()); |
| 408 | |
} |
| 409 | |
|
| 410 | |
private void auxCheckElemsInSet(List expectedElems, GSet set) { |
| 411 | 24 | def elems = set.evaluate(this.builder).collect{ it }; |
| 412 | 24 | assertEquals(expectedElems.size(), elems.size()); |
| 413 | 24 | elems.sort(); |
| 414 | 24 | expectedElems.sort(); |
| 415 | 24 | assertArrayEquals(expectedElems.toArray(), elems.toArray()); |
| 416 | |
} |
| 417 | |
} |