Search in sources :

Example 31 with ExpressionsBasedModel

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

Example 32 with ExpressionsBasedModel

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

the class LinearProblems method testMath286.

@Test
public void testMath286() {
    final Variable tmpX1 = new Variable("X1").weight(TENTH.multiply(EIGHT)).lower(TEN);
    final Variable tmpX2 = new Variable("X2").weight(TENTH.multiply(TWO)).lower(ZERO);
    final Variable tmpX3 = new Variable("X3").weight(TENTH.multiply(SEVEN)).lower(EIGHT);
    final Variable tmpX4 = new Variable("X4").weight(TENTH.multiply(THREE)).lower(ZERO);
    final Variable tmpX5 = new Variable("X5").weight(TENTH.multiply(SIX)).lower(FIVE);
    final Variable tmpX6 = new Variable("X6").weight(TENTH.multiply(FOUR)).lower(ZERO);
    final Variable[] tmpFullVars = new Variable[] { tmpX1.copy(), tmpX2.copy(), tmpX3.copy(), tmpX4.copy(), tmpX5.copy(), tmpX6.copy() };
    final Variable[] tmpOddVars = new Variable[] { tmpX1.copy(), tmpX3.copy(), tmpX5.copy() };
    final Variable[] tmpEvenVars = new Variable[] { tmpX2.copy(), tmpX4.copy(), tmpX6.copy() };
    final ExpressionsBasedModel tmpFullModel = new ExpressionsBasedModel(tmpFullVars);
    // tmpFullModel.setMaximisation();
    final ExpressionsBasedModel tmpOddModel = new ExpressionsBasedModel(tmpOddVars);
    // tmpOddModel.setMaximisation();
    final ExpressionsBasedModel tmpEvenModel = new ExpressionsBasedModel(tmpEvenVars);
    // tmpEvenModel.setMaximisation();
    // tmpFullModel.options.debug(LinearSolver.class);
    // tmpOddModel.options.debug(LinearSolver.class);
    // tmpEvenModel.options.debug(LinearSolver.class);
    final BigDecimal tmpRHS = new BigDecimal("23.0");
    final int tmpLength = tmpFullModel.countVariables();
    final Expression retVal = tmpFullModel.addExpression("C1");
    for (int i = 0; i < tmpLength; i++) {
        retVal.set(i, new BigDecimal[] { ONE, ZERO, ONE, ZERO, ONE, ZERO }[i]);
    }
    final Expression tmpAddWeightExpression = retVal;
    tmpAddWeightExpression.level(tmpRHS);
    final int tmpLength1 = tmpOddModel.countVariables();
    final Expression retVal1 = tmpOddModel.addExpression("C1");
    for (int i = 0; i < tmpLength1; i++) {
        retVal1.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression2 = retVal1;
    tmpAddWeightExpression2.level(tmpRHS);
    final int tmpLength2 = tmpFullModel.countVariables();
    final Expression retVal2 = tmpFullModel.addExpression("C2");
    for (int i = 0; i < tmpLength2; i++) {
        retVal2.set(i, new BigDecimal[] { ZERO, ONE, ZERO, ONE, ZERO, ONE }[i]);
    }
    final Expression tmpAddWeightExpression3 = retVal2;
    tmpAddWeightExpression3.level(tmpRHS);
    final int tmpLength3 = tmpEvenModel.countVariables();
    final Expression retVal3 = tmpEvenModel.addExpression("C2");
    for (int i = 0; i < tmpLength3; i++) {
        retVal3.set(i, new BigDecimal[] { ONE, ONE, ONE }[i]);
    }
    final Expression tmpAddWeightExpression4 = retVal3;
    tmpAddWeightExpression4.level(tmpRHS);
    final Expression tmpFullObjective = tmpFullModel.objective();
    final Expression tmpOddObjective = tmpOddModel.objective();
    final Expression tmpEvenObjective = tmpEvenModel.objective();
    // A valid solution of 25.8 can be produced with:
    // X1=10, X2=0, X3=8, X4=0, X5=5, X6=23
    final BigDecimal tmpClaimedValue = new BigDecimal("25.8");
    final Builder<PrimitiveMatrix> tmpBuilder = PrimitiveMatrix.FACTORY.getBuilder(6, 1);
    tmpBuilder.set(0, 0, 10);
    tmpBuilder.set(2, 0, 8);
    tmpBuilder.set(4, 0, 5);
    tmpBuilder.set(5, 0, 23);
    final BasicMatrix tmpFullSolution = tmpBuilder.build();
    final BasicMatrix tmpOddSolution = tmpFullSolution.selectRows(0, 2, 4);
    final BasicMatrix tmpEvenSolution = tmpFullSolution.selectRows(1, 3, 5);
    TestUtils.assertEquals("Claimed solution not valid!", true, tmpFullModel.validate(BigDenseStore.FACTORY.copy(tmpFullSolution), new NumberContext(7, 6)));
    final Double tmpActualValue = tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(tmpFullSolution));
    // final BigDecimal tmpActualValue = TypeUtils.toBigDecimal(tmpObjectiveValue);
    // JUnitUtils.assertEquals("Claimed objective value wrong!", 0, tmpClaimedValue.compareTo(tmpActualValue));
    TestUtils.assertEquals(tmpClaimedValue, tmpActualValue, new NumberContext(7, 6));
    // Start validating ojAlgo results
    final Optimisation.Result tmpEvenResult = tmpEvenModel.maximise();
    final Optimisation.Result tmpOddResult = tmpOddModel.maximise();
    final Optimisation.Result tmpFullResult = tmpFullModel.maximise();
    TestUtils.assertEquals(true, tmpEvenModel.validate(tmpEvenResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpOddModel.validate(tmpOddResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(true, tmpFullModel.validate(tmpFullResult, new NumberContext(7, 6)));
    TestUtils.assertEquals(tmpEvenSolution, RationalMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpOddSolution, RationalMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2), new NumberContext(7, 6));
    TestUtils.assertEquals(tmpFullSolution, RationalMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5), new NumberContext(7, 6));
    final BigDecimal tmpEvenValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpEvenObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpEvenResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpOddValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpOddObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpOddResult).selectRows(0, 1, 2)))));
    final BigDecimal tmpFullValue = new NumberContext(7, 6).enforce(TypeUtils.toBigDecimal(tmpFullObjective.toFunction().invoke(PrimitiveDenseStore.FACTORY.copy(PrimitiveMatrix.FACTORY.columns(tmpFullResult).selectRows(0, 1, 2, 3, 4, 5)))));
    TestUtils.assertEquals(0, tmpFullValue.compareTo(tmpEvenValue.add(tmpOddValue)));
    TestUtils.assertEquals(0, tmpClaimedValue.compareTo(tmpFullValue));
}
Also used : Variable(org.ojalgo.optimisation.Variable) Optimisation(org.ojalgo.optimisation.Optimisation) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) BasicMatrix(org.ojalgo.matrix.BasicMatrix) Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 33 with ExpressionsBasedModel

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

the class LinearProblems method testP20100412.

/**
 * Didn't recognise this as an infeasible problem.
 */
@Test
public void testP20100412() {
    final ExpressionsBasedModel tmpModel = OptimisationIntegerData.buildModelForP20100412().relax(true);
    // tmpModel.relax(); // Relax the integer constraints
    // Set branch state
    tmpModel.getVariable(1).lower(ONE);
    final State tmpResultState = tmpModel.maximise().getState();
    TestUtils.assertFalse("Should be INFEASIBLE", tmpResultState == State.FEASIBLE);
}
Also used : State(org.ojalgo.optimisation.Optimisation.State) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 34 with ExpressionsBasedModel

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

the class LinearProblems method testP20180310_61.

/**
 * https://github.com/optimatika/ojAlgo/issues/61
 */
@Test
public void testP20180310_61() {
    final Variable x = Variable.make("x").lower(0);
    final Variable y = Variable.make("y").lower(0);
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    model.addVariable(x);
    model.addVariable(y);
    model.addExpression("first").set(x, 2).set(y, 3).upper(1);
    model.addExpression("second").set(x, -2).set(y, 3).lower(1);
    final BigArray expected = BigArray.wrap(BigMath.ZERO, 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 35 with ExpressionsBasedModel

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

the class NetlibCase method testBoeing2.

/**
 * OK! 2010-04-19 lp_solve => -315.01872802
 */
@Test
public void testBoeing2() {
    final File tmpFile = new File(PATH + "boeing2.mps");
    final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
    final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
    this.assertMinMaxVal(tmpModel, new BigDecimal("-3.1501872802E+02"), null);
}
Also used : MathProgSysModel(org.ojalgo.optimisation.MathProgSysModel) File(java.io.File) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) 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