Search in sources :

Example 6 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method doEarly2008.

static void doEarly2008(final Variable[] variables, final Access2D<?> covariances, final Access1D<?> expected) {
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(variables);
    final Expression tmpVariance = tmpModel.addExpression("Variance");
    tmpVariance.setQuadraticFactors(tmpModel.getVariables(), covariances);
    tmpVariance.weight(BigMath.PI.multiply(BigMath.E).multiply(BigMath.HALF));
    final Expression tmpBalance = tmpModel.addExpression("Balance");
    tmpBalance.setLinearFactorsSimple(tmpModel.getVariables());
    tmpBalance.level(BigMath.ONE);
    // tmpModel.options.debug(ConvexSolver.class);
    // tmpModel.options.validate = false;
    final Result tmpActualResult = tmpModel.minimise();
    final NumberContext tmpAccuracy = StandardType.PERCENT.newPrecision(5);
    TestUtils.assertTrue(tmpModel.validate(Array1D.BIG.copy(expected), tmpAccuracy));
    TestUtils.assertTrue(tmpModel.validate(tmpActualResult, tmpAccuracy));
    final TwiceDifferentiable<Double> tmpObjective = tmpModel.objective().toFunction();
    final double tmpExpObjFuncVal = tmpObjective.invoke(Access1D.asPrimitive1D(expected));
    final double tmpActObjFuncVal = tmpObjective.invoke(Access1D.asPrimitive1D(tmpActualResult));
    TestUtils.assertEquals(tmpExpObjFuncVal, tmpActObjFuncVal, tmpAccuracy);
    TestUtils.assertEquals(expected, tmpActualResult, tmpAccuracy);
    // Test that the LinearSolver can determine feasibility
    final ExpressionsBasedModel relaxedModel = tmpModel.relax(false);
    final Optimisation.Result tmpLinearResult = relaxedModel.minimise();
    TestUtils.assertStateNotLessThanFeasible(tmpLinearResult);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, tmpAccuracy);
}
Also used : Expression(org.ojalgo.optimisation.Expression) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) NumberContext(org.ojalgo.type.context.NumberContext) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result)

Example 7 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method builAndTestModel.

static void builAndTestModel(final PrimitiveDenseStore[] matrices, final PrimitiveDenseStore expectedSolution, final NumberContext modelValidationContext, final boolean testSolverDirectly) {
    final MatrixStore<Double> tmpPartQ = expectedSolution.transpose().multiply(matrices[2].multiply(expectedSolution));
    final MatrixStore<Double> tmpPartC = matrices[3].transpose().multiply(expectedSolution);
    final double tmpExpectedValue = tmpPartQ.multiply(HALF.doubleValue()).subtract(tmpPartC).doubleValue(0);
    final Optimisation.Result tmpExpectedResult = new Optimisation.Result(Optimisation.State.OPTIMAL, tmpExpectedValue, expectedSolution);
    final ExpressionsBasedModel tmpModel = ConvexProblems.buildModel(matrices, expectedSolution);
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, modelValidationContext);
    if (DEBUG) {
        tmpModel.options.debug(ConvexSolver.class);
        tmpModel.options.validate = false;
    }
    TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(tmpExpectedResult, modelValidationContext));
    // The expected solution is written to the variables
    TestUtils.assertTrue("Expected solution not ok!", tmpModel.validate(modelValidationContext));
    // When/if the correct/optimal solution is used to kickStart ojAlgo should return that solution
    final Result tmpInitialisedModelResult = tmpModel.minimise();
    TestUtils.assertStateNotLessThanOptimal(tmpInitialisedModelResult);
    TestUtils.assertEquals(tmpExpectedResult, tmpInitialisedModelResult, modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpInitialisedModelResult.getValue(), modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpInitialisedModelResult).doubleValue(), modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
    for (final Variable tmpVariable : tmpModel.getVariables()) {
        tmpVariable.setValue(null);
    }
    // Initial variable values have been cleared
    final Result tmpUninitialisedModelResult = tmpModel.minimise();
    TestUtils.assertStateNotLessThanOptimal(tmpUninitialisedModelResult);
    TestUtils.assertEquals(tmpExpectedResult, tmpUninitialisedModelResult, modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpUninitialisedModelResult.getValue(), modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpUninitialisedModelResult).doubleValue(), modelValidationContext);
    TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().toFunction().invoke(expectedSolution).doubleValue(), modelValidationContext);
    if (testSolverDirectly) {
        final ConvexSolver.Builder tmpBuilder = new ConvexSolver.Builder(matrices);
        final ConvexSolver tmpSolver = tmpBuilder.build();
        // tmpSolver.options.debug(ConvexSolver.class);
        // tmpSolver.options.validate = false;
        final Optimisation.Result tmpResult = tmpSolver.solve();
        TestUtils.assertStateNotLessThanOptimal(tmpResult);
        TestUtils.assertEquals(tmpExpectedResult, tmpResult, NumberContext.getGeneral(2, 4));
        TestUtils.assertEquals(tmpExpectedValue, tmpModel.objective().evaluate(tmpResult).doubleValue(), NumberContext.getGeneral(4, 8));
    }
}
Also used : Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) Variable(org.ojalgo.optimisation.Variable) Result(org.ojalgo.optimisation.Optimisation.Result) Optimisation(org.ojalgo.optimisation.Optimisation) Builder(org.ojalgo.optimisation.convex.ConvexSolver.Builder) ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result)

Example 8 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class ConvexProblems method testP20111129.

/**
 * ojAlgo could not solve this, but LOQO (and others) could. I re-implemented the problem code just to
 * verify there was no problem there. http://bugzilla.optimatika.se/show_bug.cgi?id=11
 */
@Test
public void testP20111129() {
    final Variable x1 = new Variable("X1");
    final Variable x2 = new Variable("X2").lower(BigMath.HUNDRED.negate()).upper(BigMath.HUNDRED);
    final Variable x3 = new Variable("X3").lower(BigMath.ZERO);
    final Variable x4 = new Variable("X4").lower(BigMath.ZERO);
    final Variable[] tmpVariables = new Variable[] { x1, x2, x3, x4 };
    final ExpressionsBasedModel tmpModel = new ExpressionsBasedModel(tmpVariables);
    final Expression tmpObjExpr = tmpModel.addExpression("Objective");
    tmpModel.setMinimisation();
    tmpObjExpr.set(2, 2, BigMath.HALF);
    tmpObjExpr.set(3, 3, BigMath.TWO);
    tmpObjExpr.set(2, 3, BigMath.TWO.negate());
    tmpObjExpr.set(0, BigMath.THREE);
    tmpObjExpr.set(1, BigMath.TWO.negate());
    tmpObjExpr.set(2, BigMath.ONE);
    tmpObjExpr.set(3, BigMath.FOUR.negate());
    tmpObjExpr.weight(BigMath.ONE);
    Expression tmpConstrExpr;
    tmpConstrExpr = tmpModel.addExpression("C1").lower(BigMath.FOUR);
    tmpConstrExpr.set(0, BigMath.ONE);
    tmpConstrExpr.set(1, BigMath.ONE);
    tmpConstrExpr.set(2, BigMath.FOUR.negate());
    tmpConstrExpr.set(3, BigMath.TWO);
    tmpConstrExpr = tmpModel.addExpression("C2").upper(BigMath.SIX);
    tmpConstrExpr.set(0, BigMath.THREE.negate());
    tmpConstrExpr.set(1, BigMath.ONE);
    tmpConstrExpr.set(2, BigMath.TWO.negate());
    tmpConstrExpr = tmpModel.addExpression("C3").level(BigMath.NEG);
    tmpConstrExpr.set(1, BigMath.ONE);
    tmpConstrExpr.set(3, BigMath.NEG);
    tmpConstrExpr = tmpModel.addExpression("C4").level(BigMath.ZERO);
    tmpConstrExpr.set(0, BigMath.ONE);
    tmpConstrExpr.set(1, BigMath.ONE);
    tmpConstrExpr.set(2, BigMath.NEG);
    // tmpModel.options.debug(ConvexSolver.class);
    final Result tmpResult = tmpModel.minimise();
    final double tmpObjFuncVal = tmpResult.getValue();
    TestUtils.assertEquals(-5.281249989, tmpObjFuncVal, new NumberContext(7, 6));
    final double[] tmpExpected = new double[] { -1.1875, 1.5625, 0.375, 2.5625 };
    for (int i = 0; i < tmpExpected.length; i++) {
        TestUtils.assertEquals(tmpExpected[i], tmpVariables[i].getValue().doubleValue(), new NumberContext(5, 4));
    }
    OptimisationConvexTests.assertDirectAndIterativeEquals(tmpModel, null);
}
Also used : 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)

Example 9 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class FinancePortfolioProblem method testP20170508.

/**
 * There were several problems (symptoms) related to this case. This test primarily tests that the
 * returned solution is actually valid. There was a problem (among others) that a subproblem from the
 * Markowitz model class (set up this way) did not produce a valid/feasible solution.
 */
@Test
public void testP20170508() {
    final BigDecimal raf = BigDecimal.valueOf(177.82794100389228);
    final ExpressionsBasedModel model = FinancePortfolioProblem.buildModel(P20170508.COVARIANCES, P20170508.RETURNS, raf);
    // model.options.debug(Optimisation.Solver.class);
    // model.options.validate = false;
    final BigDecimal w0 = BigDecimal.valueOf(0.9639383);
    final BigDecimal w1 = BigDecimal.valueOf(0.036061702);
    model.getVariable(0).setValue(w0);
    model.getVariable(1).setValue(w1);
    final Result result = model.minimise();
    if (DEBUG) {
        BasicLogger.debug(result);
    }
    TestUtils.assertStateNotLessThanOptimal(result);
    TestUtils.assertTrue(model.validate());
    TestUtils.assertTrue(model.validate(result));
    OptimisationConvexTests.assertDirectAndIterativeEquals(model);
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) BigDecimal(java.math.BigDecimal) Result(org.ojalgo.optimisation.Optimisation.Result) Test(org.junit.jupiter.api.Test)

Example 10 with Result

use of org.ojalgo.optimisation.Optimisation.Result in project ojAlgo by optimatika.

the class P20150720 method main.

/**
 * @param args the command line arguments Objective: allocate the maximum qty, and try to keep
 *        proportionality between customer.
 */
public static void main(final String[] args) {
    final ExpressionsBasedModel model = P20150720.buildModel3();
    // - Maximise -(Sum of Square error)
    model.relax(true);
    // model.options.debug(ConvexSolver.class);
    final Result result = model.maximise();
    BasicLogger.debug(result);
// BasicLogger.debug(model);
}
Also used : ExpressionsBasedModel(org.ojalgo.optimisation.ExpressionsBasedModel) Result(org.ojalgo.optimisation.Optimisation.Result)

Aggregations

Result (org.ojalgo.optimisation.Optimisation.Result)33 Test (org.junit.jupiter.api.Test)27 ExpressionsBasedModel (org.ojalgo.optimisation.ExpressionsBasedModel)27 Variable (org.ojalgo.optimisation.Variable)20 Expression (org.ojalgo.optimisation.Expression)12 Optimisation (org.ojalgo.optimisation.Optimisation)8 NumberContext (org.ojalgo.type.context.NumberContext)8 BigArray (org.ojalgo.array.BigArray)5 BigDecimal (java.math.BigDecimal)4 ArrayList (java.util.ArrayList)3 Builder (org.ojalgo.optimisation.convex.ConvexSolver.Builder)3 Disabled (org.junit.jupiter.api.Disabled)2 PrimitiveDenseStore (org.ojalgo.matrix.store.PrimitiveDenseStore)2 File (java.io.File)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Collectors (java.util.stream.Collectors)1