Search in sources :

Example 51 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class ExpressionsBasedModel method validate.

public boolean validate(final Printer appender) {
    final NumberContext context = options.feasibility;
    final Result solution = this.getVariableValues(context);
    return this.validate(solution, context, appender);
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext)

Example 52 with NumberContext

use of org.ojalgo.type.context.NumberContext 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)

Example 53 with NumberContext

use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.

the class ConvexProblems method testP20081014.

/**
 * <p>
 * I'm trying to solve some quadratic programming systems using version 24. The ActiveSetSolver does not
 * always converge to a solution, but throws an exception, "Matrix is singular" (The exception is thrown
 * by org.ojalgo.matrix.jama.LUDecomposition). The thing is that if I run Matlabs quadprog method on the
 * exact same system, a solution is found without problems. Here is the code that produces the exception:
 * </p>
 * <p>
 * 2015-02-21: Extended the test case with a few alternatives using ExpressionsBasedModel. Numerically
 * difficult problem as the formulation includes both large and very small parameters (like 1000000000 and
 * -7.646043242556307E-15).
 * </p>
 */
@Test
public void testP20081014() {
    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.0729971273939726, -0.31619624199405116, -0.14365990081105298, -3.4914813388431334E-15, 0.9963066090106673, 0.9989967493404447, 1.0, 0.0, 0.0 }, { -2.5486810808521023E-16, 3.6687950405257466, 3.2047109656515507, 1.0, 0.08586699506600544, 0.04478275122437895, 0.0, 1.0, 0.0 }, // AE
    { -7.646043242556307E-15, -107.21808503782593, -97.434268076846, 30.0, -11.54276933307617, 7.647488207332634, 0.0, 0, 1.0 } });
    // BE
    tmpSystem[1] = tmpFactory.rows(new double[][] { { 10.461669614447484 }, { -0.5328532701990767 }, { 15.782527136201711 } });
    final PrimitiveDenseStore tmpQ = tmpFactory.makeEye(9, 9);
    tmpQ.set(3, 3, 10);
    tmpQ.set(4, 4, 10);
    tmpQ.set(5, 5, 10);
    tmpQ.set(6, 6, 1000000000);
    tmpQ.set(7, 7, 1000000000);
    tmpQ.set(8, 8, 1000000000);
    // Q
    tmpSystem[2] = tmpQ;
    // 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, 0.88830035195990, 4.56989525276369, 5.00000000000000, 0.90562154243124, -1.91718419629399, 0.06390614020590 });
    // Compare to MatLab using 3 digits and 6 decimal places
    final NumberContext tmpAccuracy = NumberContext.getGeneral(3, 6);
    ConvexProblems.builAndTestModel(tmpSystem, tmpMatlabSolution, tmpAccuracy, false);
}
Also used : PhysicalStore(org.ojalgo.matrix.store.PhysicalStore) NumberContext(org.ojalgo.type.context.NumberContext) PrimitiveDenseStore(org.ojalgo.matrix.store.PrimitiveDenseStore) Test(org.junit.jupiter.api.Test)

Example 54 with NumberContext

use of org.ojalgo.type.context.NumberContext 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 55 with NumberContext

use of org.ojalgo.type.context.NumberContext 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

NumberContext (org.ojalgo.type.context.NumberContext)91 Test (org.junit.jupiter.api.Test)63 ComplexNumber (org.ojalgo.scalar.ComplexNumber)16 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)15 BigDecimal (java.math.BigDecimal)14 BasicMatrix (org.ojalgo.matrix.BasicMatrix)13 BeforeEach (org.junit.jupiter.api.BeforeEach)12 Result (org.ojalgo.optimisation.Optimisation.Result)12 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)9 Expression (org.ojalgo.optimisation.Expression)8 Variable (org.ojalgo.optimisation.Variable)8 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)6 Optimisation (org.ojalgo.optimisation.Optimisation)6 Uniform (org.ojalgo.random.Uniform)4 BigArray (org.ojalgo.array.BigArray)3 SimultaneousPrimitive (org.ojalgo.matrix.decomposition.HermitianEvD.SimultaneousPrimitive)3 MatrixStore (org.ojalgo.matrix.store.MatrixStore)3 Tag (org.junit.jupiter.api.Tag)2 RationalMatrix (org.ojalgo.matrix.RationalMatrix)2 Solver (org.ojalgo.matrix.decomposition.MatrixDecomposition.Solver)2