Search in sources :

Example 16 with Variable

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

the class BurkardtDatasetsMps method testMPStestprob.

/**
 * A simple problem with 4 rows and 3 variables. I got my version from here (same numbers but different
 * names): <a href="http://en.wikipedia.org/wiki/MPS_(format)">testprob@wikipedia</a> and/or
 * <a href="http://lpsolve.sourceforge.net/5.5/mps-format.htm">testprob@lpsolve</a>
 */
@Test
public void testMPStestprob() {
    final Variable tmpXONE = new Variable("XONE").weight(ONE).lower(ZERO).upper(FOUR);
    final Variable tmpYTWO = new Variable("YTWO").weight(FOUR).lower(NEG).upper(ONE);
    final Variable tmpZTHREE = new Variable("ZTHREE").weight(NINE).lower(ZERO).upper(null);
    final Variable[] tmpVariables = new Variable[] { tmpXONE, tmpYTWO, tmpZTHREE };
    final ExpressionsBasedModel tmpExpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpLIM1 = tmpExpModel.addExpression("LIM1");
    for (int v = 0; v < tmpVariables.length; v++) {
        tmpLIM1.set(v, new BigDecimal[] { ONE, ONE, ZERO }[v]);
    }
    tmpLIM1.upper(FIVE);
    final Expression tmpLIM2 = tmpExpModel.addExpression("LIM2");
    for (int v = 0; v < tmpVariables.length; v++) {
        tmpLIM2.set(v, new BigDecimal[] { ONE, ZERO, ONE }[v]);
    }
    tmpLIM2.lower(TEN);
    final Expression tmpMYEQN = tmpExpModel.addExpression("MYEQN");
    for (int v = 0; v < tmpVariables.length; v++) {
        tmpMYEQN.set(v, new BigDecimal[] { ZERO, ONE.negate(), ONE }[v]);
    }
    tmpMYEQN.level(SEVEN);
    TestUtils.assertTrue(tmpExpModel.validate());
    final File tmpFile = new File(PATH + "testprob.mps");
    final MathProgSysModel tmpActModel = MathProgSysModel.make(tmpFile);
    TestUtils.assertTrue(tmpActModel.validate());
    final Result tmpExpMinRes = tmpExpModel.minimise();
    final Result tmpActMinRes = tmpActModel.minimise();
    TestUtils.assertEquals(tmpExpMinRes.getValue(), tmpActMinRes.getValue(), PRECISION);
    TestUtils.assertEquals(tmpVariables.length, tmpExpMinRes.count());
    TestUtils.assertEquals(tmpVariables.length, tmpActMinRes.count());
    TestUtils.assertEquals(tmpExpMinRes, tmpActMinRes, PRECISION);
    for (int i = 0; i < tmpVariables.length; i++) {
        TestUtils.assertEquals(tmpVariables[i].getName(), tmpExpMinRes.doubleValue(i), tmpActMinRes.doubleValue(i), PRECISION);
    }
    if (!tmpExpModel.validate(tmpExpMinRes, PRECISION)) {
        TestUtils.fail(SOLUTION_NOT_VALID);
    }
    if (!tmpActModel.validate(tmpActMinRes, PRECISION)) {
        TestUtils.fail(SOLUTION_NOT_VALID);
    }
    this.assertMinMaxVal(tmpActModel.getExpressionsBasedModel(), new BigDecimal("54"), new BigDecimal("80"));
}
Also used : Variable(org.ojalgo.optimisation.Variable) MathProgSysModel(org.ojalgo.optimisation.MathProgSysModel) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) File(java.io.File) BigDecimal(java.math.BigDecimal) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 17 with Variable

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

the class ComPictetPamBamTest method setupModel.

void setupModel() {
    // 
    // variables
    // 
    vars = new Variable[numberOfVars];
    vars[0] = new Variable("x0").lower(BigMath.ZERO).upper(BigMath.HUNDRED);
    vars[1] = new Variable("x1").lower(BigMath.ZERO).upper(BigMath.HUNDRED);
    // 
    // model
    // 
    linearModel = new ExpressionsBasedModel(vars);
    {
        // 
        // x0 = 2*x1, i.e. x0 - 2*x1 = 0
        // 
        final Expression e = linearModel.addExpression("x0 = 2*x1");
        e.set(0, BigMath.ONE);
        e.set(1, BigMath.TWO.negate());
        e.level(BigMath.ZERO);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Example 18 with Variable

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

the class LinearDesignTestCases method buildOldKnapsackTestModel.

private ExpressionsBasedModel buildOldKnapsackTestModel() {
    Variable tmpVar;
    final Variable[] tmpVariables = new Variable[8];
    tmpVar = new Variable("QRHEDGE");
    tmpVar.weight(BigMath.ZERO);
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ZERO);
    tmpVariables[0] = tmpVar;
    tmpVar = new Variable("QKORT");
    tmpVar.weight(new BigDecimal("0.0345"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ONE);
    tmpVariables[1] = tmpVar;
    tmpVar = new Variable("QHEDGE");
    tmpVar.weight(new BigDecimal("0.04"));
    tmpVar.lower(new BigDecimal("0.1846"));
    tmpVar.upper(new BigDecimal("0.1846"));
    tmpVariables[2] = tmpVar;
    tmpVar = new Variable("QLÅNG");
    tmpVar.weight(new BigDecimal("0.0412"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ONE);
    tmpVariables[3] = tmpVar;
    tmpVar = new Variable("QFF");
    tmpVar.weight(new BigDecimal("0.069575"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ZERO);
    tmpVariables[4] = tmpVar;
    tmpVar = new Variable("QGLOBAL");
    tmpVar.weight(new BigDecimal("0.0738"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ONE);
    tmpVariables[5] = tmpVar;
    tmpVar = new Variable("QSVERIGE");
    tmpVar.weight(new BigDecimal("0.1288"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ONE);
    tmpVariables[6] = tmpVar;
    tmpVar = new Variable("QFF2");
    tmpVar.weight(new BigDecimal("2.3"));
    tmpVar.lower(BigMath.ZERO);
    tmpVar.upper(BigMath.ZERO);
    tmpVariables[7] = tmpVar;
    final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
    final int tmpLength = retVal.countVariables();
    final Expression retVal2 = retVal.addExpression("100%");
    for (int i = 0; i < tmpLength; i++) {
        retVal2.set(i, ONE);
    }
    final Expression retVal1 = retVal2;
    retVal1.lower(BigMath.ONE);
    retVal1.upper(BigMath.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 19 with Variable

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

the class LinearDesignTestCases method test3LinearModelCase.

/**
 * http://optlab-server.sce.carleton.ca/POAnimations/TwoPhase.aspx
 */
@Test
public void test3LinearModelCase() {
    final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(TEN.add(FIVE)), new Variable("X2").lower(ZERO).weight(TEN) };
    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, ZERO }[i]);
    }
    tmpExprC1.upper(TWO);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ZERO, ONE }[i]);
    }
    tmpExprC2.upper(THREE);
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { ONE, ONE }[i]);
    }
    tmpExprC3.level(FOUR);
    final Optimisation.Result tmpResult = tmpModel.maximise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 2.0 }, { 2.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 20 with Variable

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

the class LinearDesignTestCases method test5LinearModelCase.

/**
 * http://www.maths.bris.ac.uk/~maxmr/opt/sm2.pdf
 */
@Test
public void test5LinearModelCase() {
    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[] { HALF, QUARTER }[i]);
    }
    tmpExprC1.upper(FOUR);
    final Expression tmpExprC2 = tmpModel.addExpression("C2");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC2.set(i, new BigDecimal[] { ONE, THREE }[i]);
    }
    tmpExprC2.lower(TEN.add(TEN));
    final Expression tmpExprC3 = tmpModel.addExpression("C3");
    for (int i = 0; i < tmpModel.countVariables(); i++) {
        tmpExprC3.set(i, new BigDecimal[] { ONE, ONE }[i]);
    }
    tmpExprC3.level(TEN);
    final Optimisation.Result tmpResult = tmpModel.minimise();
    final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
    final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 5.0 }, { 5.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)

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