Search in sources :

Example 41 with Expression

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

the class LinearProblems method testP20111010.

/**
 * Depending on how the constraints were constructed the solver could fail to solve and report the problem
 * to be unbounded.
 */
@Test
public void testP20111010() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X").lower(ZERO).weight(ONE), new Variable("Y").lower(ZERO).weight(ZERO), new Variable("Z").lower(ZERO).weight(ZERO) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExprC1 = tmpModel.addExpression("C1");
    tmpExprC1.level(ZERO);
    tmpExprC1.set(0, ONE);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    tmpExprC2.level(ZERO);
    tmpExprC2.set(0, ONE);
    tmpExprC2.set(1, NEG);
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    tmpExprC3.level(ZERO);
    tmpExprC3.set(0, ONE);
    tmpExprC3.set(2, NEG);
    final BasicMatrix tmpExpectedSolution = PrimitiveMatrix.FACTORY.makeZero(3, 1);
    final Optimisation.Result tmpResult11 = tmpModel.minimise();
    // TestUtils.assertEquals(tmpExpectedState, tmpResult11.getState());
    TestUtils.assertStateNotLessThanOptimal(tmpResult11);
    TestUtils.assertEquals(tmpExpectedSolution, RationalMatrix.FACTORY.columns(tmpResult11));
    tmpExprC2.set(0, NEG);
    tmpExprC2.set(1, ONE);
    tmpExprC3.set(0, ONE);
    tmpExprC3.set(2, NEG);
    final Optimisation.Result tmpResultN1 = tmpModel.minimise();
    // TestUtils.assertEquals(tmpExpectedState, tmpResultN1.getState());
    TestUtils.assertStateNotLessThanOptimal(tmpResultN1);
    TestUtils.assertEquals(tmpExpectedSolution, RationalMatrix.FACTORY.columns(tmpResultN1));
    tmpExprC2.set(0, ONE);
    tmpExprC2.set(1, NEG);
    tmpExprC3.set(0, NEG);
    tmpExprC3.set(2, ONE);
    final Optimisation.Result tmpResult1N = tmpModel.minimise();
    // TestUtils.assertEquals(tmpExpectedState, tmpResult1N.getState());
    TestUtils.assertStateNotLessThanOptimal(tmpResult1N);
    TestUtils.assertEquals(tmpExpectedSolution, RationalMatrix.FACTORY.columns(tmpResult1N));
    tmpExprC2.set(0, NEG);
    tmpExprC2.set(1, ONE);
    tmpExprC3.set(0, NEG);
    tmpExprC3.set(2, ONE);
    final Optimisation.Result tmpResultNN = tmpModel.minimise();
    // TestUtils.assertEquals(tmpExpectedState, tmpResultNN.getState());
    TestUtils.assertStateNotLessThanOptimal(tmpResultNN);
    TestUtils.assertEquals(tmpExpectedSolution, RationalMatrix.FACTORY.columns(tmpResultNN));
}
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) Test(org.junit.jupiter.api.Test)

Example 42 with Expression

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

the class MarketShareCase method testRedundant.

private void testRedundant(final String constraint) {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    final Expression tmpExpression = tmpModel.getExpression(constraint);
    if (DEBUG) {
        BasicLogger.debug("Fix count: {}", tmpExpression.getLinearKeySet().size());
    }
    for (final IntIndex tmpIndex : tmpExpression.getLinearKeySet()) {
        final Variable tmpVariable = tmpModel.getVariable(tmpIndex.index);
        final String tmpName = tmpVariable.getName();
        tmpVariable.level(SOLUTION.get(tmpName));
    }
    final Result tmpResult = tmpModel.minimise();
    final NumberContext tmpContext = new NumberContext(8, 13);
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), tmpContext);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), tmpContext);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) IntIndex(org.ojalgo.access.Structure1D.IntIndex) Result(org.ojalgo.optimisation.Optimisation.Result)

Example 43 with Expression

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

the class P20160701 method main.

public static void main(final String[] arg) {
    final int n = 6;
    final double[][] c = new double[n][n];
    c[0][0] = 1.7976931348623157E308;
    c[0][1] = 141.4213562373095;
    c[0][2] = 223.60679774997897;
    c[0][3] = 223.60679774997897;
    c[0][4] = 141.4213562373095;
    c[0][5] = 156.63604262201076;
    c[1][0] = 141.4213562373095;
    c[1][1] = 1.7976931348623157E308;
    c[1][2] = 100.0;
    c[1][3] = 223.60679774997897;
    c[1][4] = 200.0;
    c[1][5] = 219.25609608009617;
    c[2][0] = 223.60679774997897;
    c[2][1] = 100.0;
    c[2][2] = 1.7976931348623157E308;
    c[2][3] = 200.0;
    c[2][4] = 223.60679774997897;
    c[2][5] = 319.2543607976003;
    c[3][0] = 223.60679774997897;
    c[3][1] = 223.60679774997897;
    c[3][2] = 200.0;
    c[3][3] = 1.7976931348623157E308;
    c[3][4] = 100.0;
    c[3][5] = 377.5537017276938;
    c[4][0] = 141.4213562373095;
    c[4][1] = 200.0;
    c[4][2] = 223.60679774997897;
    c[4][3] = 100.0;
    c[4][4] = 1.7976931348623157E308;
    c[4][5] = 297.81988930943544;
    c[5][0] = 156.63604262201076;
    c[5][1] = 219.25609608009617;
    c[5][2] = 319.2543607976003;
    c[5][3] = 377.5537017276938;
    c[5][4] = 297.81988930943544;
    c[5][5] = 1.7976931348623157E308;
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    // DECISION VARIABLES
    final Variable[][] x = new Variable[n][n];
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            x[i][j] = Variable.make("x" + i + "_" + j).binary().weight(c[i][j]);
            model.addVariable(x[i][j]);
        }
    }
    final Variable[] u = new Variable[n];
    for (int i = 1; i < n; i++) {
        u[i] = new Variable("u" + i);
        model.addVariable(u[i]);
    }
    // sum(j in cities : i!=j) x[i][j]==1;
    for (int i = 0; i < n; i++) {
        final Expression constraint_line = model.addExpression("constraint_line" + i).lower(1).upper(1);
        for (int j = 0; j < n; j++) {
            if (i != j) {
                constraint_line.set(x[i][j], 1);
            }
        }
    }
    // sum(i in cities : i!=j) x[i][j]==1;
    for (int j = 0; j < n; j++) {
        final Expression constraint_column = model.addExpression("constraint_column" + j).lower(1).upper(1);
        for (int i = 0; i < n; i++) {
            if (i != j) {
                constraint_column.set(x[i][j], 1);
            }
        }
    }
    // u[i]-u[j]+n*x[i][j] <= n-1;
    for (int i = 1; i < n; i++) {
        for (int j = 1; j < n; j++) {
            if (i != j) {
                final Expression constraint_subroute = model.addExpression("constraint_subroute" + i + "_" + j).upper(n - 1);
                constraint_subroute.set(u[i], 1);
                constraint_subroute.set(u[j], -1);
                constraint_subroute.set(x[i][j], n);
            }
        }
    }
    final Optimisation.Result result = model.minimise();
    if (OptimisationIntegerTests.DEBUG) {
        System.out.print("u=\n\t  ");
        for (int i = 1; i < n; i++) {
            System.out.print(u[i].getValue().intValue() + " ");
        }
        System.out.print("\nx=\n\t");
        for (int i = 0; i < n; i++) {
            System.out.print(i + " ");
        }
        System.out.println();
        for (int i = 0; i < n; i++) {
            System.out.print(i + "\t");
            for (int j = 0; j < n; j++) {
                System.out.print(x[i][j].getValue().intValue() + " ");
            }
            System.out.println();
        }
        System.out.println("\nResult = " + result);
    }
    TestUtils.assertStateNotLessThanOptimal(result);
    TestUtils.assertEquals(917.3134949394164, result.getValue());
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Example 44 with Expression

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

the class SamplePerformanceIssueSolvingILP method solve.

private void solve(final long maxContainerSize, final List<Container> containers) {
    final ExpressionsBasedModel expressionsBasedModel = new ExpressionsBasedModel();
    expressionsBasedModel.options.time_suffice = 60 * 1000;
    expressionsBasedModel.options.iterations_suffice = 64;
    expressionsBasedModel.options.mip_gap = 0.01;
    final double firstContainerSize = maxContainerSize * FIRST_CONTAINER_SIZE_PERC;
    final Expression firstContainerExpression = expressionsBasedModel.addExpression("expression:first").lower(firstContainerSize * (1 - DEVIATION)).upper(firstContainerSize * (1 + DEVIATION));
    final double secondContainerSize = maxContainerSize * SECOND_CONTAINER_SIZE_PERC;
    final Expression secondContainerExpression = expressionsBasedModel.addExpression("expression:second").lower(secondContainerSize * (1 - DEVIATION)).upper(secondContainerSize * (1 + DEVIATION));
    final double thridContainerSize = maxContainerSize * THIRD_CONTAINER_SIZE_PERC;
    final Expression thirdContainerExpression = expressionsBasedModel.addExpression("expression:third").lower(thridContainerSize * (1 - DEVIATION)).upper(thridContainerSize * (1 + DEVIATION));
    for (int cIndex = 0; cIndex < containers.size(); cIndex++) {
        final Container container = containers.get(cIndex);
        final Variable variable = Variable.make("v" + cIndex).weight(RANDOM.nextInt(MAX_OBJECTIVE_VALUE) * container.getSize()).binary();
        expressionsBasedModel.addVariable(variable);
        if (container.getType().equals("first")) {
            firstContainerExpression.set(variable, container.getSize());
        }
        if (container.getType().equals("second")) {
            secondContainerExpression.set(variable, container.getSize());
        }
        if (container.getType().equals("third")) {
            thirdContainerExpression.set(variable, container.getSize());
        }
    }
    // expressionsBasedModel.options.debug(IntegerSolver.class);
    // expressionsBasedModel.relax(true);
    final Optimisation.Result result = expressionsBasedModel.maximise();
    System.out.println("RESULT: " + result);
    expressionsBasedModel.validate();
    expressionsBasedModel.validate(result);
    final List<Container> resultContainer = new ArrayList<>();
    for (int cIndex = 0; cIndex < containers.size(); cIndex++) {
        final BigDecimal bigDecimal = result.get(cIndex);
        if (bigDecimal.compareTo(BigDecimal.ZERO) > 0) {
            resultContainer.add(containers.get(cIndex));
        }
    }
    System.out.println("RESULT: " + resultContainer.size());
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) Optimisation(org.ojalgo.optimisation.Optimisation) ArrayList(java.util.ArrayList) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 45 with Expression

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

the class DesignCase method testSOS.

/**
 * Essentilly this test case just verifies that the SOS presolver doesn't screw things up.
 */
@Test
public void testSOS() {
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    final List<Variable> starts1 = new ArrayList<>();
    final List<Variable> works1 = new ArrayList<>();
    final List<Variable> starts2 = new ArrayList<>();
    final List<Variable> works2 = new ArrayList<>();
    final Set<Variable> orderedSet1 = new HashSet<>();
    final Set<Variable> orderedSet2 = new HashSet<>();
    for (int h = 0; h < 24; h++) {
        final Variable start1 = model.addVariable("Start activity A at " + h).binary();
        starts1.add(start1);
        final Variable start2 = model.addVariable("Start activity B at " + h).binary();
        starts2.add(start2);
        final Variable work1 = model.addVariable("Activity A ongoing at " + h).binary().weight(Math.random());
        works1.add(work1);
        orderedSet1.add(work1);
        final Variable work2 = model.addVariable("Activity B ongoing at " + h).binary().weight(Math.random());
        works2.add(work2);
        orderedSet2.add(work2);
        model.addExpression("Maximum one ongoing activity at " + h).upper(1).set(work1, 1).set(work2, 1);
    }
    model.addSpecialOrderedSet(orderedSet1, 3, 3);
    model.addSpecialOrderedSet(orderedSet2, 3, 3);
    for (int h = 0; h < 21; h++) {
        final Expression expr1 = model.addExpression("Finish A when started at " + h);
        expr1.upper(0);
        expr1.set(starts1.get(h), 3);
        expr1.set(works1.get(h), -1);
        expr1.set(works1.get(h + 1), -1);
        expr1.set(works1.get(h + 2), -1);
        final Expression expr2 = model.addExpression("Finish B when started at " + h);
        expr2.upper(0);
        expr2.set(starts2.get(h), 3);
        expr2.set(works2.get(h), -1);
        expr2.set(works2.get(h + 1), -1);
        expr2.set(works2.get(h + 2), -1);
    }
    for (int h = 21; h < 24; h++) {
        starts1.get(h).level(0);
        starts2.get(h).level(0);
    }
    model.addExpression("Only start activity A once").level(1).setLinearFactorsSimple(starts1);
    model.addExpression("Only start activity B once").level(1).setLinearFactorsSimple(starts2);
    final Result resultMin = model.minimise();
    TestUtils.assertStateNotLessThanOptimal(resultMin);
    TestUtils.assertTrue(resultMin.getValue() >= 0.0);
    TestUtils.assertTrue(resultMin.getValue() <= 6.0);
    final Result resultMax = model.maximise();
    TestUtils.assertStateNotLessThanOptimal(resultMax);
    TestUtils.assertTrue(resultMax.getValue() >= 0.0);
    TestUtils.assertTrue(resultMax.getValue() <= 6.0);
    TestUtils.assertTrue(resultMin.getValue() <= resultMax.getValue());
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ArrayList(java.util.ArrayList) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) HashSet(java.util.HashSet) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

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