Search in sources :

Example 36 with Expression

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

the class LinearDesignTestCases method test1LinearModelCase.

/**
 * http://math.uww.edu/~mcfarlat/s-prob.htm
 */
@Test
public void test1LinearModelCase() {
    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.negate()) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression retExprC1 = tmpModel.addExpression("C1");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        retExprC1.set(i, new BigDecimal[] { TWO, ONE, ONE }[i]);
    }
    retExprC1.upper(new BigDecimal("14.0"));
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { FOUR, TWO, THREE }[i]);
    }
    tmpExprC2.upper(new BigDecimal("28.0"));
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { TWO, FIVE, FIVE }[i]);
    }
    tmpExprC3.upper(new BigDecimal("30.0"));
    final Optimisation.Result tmpResult = tmpModel.maximise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 5.0 }, { 4.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 37 with Expression

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

the class LinearDesignTestCases method test7LinearModelCase.

/**
 * http://math.uww.edu/~mcfarlat/ns-prob.htm
 */
@Test
public void test7LinearModelCase() {
    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[] { ONE, ONE }[i]);
    }
    tmpExprC1.upper(TEN);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ONE, TWO }[i]);
    }
    tmpExprC2.lower(TWELVE);
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { TWO, ONE }[i]);
    }
    tmpExprC3.lower(TWELVE);
    final Optimisation.Result tmpResult = tmpModel.minimise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 4.0 }, { 4.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 38 with Expression

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

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

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

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