1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
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 | |
|
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 | |
|
66 | |
|
67 | |
protected void setUp() throws Exception { |
68 | 3 | super.setUp(); |
69 | |
|
70 | |
|
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 | |
|
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 | |
|
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 | |
|
108 | |
|
109 | 3 | this.lpSolver = CheckMathProg.constructLPSolver(); |
110 | 3 | this.lpSolver.setObjective(LPSolver.LPOBJ_MAX); |
111 | |
|
112 | |
|
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 | |
|
121 | |
|
122 | |
protected void tearDown() throws Exception { |
123 | 3 | super.tearDown(); |
124 | 3 | } |
125 | |
|
126 | |
|
127 | |
|
128 | |
|
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 | |
|
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 | |
} |