Search in sources :

Example 26 with Variable

use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.

the class LinearProblems method testMath286.

@Test
public void testMath286() {
    final Variable tmpX1 = new Variable("X1").weight(TENTH.multiply(EIGHT)).lower(TEN);
    final Variable tmpX2 = new Variable("X2").weight(TENTH.multiply(TWO)).lower(ZERO);
    final Variable tmpX3 = new Variable("X3").weight(TENTH.multiply(SEVEN)).lower(EIGHT);
    final Variable tmpX4 = new Variable("X4").weight(TENTH.multiply(THREE)).lower(ZERO);
    final Variable tmpX5 = new Variable("X5").weight(TENTH.multiply(SIX)).lower(FIVE);
    final Variable tmpX6 = new Variable("X6").weight(TENTH.multiply(FOUR)).lower(ZERO);
    final Variable[] tmpFullVars = new Variable[] { tmpX1.copy(), tmpX2.copy(), tmpX3.copy(), tmpX4.copy(), tmpX5.copy(), tmpX6.copy() };
    final Variable[] tmpOddVars = new Variable[] { tmpX1.copy(), tmpX3.copy(), tmpX5.copy() };
    final Variable[] tmpEvenVars = new Variable[] { tmpX2.copy(), tmpX4.copy(), tmpX6.copy() };
    final ExpressionsBasedModel tmpFullModel = new ExpressionsBasedModel(tmpFullVars);
    // tmpFullModel.setMaximisation();
    final ExpressionsBasedModel tmpOddModel = new ExpressionsBasedModel(tmpOddVars);
    // tmpOddModel.setMaximisation();
    final ExpressionsBasedModel tmpEvenModel = new ExpressionsBasedModel(tmpEvenVars);
    // tmpEvenModel.setMaximisation();
    // tmpFullModel.options.debug(LinearSolver.class);
    // tmpOddModel.options.debug(LinearSolver.class);
    // tmpEvenModel.options.debug(LinearSolver.class);
    final BigDecimal tmpRHS = new BigDecimal("23.0");
    final int tmpLength = tmpFullModel.countVariables();
    final Expression retVal = tmpFullModel.addExpression("C1");
    for (int i = 0; i < tmpLength; i++) {
        retVal.set(i, new BigDecimal[] { ONE, ZERO, ONE, ZERO, ONE, ZERO }[i]);
    }
    final Expression tmpAddWeightExpression = retVal;
    tmpAddWeightExpression.level(tmpRHS);
    final int tmpLength1 = tmpOddModel.countVariables();
    final Expression retVal1 = tmpOddModel.addExpression("C1");
    for (int i = 0; i < tmpLength1; i++) {
        retVal1.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression2 = retVal1;
    tmpAddWeightExpression2.level(tmpRHS);
    final int tmpLength2 = tmpFullModel.countVariables();
    final Expression retVal2 = tmpFullModel.addExpression("C2");
    for (int i = 0; i < tmpLength2; i++) {
        retVal2.set(i, new BigDecimal[] { ZERO, ONE, ZERO, ONE, ZERO, ONE }[i]);
    }
    final Expression tmpAddWeightExpression3 = retVal2;
    tmpAddWeightExpression3.level(tmpRHS);
    final int tmpLength3 = tmpEvenModel.countVariables();
    final Expression retVal3 = tmpEvenModel.addExpression("C2");
    for (int i = 0; i < tmpLength3; i++) {
        retVal3.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression4 = retVal3;
    tmpAddWeightExpression4.level(tmpRHS);
    final Expression tmpFullObjective = tmpFullModel.objective();
    final Expression tmpOddObjective = tmpOddModel.objective();
    final Expression tmpEvenObjective = tmpEvenModel.objective();
    // A valid solution of 25.8 can be produced with:
    // X1=10, X2=0, X3=8, X4=0, X5=5, X6=23
    final BigDecimal tmpClaimedValue = new BigDecimal("25.8");
    final Builder<PrimitiveMatrix> tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(6, 1);
    tmpBuilder.set(0, 0, 10);
    tmpBuilder.set(2, 0, 8);
    tmpBuilder.set(4, 0, 5);
    tmpBuilder.set(5, 0, 23);
    final BasicMatrix tmpFullSolution = tmpBuilder.build();
    final BasicMatrix tmpOddSolution = tmpFullSolution.selectRows(0, 2, 4);
    final BasicMatrix tmpEvenSolution = tmpFullSolution.selectRows(1, 3, 5);
    TestUtils.assertEquals("Claimed solution not valid!", true, tmpFullModel.validate(BigDenseStore.FACTORY.copy(tmpFullSolution), new NumberContext(7, 6)));
    final Double tmpActualValue = tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(tmpFullSolution));
    // final BigDecimal tmpActualValue = TypeUtils.toBigDecimal(tmpObjectiveValue);
    // JUnitUtils.assertEquals("Claimed objective value wrong!", 0, tmpClaimedValue.compareTo(tmpActualValue));
    TestUtils.assertEquals(tmpClaimedValue, tmpActualValue, new NumberContext(7, 6));
    // Start validating ojAlgo results
    final Optimisation.Result tmpEvenResult = tmpEvenModel.maximise();
    final Optimisation.Result tmpOddResult = tmpOddModel.maximise();
    final Optimisation.Result tmpFullResult = tmpFullModel.maximise();
    TestUtils.assertEquals(true, tmpEvenModel.validate(tmpEvenResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpOddModel.validate(tmpOddResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpFullModel.validate(tmpFullResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(tmpEvenSolution, RationalMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpOddSolution, RationalMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpFullSolution, RationalMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5), new NumberContext(7, 6));
    final BigDecimal tmpEvenValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpEvenObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpOddValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpOddObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpFullValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5)))));
    TestUtils.assertEquals(0, tmpFullValue.compareTo(tmpEvenValue.add(tmpOddValue)));
    TestUtils.assertEquals(0, tmpClaimedValue.compareTo(tmpFullValue));
}
Also used : Variable(org.ojalgo.optimisation.Variable) Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 27 with Variable

use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.

the class LinearProblems method testP20180310_61.

/**
 * https://github.com/optimatika/ojAlgo/issues/61
 */
@Test
public void testP20180310_61() {
    final Variable x = Variable.make("x").lower(0);
    final Variable y = Variable.make("y").lower(0);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addExpression("first").set(x, 2).set(y, 3).upper(1);
    model.addExpression("second").set(x, -2).set(y, 3).lower(1);
    final BigArray expected = BigArray.wrap(BigMath.ZERO, BigMath.THIRD);
    final Optimisation.Result result = model.maximise();
    TestUtils.assertEquals(expected, result);
    TestUtils.assertStateNotLessThanOptimal(result);
}
Also used : BigArray(org.ojalgo.array.BigArray) Variable(org.ojalgo.optimisation.Variable) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 28 with Variable

use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.

the class MarketShareCase method testMipButLinearConstrainedToOptimal.

@Test
@Disabled("Underscored before JUnit 5")
public void testMipButLinearConstrainedToOptimal() {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        final String tmpName = tmpVariable.getName();
        if (tmpName.startsWith("s")) {
            tmpVariable.level(SOLUTION.get(tmpName));
        }
    }
    final Result tmpResult = tmpModel.minimise();
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 29 with Variable

use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.

the class MarketShareCase method testRelaxedButIntegerConstrainedToOptimal.

@Test
public void testRelaxedButIntegerConstrainedToOptimal() {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    tmpModel.relax(true);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        final String tmpName = tmpVariable.getName();
        if (tmpName.startsWith("x")) {
            tmpVariable.level(SOLUTION.get(tmpName));
        }
    }
    final Result tmpResult = tmpModel.minimise();
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 30 with Variable

use of org.ojalgo.optimisation.Variable in project ojAlgo by optimatika.

the class MarketShareCase method testMipButSomeConstainedToOptimatl.

@Test
public void testMipButSomeConstainedToOptimatl() {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    // tmpModel.options.debug(IntegerSolver.class);
    // 37, 20
    final int tmpConstrLimit = 20;
    int tmpConstrCount = 0;
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        final String tmpName = tmpVariable.getName();
        if (tmpConstrCount < tmpConstrLimit) {
            tmpVariable.level(SOLUTION.get(tmpName));
            tmpConstrCount++;
        }
    }
    final Result tmpResult = tmpModel.minimise();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertTrue(tmpModel.validate(tmpModel.options.feasibility));
    TestUtils.assertTrue(tmpModel.validate(tmpResult, tmpModel.options.feasibility));
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), tmpModel.options.feasibility);
    final NumberContext tmpContext = tmpModel.options.solution.newScale(13);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        final String tmpName = tmpVariable.getName();
        final double tmpExpected = SOLUTION.get(tmpName).doubleValue();
        final double tmpActual = tmpVariable.getValue().doubleValue();
        TestUtils.assertEquals(tmpName, tmpExpected, tmpActual, tmpContext);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Aggregations

Variable (org.ojalgo.optimisation.Variable)69 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)59 Expression (org.ojalgo.optimisation.Expression)44 Test (org.junit.jupiter.api.Test)39 Result (org.ojalgo.optimisation.Optimisation.Result)37 BigDecimal (java.math.BigDecimal)26 Optimisation (org.ojalgo.optimisation.Optimisation)24 ArrayList (java.util.ArrayList)9 BasicMatrix (org.ojalgo.matrix.BasicMatrix)9 NumberContext (org.ojalgo.type.context.NumberContext)9 BigArray (org.ojalgo.array.BigArray)6 PrimitiveMatrix (org.ojalgo.matrix.PrimitiveMatrix)6 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 RationalMatrix (org.ojalgo.matrix.RationalMatrix)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 BasicLogger (org.ojalgo.netio.BasicLogger)4 IntIndex (org.ojalgo.access.Structure1D.IntIndex)3