Search in sources :

Example 36 with Variable

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

Example 37 with Variable

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

the class P20150127b method getModel.

public static ExpressionsBasedModel getModel(final boolean branch, final boolean old) {
    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);
    final List<int[]> coefficients = P20150127b.getCoefficients();
    int counter = 0;
    for (final int[] coeff : coefficients) {
        final Expression c = retVal.addExpression("inequality_" + ++counter);
        // We want coeff[0] * x + coeff[1] * y < 0. Since our
        // solutions are integer, we can do "<= -1".
        c.upper(BigDecimal.ONE.negate());
        c.set(x, coeff[0]);
        c.set(y, coeff[1]);
    }
    // the Branch&Bounde node which produces a wrong result uses.
    if (branch) {
        x.lower(1).upper(20);
        y.upper(-1);
        if (old) {
            // The integer solver used to set this lower limit in place of "no limit"
            y.lower(Integer.MIN_VALUE);
            retVal.relax(true);
        }
    }
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel)

Example 38 with Variable

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

the class DesignCase method testFacilityLocation.

/**
 * http://www.ohio.edu/people/melkonia/math3050/slides/IPextendedintro.ppt Slide 8
 */
@Test
public void testFacilityLocation() {
    final ArrayList<Variable> tmpVariables = new ArrayList<>();
    tmpVariables.add(Variable.makeBinary("Factory in LA").weight(9));
    tmpVariables.add(Variable.makeBinary("Factory in SF").weight(5));
    tmpVariables.add(Variable.makeBinary("Warehouse in LA").weight(6));
    tmpVariables.add(Variable.makeBinary("Warehouse in SF").weight(4));
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
    tmpModel.addVariables(tmpVariables);
    final Expression tmpBudgetCost = tmpModel.addExpression("Budget").upper(10);
    tmpBudgetCost.set(tmpVariables.get(0), 6);
    tmpBudgetCost.set(tmpVariables.get(1), 3);
    tmpBudgetCost.set(tmpVariables.get(2), 5);
    tmpBudgetCost.set(tmpVariables.get(3), 2);
    // tmpModel.options.debug(GenericSolver.class);
    final Result tmpResult = tmpModel.maximise();
    TestUtils.assertStateNotLessThanOptimal(tmpResult);
    TestUtils.assertEquals(15.0, tmpResult.getValue());
    TestUtils.assertEquals(0.0, tmpResult.doubleValue(0));
    TestUtils.assertEquals(1.0, tmpResult.doubleValue(1));
    TestUtils.assertEquals(1.0, tmpResult.doubleValue(2));
    TestUtils.assertEquals(1.0, tmpResult.doubleValue(3));
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ArrayList(java.util.ArrayList) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 39 with Variable

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

the class IntegerProblems method testP20130409a.

/**
 * apete's implementation of the original problem description.
 * <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
 */
@Test
public void testP20130409a() {
    final Variable[] tmpVariables = new Variable[] { new Variable("x1").lower(BigMath.ZERO).weight(BigMath.ONE), new Variable("x2013").lower(BigMath.ZERO).integer(true), new Variable("x2014").lower(BigMath.ZERO).integer(true) };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpExpr1 = tmpModel.addExpression("Expr1");
    tmpExpr1.set(0, -1);
    tmpExpr1.set(1, 5100);
    tmpExpr1.set(2, -5000);
    tmpExpr1.upper(BigMath.ZERO);
    final Expression tmpExpr2 = tmpModel.addExpression("Expr2");
    tmpExpr2.set(0, 1);
    tmpExpr2.set(1, 5100);
    tmpExpr2.set(2, -5000);
    tmpExpr2.lower(BigMath.ZERO);
    final Expression tmpExpr3 = tmpModel.addExpression("Expr3");
    tmpExpr3.set(1, 5000);
    tmpExpr3.set(2, 5000);
    tmpExpr3.level(new BigDecimal(19105000));
    final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4200.000000000075), BigDecimal.valueOf(1892), BigDecimal.valueOf(1929) });
    TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
    // tmpModel.options.debug(GenericSolver.class);
    // tmpModel.options.problem = NumberContext.getGeneral(12);
    final Result tmpResult = tmpModel.minimise();
    // BasicLogger.debug(tmpResult.toString());
    TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
    TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
Also used : BigArray(org.ojalgo.array.BigArray) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 40 with Variable

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

the class IntegerProblems method testP20130409b.

/**
 * Test case sent in by the user / problem reporter
 * <a href="http://bugzilla.optimatika.se/show_bug.cgi?id=178">BugZilla</a>
 */
@Test
public void testP20130409b() {
    final Variable x1 = Variable.make("x1");
    final Variable x2013 = Variable.make("x2013");
    final Variable x2014 = Variable.make("x2014");
    final Variable x2015 = Variable.make("x2015");
    x2013.setInteger(true);
    x2014.setInteger(true);
    x2015.setInteger(true);
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel();
    tmpModel.addVariable(x1);
    tmpModel.addVariable(x2013);
    tmpModel.addVariable(x2014);
    tmpModel.addVariable(x2015);
    final Expression obj = tmpModel.addExpression("obj");
    obj.set(x1, 1);
    obj.weight(BigDecimal.valueOf(1));
    final Expression c1 = tmpModel.addExpression("c1");
    c1.set(x1, 1);
    c1.lower(BigDecimal.valueOf(0));
    final Expression c2 = tmpModel.addExpression("c2");
    c2.set(x2014, -5000);
    c2.set(x2013, 5100);
    c2.set(x1, -1);
    c2.upper(BigDecimal.valueOf(0));
    final Expression c3 = tmpModel.addExpression("c3");
    c3.set(x2014, -5000);
    c3.set(x2013, 5100);
    c3.set(x1, 1);
    c3.lower(BigDecimal.valueOf(0));
    final Expression c4 = tmpModel.addExpression("c4");
    c4.set(x2014, 150);
    c4.set(x2013, 5100);
    c4.set(x2015, -5000);
    c4.set(x1, -1);
    c4.upper(BigDecimal.valueOf(0));
    final Expression c5 = tmpModel.addExpression("c5");
    c5.set(x2014, 150);
    c5.set(x2013, 5100);
    c5.set(x2015, -5000);
    c5.set(x1, 1);
    c5.lower(BigDecimal.valueOf(0));
    final Expression c6 = tmpModel.addExpression("c6");
    c6.set(x2015, 5000);
    c6.set(x2014, 5000);
    c6.set(x2013, 5000);
    c6.level(BigDecimal.valueOf(19105000));
    final BigArray tmpExpSol = BigArray.wrap(new BigDecimal[] { BigDecimal.valueOf(4849.999999997941), BigDecimal.valueOf(1245), BigDecimal.valueOf(1269), BigDecimal.valueOf(1307) });
    TestUtils.assertTrue("Expected Solution Not Valid", tmpModel.validate(tmpExpSol));
    // tmpModel.options.debug(IntegerSolver.class);
    // tmpModel.options.problem = NumberContext.getGeneral(8);
    final Result tmpResult = tmpModel.minimise();
    // BasicLogger.debug(tmpResult.toString());
    TestUtils.assertEquals("Solution Not Correct", tmpExpSol, tmpResult, new NumberContext(8, 8));
    TestUtils.assertTrue("Solver State Not Optimal", tmpResult.getState().isOptimal());
}
Also used : BigArray(org.ojalgo.array.BigArray) Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result) 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