Search in sources :

Example 51 with Variable

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

the class UCLAee236aCase method makeOriginalRootModel.

private static ExpressionsBasedModel makeOriginalRootModel() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(TWO.negate()).integer(true), new Variable("X2").lower(ZERO).weight(THREE.negate()).integer(true) };
    final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
    retVal.setMinimisation();
    final Expression tmpExprC1 = retVal.addExpression("C1");
    for (int i = 0; i < retVal.countVariables(); i++) {
        tmpExprC1.set(i, new BigDecimal[] { TWO.multiply(NINTH), QUARTER }[i]);
    }
    tmpExprC1.upper(ONE);
    final Expression tmpExprC2 = retVal.addExpression("C2");
    for (int i = 0; i < retVal.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { SEVENTH, THIRD }[i]);
    }
    tmpExprC2.upper(ONE);
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 52 with Variable

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

the class LinearDesignTestCases method testUnboundedCase.

@Test
public void testUnboundedCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").weight(ONE), new Variable("X2").weight(TWO), new Variable("X3").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.level(ONE);
    final Optimisation.Result tmpMinResult = tmpModel.maximise();
    TestUtils.assertTrue(tmpMinResult.getState().isFeasible());
    TestUtils.assertFalse(tmpMinResult.getState().isOptimal());
    TestUtils.assertTrue(tmpMinResult.getState().isFailure());
    TestUtils.assertTrue(tmpModel.validate(tmpMinResult));
    TestUtils.assertEquals(Optimisation.State.UNBOUNDED, tmpMinResult.getState());
    final Optimisation.Result tmpMaxResult = tmpModel.maximise();
    TestUtils.assertTrue(tmpMaxResult.getState().isFeasible());
    TestUtils.assertFalse(tmpMaxResult.getState().isOptimal());
    TestUtils.assertTrue(tmpMaxResult.getState().isFailure());
    TestUtils.assertTrue(tmpModel.validate(tmpMaxResult));
    TestUtils.assertEquals(Optimisation.State.UNBOUNDED, tmpMaxResult.getState());
}
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 53 with Variable

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

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

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

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