Search in sources :

Example 21 with Variable

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

the class LinearDesignTestCases method test6LinearModelCase.

/**
 * Simplest possible unbounded model
 */
@Test
public void test6LinearModelCase() {
    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(THREE) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Optimisation.Result tmpResult = tmpModel.maximise();
    TestUtils.assertEquals(State.UNBOUNDED, tmpResult.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)

Example 22 with Variable

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

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

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

the class LinearProblems method testP20180310_62.

/**
 * https://github.com/optimatika/ojAlgo/issues/62
 */
@Test
public void testP20180310_62() {
    final Variable x = Variable.make("x").lower(0).weight(1);
    final Variable y = Variable.make("y").lower(0).weight(0);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addExpression("first").set(x, 0).set(y, 1).lower(1);
    model.addExpression("second").set(x, 0).set(y, 1).upper(-1);
    TestUtils.assertEquals(Optimisation.State.INFEASIBLE, model.maximise().getState());
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 25 with Variable

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

the class LinearProblems method testP20180314_70.

/**
 * https://github.com/optimatika/ojAlgo/issues/70
 */
@Test
public void testP20180314_70() {
    Variable x = Variable.make("x").lower(0).weight(-2);
    Variable y = Variable.make("y").lower(0).weight(-2);
    ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addExpression().set(x, 3).set(y, 0).lower(2);
    model.addExpression().set(x, 1).set(y, 2).lower(-5);
    model.addExpression().set(x, 3).set(y, 1).upper(2);
    final BigArray expected = BigArray.wrap(BigMath.TWO.multiply(BigMath.THIRD), BigMath.ZERO);
    TestUtils.assertTrue(model.validate(expected));
    Optimisation.Result solution = model.maximise();
    TestUtils.assertTrue(model.validate(solution));
    TestUtils.assertStateNotLessThanOptimal(solution);
}
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

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