Coverage Report - net.sourceforge.combean.test.mathprog.grooml.TestGLPSolver
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGLPSolver
100%
14/14
67%
8/12
0
TestGLPSolver$_solveAndCheck_closure4
100%
1/1
83%
5/6
0
TestGLPSolver$_testMultipleVariables_closure3
100%
4/4
50%
3/6
0
TestGLPSolver$_testMultipleVariables_closure3_closure11
100%
1/1
N/A
0
TestGLPSolver$_testMultipleVariables_closure3_closure12
100%
1/1
N/A
0
TestGLPSolver$_testMultipleVariables_closure3_closure13
100%
2/2
83%
5/6
0
TestGLPSolver$_testSimpleEquationWithListIndex_closure2
100%
3/3
50%
2/4
0
TestGLPSolver$_testSimpleEquationWithListIndex_closure2_closure8
100%
1/1
100%
2/2
0
TestGLPSolver$_testSimpleEquationWithListIndex_closure2_closure9
100%
1/1
100%
4/4
0
TestGLPSolver$_testSimpleEquationWithListIndex_closure2_closure9_closure10
100%
1/1
N/A
0
TestGLPSolver$_testSimpleEquation_closure1
100%
2/2
50%
2/4
0
TestGLPSolver$_testSimpleEquation_closure1_closure5
100%
1/1
100%
2/2
0
TestGLPSolver$_testSimpleEquation_closure1_closure6
100%
2/2
100%
4/4
0
TestGLPSolver$_testSimpleEquation_closure1_closure6_closure7
100%
1/1
N/A
0
 
 1  
 /*
 2  
     This file is part of Combean.
 3  
 
 4  
     Combean is free software; you can redistribute it and/or modify
 5  
     it under the terms of the GNU General Public License as published by
 6  
     the Free Software Foundation; either version 2 of the License, or
 7  
     (at your option) any later version.
 8  
 
 9  
     Combean is distributed in the hope that it will be useful,
 10  
     but WITHOUT ANY WARRANTY; without even the implied warranty of
 11  
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 12  
     GNU General Public License for more details.
 13  
 
 14  
     You should have received a copy of the GNU General Public License
 15  
     along with Foobar; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 package net.sourceforge.combean.test.mathprog.grooml;
 19  
 
 20  
 
 21  
 import net.sourceforge.combean.interfaces.mathprog.linalg.VectorOrientation;
 22  
 import net.sourceforge.combean.mathprog.lp.model.LPModelIndexImpl;
 23  
 import net.sourceforge.combean.mathprog.grooml.GExpression;
 24  
 import net.sourceforge.combean.mathprog.grooml.GroomlInterpreter;
 25  
 import net.sourceforge.combean.mathprog.grooml.GLPSolver;
 26  
 
 27  
 
 28  
 class TestGLPSolver extends GroovyTestCase {
 29  
     
 30  
     private GroomlInterpreter builder;
 31  
     private GLPSolver solver;
 32  
     
 33  
     void setUp() {
 34  9
         this.solver = new GLPSolver();
 35  9
         this.builder = new GroomlInterpreter();
 36  
     }
 37  
 
 38  
         void testSimpleEquation() {
 39  3
             def coeff = [1:-2.0, 2:2.0];
 40  3
             builder.load{
 41  3
                 vars("x", 1..2) { [coeff[it], -1..1] };
 42  3
                 row("foo") {
 43  9
                     sum(i:1..2) { "x[$i]" } | 1.0
 44  
                 }
 45  
             }
 46  3
             solveAndCheck(-2.0, ["x[1]":1.0, "x[2]":0.0], 0.001);
 47  
         }
 48  
 
 49  
         void testSimpleEquationWithListIndex() {
 50  3
             def idxSet = ["a", "b"];
 51  3
             builder.load{
 52  3
                     def coeff = [a:-2.0, b:2.0];
 53  3
                 vars("x", [it:idxSet]) { [coeff[it], -1..1] };
 54  3
                 row("foo") {
 55  9
                     sum(i:idxSet) { "x[$i]" } | 1.0
 56  
                 }
 57  
             }
 58  3
             solveAndCheck(-2.0, ["x[a]":1.0, "x[b]":0.0], 0.001);
 59  
         }
 60  
 
 61  
         void testMultipleVariables() {
 62  3
             builder.load{
 63  3
                 var("x") { [-2, -1..1] };
 64  3
                 var("y") { [2, -1..1] };
 65  3
                 row("foo") {
 66  9
                     expr("x[]") + "y[]" | 1.0
 67  
                 }
 68  
             }
 69  3
             solveAndCheck(-2.0, ["x[]":1.0, "y[]":0.0], 0.001);
 70  
         }
 71  
         
 72  
         private void solveAndCheck(double expectedSolution, Map varIdToExpectedVal,
 73  
                 double precision) {
 74  
             
 75  9
             this.solver.loadModel(this.builder.getModel());
 76  9
             this.solver.solve();
 77  
 
 78  9
             assertEquals(expectedSolution, solver.LPSolver.getSolutionValue(),
 79  
                     precision);
 80  9
             varIdToExpectedVal.keySet().each {
 81  18
                 assertEquals(varIdToExpectedVal[it], solver.getSolution(it),
 82  
                         precision);
 83  
             }
 84  
         }
 85  
 }