use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class ConvexProblems method testP20150809.
/**
* Issue reported at GitHub. A set of problems related to when Q is zero - a linear problem. Generally the
* ConvexSolver is not the right option to handle linear problems, but there is some desireable behaviour.
*/
@Test
public void testP20150809() {
final NumberContext precision = new NumberContext(11, 14, RoundingMode.HALF_EVEN);
final Primitive64Array tmpExpectedSolution = Primitive64Array.wrap(new double[] { 0.12, -0.05, 0.08, 0.07 });
final Primitive64Array tmpBoundedSolution = Primitive64Array.wrap(new double[] { 99999, -99999, 99999, 99999 });
ConvexSolver tmpSolver = P20150809.buildModel(true, false);
Result tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
tmpSolver = P20150809.buildModel(true, true);
tmpResult = tmpSolver.solve();
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpExpectedSolution, tmpResult, precision);
tmpSolver = P20150809.buildModel(false, false);
tmpResult = tmpSolver.solve();
TestUtils.assertEquals(Optimisation.State.UNBOUNDED, tmpResult.getState());
tmpSolver = P20150809.buildModel(false, true);
tmpResult = tmpSolver.solve();
// Since it is now constrained, the solver should be able find the optimal solution.
TestUtils.assertStateNotLessThanOptimal(tmpResult);
TestUtils.assertEquals(tmpBoundedSolution, tmpResult, precision);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class ConvexProblems method testP20111205.
/**
* Continuation of P20111129 http://bugzilla.optimatika.se/show_bug.cgi?id=15 Have to turn off validation
* as Q is not positive semidefinite.
* <p>
* 2016-03-07: Initially the solution (from AMPL/LOQO) was stated to be:
* <p>
*
* <pre>
* 1.78684, 0.000326128, 1.78665, 0.000136478, 495.429, 0.00358488, 495.427, 0.00178874, 8.90701, 0.000339811, 8.90684, 0.000174032
* </pre>
* <p>
* The ExpressionsBasedModel can only validate this solution to be correct using a very "poor" accuracy
* context. When ExpressionsBasedModel uses CPLEX as the solver a slightly different solution is returned
* that validates much better. Switched to using that solution as the expected solution in this test:
* <p>
*
* <pre>
* 1.7856570552, 1.216415374E-5, 1.78565097263, 6.08157995E-6, 495.426247828, 2.478968927E-5, 495.426235433, 1.239483719E-5, 8.90673094088, 6.04347562E-6, 8.90672791911, 3.02171321E-6
* </pre>
* </p>
*/
@Test
public void testP20111205() {
final PrimitiveDenseStore tmpAE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0 }, { 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 1.0, -1.0, -1.0, 1.0, 0.0, 0.0, 0.0, 0.0 } });
final PrimitiveDenseStore tmpBE = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 } });
final PrimitiveDenseStore tmpQ = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 42.58191012032541, -42.58191012032541, 0.0, 0.0, 0.029666091804595635, -0.029666091804595635, 0.0, 0.0, 9.954580659495097, -9.954580659495097, 0.0, 0.0 }, { -42.58191012032541, 42.58191012032541, 0.0, 0.0, -0.029666091804595635, 0.029666091804595635, 0.0, 0.0, -9.954580659495097, 9.954580659495097, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.029666091804595635, -0.029666091804595635, 0.0, 0.0, 0.8774199042430086, -0.8774199042430086, 0.0, 0.0, -3.537087573378497, 3.537087573378497, 0.0, 0.0 }, { -0.029666091804595635, 0.029666091804595635, 0.0, 0.0, -0.8774199042430086, 0.8774199042430086, 0.0, 0.0, 3.537087573378497, -3.537087573378497, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 9.954580659495097, -9.954580659495097, 0.0, 0.0, -3.537087573378497, 3.537087573378497, 0.0, 0.0, 153.76101274121527, -153.76101274121527, 0.0, 0.0 }, { -9.954580659495097, 9.954580659495097, 0.0, 0.0, 3.537087573378497, -3.537087573378497, 0.0, 0.0, -153.76101274121527, 153.76101274121527, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 } });
final PrimitiveDenseStore tmpC = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 185.8491751747291 }, { -192.3021967647291 }, { -6.45302159 }, { -6.45302159 }, { 406.4118818820076 }, { -409.5778277520076 }, { -3.16594587 }, { -3.16594587 }, { -352.0970015985486 }, { 339.11043506854867 }, { -12.986566530000001 }, { -12.986566530000001 } });
final PrimitiveDenseStore tmpAI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0, 0.0 }, { 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0 } });
final PrimitiveDenseStore tmpBI = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 }, { 0.0 } });
// Optimisation.Result from CPLEX (via ExpressionsBasedModel)
final double[] tmpExpected = new double[] { 1.7856570552, 1.216415374E-5, 1.78565097263, 6.08157995E-6, 495.426247828, 2.478968927E-5, 495.426235433, 1.239483719E-5, 8.90673094088, 6.04347562E-6, 8.90672791911, 3.02171321E-6 };
final PrimitiveDenseStore[] tmpMatrices = new PrimitiveDenseStore[6];
tmpMatrices[0] = tmpAE;
tmpMatrices[1] = tmpBE;
tmpMatrices[2] = tmpQ;
tmpMatrices[3] = tmpC;
tmpMatrices[4] = tmpAI;
tmpMatrices[5] = tmpBI;
// The original AMPL/LOQO solution was given with 6 digits precision and never more than 9 decimals
// ojAlgo can only get roughly the same solution
final NumberContext tmpAccuracy = NumberContext.getGeneral(3, 3);
ConvexProblems.builAndTestModel(tmpMatrices, tmpExpected, tmpAccuracy, true);
}
use of org.ojalgo.type.context.NumberContext 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);
}
use of org.ojalgo.type.context.NumberContext 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);
}
use of org.ojalgo.type.context.NumberContext in project ojAlgo by optimatika.
the class LinearDesignTestCases method test4LinearModelCase.
/**
* http://www.saintmarys.edu/~psmith/338act14.html
*/
@Test
public void test4LinearModelCase() {
final Variable[] tmpVariables = new Variable[] { new Variable("X1").lower(ZERO).weight(ONE.negate()), new Variable("X2").lower(ZERO).weight(ONE), new Variable("X3").lower(ZERO).weight(ZERO) };
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[] { SIX, ONE.negate(), ZERO }[i]);
}
tmpExprC1.upper(TEN);
final Expression tmpExprC2 = tmpModel.addExpression("C2");
for (int i = 0; i < tmpModel.countVariables(); i++) {
tmpExprC2.set(i, new BigDecimal[] { ONE, FIVE, ZERO }[i]);
}
tmpExprC2.lower(FOUR);
final Expression tmpExprC3 = tmpModel.addExpression("C3");
for (int i = 0; i < tmpModel.countVariables(); i++) {
tmpExprC3.set(i, new BigDecimal[] { ONE, FIVE, ONE }[i]);
}
tmpExprC3.level(FIVE);
final Optimisation.Result tmpResult = tmpModel.minimise();
final BasicMatrix tmpSolution = RationalMatrix.FACTORY.columns(tmpResult);
final PhysicalStore<Double> tmpExpX = PrimitiveDenseStore.FACTORY.rows(new double[][] { { 1.74 }, { 0.45 }, { 1.0 } });
final PhysicalStore<Double> tmpActX = PrimitiveDenseStore.FACTORY.copy(tmpSolution.selectRows(new int[] { 0, 1, 2 }));
tmpActX.modifyAll(new NumberContext(7, 2).getFunction(PrimitiveFunction.getSet()));
TestUtils.assertEquals(tmpExpX, tmpActX);
}
Aggregations