Search in sources :

Example 31 with Variable

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

the class MarketShareCase method testRelaxedButAllConstrainedToOptimal.

@Test
public void testRelaxedButAllConstrainedToOptimal() {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    tmpModel.relax(true);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        tmpVariable.level(SOLUTION.get(tmpVariable.getName()));
    }
    final Result tmpResult = tmpModel.minimise();
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 32 with Variable

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

the class MarketShareCase method testFullMIP.

@Test
@Disabled("Underscored before JUnit 5")
public void testFullMIP() {
    final ExpressionsBasedModel tmpModel = MarketShareCase.makeModel();
    // tmpModel.options.debug_stream = BasicLogger.DEBUG;
    // tmpModel.options.debug_solver = IntegerSolver.class;
    // tmpModel.options.validate = true;
    final Result tmpResult = tmpModel.minimise();
    TestUtils.assertEquals("OBJECTIVE_MIP", OBJECTIVE_MIP.doubleValue(), tmpResult.getValue(), 1E-14 / PrimitiveMath.THREE);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        TestUtils.assertEquals(tmpVariable.getName(), SOLUTION.get(tmpVariable.getName()).doubleValue(), tmpVariable.getValue().doubleValue(), 1E-14 / PrimitiveMath.THREE);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test) Disabled(org.junit.jupiter.api.Disabled)

Example 33 with Variable

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

the class MipLibCase method assertMinMaxVal.

protected static void assertMinMaxVal(final String modelName, final BigDecimal expMinVal, final BigDecimal expMaxVal, final boolean relax, final Map<String, BigDecimal> solution) {
    if (DEBUG) {
        BasicLogger.DEBUG.println();
        BasicLogger.DEBUG.println();
        BasicLogger.DEBUG.println(modelName);
        BasicLogger.DEBUG.println();
    }
    final File tmpFile = new File(PATH + modelName);
    final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
    final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
    if (relax) {
        tmpModel.relax(true);
    }
    if (solution != null) {
        for (final Variable tmpVariable : tmpModel.getVariables()) {
            final BigDecimal tmpValue = solution.get(tmpVariable.getName());
            if (tmpValue != null) {
                tmpVariable.setValue(tmpValue);
            } else {
                tmpVariable.setValue(BigMath.ZERO);
            }
        }
        if (!tmpModel.validate(PRECISION.newScale(4))) {
            TestUtils.fail(SOLUTION_NOT_VALID);
        }
    }
    tmpModel.options.mip_gap = 0.001;
    tmpModel.options.time_suffice = 5L * CalendarDateUnit.MINUTE.size();
    tmpModel.options.time_abort = 15L * CalendarDateUnit.MINUTE.size();
    tmpModel.options.progress(IntegerSolver.class);
    TestUtils.assertTrue(tmpModel.validate());
    if (expMinVal != null) {
        final double tmpMinimum = tmpModel.minimise().getValue();
        if (!tmpModel.validate(PRECISION)) {
            TestUtils.fail(SOLUTION_NOT_VALID);
        }
        final double tmpExpected = expMinVal.doubleValue();
        final double tmpError = expMinVal.ulp().doubleValue();
        TestUtils.assertEquals(tmpExpected, tmpMinimum, tmpError);
    }
    if (expMaxVal != null) {
        final double tmpMaximum = tmpModel.maximise().getValue();
        if (!tmpModel.validate(PRECISION)) {
            TestUtils.fail(SOLUTION_NOT_VALID);
        }
        final double tmpExpected = expMaxVal.doubleValue();
        final double tmpError = expMaxVal.ulp().doubleValue();
        TestUtils.assertEquals(tmpExpected, tmpMaximum, tmpError);
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) MathProgSysModel(org.ojalgo.optimisation.MathProgSysModel) File(java.io.File) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 34 with Variable

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

the class OptimisationIntegerData method buildModelForP20100412.

public static ExpressionsBasedModel buildModelForP20100412() {
    final KnapsackItem[] tmpItems = { new KnapsackItem(20, 2), new KnapsackItem(30, 4) };
    final Variable[] tmpVariables = new Variable[tmpItems.length];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("Var" + String.valueOf(i));
        tmpVariables[i].lower(ZERO).upper(ONE).weight(tmpItems[i].value).integer(true);
    }
    final ExpressionsBasedModel retVal = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpTotalWeightExpr = retVal.addExpression("Total Weight");
    for (int i = 0; i < tmpItems.length; i++) {
        tmpTotalWeightExpr.set(i, tmpItems[i].weight);
    }
    tmpTotalWeightExpr.lower(ZERO).upper(THREE);
    retVal.setMaximisation();
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Example 35 with Variable

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

the class P20130225 method makeModel.

static ExpressionsBasedModel makeModel() {
    final double alpha = 0.1;
    final TreeMap preCalculateCosts = P20130225.preCalculateCosts();
    final TreeMap variablesStation = new TreeMap();
    final TreeMap variablesUVStation = new TreeMap();
    final ArrayList allVariables = new ArrayList();
    for (int i = 0; i < preCalculateCosts.size(); i++) {
        final double[] costs = (double[]) preCalculateCosts.get(i);
        // Cost function = Min(sum(C_i_j*X_i_j + alpha*(sum(Ui + Vi))
        final int availableDocks = costs.length;
        final Variable u = new Variable("U_" + i).lower(BigDecimal.valueOf(0)).weight(BigDecimal.valueOf(alpha));
        final Variable v = new Variable("V_" + i).lower(BigDecimal.valueOf(0)).weight(BigDecimal.valueOf(alpha));
        allVariables.add(u);
        allVariables.add(v);
        final ArrayList uvVariables = new ArrayList();
        uvVariables.add(u);
        uvVariables.add(v);
        variablesUVStation.put(i, uvVariables);
        for (int j = 0; j < availableDocks; j++) {
            final double cost = costs[j];
            final Variable variable = new Variable("X_" + i + "_" + j).binary().weight(BigDecimal.valueOf(cost));
            if (variablesStation.containsKey(i)) {
                final ArrayList vars = (ArrayList) variablesStation.get(i);
                vars.add(variable);
            } else {
                final ArrayList vars = new ArrayList();
                vars.add(variable);
                variablesStation.put(i, vars);
            }
            allVariables.add(variable);
        }
    }
    final ExpressionsBasedModel tmpIntegerModel = new ExpressionsBasedModel(allVariables);
    // Exp_total_bikes = sum(j*X_i_j) <= 91;
    final Expression expresion1 = tmpIntegerModel.addExpression("Exp_total_bikes");
    for (int i = 0; i < tmpIntegerModel.countVariables(); i++) {
        final Variable v = tmpIntegerModel.getVariable(i);
        final String name = v.getName();
        if (name.startsWith("X_")) {
            final String state = name.substring(name.lastIndexOf("_") + 1, name.length());
            expresion1.set(v, BigDecimal.valueOf((Integer.parseInt(state))));
        }
    }
    expresion1.upper(BigDecimal.valueOf(91));
    for (int i = 0; i < preCalculateCosts.size(); i++) {
        // Exp_i = sum(X_i_j) = 1
        final ArrayList varsStation = (ArrayList) variablesStation.get(i);
        final Expression expresion2 = tmpIntegerModel.addExpression("Exp_" + i);
        expresion2.setLinearFactorsSimple(varsStation);
        expresion2.level(BigDecimal.valueOf(1));
    }
    for (int i = 0; i < preCalculateCosts.size(); i++) {
        // Exp_UV_i = Ui - Vi + sum(j*X_i_j) = 5
        final Expression expresion3 = tmpIntegerModel.addExpression("Exp_UV_" + i);
        final ArrayList varsStation = (ArrayList) variablesStation.get(i);
        for (int j = 0; j < varsStation.size(); j++) {
            final Variable v = (Variable) varsStation.get(j);
            final String name = v.getName();
            final int state = Integer.parseInt(name.substring(name.lastIndexOf("_") + 1, name.length()));
            expresion3.set(v, state);
        }
        final ArrayList uvStation = (ArrayList) variablesUVStation.get(i);
        final Variable u = (Variable) uvStation.get(0);
        final Variable v = (Variable) uvStation.get(1);
        expresion3.set(u, BigDecimal.ONE);
        expresion3.set(v, BigDecimal.valueOf(-1));
        expresion3.level(BigDecimal.valueOf(5));
    }
    return tmpIntegerModel;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ArrayList(java.util.ArrayList) TreeMap(java.util.TreeMap) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

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