Search in sources :

Example 6 with MatrixStore

use of org.ojalgo.matrix.store.MatrixStore in project ojAlgo by optimatika.

the class ConvexProblems method testP20080819.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation. Slightly different case (I
 * believe). The main reason/difficulty seemed to be that the algorithm would both add and remove
 * constraints in the iteration. Modified the algorithm to only do one thing with each iteration - either
 * add or remove.
 */
@SuppressWarnings("unchecked")
@Test
public void testP20080819() {
    final Factory<PrimitiveMatrix> tmpMtrxFact = PrimitiveMatrix.FACTORY;
    final NumberContext tmpEvalCntxt = StandardType.DECIMAL_032;
    final BasicMatrix[] tmpMatrices = new PrimitiveMatrix[8];
    tmpMatrices[0] = tmpMtrxFact.rows(new double[][] { { 1.0, 1.0, 1.0, 1.0 } });
    tmpMatrices[1] = tmpMtrxFact.rows(new double[][] { { 1.0 } });
    tmpMatrices[2] = tmpMtrxFact.rows(new double[][] { { 15.889978159746546, 7.506345724913546, 0.8416674706550127, 0.435643236753381 }, { 7.506345724913546, 8.325860065234632, 0.4230651628792374, 0.1670802923999648 }, { 0.8416674706550127, 0.4230651628792374, 1.00134099479915, 0.6558469727234849 }, { 0.435643236753381, 0.1670802923999648, 0.6558469727234849, 0.6420451103682865 } });
    tmpMatrices[3] = tmpMtrxFact.rows(new double[][] { { -0.15804736429388952 }, { -0.11226063792731895 }, { -0.10509261785657838 }, { -0.0848686735786316 } });
    tmpMatrices[4] = tmpMtrxFact.rows(new double[][] { { 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0 }, { -0.15804736429388952, -0.11226063792731895, -0.10509261785657838, -0.0848686735786316 }, { -1.0, 0.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0 } });
    tmpMatrices[5] = tmpMtrxFact.rows(new double[][] { { 0.9 }, { 0.8 }, { 0.7 }, { 0.6 }, { 0.0 }, { -0.1 }, { -0.2 }, { -0.3 }, { -0.4 } });
    tmpMatrices[6] = tmpMtrxFact.rows(new double[][] { { 0.1 }, { 0.2 }, { 0.3 }, { 0.4 } });
    tmpMatrices[7] = null;
    final MatrixStore<Double>[] retVal = new MatrixStore[tmpMatrices.length];
    for (int i = 0; i < retVal.length; i++) {
        if (tmpMatrices[i] != null) {
            if (i == 3) {
                retVal[i] = PrimitiveDenseStore.FACTORY.copy(tmpMatrices[i].negate());
            } else {
                retVal[i] = PrimitiveDenseStore.FACTORY.copy(tmpMatrices[i]);
            }
        }
    }
    final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(retVal);
    // final ActiveSetSolver tmpSolver = new ActiveSetSolver(tmpMatrices);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    // Test that the matrices were input in the right order
    // JUnitUtils.assertEquals(tmpSolver.getAE(), tmpMatrices[0].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getBE(), tmpMatrices[1].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getQ(), tmpMatrices[2].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getC(), tmpMatrices[3].negate().toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getAI(), tmpMatrices[4].toPrimitiveStore(),
    // tmpEvalCntxt);
    // JUnitUtils.assertEquals(tmpSolver.getBI(), tmpMatrices[5].toPrimitiveStore(),
    // tmpEvalCntxt);
    final Optimisation.Result tmpResult = tmpSolver.solve();
    TestUtils.assertEquals(tmpMatrices[6], RationalMatrix.FACTORY.columns(tmpResult), tmpEvalCntxt);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpBuilder, null);
}
Also used : MatrixStore(org.ojalgo.matrix.store.MatrixStore) Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) NumberContext(org.ojalgo.type.context.NumberContext) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 7 with MatrixStore

use of org.ojalgo.matrix.store.MatrixStore in project ojAlgo by optimatika.

the class ConvexProblems method testP20090202.

/**
 * "I just tested ojalgo v.26 and experienced nullpointer-exceptions when I tried to optimize any QP
 * without equality-constraints." This test case is the same as (same numbers)
 * {@linkplain #testP20091102a()} but with the equality constraints removed.
 */
@SuppressWarnings("unchecked")
@Test
public void testP20090202() {
    final MatrixStore<Double>[] tmpMtrxs = new MatrixStore[6];
    tmpMtrxs[0] = null;
    tmpMtrxs[1] = null;
    tmpMtrxs[2] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 3.400491304172128, 5.429710780966787, 5.910932781021423 }, { 5.429710780966787, 23.181215288234903, 27.883770791602895 }, { 5.910932781021423, 27.883770791602895, 34.37266787775051 } });
    tmpMtrxs[3] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.053 }, { 0.0755 }, { 0.0788 } });
    tmpMtrxs[4] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0 }, { 0.0, 0.0, 1.0 }, { -0.053, -0.0755, -0.0788 }, { -1.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0 }, { 0.0, 0.0, -1.0 } });
    tmpMtrxs[5] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 }, { 1.0 }, { 1.0 }, { -0.06 }, { 0.0 }, { 0.0 }, { 0.0 } });
    final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(tmpMtrxs);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    final Optimisation.Result tmpResult = tmpSolver.solve();
    TestUtils.assertEquals(State.OPTIMAL, tmpResult.getState());
    final PhysicalStore<BigDecimal> tmpSolution = BigDenseStore.FACTORY.copy(RationalMatrix.FACTORY.columns(tmpResult));
    tmpSolution.modifyAll(new NumberContext(7, 6).getFunction(BigFunction.getSet()));
    for (final BigDecimal tmpBigDecimal : tmpSolution.asList()) {
        if ((tmpBigDecimal.compareTo(BigMath.ZERO) == -1) || (tmpBigDecimal.compareTo(BigMath.ONE) == 1)) {
            TestUtils.fail("!(0.0 <= " + tmpBigDecimal + " <= 1.0)");
        }
    }
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpBuilder, null);
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) MatrixStore(org.ojalgo.matrix.store.MatrixStore) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) NumberContext(org.ojalgo.type.context.NumberContext) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Aggregations

MatrixStore (org.ojalgo.matrix.store.MatrixStore)7 Test (org.junit.jupiter.api.Test)6 Optimisation (org.ojalgo.optimisation.Optimisation)6 Result (org.ojalgo.optimisation.Optimisation.Result)6 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)6 NumberContext (org.ojalgo.type.context.NumberContext)3 BigDecimal (java.math.BigDecimal)1 BasicMatrix (org.ojalgo.matrix.BasicMatrix)1 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)1