Search in sources :

Example 51 with PrimitiveDenseStore

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

the class ConvexProblems method builAndTestModel.

private static void builAndTestModel(final PrimitiveDenseStore[] matrices, final double[] expectedSolution, final NumberContext modelValidationContext, final boolean testSolverDirectly) {
    final PrimitiveDenseStore tmpExpectedSolution = PrimitiveDenseStore.FACTORY.columns(expectedSolution);
    ConvexProblems.builAndTestModel(matrices, tmpExpectedSolution, modelValidationContext, testSolverDirectly);
}
Also used : PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore)

Example 52 with PrimitiveDenseStore

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

the class ConvexProblems method testP20081119.

/**
 * <p>
 * Continuation of {@link #testP20081014()} and {@link #testP20081015()}.
 * </p>
 * <p>
 * Originally the problem was an ArrayIndexOutOfBoundsException. When that was fixed it had the same
 * numerical difficulties as the previous versions.
 * </p>
 * <p>
 * 2015-02-28: Var tvungen att ändra från new NumberContext(7, 11) till new NumberContext(3, 3) för
 * lösningen.
 * </p>
 */
@Test
public void testP20081119() {
    final PhysicalStore.Factory<Double, PrimitiveDenseStore> tmpFactory = PrimitiveDenseStore.FACTORY;
    final PrimitiveDenseStore[] tmpSystem = new PrimitiveDenseStore[6];
    // {[AE], [BE], [Q], [C], [AI], [BI]}
    tmpSystem[0] = tmpFactory.rows(new double[][] { { -10.630019918689772, 0.15715259580856766, -24.006889886456438, -3.4914813388431334E-15, 0.9987922086746552, 0.9018272287390979, 1.0, 0.0, 0.0 }, { -3.711451617763614E-14, -3.1946032406211518, 50.10466796063192, 1.0, 0.04913373475326318, 0.4320968057099691, 0.0, 1.0, 0.0 }, // AE
    { -1.1134354853290842E-12, 94.42372385635744, -1719.2020477970657, 30.0, -10.463141920669791, -4.8464591126471905, 0.0, 0.0, 1.0 } });
    // BE
    tmpSystem[1] = tmpFactory.rows(new double[][] { { 14.272908058664967 }, { -3.888270819999793 }, { -0.06992907379067503 } });
    // Q
    tmpSystem[2] = tmpFactory.makeEye(9, 9);
    tmpSystem[2].set(3, 3, 10);
    tmpSystem[2].set(4, 4, 10);
    tmpSystem[2].set(5, 5, 10);
    tmpSystem[2].set(6, 6, 1000000000);
    tmpSystem[2].set(7, 7, 1000000000);
    tmpSystem[2].set(8, 8, 1000000000);
    // C
    tmpSystem[3] = tmpFactory.rows(new double[][] { { 0 }, { 0 }, { 0 }, { -1 }, { -1 }, { 1 }, { 0 }, { 0 }, { 0 } });
    final double[][] tmpAI = new double[18][9];
    for (int i = 0; i < 9; i++) {
        tmpAI[i][i] = 1;
        tmpAI[i + 9][i] = -1;
    }
    // AI
    tmpSystem[4] = tmpFactory.rows(tmpAI);
    tmpSystem[5] = tmpFactory.rows(new double[][] { { 0 }, { 0.0175 }, { 0.0175 }, { 5 }, { 5 }, { 5 }, { 100000 }, { 100000 }, { 100000 }, { 0 }, { 0.0175 }, { 0.0175 }, { 5 }, { 5 }, { 5 }, { 100000 }, { 100000 }, // BI
    { 100000 } });
    final PrimitiveDenseStore tmpMatlabSolution = tmpFactory.columns(new double[] { 0.00000000000000, 0.01750000000000, -0.01750000000000, 1.46389524463679, 5.00000000000000, 4.87681260745493, 4.45803387299108, -6.77235264210831, 0.22574508859158 });
    ConvexProblems.builAndTestModel(tmpSystem, tmpMatlabSolution, NumberContext.getGeneral(2, 14), false);
}
Also used : PhysicalStore(org.ojalgo.matrix.store.PhysicalStore) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 53 with PrimitiveDenseStore

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

the class ConvexProblems method testP20140109.

/**
 * <p>
 * I tried to use ojAlgo to implement a norm minimization problem, but the solver fails even for very
 * simple instances. The following example is one particular simple instance. Q is the identity matrix, C
 * the zero vector. The constraints express that the solution is a probability function (AE for
 * normalization and AI for non-negativity). Q is positive definite and the solution should be (0.5, 0.5),
 * but qSolver fails.
 * </p>
 * <p>
 * apete: The problem was incorrectly specified with a transposed "C" vector. Modified the builder to
 * actually throw an exception.
 * </p>
 */
@Test
public void testP20140109() {
    final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1, 0 }, { 0, 1 } });
    final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0, 0 } });
    final PrimitiveDenseStore tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1, 1 } });
    final PrimitiveDenseStore tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1 } });
    final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1, 0 }, { 0, -1 } });
    final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0 }, { 0 } });
    try {
        final ConvexSolver qSolver = new ConvexSolver.Builder(tmpQ, tmpC).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI).build();
        // qSolver.options.debug(ConvexSolver.class);
        final Optimisation.Result tmpResult = qSolver.solve();
        // Shouldn't get this far. There should be an exception
        TestUtils.assertStateLessThanFeasible(tmpResult);
        TestUtils.fail();
    } catch (final ProgrammingError exception) {
        TestUtils.assertTrue("Yes!", true);
    }
    // ... and check that the correctly defined problem does solve.
    final ConvexSolver tmpCorrectSolver = new ConvexSolver.Builder(tmpQ, tmpC.transpose()).equalities(tmpAE, tmpBE).inequalities(tmpAI, tmpBI).build();
    final Optimisation.Result tmpResult = tmpCorrectSolver.solve();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(Primitive64Array.wrap(new double[] { 0.5, 0.5 }), tmpResult);
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ProgrammingError(org.ojalgo.ProgrammingError) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 54 with PrimitiveDenseStore

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

the class ConvexProblems method testP20081015.

/**
 * <p>
 * Continuation of {@link #testP20081014()}.
 * </p>
 * <p>
 * Thanks for your answer, Anders, it did solve my system (even though the result state was FAILED). As
 * you might have guessed, I am using the ActiveSetSolver as a part of a larger system where the system
 * matrixes to be solved changes all the time (not the dimensions but the values of the matrixes). I still
 * get errors in certain situations. I will present a system that triggers an
 * ArrayIndexOutOfBoundsException in ActiveSetSolver. Again, Matlabs quadprog produces a correct result.
 * </p>
 * <p>
 * 2015-02-28: Var tvungen att ändra från new NumberContext(7, 6) till new NumberContext(5, 6) för
 * lösningen.
 * </p>
 */
@Test
public void testP20081015() {
    final PhysicalStore.Factory<Double, PrimitiveDenseStore> tmpFactory = PrimitiveDenseStore.FACTORY;
    final PrimitiveDenseStore[] tmpSystem = new PrimitiveDenseStore[6];
    // {[AE], [BE], [Q], [C], [AI], [BI]}
    tmpSystem[0] = tmpFactory.rows(new double[][] { { -0.6864742690952357, -0.5319998214213948, 1.2385363215384646, -3.4914813388431334E-15, 0.976619978072726, 0.8727726942384015, 1.0, 0.0, 0.0 }, { -2.396812100141995E-15, 2.4168686217298863, -2.2145077177955423, 1.0, 0.21497306442721648, 0.48812685256175126, 0.0, 1.0, 0.0 }, // AE
    { -7.190436300425984E-14, -67.71806025910404, 77.58205842771245, 30.0, -15.23877173547103, -6.788851328706924, 0.0, 0.0, 1.0 } });
    // BE
    tmpSystem[1] = tmpFactory.rows(new double[][] { { 0.459002008118756 }, { 0.002566161917554134 }, { -0.03315618953218959 } });
    // Q
    tmpSystem[2] = tmpFactory.makeEye(9, 9);
    tmpSystem[2].set(3, 3, 10);
    tmpSystem[2].set(4, 4, 10);
    tmpSystem[2].set(5, 5, 10);
    tmpSystem[2].set(6, 6, 1000000000);
    tmpSystem[2].set(7, 7, 1000000000);
    tmpSystem[2].set(8, 8, 1000000000);
    // C
    tmpSystem[3] = tmpFactory.rows(new double[][] { { 0 }, { 0 }, { 0 }, { -1 }, { -1 }, { 1 }, { 0 }, { 0 }, { 0 } });
    final double[][] tmpAI = new double[18][9];
    for (int i = 0; i < 9; i++) {
        tmpAI[i][i] = 1;
        tmpAI[i + 9][i] = -1;
    }
    // AI
    tmpSystem[4] = tmpFactory.rows(tmpAI);
    tmpSystem[5] = tmpFactory.rows(new double[][] { { 0 }, { 0.0175 }, { 0.0175 }, { 0.5 }, { 0.5 }, { 0.5 }, { 100000 }, { 100000 }, { 100000 }, { 0 }, { 0.0175 }, { 0.0175 }, { 0.5 }, { 0.5 }, { 0.5 }, { 100000 }, { 100000 }, // BI
    { 100000 } });
    final PrimitiveDenseStore tmpMatlabSolution = tmpFactory.columns(new double[] { -0.00000000000000, -0.01750000000000, 0.01750000000000, 0.13427356981778, 0.50000000000000, -0.14913060410765, 0.06986475572103, -0.08535020176844, 0.00284500680371 });
    ConvexProblems.builAndTestModel(tmpSystem, tmpMatlabSolution, NumberContext.getGeneral(4, 14), true);
}
Also used : PhysicalStore(org.ojalgo.matrix.store.PhysicalStore) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 55 with PrimitiveDenseStore

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

the class UCLAee236aCase method testRelaxedNodeP11.

/**
 * P11
 */
@Test
public void testRelaxedNodeP11() {
    final ExpressionsBasedModel tmpModel = UCLAee236aCase.makeOriginalRootModel().relax(true);
    tmpModel.getVariable(0).lower(THREE);
    tmpModel.getVariable(1).upper(ONE);
    tmpModel.getVariable(0).lower(FOUR);
    tmpModel.getVariable(1).upper(ZERO);
    tmpModel.getVariable(0).upper(FOUR);
    final Optimisation.Result tmpResult = tmpModel.minimise();
    // TestUtils.assertEquals(State.OPTIMAL, tmpResult.getState());
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    final PrimitiveDenseStore tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.00 }, { 0.00 } });
    TestUtils.assertEquals(tmpExpX, tmpResult, PRECISION);
    TestUtils.assertEquals(-8.00, tmpModel.minimise().getValue(), PRECISION);
}
Also used : Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Aggregations

PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)72 Test (org.junit.jupiter.api.Test)37 Optimisation (org.ojalgo.optimisation.Optimisation)16 NumberContext (org.ojalgo.type.context.NumberContext)15 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)12 ComparableToDouble (org.ojalgo.type.keyvalue.ComparableToDouble)7 Result (org.ojalgo.optimisation.Optimisation.Result)6 ComplexNumber (org.ojalgo.scalar.ComplexNumber)4 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 PhysicalStore (org.ojalgo.matrix.store.PhysicalStore)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 IntRowColumn (org.ojalgo.access.Structure2D.IntRowColumn)2 ComplexDenseStore (org.ojalgo.matrix.store.ComplexDenseStore)2 LinearSolver (org.ojalgo.optimisation.linear.LinearSolver)2 Normal (org.ojalgo.random.Normal)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 Disabled (org.junit.jupiter.api.Disabled)1 Tag (org.junit.jupiter.api.Tag)1 ProgrammingError (org.ojalgo.ProgrammingError)1