Search in sources :

Example 56 with ExpressionsBasedModel

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

the class KnapsackTest method testVaryingMaxWeight3.

@Test
public void testVaryingMaxWeight3() {
    ExpressionsBasedModel model = new KnapsackProblemBuilder(10d).addItem(20, 2).addItem(30, 4).build();
    model.maximise();
    // Expected: both
    this.assertOne(model.getVariables().get(0));
    this.assertOne(model.getVariables().get(1));
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 57 with ExpressionsBasedModel

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

the class KnapsackTest method testVaryingMaxWeight2.

@Test
public void testVaryingMaxWeight2() {
    ExpressionsBasedModel model = new KnapsackProblemBuilder(0d).addItem(20, 2).addItem(30, 4).build();
    model.maximise();
    // Expected: nothing
    this.assertZero(model.getVariables().get(0));
    this.assertZero(model.getVariables().get(1));
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 58 with ExpressionsBasedModel

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

the class KnapsackTest method testVaryingMaxWeight4.

@Test
public void testVaryingMaxWeight4() {
    ExpressionsBasedModel model = new KnapsackProblemBuilder(5d).addItem(20, 2).addItem(30, 4).build();
    // model.options.debug(IntegerSolver.class);
    model.maximise();
    // Expected: just second item
    this.assertOne(model.getVariables().get(1));
    this.assertZero(model.getVariables().get(0));
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Test(org.junit.jupiter.api.Test)

Example 59 with ExpressionsBasedModel

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

the class LpsolveSemiContCase method testSemiContNot.

@Test
@Disabled("Underscored before JUnit 5")
public void testSemiContNot() {
    final File tmpFile = new File(PATH + "lpsolve_sc_not.mps");
    final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
    final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
    tmpModel.minimise();
    TestUtils.assertTrue(tmpModel.validate());
    final BigDecimal tmpExpVal = new BigDecimal("3.93333");
    final double tmpActVal = tmpModel.minimise().getValue();
    if (!tmpModel.validate(PRECISION)) {
        TestUtils.fail(SOLUTION_NOT_VALID);
    }
    TestUtils.assertEquals(tmpExpVal.doubleValue(), tmpActVal, PRECISION);
}
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) Disabled(org.junit.jupiter.api.Disabled)

Example 60 with ExpressionsBasedModel

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

the class NewIntegerSolver method setup.

void setup() {
    normal = true;
    final NodeKey[] retVal = new NodeKey[2];
    final ExpressionsBasedModel tmpIntegerModel = NewIntegerSolver.this.getIntegerModel();
    final List<Variable> tmpIntegerVariables = tmpIntegerModel.getIntegerVariables();
    NodeKey myKey;
    myKey = new NodeKey(tmpIntegerModel);
    final ExpressionsBasedModel tmpRootModel = NewIntegerSolver.this.makeNodeModel(myKey);
    final Result tmpRootResult = tmpRootModel.solve(tmpIntegerModel.getVariableValues());
    final double tmpRootValue = tmpRootResult.getValue();
    double tmpMinValue = PrimitiveMath.MACHINE_LARGEST;
    double tmpMaxValue = -PrimitiveMath.MACHINE_LARGEST;
    final double tmpBestValue = tmpRootModel.isMinimisation() ? PrimitiveMath.MACHINE_LARGEST : -PrimitiveMath.MACHINE_LARGEST;
    final double[] tmpSignificance = new double[tmpIntegerVariables.size()];
    for (int i = 0; i < tmpIntegerVariables.size(); i++) {
        final int tmpGlobalIndex = NewIntegerSolver.this.getGlobalIndex(i);
        final double tmpVariableValue = tmpRootResult.doubleValue(tmpGlobalIndex);
        final NodeKey tmpLowerNodeKey = myKey.createLowerBranch(i, tmpVariableValue, tmpRootValue);
        final ExpressionsBasedModel tmpLowerModel = NewIntegerSolver.this.makeNodeModel(tmpLowerNodeKey);
        final Result tmpLowerResult = tmpLowerModel.solve(tmpRootResult);
        final double tmpLowerValue = tmpLowerResult.getValue();
        if (tmpLowerValue < tmpMinValue) {
            tmpMinValue = tmpLowerValue;
        }
        if (tmpLowerValue > tmpMaxValue) {
            tmpMaxValue = tmpLowerValue;
        }
        final NodeKey tmpUpperNodeKey = myKey.createUpperBranch(i, tmpVariableValue, tmpRootValue);
        final ExpressionsBasedModel tmpUpperModel = NewIntegerSolver.this.makeNodeModel(tmpUpperNodeKey);
        final Result tmpUpperResult = tmpUpperModel.solve(tmpRootResult);
        final double tmpUpperValue = tmpUpperResult.getValue();
        if (tmpUpperValue < tmpMinValue) {
            tmpMinValue = tmpUpperValue;
        }
        if (tmpUpperValue > tmpMaxValue) {
            tmpMaxValue = tmpUpperValue;
        }
        if (tmpLowerResult.getState().isFeasible() && tmpUpperResult.getState().isFeasible()) {
            if (tmpRootModel.isMinimisation() && ((tmpLowerValue < tmpBestValue) || (tmpUpperValue < tmpBestValue))) {
                retVal[0] = tmpLowerNodeKey;
                retVal[1] = tmpUpperNodeKey;
            } else if (tmpRootModel.isMaximisation() && ((tmpLowerValue > tmpBestValue) || (tmpUpperValue > tmpBestValue))) {
                retVal[0] = tmpLowerNodeKey;
                retVal[1] = tmpUpperNodeKey;
            }
        }
        if (!Double.isNaN(tmpUpperValue) && !Double.isNaN(tmpLowerValue)) {
            tmpSignificance[i] = PrimitiveFunction.ABS.invoke(tmpUpperValue - tmpLowerValue);
        }
    }
    double tmpScale = tmpMaxValue - tmpMinValue;
    final double value = tmpScale;
    if (PrimitiveScalar.isSmall(PrimitiveMath.ONE, value)) {
        tmpScale = PrimitiveMath.ONE;
    }
    for (int i = 0; i < tmpSignificance.length; i++) {
        final int index = i;
        NewIntegerSolver.this.addIntegerSignificance(NewIntegerSolver.this.getGlobalIndex(index), 0.5 + (tmpSignificance[i] / tmpScale));
    }
    if ((retVal[0] != null) && (retVal[1] != null)) {
        NewIntegerSolver.this.add(retVal[0]);
        NewIntegerSolver.this.add(retVal[1]);
    } else {
        NewIntegerSolver.this.add(new NodeKey(tmpIntegerModel));
    }
    final Future<Boolean> tmpFuture = DaemonPoolExecutor.invoke(new NodeWorker());
    try {
        normal = normal && tmpFuture.get();
    } catch (InterruptedException | ExecutionException anException) {
        normal &= false;
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) ExecutionException(java.util.concurrent.ExecutionException)

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