Coverage Report - net.sourceforge.combean.test.graph.alg.lp.TestGraphAsLPModel
 
Classes in this File Line Coverage Branch Coverage Complexity
TestGraphAsLPModel
95%
41/43
N/A
1
 
 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 Combean; if not, write to the Free Software
 16  
     Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 17  
 */
 18  
 /*
 19  
  * Created on 07.08.2005
 20  
  *
 21  
  */
 22  
 package net.sourceforge.combean.test.graph.alg.lp;
 23  
 
 24  
 import junit.framework.TestCase;
 25  
 import net.sourceforge.combean.graph.alg.lp.EdgesAsLPVariableSequence;
 26  
 import net.sourceforge.combean.graph.alg.lp.GraphAsLPModelColumns;
 27  
 import net.sourceforge.combean.graph.alg.lp.NodesAsLPConstraintSequence;
 28  
 import net.sourceforge.combean.graph.containers.doubleval.EdgeMapAsDoubleEdgeMap;
 29  
 import net.sourceforge.combean.graph.containers.doubleval.NodeMapAsDoubleNodeMap;
 30  
 import net.sourceforge.combean.graph.prop.statics.ConstructableNumberedGraphBuilder;
 31  
 import net.sourceforge.combean.interfaces.graph.containers.doubleval.DoubleEdgeMap;
 32  
 import net.sourceforge.combean.interfaces.graph.containers.doubleval.DoubleNodeMap;
 33  
 import net.sourceforge.combean.interfaces.graph.prop.ConstructableNumberedGraphProp;
 34  
 import net.sourceforge.combean.interfaces.mathprog.lp.LPConstraint;
 35  
 import net.sourceforge.combean.interfaces.mathprog.lp.LPSolver;
 36  
 import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelSolver;
 37  
 import net.sourceforge.combean.mathprog.lp.model.ConstructableLPModel;
 38  
 import net.sourceforge.combean.mathprog.lp.model.LPModelSolverWithSequentialLoading;
 39  
 import net.sourceforge.combean.test.helpers.checks.CheckMathProg;
 40  
 import net.sourceforge.combean.test.helpers.factories.GraphFixtureFactory;
 41  
 
 42  
 /**
 43  
  * @author schickin
 44  
  *
 45  
  */
 46  
 public class TestGraphAsLPModel extends TestCase {
 47  
     
 48  3
     private ConstructableNumberedGraphProp constructG = null;
 49  
     
 50  3
     private ConstructableLPModel lpModel = null;
 51  
     
 52  3
     private static final double[] pathEdgeWeights = {1.0, 1.0, 1.0, 1.0, 1.0};
 53  
     private static final double sumFlowOnEdges = 5.0;
 54  3
     private static final double[] nodeFlowBalance = {-1.0, 0.0, 0.0, 0.0, 0.0, 1.0};
 55  
     
 56  3
     private LPSolver lpSolver = null;
 57  
     
 58  3
     private LPModelSolver modelSolver = null;
 59  
     
 60  
     public static void main(String[] args) {
 61  0
         junit.textui.TestRunner.run(TestGraphAsLPModel.class);
 62  0
     }
 63  
 
 64  
     /*
 65  
      * @see TestCase#setUp()
 66  
      */
 67  
     protected void setUp() throws Exception {
 68  3
         super.setUp();
 69  
         
 70  
         // build graph
 71  
         
 72  3
         this.constructG = GraphFixtureFactory.createConstructableNumberedGraph();
 73  
         
 74  3
         ConstructableNumberedGraphBuilder builder =
 75  
             new ConstructableNumberedGraphBuilder(this.constructG);
 76  
         
 77  3
         builder.addNodes(nodeFlowBalance.length);
 78  3
         builder.addPathEdges(0, nodeFlowBalance.length-1);
 79  
         
 80  
         // build edge and node maps
 81  
         
 82  3
         DoubleEdgeMap edgeWeightMap = new EdgeMapAsDoubleEdgeMap();
 83  3
         builder.fillDoubleEdgeMap(edgeWeightMap, pathEdgeWeights);
 84  
         
 85  3
         DoubleNodeMap nodeFlowBalanceMap = new NodeMapAsDoubleNodeMap();
 86  3
         builder.fillDoubleNodeMap(nodeFlowBalanceMap, nodeFlowBalance);
 87  
 
 88  
         // build LPModel
 89  
         
 90  3
         this.lpModel = new ConstructableLPModel();
 91  
         
 92  3
         NodesAsLPConstraintSequence flowBalanceConstraints =
 93  
             new NodesAsLPConstraintSequence("bal", this.constructG);
 94  3
         flowBalanceConstraints.setNodeRhs(nodeFlowBalanceMap);
 95  3
         flowBalanceConstraints.setRelation(LPConstraint.REL_LESS);
 96  3
         this.lpModel.addConstraintSequence(flowBalanceConstraints);
 97  
         
 98  3
         EdgesAsLPVariableSequence edgeFlowVariables =
 99  
             new EdgesAsLPVariableSequence("flow", this.constructG);
 100  3
         edgeFlowVariables.setEdgeCost(edgeWeightMap);
 101  3
         this.lpModel.addVariableSequence(edgeFlowVariables);
 102  
 
 103  3
         GraphAsLPModelColumns graphAsLP =
 104  
             new GraphAsLPModelColumns("flow", "bal", this.constructG);
 105  3
         this.lpModel.appendModelComponent(graphAsLP);
 106  
         
 107  
         // build LPSolver
 108  
         
 109  3
         this.lpSolver = CheckMathProg.constructLPSolver();
 110  3
         this.lpSolver.setObjective(LPSolver.LPOBJ_MAX);
 111  
         
 112  
         // load model into solver
 113  
         
 114  3
         this.modelSolver = new LPModelSolverWithSequentialLoading();
 115  3
         this.modelSolver.setLPSolver(this.lpSolver);
 116  3
         this.modelSolver.loadModel(this.lpModel);
 117  3
     }
 118  
 
 119  
     /*
 120  
      * @see TestCase#tearDown()
 121  
      */
 122  
     protected void tearDown() throws Exception {
 123  3
         super.tearDown();
 124  3
     }
 125  
 
 126  
     /**
 127  
      * Constructor for TestGraphAsLPModel.
 128  
      * @param name
 129  
      */
 130  
     public TestGraphAsLPModel(String name) {
 131  3
         super(name);
 132  3
     }
 133  
     
 134  
     public final void testSolveLoadedLP() {
 135  3
         assertEquals(pathEdgeWeights.length,
 136  
                 this.modelSolver.getLPSolver().getNumColumns());
 137  3
         assertEquals(nodeFlowBalance.length,
 138  
                 this.modelSolver.getLPSolver().getNumRows());
 139  
 
 140  
         // this test case did not work with the DRA-Systems and the QSOpt-Solvers
 141  3
         this.modelSolver.solve();
 142  
         
 143  3
         assertEquals(LPSolver.LPSTAT_SOLVED,
 144  
                 this.modelSolver.getLPSolver().getSolutionStatus());
 145  3
         assertEquals(sumFlowOnEdges,
 146  
                 this.modelSolver.getLPSolver().getSolutionValue(), 0.0001);
 147  3
     }
 148  
 }