Search in sources :

Example 86 with ExpressionsBasedModel

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

the class LinearDesignTestCases method testInfeasibleCase.

@Test
public void testInfeasibleCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ONE).weight(ONE), new Variable("X2").lower(ONE).weight(TWO), new Variable("X3").lower(ONE).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, ONE);
    }
    tmpExprC1.upper(TWO);
    final Optimisation.Result tmpResult = tmpModel.maximise();
    TestUtils.assertFalse(tmpResult.getState().isFeasible());
}
Also used : 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 87 with ExpressionsBasedModel

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

the class LinearDesignTestCases method test8LinearModelCase.

/**
 * http://books.google.com/books?id=3iZznGJq4ZMC page 192
 */
@Test
public void test8LinearModelCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(ONE), new Variable("X2").lower(ZERO).weight(TWO), 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[] { THREE, ONE, NEG }[i]);
    }
    tmpExprC1.level(TEN.add(FIVE));
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { EIGHT, FOUR, NEG }[i]);
    }
    tmpExprC2.level(FIVE.multiply(TEN));
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { TWO, TWO, ONE }[i]);
    }
    tmpExprC3.level(TEN.add(TEN));
    final Optimisation.Result tmpResult = tmpModel.maximise();
    TestUtils.assertTrue(tmpResult.getState().isFeasible());
}
Also used : 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 88 with ExpressionsBasedModel

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

the class LinearDesignTestCases method testMinOldKnapsackTestModel.

@Test
public void testMinOldKnapsackTestModel() {
    final ExpressionsBasedModel tmpModel = this.buildOldKnapsackTestModel();
    final Optimisation.Result tmpResult = tmpModel.minimise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final MatrixStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.8154 }, { 0.1846 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 } });
    final MatrixStore<Double> tmpActX = PrimitiveDenseStore.FACTORY.copy(tmpSolution.selectRows(new int[] { 0, 1, 2, 3, 4, 5, 6, 7 }));
    TestUtils.assertEquals(tmpExpX, tmpActX);
}
Also used : BasicMatrix(org.ojalgo.matrix.BasicMatrix) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 89 with ExpressionsBasedModel

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

the class LinearDesignTestCases method testP20130409b.

/**
 * A specific node of {@linkplain org.ojalgo.optimisation.integer.IntegerProblems#testP20130409b}. Based
 * on some changes in ExpressionBasedModel and/or IntegerSolver some nodes started to fail as UNBOUNDED.
 * Which seems unreasonable. Must be a problem with either ExpressionBasedModel or LinearSolver. 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);
    x2013.lower(BigDecimal.valueOf(1245L));
    x2014.lower(BigDecimal.valueOf(1269L));
    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(LinearSolver.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 90 with ExpressionsBasedModel

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

the class LinearProblems method testP20180311_64.

/**
 * https://github.com/optimatika/ojAlgo/issues/64
 */
@Test
public void testP20180311_64() {
    final Variable x = Variable.make("x").lower(0).weight(3);
    final Variable y = Variable.make("y").lower(0).weight(-2);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addExpression().set(x, -1).set(y, 0).lower(0);
    model.addExpression().set(x, -1).set(y, 3).level(2);
    final BigArray expected = BigArray.wrap(BigMath.ZERO, BigMath.TWO.multiply(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)

Aggregations

ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)109 Test (org.junit.jupiter.api.Test)78 Variable (org.ojalgo.optimisation.Variable)59 Optimisation (org.ojalgo.optimisation.Optimisation)50 Result (org.ojalgo.optimisation.Optimisation.Result)49 Expression (org.ojalgo.optimisation.Expression)45 BigDecimal (java.math.BigDecimal)33 File (java.io.File)15 MathProgSysModel (org.ojalgo.optimisation.MathProgSysModel)15 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)13 BasicMatrix (org.ojalgo.matrix.BasicMatrix)11 NumberContext (org.ojalgo.type.context.NumberContext)10 ArrayList (java.util.ArrayList)9 BigArray (org.ojalgo.array.BigArray)6 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 Disabled (org.junit.jupiter.api.Disabled)4