Search in sources :

Example 1 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo-finance by optimatika.

the class EfficientFrontier method calculateAssetWeights.

@Override
protected BasicMatrix calculateAssetWeights() {
    myOptimisationModel.getExpression(VARIANCE).weight(this.getRiskAversion().doubleValue() / 2.0);
    final Result tmpResult = myOptimisationModel.minimise();
    return this.handle(tmpResult);
}
Also used : Result(org.ojalgo.optimisation.Optimisation.Result)

Example 2 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method testP20140522.

/**
 * I’ve been using the QuadraticSolver for a while, and suddenly stumbled over an unexpected failure to
 * solve a problem. The solution state was APPROXIMATE and the solution was not correct. I tested the same
 * system with another QP-solver and got the result I expected. I then condensed the problem as much as I
 * could and made a test out of it (see below). To my surprise the test sometimes fails and sometimes
 * passes(!). I’ve been running this test (alone) in TestNG. I’m using Ojalgo v35 and Java 1.7.55. The Q
 * matrix is positive definite.
 *
 * @see "http://bugzilla.optimatika.se/show_bug.cgi?id=210"
 */
@Test
public void testP20140522() {
    final double[][] q = new double[][] { { 49.0, 31.0, 17.0, 6.0 }, { 31.0, 25.0, 13.0, 5.0 }, { 17.0, 13.0, 11.0, 3.5 }, { 6.0, 5.0, 3.5, 4.0 } };
    final RawStore JamaQ = RawStore.FACTORY.rows(q);
    final double[] c = new double[] { 195.0, 59.0, -1.8, -11.7 };
    final RawStore JamaC = RawStore.FACTORY.columns(c);
    final double[][] ai = new double[][] { { 1.0, 0.0, 0.0, 0.0 }, { -1.0, 0.0, 0.0, 0.0 }, { 1.0, 1.0, 0.0, 0.0 }, { -1.0, -1.0, 0.0, 0.0 }, { 1.0, 1.0, 1.0, 0.0 }, { -1.0, -1.0, -1.0, 0.0 }, { 0.1, 0.0, 0.0, 0.0 }, { 0.01, 0.0, 0.0, 0.0 }, { 0.18, 0.1, 0.0, 0.0 }, { -0.01, 0.0, 0.0, 0.0 }, { -0.183, -0.1, 0.0, 0.0 }, { 0.0283, 0.01, 0.0, 0.0 }, { 0.25, 0.183, 0.1, 0.0 } };
    final RawStore JamaAI = RawStore.FACTORY.rows(ai);
    final double[] bi = new double[] { 0.13, 0.87, 0.18, 0.82, 0.23, 0.77, -0.04, 99.67, -0.06, 100.33, 1.06, 99.62, -0.08 };
    final RawStore JamaBI = RawStore.FACTORY.columns(bi);
    Optimisation.Result result = null;
    try {
        final ConvexSolver.Builder qsBuilder = new ConvexSolver.Builder(JamaQ, JamaC);
        qsBuilder.inequalities(JamaAI, JamaBI);
        final ConvexSolver qSolver = qsBuilder.build();
        // qSolver.options.debug(ConvexSolver.class);
        result = qSolver.solve();
        OptimisationConvexTests.assertDirectAndIterativeEquals(qsBuilder, null);
    } catch (final Exception e) {
        e.printStackTrace();
        assert false;
    }
    final CompoundFunction<Double> tmpObj = CompoundFunction.makePrimitive(JamaQ.multiply(0.5), JamaC.multiply(-1.0));
    TestUtils.assertEquals(State.OPTIMAL, result.getState());
    final int numElm = (int) result.count();
    final double[] expectedSolution = new double[] { -0.4, 0.12, -0.0196, -2.45785 };
    tmpObj.invoke(Access1D.wrap(expectedSolution));
    tmpObj.invoke(Access1D.asPrimitive1D(result));
    JamaBI.subtract(JamaAI.multiply(PrimitiveDenseStore.FACTORY.columns(expectedSolution)));
    JamaBI.subtract(JamaAI.multiply(PrimitiveDenseStore.FACTORY.columns(result)));
    for (int i = 0; i < numElm; i++) {
        TestUtils.assertEquals(expectedSolution[i], result.doubleValue(i), 1e-4);
    }
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) RawStore(org.ojalgo.matrix.store.RawStore) Test(org.junit.jupiter.api.Test)

Example 3 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method testP20150922.

/**
 * <p>
 * I recently upgraded to v38.2 of Ojalgo for solving some quadratic programs. It seems that somewhere in
 * the code there is an assumption that whenever there are inequality constraints there must be at least
 * one equality constraint. My problem has a bunch of inequality constraints but no equality constraints
 * and running it gives a "divide by zero" error. Again, this only seems to manifest itself when there are
 * inequality constraints but no equality constraints. I have reproduced it below with a simple example.
 * </p>
 * <p>
 * apete. Most likely the same problem as P20150908 (Cannot reproduce the problem with the leatest code.)
 * </p>
 */
@Test
public void testP20150922() {
    final PrimitiveDenseStore Q = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0 }, { 0, 1.0 } });
    final PrimitiveDenseStore C = PrimitiveDenseStore.FACTORY.columns(new double[] { 0, 0 });
    final ConvexSolver.Builder myBuilderI = new ConvexSolver.Builder(Q, C);
    final PrimitiveDenseStore AI = PrimitiveDenseStore.FACTORY.rows(new double[] { 1, 1 });
    final PrimitiveDenseStore BI = PrimitiveDenseStore.FACTORY.columns(new double[] { 1 });
    myBuilderI.inequalities(AI, BI);
    final ConvexSolver prob = myBuilderI.build();
    // java.lang.ArithmeticException: / by zero
    final Result solved = prob.solve();
    if (DEBUG) {
        BasicLogger.debug(solved);
    }
    final PrimitiveDenseStore AI2 = PrimitiveDenseStore.FACTORY.rows(new double[] { 1, 1 });
    final PrimitiveDenseStore BI2 = PrimitiveDenseStore.FACTORY.columns(new double[] { 2 });
    // Discovered that you got (fixed now) a problem if you modify a builder after it has been used to build a solver
    myBuilderI.inequalities(AI2, BI2);
    final ConvexSolver prob2 = myBuilderI.build();
    // java.lang.ArithmeticException: / by zero
    final Result solved2 = prob2.solve();
    if (DEBUG) {
        BasicLogger.debug(solved2);
    }
    OptimisationConvexTests.assertDirectAndIterativeEquals(myBuilderI, null);
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 4 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method testP20150720.

/**
 * Issue reported at GitHub
 * <p>
 * apete: I believe there are problems with the models the user supplied, but ojAlgo fails to correctly
 * identify and report these problems. Instead ojAlgo struggles and returns different solutions with
 * sequential executions. This test is designed to (only) ensure consistency between exections. (I don't
 * know what the correct solution is.)
 * </p>
 *
 * @see <a href="https://github.com/optimatika/ojAlgo/issues/5">GitHub Issue 5</a>
 */
@Test
public void testP20150720() {
    final ExpressionsBasedModel tmpModel1 = P20150720.buildModel1();
    final ExpressionsBasedModel tmpModel2 = P20150720.buildModel2();
    final ExpressionsBasedModel tmpModel3 = P20150720.buildModel3();
    // The problem is with the ConvexSolver, and it is present without integer constraints
    tmpModel1.relax(true);
    tmpModel2.relax(true);
    tmpModel3.relax(true);
    final Result tmpBaseResult1 = tmpModel1.maximise();
    final Result tmpBaseResult2 = tmpModel2.maximise();
    final Result tmpBaseResult3 = tmpModel3.maximise();
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel1, null);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel2, null);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel3, null);
    for (int l = 0; l < 10; l++) {
        final Result tmpResult1 = tmpModel1.maximise();
        if (OptimisationConvexTests.DEBUG) {
            BasicLogger.debug();
            BasicLogger.debug("Model 1");
            BasicLogger.debug(tmpResult1);
            BasicLogger.debug(tmpModel1);
        }
        TestUtils.assertStateNotLessThanFeasible(tmpResult1);
        TestUtils.assertEquals("Model 1 State @" + l, tmpBaseResult1.getState(), tmpResult1.getState());
        TestUtils.assertEquals("Model 1 Value @" + l, tmpBaseResult1.getValue(), tmpResult1.getValue());
        TestUtils.assertEquals("Model 1 Solution @" + l, tmpBaseResult1, tmpResult1);
        final Result tmpResult2 = tmpModel2.maximise();
        if (OptimisationConvexTests.DEBUG) {
            BasicLogger.debug();
            BasicLogger.debug("Model 2");
            BasicLogger.debug(tmpResult2);
            BasicLogger.debug(tmpModel2);
        }
        TestUtils.assertStateNotLessThanFeasible(tmpResult2);
        TestUtils.assertEquals("Model 2 State @" + l, tmpBaseResult2.getState(), tmpResult2.getState());
        TestUtils.assertEquals("Model 2 Value @" + l, tmpBaseResult2.getValue(), tmpResult2.getValue());
        TestUtils.assertEquals("Model 2 Solution @" + l, tmpBaseResult2, tmpResult2);
        final Result tmpResult3 = tmpModel3.maximise();
        if (OptimisationConvexTests.DEBUG) {
            BasicLogger.debug();
            BasicLogger.debug("Model 3");
            BasicLogger.debug(tmpResult3);
            BasicLogger.debug(tmpModel3);
        }
        TestUtils.assertStateNotLessThanFeasible(tmpResult3);
        TestUtils.assertEquals("Model 3 State @" + l, tmpBaseResult3.getState(), tmpResult3.getState());
        TestUtils.assertEquals("Model 3 Value @" + l, tmpBaseResult3.getValue(), tmpResult3.getValue());
        TestUtils.assertEquals("Model 3 Solution @" + l, tmpBaseResult3, tmpResult3);
    }
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 5 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method testP20150809.

/**
 * Issue reported at GitHub. A set of problems related to when Q is zero - a linear problem. Generally the
 * ConvexSolver is not the right option to handle linear problems, but there is some desireable behaviour.
 */
@Test
public void testP20150809() {
    final NumberContext precision = new NumberContext(11, 14, RoundingMode.HALF_EVEN);
    final Primitive64Array tmpExpectedSolution = Primitive64Array.wrap(new double[] { 0.12, -0.05, 0.08, 0.07 });
    final Primitive64Array tmpBoundedSolution = Primitive64Array.wrap(new double[] { 99999, -99999, 99999, 99999 });
    ConvexSolver tmpSolver = P20150809.buildModel(true, false);
    Result tmpResult = tmpSolver.solve();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
    tmpSolver = P20150809.buildModel(true, true);
    tmpResult = tmpSolver.solve();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
    tmpSolver = P20150809.buildModel(false, false);
    tmpResult = tmpSolver.solve();
    TestUtils.assertEquals(Optimisation.State.UNBOUNDED, tmpResult.getState());
    tmpSolver = P20150809.buildModel(false, true);
    tmpResult = tmpSolver.solve();
    // Since it is now constrained, the solver should be able find the optimal solution.
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(tmpBoundedSolution, tmpResult, precision);
}
Also used : Primitive64Array(org.ojalgo.array.Primitive64Array) NumberContext(org.ojalgo.type.context.NumberContext) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Aggregations

Result (org.ojalgo.optimisation.Optimisation.Result)33 Test (org.junit.jupiter.api.Test)27 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)27 Variable (org.ojalgo.optimisation.Variable)20 Expression (org.ojalgo.optimisation.Expression)12 Optimisation (org.ojalgo.optimisation.Optimisation)8 NumberContext (org.ojalgo.type.context.NumberContext)8 BigArray (org.ojalgo.array.BigArray)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 Disabled (org.junit.jupiter.api.Disabled)2 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)2 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1