Search in sources :

Example 36 with NumberContext

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

the class IntegerProblems method testP20130409a.

/**
 * apete's implementation of the original problem description.
 * <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
 */
@Test
public void testP20130409a() {
    final Variable[] tmpVariables = new Variable[] { new Variable("x1").lower(BigMath.ZERO).weight(BigMath.ONE), new Variable("x2013").lower(BigMath.ZERO).integer(true), new Variable("x2014").lower(BigMath.ZERO).integer(true) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExpr1 = tmpModel.addExpression("Expr1");
    tmpExpr1.set(0, -1);
    tmpExpr1.set(1, 5100);
    tmpExpr1.set(2, -5000);
    tmpExpr1.upper(BigMath.ZERO);
    final Expression tmpExpr2 = tmpModel.addExpression("Expr2");
    tmpExpr2.set(0, 1);
    tmpExpr2.set(1, 5100);
    tmpExpr2.set(2, -5000);
    tmpExpr2.lower(BigMath.ZERO);
    final Expression tmpExpr3 = tmpModel.addExpression("Expr3");
    tmpExpr3.set(1, 5000);
    tmpExpr3.set(2, 5000);
    tmpExpr3.level(new BigDecimal(19105000));
    final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4200.000000000075), BigDecimal.valueOf(1892), BigDecimal.valueOf(1929) });
    TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
    // tmpModel.options.debug(GenericSolver.class);
    // tmpModel.options.problem = NumberContext.getGeneral(12);
    final Result tmpResult = tmpModel.minimise();
    // BasicLogger.debug(tmpResult.toString());
    TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
    TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
Also used : BigArray(org.ojalgo.array.BigArray) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 37 with NumberContext

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

the class IntegerProblems method testP20130409b.

/**
 * Test case sent in by the user / problem reporter
 * <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
 */
@Test
public void testP20130409b() {
    final Variable x1 = Variable.make("x1");
    final Variable x2013 = Variable.make("x2013");
    final Variable x2014 = Variable.make("x2014");
    final Variable x2015 = Variable.make("x2015");
    x2013.setInteger(true);
    x2014.setInteger(true);
    x2015.setInteger(true);
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
    tmpModel.addVariable(x1);
    tmpModel.addVariable(x2013);
    tmpModel.addVariable(x2014);
    tmpModel.addVariable(x2015);
    final Expression obj = tmpModel.addExpression("obj");
    obj.set(x1, 1);
    obj.weight(BigDecimal.valueOf(1));
    final Expression c1 = tmpModel.addExpression("c1");
    c1.set(x1, 1);
    c1.lower(BigDecimal.valueOf(0));
    final Expression c2 = tmpModel.addExpression("c2");
    c2.set(x2014, -5000);
    c2.set(x2013, 5100);
    c2.set(x1, -1);
    c2.upper(BigDecimal.valueOf(0));
    final Expression c3 = tmpModel.addExpression("c3");
    c3.set(x2014, -5000);
    c3.set(x2013, 5100);
    c3.set(x1, 1);
    c3.lower(BigDecimal.valueOf(0));
    final Expression c4 = tmpModel.addExpression("c4");
    c4.set(x2014, 150);
    c4.set(x2013, 5100);
    c4.set(x2015, -5000);
    c4.set(x1, -1);
    c4.upper(BigDecimal.valueOf(0));
    final Expression c5 = tmpModel.addExpression("c5");
    c5.set(x2014, 150);
    c5.set(x2013, 5100);
    c5.set(x2015, -5000);
    c5.set(x1, 1);
    c5.lower(BigDecimal.valueOf(0));
    final Expression c6 = tmpModel.addExpression("c6");
    c6.set(x2015, 5000);
    c6.set(x2014, 5000);
    c6.set(x2013, 5000);
    c6.level(BigDecimal.valueOf(19105000));
    final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4849.999999997941), BigDecimal.valueOf(1245), BigDecimal.valueOf(1269), BigDecimal.valueOf(1307) });
    TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
    // tmpModel.options.debug(IntegerSolver.class);
    // tmpModel.options.problem = NumberContext.getGeneral(8);
    final Result tmpResult = tmpModel.minimise();
    // BasicLogger.debug(tmpResult.toString());
    TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
    TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
Also used : BigArray(org.ojalgo.array.BigArray) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 38 with NumberContext

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

the class QRTest method testDiagonalCase.

@Test
public void testDiagonalCase() {
    final PhysicalStore<Double> tmpOriginalMatrix = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.0, 3.0, 2.0, 1.0 }, { 0.0, 3.0, 2.0, 1.0 }, { 0.0, 0.0, 2.0, 1.0 }, { 0.0, 0.0, 0.0, 1.0 } });
    final QR<Double> tmpDecomp = QR.PRIMITIVE.make();
    tmpDecomp.decompose(tmpOriginalMatrix);
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Should be I", tmpDecomp.getQ());
        BasicLogger.debug("Should be A", tmpDecomp.getR());
    }
    TestUtils.assertEquals(tmpOriginalMatrix, tmpDecomp, new NumberContext(7, 6));
// TODO See if possible to fix so that Q == I when the original A is already triangular
// TestUtils.assertEquals(PrimitiveDenseStore.FACTORY.makeEye(4, 4), tmpDecomp.getQ(), new NumberContext(7, 6));
// TestUtils.assertEquals(tmpOriginalMatrix, tmpDecomp.getR(), new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) Test(org.junit.jupiter.api.Test)

Example 39 with NumberContext

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

the class QRTest method testHermitian.

@Test
public void testHermitian() {
    final int tmpLim = DIMENSION - 1;
    final MatrixStore<ComplexNumber> tmpOriginal = QRTest.makeHermitianMatrix();
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug("Original", tmpOriginal);
    }
    final QR<ComplexNumber> tmpDecomposition = QR.COMPLEX.make();
    tmpDecomposition.decompose(tmpOriginal);
    final MatrixStore<ComplexNumber> tmpDecompQ = tmpDecomposition.getQ();
    final MatrixStore<ComplexNumber> tmpDecompR = tmpDecomposition.getR();
    final DecompositionStore<ComplexNumber> tmpInPlace = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpNowQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final DecompositionStore<ComplexNumber> tmpNowR = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpForwardQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final DecompositionStore<ComplexNumber> tmpForwardR = ComplexDenseStore.FACTORY.copy(tmpOriginal);
    final DecompositionStore<ComplexNumber> tmpReverseQ = ComplexDenseStore.FACTORY.makeEye(DIMENSION, DIMENSION);
    final Householder.Complex[] tmpHouseholders = new Householder.Complex[tmpLim];
    for (int ij = 0; ij < tmpLim; ij++) {
        final Householder.Complex tmpVector = new Householder.Complex(DIMENSION);
        if (tmpInPlace.generateApplyAndCopyHouseholderColumn(ij, ij, tmpVector)) {
            tmpInPlace.transformLeft(tmpVector, ij + 1);
            tmpNowQ.transformRight(tmpVector, 0);
            tmpNowR.transformLeft(tmpVector, ij);
        }
        tmpHouseholders[ij] = tmpVector;
    }
    for (int h = 0; h < tmpHouseholders.length; h++) {
        final Householder.Complex tmpVector = tmpHouseholders[h];
        tmpForwardQ.transformRight(tmpVector, 0);
        tmpForwardR.transformLeft(tmpVector, h);
    }
    for (int h = tmpHouseholders.length - 1; h >= 0; h--) {
        final Householder.Complex tmpVector = tmpHouseholders[h];
        tmpReverseQ.transformLeft(tmpVector, 0);
    }
    if (MatrixDecompositionTests.DEBUG) {
        BasicLogger.debug();
        BasicLogger.debug("Decomp Q", tmpDecompQ);
        BasicLogger.debug("Now Q", tmpNowQ);
        BasicLogger.debug("Forward Q", tmpForwardQ);
        BasicLogger.debug("Reverse Q", tmpReverseQ);
        BasicLogger.debug();
        BasicLogger.debug("Decomp R", tmpDecompR);
        BasicLogger.debug("Now R", tmpNowR);
        BasicLogger.debug("Forward R", tmpForwardR);
    }
    TestUtils.assertEquals(tmpOriginal, tmpDecomposition, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpNowQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpForwardQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompQ, tmpReverseQ, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompR, tmpNowR, new NumberContext(7, 6));
    TestUtils.assertEquals(tmpDecompR, tmpForwardR, new NumberContext(7, 6));
}
Also used : Householder(org.ojalgo.matrix.transformation.Householder) NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) Test(org.junit.jupiter.api.Test)

Example 40 with NumberContext

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

the class SchurTest method testP20061119Case.

@Test
public void testP20061119Case() {
    final PhysicalStore<Double> tmpOriginalMatrix = PrimitiveDenseStore.FACTORY.copy(P20061119Case.getProblematic());
    final ComplexNumber tmp00 = ComplexNumber.valueOf(26.14421883828456);
    final ComplexNumber tmp11 = ComplexNumber.of(2.727890580857718, 3.6223578444417908);
    final ComplexNumber tmp22 = tmp11.conjugate();
    final ComplexNumber tmp33 = ComplexNumber.ZERO;
    final ComplexNumber tmp44 = tmp33;
    final Array1D<ComplexNumber> tmpExpectedDiagonal = Array1D.COMPLEX.copy(new ComplexNumber[] { tmp00, tmp11, tmp22, tmp33, tmp44 });
    SchurTest.doTest(tmpOriginalMatrix, tmpExpectedDiagonal, new NumberContext(7, 6));
}
Also used : NumberContext(org.ojalgo.type.context.NumberContext) ComplexNumber(org.ojalgo.scalar.ComplexNumber) 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