Search in sources :

Example 6 with Expression

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

the class P20150720 method buildModel1.

public static ExpressionsBasedModel buildModel1() {
    // List of Variable Names
    final List<String> variablesName = new ArrayList<>();
    variablesName.add("CUSTOMER_1|PRODUCT_TYPE_1");
    variablesName.add("CUSTOMER_2|PRODUCT_TYPE_1");
    variablesName.add("CUSTOMER_3|PRODUCT_TYPE_1");
    variablesName.add("CUSTOMER_3|PRODUCT_TYPE_2");
    variablesName.add("CUSTOMER_3|PRODUCT_TYPE_3");
    variablesName.add("CUSTOMER_3|PRODUCT_TYPE_4");
    variablesName.add("CUSTOMER_3|PRODUCT_TYPE_5");
    variablesName.add("CUSTOMER_4|PRODUCT_TYPE_1");
    variablesName.add("CUSTOMER_4|PRODUCT_TYPE_2");
    variablesName.add("CUSTOMER_5|PRODUCT_TYPE_2");
    variablesName.add("CUSTOMER_5|PRODUCT_TYPE_3");
    variablesName.add("CUSTOMER_5|PRODUCT_TYPE_4");
    variablesName.add("CUSTOMER_6|PRODUCT_TYPE_2");
    variablesName.add("CUSTOMER_6|PRODUCT_TYPE_3");
    variablesName.add("CUSTOMER_6|PRODUCT_TYPE_4");
    variablesName.add("CUSTOMER_6|PRODUCT_TYPE_5");
    /*
         * Constraints for each Customer = Demand of each Customer Sum of all variable linked to the customer
         * <= Demand.
         */
    final Map<String, Integer> constraintsCustomer = new LinkedHashMap<>();
    constraintsCustomer.put("CUSTOMER_1", 40_000);
    constraintsCustomer.put("CUSTOMER_2", 25_000);
    constraintsCustomer.put("CUSTOMER_3", 15_000);
    constraintsCustomer.put("CUSTOMER_4", 5_000);
    constraintsCustomer.put("CUSTOMER_5", 2_000);
    final double demandTotal = constraintsCustomer.values().stream().mapToDouble(e -> e.doubleValue()).sum();
    /*
         * Constraits for each product type = Stock per product type. Sum of all variable linked to this
         * product type <=
         */
    final Map<String, Integer> constraintsProduct = new HashMap<>();
    constraintsProduct.put("PRODUCT_TYPE_1", 50_000);
    constraintsProduct.put("PRODUCT_TYPE_2", 0);
    constraintsProduct.put("PRODUCT_TYPE_3", 0);
    constraintsProduct.put("PRODUCT_TYPE_4", 0);
    constraintsProduct.put("PRODUCT_TYPE_5", 0);
    final double stockTotal = constraintsProduct.values().stream().mapToDouble(e -> e.doubleValue()).sum();
    /*
         *
         */
    final ExpressionsBasedModel model = new ExpressionsBasedModel();
    final List<Variable> variables = new ArrayList<>();
    // Create All Variables:
    // BasicLogger.debug("---- Variable creation ------");
    variablesName.forEach(name -> {
        final Variable var = Variable.make(name).integer(true).lower(0.0);
        model.addVariable(var);
        variables.add(var);
    // BasicLogger.debug(var);
    });
    // BasicLogger.debug("---- Constraints customers ------");
    // Apply Customers constraints.
    constraintsCustomer.entrySet().forEach(entry -> {
        final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
        final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
        constraint.lower(0.0).upper(entry.getValue().doubleValue());
        constraint.setLinearFactorsSimple(linked);
        linked.forEach(v -> v.upper(entry.getValue().doubleValue()));
    // BasicLogger.debug(constraint);
    });
    // BasicLogger.debug("---- Constraints Product ------");
    // Apply Product Type constraints.
    constraintsProduct.entrySet().forEach(entry -> {
        final List<Variable> linked = variables.stream().filter(v -> v.getName().endsWith(entry.getKey())).collect(Collectors.toList());
        final Expression constraint = model.addExpression("CONSTRAINTS_" + entry.getKey());
        constraint.lower(0.0).upper(entry.getValue().doubleValue());
        constraint.setLinearFactorsSimple(linked);
    // BasicLogger.debug(constraint);
    });
    /*
         * Objective expression. - Maximize the Sum of all variables - Minimize the Sum of square error vs
         * proportionality.
         */
    // BasicLogger.debug("---- Objective  ------");
    final Expression objective = model.addExpression("OBJECTIVE").weight(1.0);
    // - Maximize the Sum of all variables
    objective.setLinearFactorsSimple(variables);
    final List<Variable> errors = new ArrayList<>();
    // BasicLogger.debug("---- Error formula ------");
    constraintsCustomer.entrySet().forEach(entry -> {
        final List<Variable> linked = variables.stream().filter(v -> v.getName().startsWith(entry.getKey())).collect(Collectors.toList());
        if (!linked.isEmpty() && (entry.getValue().doubleValue() > 0)) {
            final Variable error = Variable.make("ERROR:" + entry.getKey());
            model.addVariable(error);
            errors.add(error);
            final Expression errorExp = model.addExpression("ERROR_EXP:" + entry.getKey()).level(stockTotal / demandTotal);
            linked.forEach(v -> errorExp.set(v, 1.0 / entry.getValue().doubleValue()));
            errorExp.set(error, -1.0);
            // - Squared of error.
            objective.set(error, error, -1.0);
        }
    });
    return model;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Result(org.ojalgo.optimisation.Optimisation.Result) Expression(org.ojalgo.optimisation.Expression) Map(java.util.Map) BasicLogger(org.ojalgo.netio.BasicLogger) BigDenseStore(org.ojalgo.matrix.store.BigDenseStore) Variable(org.ojalgo.optimisation.Variable) HashMap(java.util.HashMap) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Variable(org.ojalgo.optimisation.Variable) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) LinkedHashMap(java.util.LinkedHashMap) Expression(org.ojalgo.optimisation.Expression)

Example 7 with Expression

use of org.ojalgo.optimisation.Expression 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 8 with Expression

use of org.ojalgo.optimisation.Expression 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 9 with Expression

use of org.ojalgo.optimisation.Expression 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 10 with Expression

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

Aggregations

Expression (org.ojalgo.optimisation.Expression)49 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)45 Variable (org.ojalgo.optimisation.Variable)44 Result (org.ojalgo.optimisation.Optimisation.Result)27 Test (org.junit.jupiter.api.Test)23 BigDecimal (java.math.BigDecimal)18 Optimisation (org.ojalgo.optimisation.Optimisation)18 ArrayList (java.util.ArrayList)9 BasicMatrix (org.ojalgo.matrix.BasicMatrix)9 NumberContext (org.ojalgo.type.context.NumberContext)9 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 BasicLogger (org.ojalgo.netio.BasicLogger)4 IntIndex (org.ojalgo.access.Structure1D.IntIndex)3 BigArray (org.ojalgo.array.BigArray)3 BigDenseStore (org.ojalgo.matrix.store.BigDenseStore)3 File (java.io.File)1