Search in sources :

Example 11 with Expression

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

the class LinearDesignTestCases method test5LinearModelCase.

/**
 * http://www.maths.bris.ac.uk/~maxmr/opt/sm2.pdf
 */
@Test
public void test5LinearModelCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(TWO), new Variable("X2").lower(ZERO).weight(THREE) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExprC1 = tmpModel.addExpression("C1");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC1.set(i, new BigDecimal[] { HALF, QUARTER }[i]);
    }
    tmpExprC1.upper(FOUR);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ONE, THREE }[i]);
    }
    tmpExprC2.lower(TEN.add(TEN));
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { ONE, ONE }[i]);
    }
    tmpExprC3.level(TEN);
    final Optimisation.Result tmpResult = tmpModel.minimise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 5.0 }, { 5.0 } });
    final PhysicalStore<Double> tmpActX = PrimitiveDenseStore.FACTORY.copy(tmpSolution.selectRows(new int[] { 0, 1 }));
    TestUtils.assertEquals(tmpExpX, tmpActX);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 12 with Expression

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

the class LinearDesignTestCases method test2LinearModelCase.

/**
 * http://www.stats.ox.ac.uk/~yu/4_Simplex_II.pdf
 */
@Test
public void test2LinearModelCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(THREE), new Variable("X2").lower(ZERO).weight(ZERO), new Variable("X3").lower(ZERO).weight(ONE) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExprC1 = tmpModel.addExpression("C1");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC1.set(i, new BigDecimal[] { ONE, TWO, ONE }[i]);
    }
    tmpExprC1.level(TEN);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ONE, TWO.negate(), TWO }[i]);
    }
    tmpExprC2.level(SIX);
    final Optimisation.Result tmpResult = tmpModel.maximise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 8.0 }, { 1.0 }, { 0.0 } });
    final PhysicalStore<Double> tmpActX = PrimitiveDenseStore.FACTORY.copy(tmpSolution.selectRows(new int[] { 0, 1, 2 }));
    TestUtils.assertEquals(tmpExpX, tmpActX);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 13 with Expression

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

the class LinearDesignTestCases method test4LinearModelCase.

/**
 * http://www.saintmarys.edu/~psmith/338act14.html
 */
@Test
public void test4LinearModelCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(ONE.negate()), new Variable("X2").lower(ZERO).weight(ONE), new Variable("X3").lower(ZERO).weight(ZERO) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExprC1 = tmpModel.addExpression("C1");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC1.set(i, new BigDecimal[] { SIX, ONE.negate(), ZERO }[i]);
    }
    tmpExprC1.upper(TEN);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ONE, FIVE, ZERO }[i]);
    }
    tmpExprC2.lower(FOUR);
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { ONE, FIVE, ONE }[i]);
    }
    tmpExprC3.level(FIVE);
    final Optimisation.Result tmpResult = tmpModel.minimise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.74 }, { 0.45 }, { 1.0 } });
    final PhysicalStore<Double> tmpActX = PrimitiveDenseStore.FACTORY.copy(tmpSolution.selectRows(new int[] { 0, 1, 2 }));
    tmpActX.modifyAll(new NumberContext(7, 2).getFunction(PrimitiveFunction.getSet()));
    TestUtils.assertEquals(tmpExpX, tmpActX);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Test(org.junit.jupiter.api.Test)

Example 14 with Expression

use of org.ojalgo.optimisation.Expression 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 15 with Expression

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

the class OptimisationIntegerData method buildModelForP20100412.

public static ExpressionsBasedModel buildModelForP20100412() {
    final KnapsackItem[] tmpItems = { new KnapsackItem(20, 2), new KnapsackItem(30, 4) };
    final Variable[] tmpVariables = new Variable[tmpItems.length];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("Var" + String.valueOf(i));
        tmpVariables[i].lower(ZERO).upper(ONE).weight(tmpItems[i].value).integer(true);
    }
    final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpTotalWeightExpr = retVal.addExpression("Total Weight");
    for (int i = 0; i < tmpItems.length; i++) {
        tmpTotalWeightExpr.set(i, tmpItems[i].weight);
    }
    tmpTotalWeightExpr.lower(ZERO).upper(THREE);
    retVal.setMaximisation();
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Aggregations

Expression (org.ojalgo.optimisation.Expression)49 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)45 Variable (org.ojalgo.optimisation.Variable)44 Result (org.ojalgo.optimisation.Optimisation.Result)27 Test (org.junit.jupiter.api.Test)23 BigDecimal (java.math.BigDecimal)18 Optimisation (org.ojalgo.optimisation.Optimisation)18 ArrayList (java.util.ArrayList)9 BasicMatrix (org.ojalgo.matrix.BasicMatrix)9 NumberContext (org.ojalgo.type.context.NumberContext)9 List (java.util.List)5 Map (java.util.Map)5 Collectors (java.util.stream.Collectors)5 HashMap (java.util.HashMap)4 LinkedHashMap (java.util.LinkedHashMap)4 BasicLogger (org.ojalgo.netio.BasicLogger)4 IntIndex (org.ojalgo.access.Structure1D.IntIndex)3 BigArray (org.ojalgo.array.BigArray)3 BigDenseStore (org.ojalgo.matrix.store.BigDenseStore)3 File (java.io.File)1