use of org.ojalgo.optimisation.Optimisation.State in project ojAlgo-finance by optimatika.
the class GitHubIssue24 method testSuccess.
@Test
public void testSuccess() throws Exception {
final MarkowitzModel markowitzModel = GitHubIssue24.buildMarkowitzModel(0.015, false, false, false);
final double tmpMeanReturn = markowitzModel.getMeanReturn();
if (DEBUG) {
BasicLogger.debug(tmpMeanReturn);
}
final State tmpOptimisationState = markowitzModel.optimiser().getState();
TestUtils.assertTrue(tmpOptimisationState.isOptimal());
}
use of org.ojalgo.optimisation.Optimisation.State in project ojAlgo-finance by optimatika.
the class PortfolioProblems method testP20130329.
/**
* we have a problem with MarkowitzModel. I have produced a little source that explain the problem. We
* have set 3 different TargetReturn on the same data.. With targets near to the best end worst target
* return, the MarkowitzModel works fine. With targets within the interval of best and worst return, seem
* that the MarkowitzModel is not able to find a correct list of weights. If you try this program and use
* target of 0.08 or 0.13 or 0.12 you can see a correct solution. With a target of 0.10 MarkowitzModel is
* not able to find a valid solution.
*/
@Test
public void testP20130329() {
final BasicMatrix tmpCovariances = RationalMatrix.FACTORY.rows(new double[][] { { 0.00360000, 0.001800000000 }, { 0.001800000000, 0.00090000 } });
// final Eigenvalue<Double> tmpEvD = Eigenvalue.makePrimitive(true);
// tmpEvD.compute(tmpCovariances, true);
// BasicLogger.debug("Eigenvalues: {}", tmpEvD.getEigenvalues());
final MarketEquilibrium tmpMarketEquilibrium = new MarketEquilibrium(tmpCovariances, BigMath.THOUSAND);
final Builder<RationalMatrix> tmpExcessReturnsBuilder = RationalMatrix.FACTORY.getBuilder(2, 1);
tmpExcessReturnsBuilder.set(0, 0, 0.1400);
tmpExcessReturnsBuilder.set(1, 0, 0.0800);
final BasicMatrix tmpExcessReturns = tmpExcessReturnsBuilder.build();
final MarkowitzModel tmpMarkowitzModel = new MarkowitzModel(tmpMarketEquilibrium, tmpExcessReturns);
tmpMarkowitzModel.setLowerLimit(0, BigMath.ZERO);
tmpMarkowitzModel.setUpperLimit(0, BigMath.ONE);
tmpMarkowitzModel.setLowerLimit(1, BigMath.ZERO);
tmpMarkowitzModel.setUpperLimit(1, BigMath.ONE);
tmpMarkowitzModel.setShortingAllowed(false);
for (int t = 8; t <= 14; t++) {
final BigDecimal tmpTargetReturn = BigFunction.DIVIDE.invoke(new BigDecimal(t), BigMath.HUNDRED);
tmpMarkowitzModel.setTargetReturn(tmpTargetReturn);
final List<BigDecimal> tmpWeights = tmpMarkowitzModel.getWeights();
final State tmpOptimisationState = tmpMarkowitzModel.optimiser().getState();
if (DEBUG) {
BasicLogger.debug("State {} {}", tmpOptimisationState, tmpWeights);
}
TestUtils.assertTrue("Optimisation State", tmpOptimisationState.isOptimal());
TestUtils.assertTrue("Asset0 >= 0.0", tmpWeights.get(0).signum() >= 0);
TestUtils.assertTrue("Asset1 >= 0.0", tmpWeights.get(1).signum() >= 0);
TestUtils.assertTrue("Asset0 <= 1.0", tmpWeights.get(0).compareTo(BigMath.ONE) <= 0);
TestUtils.assertTrue("Asset1 <= 1.0", tmpWeights.get(1).compareTo(BigMath.ONE) <= 0);
TestUtils.assertEquals("Asset0 + Asset1 == 1.0", 1.0, tmpWeights.get(0).add(tmpWeights.get(1)).doubleValue(), 0.0001);
}
}
use of org.ojalgo.optimisation.Optimisation.State in project ojAlgo by optimatika.
the class SpecificBranchCase method testVpm2FirstBranch.
@Test
public void testVpm2FirstBranch() {
final File tmpFile = new File(MipLibCase.PATH + "vpm2.mps");
final MathProgSysModel tmpMPS = MathProgSysModel.make(tmpFile);
final ExpressionsBasedModel tmpModel = tmpMPS.getExpressionsBasedModel();
TestUtils.assertTrue(tmpModel.validate());
final ExpressionsBasedModel tmpLowerBranchModel = tmpModel.relax(false);
final ExpressionsBasedModel tmpUpperBranchModel = tmpModel.relax(false);
tmpLowerBranchModel.getVariable(106).upper(BigMath.ZERO);
tmpUpperBranchModel.getVariable(106).lower(BigMath.ONE);
final Optimisation.Result tmpLowerResult = tmpLowerBranchModel.minimise();
final Optimisation.Result tmpUpperResult = tmpUpperBranchModel.minimise();
final State tmpLowerState = tmpLowerResult.getState();
final State tmpUpperState = tmpUpperResult.getState();
if (!tmpLowerState.isFeasible() && !tmpUpperState.isFeasible()) {
TestUtils.fail("Both these branches cannot be infeasible!");
}
tmpLowerBranchModel.minimise();
if (tmpLowerState.isFeasible() && !tmpLowerBranchModel.validate(MipLibCase.PRECISION)) {
TestUtils.fail(MipLibCase.SOLUTION_NOT_VALID);
}
tmpUpperBranchModel.minimise();
if (tmpUpperState.isFeasible() && !tmpUpperBranchModel.validate(MipLibCase.PRECISION)) {
TestUtils.fail(MipLibCase.SOLUTION_NOT_VALID);
}
}
use of org.ojalgo.optimisation.Optimisation.State in project ojAlgo by optimatika.
the class LinearProblems method testP20100412.
/**
* Didn't recognise this as an infeasible problem.
*/
@Test
public void testP20100412() {
final ExpressionsBasedModel tmpModel = OptimisationIntegerData.buildModelForP20100412().relax(true);
// tmpModel.relax(); // Relax the integer constraints
// Set branch state
tmpModel.getVariable(1).lower(ONE);
final State tmpResultState = tmpModel.maximise().getState();
TestUtils.assertFalse("Should be INFEASIBLE", tmpResultState == State.FEASIBLE);
}
Aggregations