Search in sources :

Example 1 with MatrixStore

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

the class ConvexProblems method testP20091102b.

/**
 * Infeasible problem, but solver reports optimal solution!
 */
@SuppressWarnings("unchecked")
@Test
public void testP20091102b() {
    final MatrixStore<Double>[] tmpMtrxs = new MatrixStore[6];
    tmpMtrxs[0] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0 } });
    tmpMtrxs[1] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 } });
    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.8 }, { 0.0 }, { 0.0 } });
    final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(tmpMtrxs);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    final Optimisation.Result tmpResult = tmpSolver.solve();
    TestUtils.assertStateLessThanFeasible(tmpResult);
    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) Test(org.junit.jupiter.api.Test)

Example 2 with MatrixStore

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

the class ConvexProblems method testP20090115.

/**
 * A lower level version of {@linkplain org.ojalgo.finance.portfolio.PortfolioProblems#testP20090115()}.
 * The solver returns negative, constraint breaking, variables with STATE == OPTIMAL.
 */
@SuppressWarnings("unchecked")
@Test
public void testP20090115() {
    final MatrixStore<Double>[] tmpMtrxs = new MatrixStore[6];
    tmpMtrxs[0] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 } });
    tmpMtrxs[1] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 } });
    tmpMtrxs[2] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 3.048907897157133E-4, 1.6671472561019247E-4, 4.4500080981934345E-4, -5.389129745055723E-4, -2.6090705011393183E-4, -1.2633284900760366E-4, -6.485428846447651E-7 }, { 1.6671472561019247E-4, 2.341985572849691E-4, 2.9113916450678265E-4, -4.5760873539850514E-4, 1.3078636134987255E-5, -2.354289901013046E-5, -7.578030042426654E-7 }, { 4.4500080981934345E-4, 2.9113916450678265E-4, 7.46023915996829E-4, -0.0010247176498305568, -2.6745504327902895E-4, -1.6563544154823496E-4, -8.293698990696063E-7 }, { -5.389129745055723E-4, -4.5760873539850514E-4, -0.0010247176498305568, 0.001754169535149865, 2.0293065310212377E-4, 2.1401092557826588E-4, 1.0252846778608953E-7 }, { -2.6090705011393183E-4, 1.3078636134987255E-5, -2.6745504327902895E-4, 2.0293065310212377E-4, 4.632320892679136E-4, 1.7969731066037214E-4, 2.4953495129362833E-8 }, { -1.2633284900760366E-4, -2.354289901013046E-5, -1.6563544154823496E-4, 2.1401092557826588E-4, 1.7969731066037214E-4, 8.346410612364995E-5, -7.02099350897589E-8 }, { -6.485428846447651E-7, -7.578030042426654E-7, -8.293698990696063E-7, 1.0252846778608953E-7, 2.4953495129362833E-8, -7.02099350897589E-8, 8.367244992498656E-9 } });
    tmpMtrxs[3] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -0.010638291263564232 }, { -0.013500370827906071 }, { -0.011390037735101773 }, { -0.010385042339767682 }, { -3.812208389845893E-4 }, { -0.002315505853720011 }, { -0.0 } });
    tmpMtrxs[4] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0 } }).transpose();
    tmpMtrxs[5] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 }, { 1.0 }, { 1.0 }, { 1.0 }, { 1.0 }, { 1.0 }, { 1.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 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.assertBounds(BigMath.ZERO, tmpResult, BigMath.ONE, StandardType.PERCENT);
    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) Test(org.junit.jupiter.api.Test)

Example 3 with MatrixStore

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

the class ConvexProblems method testP20091102a.

/**
 * Fick state "failed" men det berodde bara på att antalet iterationer inte räckte för att hitta en
 * korrekt lösning. Ökade "iterationsLimit" så fungerade det. Felet uppstod inte varje gång, men om man
 * körde samma problem (det problem som är test caset) uppreapde gånger fick man till slut ett fel.
 */
@SuppressWarnings("unchecked")
@Test
public void testP20091102a() {
    final MatrixStore<Double>[] tmpMtrxs = new MatrixStore[6];
    tmpMtrxs[0] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0 } });
    tmpMtrxs[1] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 } });
    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 } });
    // Solve the same problem several times
    for (int i = 0; i < 20; i++) {
        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());
        TestUtils.assertEquals(PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.68888888888888888 }, { 0.311111111111111111 }, { 0.0 } }), RationalMatrix.FACTORY.columns(tmpResult));
    }
}
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) Test(org.junit.jupiter.api.Test)

Example 4 with MatrixStore

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

the class ConvexProblems method testP20090924.

/**
 * Infeasible problem, but solver reports optimal solution!
 */
@SuppressWarnings("unchecked")
@Test
public void testP20090924() {
    final MatrixStore<Double>[] tmpMtrxs = new MatrixStore[6];
    tmpMtrxs[0] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0 }, { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } });
    tmpMtrxs[1] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.0 }, { 0.7027946085029227 } });
    tmpMtrxs[2] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 2.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 2.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 2.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 2.0 } });
    tmpMtrxs[3] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -0.0 }, { 0.5 }, { 0.25 }, { 0.25 }, { 0.3 }, { -0.0 }, { 0.62 } });
    tmpMtrxs[4] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, { 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0 }, { 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0 }, { 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0 } });
    tmpMtrxs[5] = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.17 }, { 0.52 }, { 0.3 }, { 0.3 }, { 0.3 }, { 0.15 }, { 1.0 }, { 0.31 }, { -0.05960220972942152 }, { -0.1144024630877301 }, { -0.12289286964304823 }, { 0.0 }, { -0.02 }, { 0.0 } });
    final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(tmpMtrxs);
    final ConvexSolver tmpSolver = tmpBuilder.build();
    final Optimisation.Result tmpResult = tmpSolver.solve();
    TestUtils.assertStateLessThanFeasible(tmpResult);
    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) Test(org.junit.jupiter.api.Test)

Example 5 with MatrixStore

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

the class JacobiSolver method solve.

@SuppressWarnings("unchecked")
public final MatrixStore<Double> solve(final Access2D<?> body, final Access2D<?> rhs, final PhysicalStore<Double> current) throws RecoverableCondition {
    MatrixStore<Double> tmpBody = null;
    if ((body instanceof MatrixStore<?>) && (body.get(0L) instanceof Double)) {
        tmpBody = (MatrixStore<Double>) body;
    } else {
        tmpBody = MatrixStore.PRIMITIVE.makeWrapper(body).get();
    }
    final MatrixStore<Double> tmpBodyDiagonal = PrimitiveDenseStore.FACTORY.columns(tmpBody.sliceDiagonal(0L, 0L));
    MatrixStore<Double> tmpRHS = null;
    if ((rhs instanceof MatrixStore<?>) && (rhs.get(0L) instanceof Double)) {
        tmpRHS = (MatrixStore<Double>) rhs;
    } else {
        tmpRHS = MatrixStore.PRIMITIVE.makeWrapper(rhs).get();
    }
    final PhysicalStore<Double> tmpIncrement = this.preallocate(body, rhs);
    double tmpNormErr = POSITIVE_INFINITY;
    final double tmpNormRHS = tmpRHS.aggregateAll(Aggregator.NORM2);
    int tmpIterations = 0;
    final int tmpLimit = this.getIterationsLimit();
    final NumberContext tmpCntxt = this.getAccuracyContext();
    final double tmpRelaxation = this.getRelaxationFactor();
    do {
        current.premultiply(tmpBody).operateOnMatching(tmpRHS, SUBTRACT).supplyTo(tmpIncrement);
        tmpNormErr = tmpIncrement.aggregateAll(Aggregator.NORM2);
        tmpIncrement.modifyMatching(DIVIDE, tmpBodyDiagonal);
        if (this.getAccuracyContext().isDifferent(ONE, tmpRelaxation)) {
            tmpIncrement.multiply(tmpRelaxation);
        }
        current.modifyMatching(ADD, tmpIncrement);
        tmpIterations++;
        if (this.isDebugPrinterSet()) {
            this.debug(tmpIterations, current);
        }
    } while ((tmpIterations < tmpLimit) && !tmpCntxt.isSmall(tmpNormRHS, tmpNormErr));
    return current;
}
Also used : MatrixStore(org.ojalgo.matrix.store.MatrixStore) NumberContext(org.ojalgo.type.context.NumberContext)

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