| 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.mathprog.lp.model; |
| 23 | |
|
| 24 | |
import junit.framework.TestCase; |
| 25 | |
import net.sourceforge.combean.interfaces.mathprog.linalg.VectorOrientation; |
| 26 | |
import net.sourceforge.combean.interfaces.mathprog.lp.LPConstraint; |
| 27 | |
import net.sourceforge.combean.interfaces.mathprog.lp.LPSolver; |
| 28 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPConstraintSequence; |
| 29 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelColumns; |
| 30 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelComponent; |
| 31 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelMatrix; |
| 32 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelRows; |
| 33 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPModelSolver; |
| 34 | |
import net.sourceforge.combean.interfaces.mathprog.lp.model.LPVariableSequence; |
| 35 | |
import net.sourceforge.combean.mathprog.linalg.DoubleMatrix; |
| 36 | |
import net.sourceforge.combean.mathprog.lp.model.ConcatenatedLPModelColumns; |
| 37 | |
import net.sourceforge.combean.mathprog.lp.model.ConcatenatedLPModelRows; |
| 38 | |
import net.sourceforge.combean.mathprog.lp.model.ConstructableLPModel; |
| 39 | |
import net.sourceforge.combean.mathprog.lp.model.DoubleLPConstraintSequence; |
| 40 | |
import net.sourceforge.combean.mathprog.lp.model.DoubleLPVariableSequence; |
| 41 | |
import net.sourceforge.combean.mathprog.lp.model.MatrixAsLPModelMatrix; |
| 42 | |
import net.sourceforge.combean.test.helpers.checks.CheckMathProg; |
| 43 | |
|
| 44 | |
import org.apache.commons.logging.Log; |
| 45 | |
import org.apache.commons.logging.LogFactory; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
|
| 51 | |
public abstract class AbstractTestLPModelSolver extends TestCase { |
| 52 | |
|
| 53 | 3 | private static final Log log = |
| 54 | |
LogFactory.getLog(AbstractTestLPModelSolver.class); |
| 55 | |
|
| 56 | 18 | private ConstructableLPModel lpModel = null; |
| 57 | 18 | private LPSolver lpSolver = null; |
| 58 | 18 | private LPModelSolver modelSolver = null; |
| 59 | |
|
| 60 | 3 | private static final double[] simpleCoeffs = {4, 10}; |
| 61 | 3 | private static final double[] simpleRhs = { |
| 62 | |
2, |
| 63 | |
2, |
| 64 | |
10 |
| 65 | |
}; |
| 66 | |
|
| 67 | 3 | private static final double[][] feasibleMatrix = { |
| 68 | |
{1, 0}, |
| 69 | |
{0, 1}, |
| 70 | |
{2, 5} |
| 71 | |
}; |
| 72 | |
|
| 73 | 3 | private static final double[] halvedRhs = { |
| 74 | |
1, |
| 75 | |
1, |
| 76 | |
5 |
| 77 | |
}; |
| 78 | |
|
| 79 | 3 | private static final double[] tripleCoeffs = {12, 30}; |
| 80 | |
|
| 81 | |
private static final double simpleSolution = 20; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
public AbstractTestLPModelSolver(String name) { |
| 88 | 18 | super(name); |
| 89 | 18 | } |
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
protected void setUp() throws Exception { |
| 95 | 18 | super.setUp(); |
| 96 | |
|
| 97 | 18 | this.lpModel = new ConstructableLPModel(); |
| 98 | |
|
| 99 | 18 | this.lpSolver = CheckMathProg.constructLPSolver(); |
| 100 | 18 | this.lpSolver.setObjective(LPSolver.LPOBJ_MAX); |
| 101 | |
|
| 102 | 18 | this.modelSolver = createLPModelSolver(); |
| 103 | 18 | this.modelSolver.setLPSolver(this.lpSolver); |
| 104 | 18 | } |
| 105 | |
|
| 106 | |
|
| 107 | |
|
| 108 | |
|
| 109 | |
protected void tearDown() throws Exception { |
| 110 | 18 | super.tearDown(); |
| 111 | 18 | } |
| 112 | |
|
| 113 | |
protected abstract LPModelSolver createLPModelSolver(); |
| 114 | |
|
| 115 | |
public void testColumnwiseConstructionOfFeasibleLPWithOneComponent() { |
| 116 | 3 | if (log.isDebugEnabled()) { |
| 117 | 0 | log.debug("testColumnwiseConstructionOfFeasibleLPWithOneComponent"); |
| 118 | |
} |
| 119 | |
|
| 120 | 3 | LPModelComponent component = |
| 121 | |
constructSimpleModel(VectorOrientation.ROWWISE); |
| 122 | 3 | this.lpModel.appendModelComponent(component); |
| 123 | |
|
| 124 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 125 | |
|
| 126 | 3 | this.modelSolver.solve(); |
| 127 | |
|
| 128 | 3 | if (log.isDebugEnabled()) { |
| 129 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 130 | |
} |
| 131 | |
|
| 132 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length); |
| 133 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length); |
| 134 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 135 | |
this.lpSolver.getSolutionStatus()); |
| 136 | 3 | assertEquals(simpleSolution, this.lpSolver.getSolutionValue(), 0.0); |
| 137 | 3 | } |
| 138 | |
|
| 139 | |
public void testRowwiseConstructionOfFeasibleLPWithOneComponent() { |
| 140 | 3 | if (log.isDebugEnabled()) { |
| 141 | 0 | log.debug("testRowwiseConstructionOfFeasibleLPWithOneComponent"); |
| 142 | |
} |
| 143 | |
|
| 144 | 3 | LPModelComponent component = |
| 145 | |
constructSimpleModel(VectorOrientation.ROWWISE); |
| 146 | 3 | this.lpModel.appendModelComponent(component); |
| 147 | |
|
| 148 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 149 | |
|
| 150 | 3 | this.modelSolver.solve(); |
| 151 | |
|
| 152 | 3 | if (log.isDebugEnabled()) { |
| 153 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 154 | |
} |
| 155 | |
|
| 156 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length); |
| 157 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length); |
| 158 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 159 | |
this.lpSolver.getSolutionStatus()); |
| 160 | 3 | assertEquals(simpleSolution, this.lpSolver.getSolutionValue(), 0.0); |
| 161 | 3 | } |
| 162 | |
|
| 163 | |
public void testTwoStackedRowwiseFeasibleMatrices() { |
| 164 | 3 | if (log.isDebugEnabled()) { |
| 165 | 0 | log.debug("testTwoStackedRowwiseFeasibleMatrices"); |
| 166 | |
} |
| 167 | |
|
| 168 | 3 | LPModelComponent component = |
| 169 | |
constructSimpleModel(VectorOrientation.ROWWISE); |
| 170 | 3 | this.lpModel.appendModelComponent(component); |
| 171 | 3 | component = |
| 172 | |
constructRowsWithHalvedRhs(VectorOrientation.ROWWISE); |
| 173 | 3 | this.lpModel.appendModelComponent(component); |
| 174 | |
|
| 175 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 176 | |
|
| 177 | 3 | this.modelSolver.solve(); |
| 178 | |
|
| 179 | 3 | if (log.isDebugEnabled()) { |
| 180 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 181 | |
} |
| 182 | |
|
| 183 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length*2); |
| 184 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length); |
| 185 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 186 | |
this.lpSolver.getSolutionStatus()); |
| 187 | 3 | assertEquals(simpleSolution/2, this.lpSolver.getSolutionValue(), 0.0); |
| 188 | 3 | } |
| 189 | |
|
| 190 | |
public void testTwoStackedColumnwiseFeasibleMatrices() { |
| 191 | 3 | if (log.isDebugEnabled()) { |
| 192 | 0 | log.debug("testTwoStackedColumnwiseFeasibleMatrices"); |
| 193 | |
} |
| 194 | |
|
| 195 | 3 | ConcatenatedLPModelColumns concatenatedCols = |
| 196 | |
new ConcatenatedLPModelColumns(); |
| 197 | |
|
| 198 | 3 | LPModelComponent component = |
| 199 | |
constructSimpleModel(VectorOrientation.COLUMNWISE); |
| 200 | 3 | concatenatedCols.concatenateColumns((LPModelColumns) component); |
| 201 | 3 | component = constructRowsWithHalvedRhs(VectorOrientation.COLUMNWISE); |
| 202 | 3 | concatenatedCols.concatenateColumns((LPModelColumns) component); |
| 203 | |
|
| 204 | 3 | this.lpModel.appendModelComponent(concatenatedCols); |
| 205 | |
|
| 206 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 207 | |
|
| 208 | 3 | this.modelSolver.solve(); |
| 209 | |
|
| 210 | 3 | if (log.isDebugEnabled()) { |
| 211 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 212 | |
} |
| 213 | |
|
| 214 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length*2); |
| 215 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length); |
| 216 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 217 | |
this.lpSolver.getSolutionStatus()); |
| 218 | 3 | assertEquals(simpleSolution/2, this.lpSolver.getSolutionValue(), 0.0); |
| 219 | 3 | } |
| 220 | |
|
| 221 | |
public void testTwoAdjacentColumnwiseFeasibleMatrices() { |
| 222 | 3 | if (log.isDebugEnabled()) { |
| 223 | 0 | log.debug("testTwoAdjacentColumnwiseFeasibleMatrices"); |
| 224 | |
} |
| 225 | |
|
| 226 | 3 | LPModelComponent component = |
| 227 | |
constructSimpleModel(VectorOrientation.COLUMNWISE); |
| 228 | 3 | this.lpModel.appendModelComponent(component); |
| 229 | 3 | component = constructColumnsWithTripleCoefficient(VectorOrientation.COLUMNWISE); |
| 230 | 3 | this.lpModel.appendModelComponent(component); |
| 231 | |
|
| 232 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 233 | |
|
| 234 | 3 | this.modelSolver.solve(); |
| 235 | |
|
| 236 | 3 | if (log.isDebugEnabled()) { |
| 237 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 238 | |
} |
| 239 | |
|
| 240 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length); |
| 241 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length*2); |
| 242 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 243 | |
this.lpSolver.getSolutionStatus()); |
| 244 | 3 | assertEquals(simpleSolution*3, this.lpSolver.getSolutionValue(), |
| 245 | |
0.000000001); |
| 246 | 3 | } |
| 247 | |
|
| 248 | |
public void testTwoAdjacentRowwiseFeasibleMatrices() { |
| 249 | 3 | if (log.isDebugEnabled()) { |
| 250 | 0 | log.debug("testTwoAdjacentRowwiseFeasibleMatrices"); |
| 251 | |
} |
| 252 | |
|
| 253 | 3 | ConcatenatedLPModelRows concatenatedRows = |
| 254 | |
new ConcatenatedLPModelRows(); |
| 255 | |
|
| 256 | 3 | LPModelComponent component = |
| 257 | |
constructSimpleModel(VectorOrientation.ROWWISE); |
| 258 | 3 | concatenatedRows.concatenateRows((LPModelRows) component); |
| 259 | 3 | component = constructColumnsWithTripleCoefficient(VectorOrientation.ROWWISE); |
| 260 | 3 | concatenatedRows.concatenateRows((LPModelRows) component); |
| 261 | |
|
| 262 | 3 | this.lpModel.appendModelComponent(concatenatedRows); |
| 263 | |
|
| 264 | 3 | this.modelSolver.loadModel(this.lpModel); |
| 265 | |
|
| 266 | 3 | this.modelSolver.solve(); |
| 267 | |
|
| 268 | 3 | if (log.isDebugEnabled()) { |
| 269 | 0 | log.debug("solution: " + this.lpSolver.getSolution()); |
| 270 | |
} |
| 271 | |
|
| 272 | 3 | assertEquals(this.lpSolver.getNumRows(), feasibleMatrix.length); |
| 273 | 3 | assertEquals(this.lpSolver.getNumColumns(), feasibleMatrix[0].length*2); |
| 274 | 3 | assertEquals(LPSolver.LPSTAT_SOLVED, |
| 275 | |
this.lpSolver.getSolutionStatus()); |
| 276 | 3 | assertEquals(simpleSolution*3, this.lpSolver.getSolutionValue(), 0.00000001); |
| 277 | 3 | } |
| 278 | |
|
| 279 | |
private LPModelComponent constructSimpleModel(VectorOrientation orientation) { |
| 280 | 18 | LPConstraintSequence constraints = |
| 281 | |
new DoubleLPConstraintSequence("constr", |
| 282 | |
LPConstraint.REL_LESS, simpleRhs); |
| 283 | 18 | this.lpModel.addConstraintSequence(constraints); |
| 284 | |
|
| 285 | 18 | DoubleLPVariableSequence variables = |
| 286 | |
new DoubleLPVariableSequence("var", simpleCoeffs, |
| 287 | |
0, Double.POSITIVE_INFINITY); |
| 288 | 18 | this.lpModel.addVariableSequence(variables); |
| 289 | |
|
| 290 | 18 | LPModelMatrix model = |
| 291 | |
new MatrixAsLPModelMatrix(orientation, "constr", "var", |
| 292 | |
new DoubleMatrix(feasibleMatrix)); |
| 293 | |
|
| 294 | 18 | return model; |
| 295 | |
} |
| 296 | |
|
| 297 | |
private LPModelComponent constructRowsWithHalvedRhs(VectorOrientation orientation) { |
| 298 | 6 | LPConstraintSequence constraints = |
| 299 | |
new DoubleLPConstraintSequence("halved", |
| 300 | |
LPConstraint.REL_LESS, halvedRhs); |
| 301 | 6 | this.lpModel.addConstraintSequence(constraints); |
| 302 | |
|
| 303 | 6 | MatrixAsLPModelMatrix model = |
| 304 | |
new MatrixAsLPModelMatrix(orientation, "halved", "var", |
| 305 | |
new DoubleMatrix(feasibleMatrix)); |
| 306 | |
|
| 307 | 6 | return model; |
| 308 | |
} |
| 309 | |
|
| 310 | |
private LPModelComponent constructColumnsWithTripleCoefficient( |
| 311 | |
VectorOrientation orientation) { |
| 312 | 6 | LPVariableSequence variables = |
| 313 | |
new DoubleLPVariableSequence("tripled", tripleCoeffs, |
| 314 | |
0, Double.POSITIVE_INFINITY); |
| 315 | 6 | this.lpModel.addVariableSequence(variables); |
| 316 | |
|
| 317 | 6 | MatrixAsLPModelMatrix model = |
| 318 | |
new MatrixAsLPModelMatrix(orientation, "constr", "tripled", |
| 319 | |
new DoubleMatrix(feasibleMatrix)); |
| 320 | 6 | model.setOrientation(VectorOrientation.COLUMNWISE); |
| 321 | |
|
| 322 | 6 | return model; |
| 323 | |
} |
| 324 | |
} |