Search in sources :

Example 1 with Builder

use of org.ojalgo.optimisation.convex.ConvexSolver.Builder in project ojAlgo by optimatika.

the class ConvexProblems method testP20150908.

/**
 * Problem reported to ojalgo-user. A simple bug caused a java.lang.ArithmeticException: / by zero. ojAlgo
 * did not handle the case with inequality constraints that are all active (the set of excluded
 * constraints was empty).
 */
@Test
public void testP20150908() {
    final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2, 0 }, { 0, 2 } });
    final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.columns(new double[] { 0, 0 });
    final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1, -1 } });
    final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.columns(new double[] { -1 });
    final Builder tmpBuilder = new ConvexSolver.Builder(tmpQ, tmpC).inequalities(tmpAI, tmpBI);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    final Optimisation.Result tmpResult = tmpSolver.solve();
    final PrimitiveDenseStore tmpExpectedSolution = PrimitiveDenseStore.FACTORY.columns(new double[] { 0.5, 0.5 });
    final Optimisation.Result tmpExpectedResult = new Optimisation.Result(Optimisation.State.OPTIMAL, 0.5, tmpExpectedSolution);
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(tmpExpectedResult, tmpResult);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpBuilder, null);
}
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) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 2 with Builder

use of org.ojalgo.optimisation.convex.ConvexSolver.Builder 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 3 with Builder

use of org.ojalgo.optimisation.convex.ConvexSolver.Builder 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)

Aggregations

Test (org.junit.jupiter.api.Test)3 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)3 Result (org.ojalgo.optimisation.Optimisation.Result)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 Optimisation (org.ojalgo.optimisation.Optimisation)2 ProgrammingError (org.ojalgo.ProgrammingError)1