Search in sources :

Example 56 with Variable

use of org.ojalgo.optimisation.Variable 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 57 with Variable

use of org.ojalgo.optimisation.Variable 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 58 with Variable

use of org.ojalgo.optimisation.Variable 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)

Example 59 with Variable

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

the class LinearProblems method testP20180311_66.

/**
 * https://github.com/optimatika/ojAlgo/issues/66
 */
@Test
public void testP20180311_66() {
    final Variable x = Variable.make("x").lower(0).weight(2);
    final Variable y = Variable.make("y").lower(0).weight(-1);
    final Variable z = Variable.make("z").lower(0).weight(4);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addVariable(z);
    model.addExpression().set(x, 3).set(y, 2).set(z, 2).upper(0);
    model.addExpression().set(x, 0).set(y, 3).set(z, -2).lower(2);
    final Optimisation.Result result = model.maximise();
    TestUtils.assertEquals(Optimisation.State.INFEASIBLE, result.getState());
    TestUtils.assertFalse(model.validate(result));
}
Also used : 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 60 with Variable

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

the class LinearProblems method testP20180312_69.

/**
 * https://github.com/optimatika/ojAlgo/issues/69
 */
@Test
public void testP20180312_69() {
    final Variable x = Variable.make("x").lower(0).weight(3);
    final Variable y = Variable.make("y").lower(0).weight(2);
    final Variable z = Variable.make("z").lower(0).weight(-2);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addVariable(z);
    model.addExpression().set(x, 0).set(y, 1).set(z, -2).level(4);
    model.addExpression().set(x, 0).set(y, 4).set(z, -2).upper(1);
    // ExpressionsBasedModel.clearPresolvers();
    final Optimisation.Result result = model.maximise();
    TestUtils.assertFalse(model.validate(result));
    TestUtils.assertEquals(Optimisation.State.INFEASIBLE, result.getState());
}
Also used : 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

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