Search in sources :

Example 1 with Variable

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

the class NewIntegerSolver method makeNodeModel.

ExpressionsBasedModel makeNodeModel(final NodeKey nodeKey) {
    final ExpressionsBasedModel retVal = this.getIntegerModel().relax(false);
    final int[] tmpIntegerIndeces = this.getIntegerIndices();
    for (int i = 0; i < tmpIntegerIndeces.length; i++) {
        final BigDecimal tmpLowerBound = nodeKey.getLowerBound(i);
        final BigDecimal tmpUpperBound = nodeKey.getUpperBound(i);
        final Variable tmpVariable = retVal.getVariable(tmpIntegerIndeces[i]);
        tmpVariable.lower(tmpLowerBound);
        tmpVariable.upper(tmpUpperBound);
        BigDecimal tmpValue = tmpVariable.getValue();
        if (tmpValue != null) {
            if (tmpLowerBound != null) {
                tmpValue = tmpValue.max(tmpLowerBound);
            }
            if (tmpUpperBound != null) {
                tmpValue = tmpValue.min(tmpUpperBound);
            }
            tmpVariable.setValue(tmpValue);
        }
    }
    if (this.isIntegerSolutionFound()) {
        final double tmpBestValue = this.getBestResultSoFar().getValue();
        final double tmpGap = PrimitiveFunction.ABS.invoke(tmpBestValue * options.mip_gap);
        if (retVal.isMinimisation()) {
            retVal.limitObjective(null, TypeUtils.toBigDecimal(tmpBestValue - tmpGap, options.feasibility));
        } else {
            retVal.limitObjective(TypeUtils.toBigDecimal(tmpBestValue + tmpGap, options.feasibility), null);
        }
    }
    return retVal;
}
Also used : Variable(org.ojalgo.optimisation.Variable) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 2 with Variable

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

the class NodeKey method bound.

void bound(final ExpressionsBasedModel model, final int[] integerIndices) {
    for (int i = 0; i < integerIndices.length; i++) {
        final BigDecimal lowerBound = this.getLowerBound(i);
        final BigDecimal upperBound = this.getUpperBound(i);
        final Variable variable = model.getVariable(integerIndices[i]);
        variable.lower(lowerBound);
        variable.upper(upperBound);
        final BigDecimal value = variable.getValue();
        if (value != null) {
            // Re-setting will ensure the new bounds are not violated
            variable.setValue(value);
        }
    }
}
Also used : Variable(org.ojalgo.optimisation.Variable) BigDecimal(java.math.BigDecimal)

Example 3 with Variable

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

the class ComPictetPamBamTest method setupModel.

/**
 * @param numberOfVars greater than 6
 */
void setupModel(final int numberOfVars) {
    if (numberOfVars < 6) {
        throw new IllegalArgumentException("numberOfVars must be >= 6 !!!");
    }
    // 
    // variables
    // 
    vars = new Variable[numberOfVars];
    vars[0] = new Variable("x0").lower(new BigDecimal(00.0)).upper(new BigDecimal(15.0));
    vars[1] = new Variable("x1").lower(new BigDecimal(17.0)).upper(new BigDecimal(27.0));
    vars[2] = new Variable("x2").lower(new BigDecimal(19.0)).upper(new BigDecimal(34.0));
    vars[3] = new Variable("x3").lower(new BigDecimal(25.0)).upper(new BigDecimal(48.0));
    vars[4] = new Variable("x4").lower(new BigDecimal(05.0)).upper(new BigDecimal(18.0));
    vars[5] = new Variable("x5").lower(new BigDecimal(02.0)).upper(new BigDecimal(09.0));
    for (int i = 6; i < numberOfVars; ++i) {
        vars[i] = new Variable("x" + i).level(BigMath.ZERO);
    }
    // 
    // minimise distance to this point
    // 
    point = new BigDecimal[numberOfVars];
    point[0] = new BigDecimal(1.0);
    point[1] = new BigDecimal(25.0);
    point[2] = new BigDecimal(33.0);
    point[3] = new BigDecimal(29.0);
    point[4] = new BigDecimal(9.0);
    point[5] = new BigDecimal(2.0);
    for (int i = 6; i < numberOfVars; ++i) {
        point[i] = new BigDecimal(0.0);
    }
    // 
    // model
    // 
    model = new ExpressionsBasedModel(vars);
    // 
    // objective function
    // 
    {
        final int tmpLength = model.countVariables();
        final Expression retVal = model.addExpression("objective");
        for (int ij = 0; ij < tmpLength; ij++) {
            retVal.set(ij, ij, ONE);
        }
        final BigDecimal tmpLinearWeight = TWO.negate();
        for (int i = 0; i < tmpLength; i++) {
            retVal.set(i, Arrays.asList(point).get(i).multiply(tmpLinearWeight));
        }
        final Expression e = retVal;
        e.weight(BigMath.HALF);
    }
    // 
    // sum(xi) = 100.0
    // 
    {
        final int tmpLength = model.countVariables();
        final Expression retVal = model.addExpression("sum(xi) = 100.0");
        for (int i = 0; i < tmpLength; i++) {
            retVal.set(i, ONE);
        }
        final Expression e = retVal;
        e.level(BigMath.HUNDRED);
    }
    // 
    // x1 + x2 <= 45
    // 
    {
        final Expression e = model.addExpression("x1 + x2 <= 45.0");
        e.set(1, BigMath.ONE);
        e.set(2, BigMath.ONE);
        e.lower(BigMath.ZERO).upper(new BigDecimal(45.0));
    }
    // 
    // x4 - 2*x5 = 0
    // 
    {
        final Expression e = model.addExpression("x4 - 2*x5 = 0");
        e.set(4, BigMath.ONE);
        e.set(5, BigMath.TWO.negate());
        e.level(BigMath.ZERO);
    }
// model.options.debug(ConvexSolver.class);
}
Also used : Variable(org.ojalgo.optimisation.Variable) Expression(org.ojalgo.optimisation.Expression) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal)

Example 4 with Variable

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

the class ConvexProblems method testP20080208.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080208() {
    // create expected returns matrix
    final PrimitiveMatrix tmpExpectedReturns = PrimitiveMatrix.FACTORY.rows(new double[][] { { 9.997829 }, { 10.008909 }, { 10.010849 }, { 9.998919 }, { 10.055549 }, { 9.999127 }, { 9.999720 }, { 10.049002 }, { 9.988769 }, { 9.990095 } });
    // create covariance matrix
    final PrimitiveMatrix tmpCovariances = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.014661954677318977, 3.459112088561122E-4, 0.007798752920910871, 0.0020921425081866503, 0.001846944297640248, 1.0531906931335766E-4, -2.7515614291198E-4, 0.0034083900074454894, 1.1859491261103433E-4, -0.0027421673864628264 }, { 3.459112088561122E-4, 0.008695862475003915, 0.004154360841751649, -2.661685231819661E-4, -0.0015999007544258263, 3.590680217774603E-4, -0.00186976624370318, 0.0010975416828213752, -5.512038393911129E-4, -0.0010605923775744853 }, { 0.007798752920910871, 0.004154360841751649, 0.032945930970836965, 0.0037716078815399324, -2.2919474365382624E-4, 3.3938035033219876E-4, -0.0015613122026082874, 0.0010975697179894332, 2.296422665244149E-4, -0.001709517941787044 }, { 0.0020921425081866503, -2.661685231819661E-4, 0.0037716078815399324, 0.0057162979859706736, 5.573137056500744E-4, 4.91132887765294E-4, -9.94830474250937E-4, 8.331708084069932E-4, -6.595917138470072E-4, -0.0018693519327569541 }, { 0.001846944297640248, -0.0015999007544258263, -2.2919474365382624E-4, 5.573137056500744E-4, 0.03230071314144326, -2.2320789666419312E-4, -2.2639506820057415E-4, 0.010695663287043154, 0.0014569847730040847, 0.002160537177809949 }, { 1.0531906931335766E-4, 3.590680217774603E-4, 3.3938035033219876E-4, 4.91132887765294E-4, -2.2320789666419312E-4, 0.0017540170708301957, 5.153195618913916E-5, 7.339825618468765E-4, -9.309096233432093E-6, -1.814362059740286E-4 }, { -2.7515614291198E-4, -0.00186976624370318, -0.0015613122026082874, -9.94830474250937E-4, -2.2639506820057415E-4, 5.153195618913916E-5, 0.00809348822665732, -0.0017672866424053742, 3.058672988166145E-4, 0.001201578905822851 }, { 0.0034083900074454894, 0.0010975416828213752, 0.0010975697179894332, 8.331708084069932E-4, 0.010695663287043154, 7.339825618468765E-4, -0.0017672866424053742, 0.013913761913235494, 0.0012785124957521252, 5.298368056593439E-4 }, { 1.1859491261103433E-4, -5.512038393911129E-4, 2.296422665244149E-4, -6.595917138470072E-4, 0.0014569847730040847, -9.309096233432093E-6, 3.058672988166145E-4, 0.0012785124957521252, 0.004650801896027841, 5.437156659657787E-4 }, { -0.0027421673864628264, -0.0010605923775744853, -0.001709517941787044, -0.0018693519327569541, 0.002160537177809949, -1.814362059740286E-4, 0.001201578905822851, 5.298368056593439E-4, 5.437156659657787E-4, 0.007359495478781133 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) tmpExpectedReturns.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(tmpExpectedReturns.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 8% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.08"));
        // require no more than 12% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.12"));
    }
    // exception here...
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.07999999999998897 }, { 0.1199999999999636 }, { 0.07999999999999526 }, { 0.08000000000004488 }, { 0.11999999999999084 }, { 0.12000000000018606 }, { 0.11999999999996151 }, { 0.12000000000000167 }, { 0.08000000000001738 }, { 0.08000000000005617 } });
    ConvexProblems.doEarly2008(tmpVariables, tmpCovariances, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) Test(org.junit.jupiter.api.Test)

Example 5 with Variable

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

the class ConvexProblems method testP20080124.

/**
 * Another case of looping in the ActiveSetSolver's constraint (de)activation.
 */
@Test
public void testP20080124() {
    // create expected returns matrix
    final PrimitiveMatrix expectedReturnsMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 10.012158 }, { 9.996046 }, { 10.000744 }, { 9.990585 }, { 9.998392 }, { 9.996614 }, { 10.010531 }, { 10.001401 }, { 9.997447 }, { 9.993817 }, { 9.998537 }, { 9.995741 }, { 9.987224 }, { 9.992392 } });
    // create covariance matrix
    final PrimitiveMatrix covarianceMatrix = PrimitiveMatrix.FACTORY.rows(new double[][] { { 0.0013191354374342357, 7.786471466322114E-5, -3.810886655309235E-5, -2.28102405899103E-4, -1.2589115740653127E-4, -1.3247692268411991E-5, 1.422624656557158E-4, -2.7176361887359125E-5, 8.675127894495302E-5, -8.116577287090551E-5, -8.468380774247271E-6, 4.930080166695193E-5, -2.774138231533918E-4, -3.148322898570031E-5 }, { 7.786471466322114E-5, 0.001028250547816086, 8.986425197170406E-4, -1.0341435238579975E-5, 6.472902968147139E-4, 2.9014435841747375E-4, 1.0640414444602855E-4, 5.638694128451113E-4, 6.024515366195699E-4, -1.094867665517237E-4, 6.177221606260711E-6, -5.682215091954099E-5, 2.7178074500896235E-4, 0.0010146062950574643 }, { -3.810886655309235E-5, 8.986425197170406E-4, 0.0012477403456464075, -1.8104847201530489E-4, 9.299199981666304E-4, 3.486383951982303E-4, 1.0246402606579107E-4, 7.009722990366382E-4, 6.545695073447614E-4, -1.1680969171500155E-4, 7.123493385355658E-5, 1.559414390174896E-5, 1.972605480880284E-4, 9.368808845809186E-4 }, { -2.28102405899103E-4, -1.0341435238579975E-5, -1.8104847201530489E-4, 6.250793590180099E-4, -5.4721911720097E-6, 1.3081826023829458E-4, -5.644046856412501E-5, -1.1282043806099452E-5, -6.729835202722053E-5, 1.3929681542737307E-4, 3.698155248637573E-6, 5.0269944317023966E-5, 5.344931460074395E-4, -1.1654882792112444E-4 }, { -1.2589115740653127E-4, 6.472902968147139E-4, 9.299199981666304E-4, -5.4721911720097E-6, 0.001181357476541527, 3.0334522038028824E-4, 2.6983840497611894E-4, 6.983493701701867E-4, 5.68816790613126E-4, -7.899505299987754E-5, 1.05074262063586E-5, 1.137295188785598E-4, 1.9732025136606058E-4, 6.631330613471645E-4 }, { -1.3247692268411991E-5, 2.9014435841747375E-4, 3.486383951982303E-4, 1.3081826023829458E-4, 3.0334522038028824E-4, 3.372068413122505E-4, 1.1067468759384309E-4, 2.6589126866881173E-4, 2.1364931019670806E-4, -4.201239472520589E-5, 2.32769639721745E-5, 5.847559594073046E-6, 1.9925897592339058E-4, 1.9671375386540353E-4 }, { 1.422624656557158E-4, 1.0640414444602855E-4, 1.0246402606579107E-4, -5.644046856412501E-5, 2.6983840497611894E-4, 1.1067468759384309E-4, 0.001484755064835215, 1.2295961703024863E-4, 1.0843198781689372E-4, -2.1292328294313923E-5, -4.152686600769749E-6, 1.163599038579726E-4, -3.14739599261259E-4, 2.4519847977412686E-4 }, { -2.7176361887359125E-5, 5.638694128451113E-4, 7.009722990366382E-4, -1.1282043806099452E-5, 6.983493701701867E-4, 2.6589126866881173E-4, 1.2295961703024863E-4, 5.563328439145604E-4, 4.4816730200338125E-4, -3.4729832814007256E-5, -6.028818604193519E-7, 3.192976987126335E-5, 1.7402262469809026E-4, 5.182632389125651E-4 }, { 8.675127894495302E-5, 6.024515366195699E-4, 6.545695073447614E-4, -6.729835202722053E-5, 5.68816790613126E-4, 2.1364931019670806E-4, 1.0843198781689372E-4, 4.4816730200338125E-4, 6.277134808325468E-4, -4.988229718603287E-5, -5.5018781802344255E-6, -1.3231260300518203E-5, 8.214207901880769E-5, 5.841470978796527E-4 }, { -8.116577287090551E-5, -1.094867665517237E-4, -1.1680969171500155E-4, 1.3929681542737307E-4, -7.899505299987754E-5, -4.201239472520589E-5, -2.1292328294313923E-5, -3.4729832814007256E-5, -4.988229718603287E-5, 3.5152692612068785E-4, -9.358092257358399E-6, 4.962216896551324E-6, 1.291957229930161E-4, -1.5046975508620905E-4 }, { -8.468380774247271E-6, 6.177221606260711E-6, 7.123493385355658E-5, 3.698155248637573E-6, 1.05074262063586E-5, 2.32769639721745E-5, -4.152686600769749E-6, -6.028818604193519E-7, -5.5018781802344255E-6, -9.358092257358399E-6, 4.8495980378967104E-5, 1.1704645004909169E-5, 1.814918597253607E-5, 1.2448218299234062E-5 }, { 4.930080166695193E-5, -5.682215091954099E-5, 1.559414390174896E-5, 5.0269944317023966E-5, 1.137295188785598E-4, 5.847559594073046E-6, 1.163599038579726E-4, 3.192976987126335E-5, -1.3231260300518203E-5, 4.962216896551324E-6, 1.1704645004909169E-5, 1.802684481609152E-4, 1.0475986793792914E-5, -4.113641419540392E-5 }, { -2.774138231533918E-4, 2.7178074500896235E-4, 1.972605480880284E-4, 5.344931460074395E-4, 1.9732025136606058E-4, 1.9925897592339058E-4, -3.14739599261259E-4, 1.7402262469809026E-4, 8.214207901880769E-5, 1.291957229930161E-4, 1.814918597253607E-5, 1.0475986793792914E-5, 7.843917688960864E-4, 1.231995848356005E-4 }, { -3.148322898570031E-5, 0.0010146062950574643, 9.368808845809186E-4, -1.1654882792112444E-4, 6.631330613471645E-4, 1.9671375386540353E-4, 2.4519847977412686E-4, 5.182632389125651E-4, 5.841470978796527E-4, -1.5046975508620905E-4, 1.2448218299234062E-5, -4.113641419540392E-5, 1.231995848356005E-4, 0.0011885193322126312 } });
    // create asset variables - cost and weighting constraints
    final Variable[] tmpVariables = new Variable[(int) expectedReturnsMatrix.countRows()];
    for (int i = 0; i < tmpVariables.length; i++) {
        tmpVariables[i] = new Variable("VAR" + i);
        final int row = i;
        tmpVariables[i].weight(TypeUtils.toBigDecimal(expectedReturnsMatrix.get(row, 0)).negate());
        // set the constraints on the asset weights
        // require at least a 2% allocation to each asset
        tmpVariables[i].lower(new BigDecimal("0.05"));
        // require no more than 80% allocation to each asset
        tmpVariables[i].upper(new BigDecimal("0.35"));
    // tmpVariables[i].setUpperLimit(new BigDecimal("1.00"));
    }
    final RationalMatrix tmpExpected = RationalMatrix.FACTORY.rows(new double[][] { { 0.3166116715239731 }, { 0.050000000001624065 }, { 0.04999999999827016 }, { 0.05000000000034928 }, { 0.049999999999891145 }, { 0.049999999997416125 }, { 0.08338832846287945 }, { 0.05000000000178943 }, { 0.05000000000085164 }, { 0.04999999999937388 }, { 0.050000000012470555 }, { 0.04999999999966884 }, { 0.050000000000484546 }, { 0.049999999995857476 } });
    ConvexProblems.doEarly2008(tmpVariables, covarianceMatrix, tmpExpected);
}
Also used : Variable(org.ojalgo.optimisation.Variable) PrimitiveMatrix(org.ojalgo.matrix.PrimitiveMatrix) BigDecimal(java.math.BigDecimal) RationalMatrix(org.ojalgo.matrix.RationalMatrix) 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