Search in sources :

Example 41 with ExpressionsBasedModel

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

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

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

Example 44 with ExpressionsBasedModel

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

the class P20150127a method main.

public static void main(final String[] args) throws Exception {
    final ExpressionsBasedModel model = P20150127a.getModel();
    // final Optimisation.Result result = model.solve(null);
    final Optimisation.Result result = model.minimise();
    if (!result.getState().isSuccess()) {
        throw new Exception("Model should be solvable (e.g. x=201, y=-10)?!");
    }
    final BigDecimal valX = result.get(0);
    final BigDecimal valY = result.get(1);
    final int intX = valX.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    final int intY = valY.setScale(0, BigDecimal.ROUND_HALF_UP).intValue();
    // model.options.debug(LinearSolver.class);
    System.out.println("x = " + valX + " ~ " + intX);
    System.out.println("y = " + valY + " ~ " + intY);
    model.validate();
    model.validate(result);
    final List<int[]> coefficients = P20150127a.getCoefficients();
    // Verify solution
    for (final int[] coeff : coefficients) {
        final int value = (coeff[0] * intX) + (coeff[1] * intY);
        final BigDecimal exact = valX.multiply(BigDecimal.valueOf(coeff[0])).add(valY.multiply(BigDecimal.valueOf(coeff[1])));
        if (value >= 0) {
            throw new Exception(coeff[0] + "*x + " + coeff[1] + "*y = " + value + " must be negative (exact: " + exact + ")");
        }
    }
}
Also used : Optimisation(org.ojalgo.optimisation.Optimisation) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 45 with ExpressionsBasedModel

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

the class P20150127a method getModel.

public static ExpressionsBasedModel getModel() {
    final ExpressionsBasedModel retVal = new ExpressionsBasedModel();
    final Variable x = Variable.make("x").integer(true);
    final Variable y = Variable.make("y").integer(true);
    retVal.addVariable(x);
    retVal.addVariable(y);
    int counter = 0;
    for (final int[] coeff : P20150127a.getCoefficients()) {
        final Expression tmpExpression = retVal.addExpression("inequality_" + ++counter);
        // We want coeff[0] * x + coeff[1] * y < 0. Since our
        // solutions are integer, we can do "<= -1".
        tmpExpression.upper(BigDecimal.ONE.negate());
        tmpExpression.set(x, coeff[0]);
        tmpExpression.set(y, coeff[1]);
    }
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

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