From c5b943fe263f052be9d3815fdfb99aaee302d523 Mon Sep 17 00:00:00 2001 From: Thomas Neidhart Date: Fri, 8 Nov 2013 15:19:46 +0000 Subject: [PATCH] Fix failing unit tests for BOBYQAOptimizer when executed with a 1.5 jvm due to slight differences in accuracy of the certain Math functions. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1540075 13f79535-47bb-0310-9956-ffa450edef68 --- src/changes/changes.xml | 3 +++ .../scalar/noderiv/BOBYQAOptimizerTest.java | 26 +++++++++--------- .../direct/BOBYQAOptimizerTest.java | 27 ++++++++++--------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/changes/changes.xml b/src/changes/changes.xml index 1a216d7b9..3c4df2dd3 100644 --- a/src/changes/changes.xml +++ b/src/changes/changes.xml @@ -51,6 +51,9 @@ If the output is not quite correct, check for invisible trailing spaces! + + Fixed failing unit tests for "BOBYQAOptimizer" when executed with a Oracle/Sun JVM 1.5. + A call to "KalmanFilter#correct(...)" may have resulted in "NonSymmetricMatrixException" as the internally used matrix inversion method was using a too strict symmetry check. diff --git a/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java b/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java index 252cf8a39..f2f36e93f 100644 --- a/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java +++ b/src/test/java/org/apache/commons/math3/optim/nonlinear/scalar/noderiv/BOBYQAOptimizerTest.java @@ -18,6 +18,7 @@ package org.apache.commons.math3.optim.nonlinear.scalar.noderiv; import java.util.Arrays; import java.util.Random; + import org.apache.commons.math3.analysis.MultivariateFunction; import org.apache.commons.math3.exception.DimensionMismatchException; import org.apache.commons.math3.exception.TooManyEvaluationsException; @@ -29,6 +30,7 @@ import org.apache.commons.math3.optim.nonlinear.scalar.GoalType; import org.apache.commons.math3.optim.PointValuePair; import org.apache.commons.math3.optim.InitialGuess; import org.apache.commons.math3.optim.SimpleBounds; +import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -208,7 +210,7 @@ public class BOBYQAOptimizerTest { new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, boundaries, GoalType.MINIMIZE, - 1e-8, 1e-5, 1000, expected); + 1e-7, 1e-5, 1000, expected); } @Test @@ -464,7 +466,7 @@ public class BOBYQAOptimizerTest { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) - f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; + f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } @@ -484,7 +486,7 @@ public class BOBYQAOptimizerTest { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) - f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; + f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } @@ -501,7 +503,7 @@ public class BOBYQAOptimizerTest { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) - f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i + f += FastMath.pow(FastMath.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); // System.out.print("" + (fcount++) + ") "); // for (int i = 0; i < x.length; i++) @@ -514,7 +516,7 @@ public class BOBYQAOptimizerTest { private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { - double f = Math.pow(new DiffPow().value(x), 0.25); + double f = FastMath.pow(new DiffPow().value(x), 0.25); return f; } } @@ -546,12 +548,12 @@ public class BOBYQAOptimizerTest { double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { - fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); + fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; - res2 += Math.cos(2. * Math.PI * fac * x[i]); + res2 += FastMath.cos(2. * FastMath.PI * fac * x[i]); } - f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) - + Math.exp(1.) - Math.exp(res2 / x.length)); + f = (20. - 20. * FastMath.exp(-0.2 * FastMath.sqrt(f / x.length)) + + FastMath.exp(1.) - FastMath.exp(res2 / x.length)); return f; } } @@ -574,11 +576,11 @@ public class BOBYQAOptimizerTest { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { - fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); + fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude - * (1. - Math.cos(2. * Math.PI * fac * x[i])); + * (1. - FastMath.cos(2. * FastMath.PI * fac * x[i])); } return f; } @@ -623,7 +625,7 @@ public class BOBYQAOptimizerTest { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) - basis[i][k] /= Math.sqrt(sp); + basis[i][k] /= FastMath.sqrt(sp); } } } diff --git a/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest.java b/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest.java index d17ae6e76..8ed6cb703 100644 --- a/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest.java +++ b/src/test/java/org/apache/commons/math3/optimization/direct/BOBYQAOptimizerTest.java @@ -28,6 +28,7 @@ import org.apache.commons.math3.optimization.GoalType; import org.apache.commons.math3.optimization.PointValuePair; import org.apache.commons.math3.optimization.InitialGuess; import org.apache.commons.math3.optimization.SimpleBounds; +import org.apache.commons.math3.util.FastMath; import org.junit.Assert; import org.junit.Ignore; import org.junit.Test; @@ -207,7 +208,7 @@ public class BOBYQAOptimizerTest { new PointValuePair(point(DIM,0.0),0.0); doTest(new Ackley(), startPoint, boundaries, GoalType.MINIMIZE, - 1e-8, 1e-5, 1000, expected); + 1e-7, 1e-5, 1000, expected); } @Test @@ -317,7 +318,7 @@ public class BOBYQAOptimizerTest { int dim = startPoint.length; // MultivariateOptimizer optim = -// new PowellOptimizer(1e-13, Math.ulp(1d)); +// new PowellOptimizer(1e-13, FastMath.ulp(1d)); // PointValuePair result = optim.optimize(100000, func, goal, startPoint); final double[] lB = boundaries == null ? null : boundaries[0]; final double[] uB = boundaries == null ? null : boundaries[1]; @@ -462,7 +463,7 @@ public class BOBYQAOptimizerTest { double f = 0; x = B.Rotate(x); for (int i = 0; i < x.length; ++i) - f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; + f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } @@ -482,7 +483,7 @@ public class BOBYQAOptimizerTest { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) - f += Math.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; + f += FastMath.pow(factor, i / (x.length - 1.)) * x[i] * x[i]; return f; } } @@ -499,7 +500,7 @@ public class BOBYQAOptimizerTest { public double value(double[] x) { double f = 0; for (int i = 0; i < x.length; ++i) - f += Math.pow(Math.abs(x[i]), 2. + 10 * (double) i + f += FastMath.pow(FastMath.abs(x[i]), 2. + 10 * (double) i / (x.length - 1.)); // System.out.print("" + (fcount++) + ") "); // for (int i = 0; i < x.length; i++) @@ -512,7 +513,7 @@ public class BOBYQAOptimizerTest { private static class SsDiffPow implements MultivariateFunction { public double value(double[] x) { - double f = Math.pow(new DiffPow().value(x), 0.25); + double f = FastMath.pow(new DiffPow().value(x), 0.25); return f; } } @@ -544,12 +545,12 @@ public class BOBYQAOptimizerTest { double res2 = 0; double fac = 0; for (int i = 0; i < x.length; ++i) { - fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); + fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.)); f += fac * fac * x[i] * x[i]; - res2 += Math.cos(2. * Math.PI * fac * x[i]); + res2 += FastMath.cos(2. * FastMath.PI * fac * x[i]); } - f = (20. - 20. * Math.exp(-0.2 * Math.sqrt(f / x.length)) - + Math.exp(1.) - Math.exp(res2 / x.length)); + f = (20. - 20. * FastMath.exp(-0.2 * FastMath.sqrt(f / x.length)) + + FastMath.exp(1.) - FastMath.exp(res2 / x.length)); return f; } } @@ -572,11 +573,11 @@ public class BOBYQAOptimizerTest { double f = 0; double fac; for (int i = 0; i < x.length; ++i) { - fac = Math.pow(axisratio, (i - 1.) / (x.length - 1.)); + fac = FastMath.pow(axisratio, (i - 1.) / (x.length - 1.)); if (i == 0 && x[i] < 0) fac *= 1.; f += fac * fac * x[i] * x[i] + amplitude - * (1. - Math.cos(2. * Math.PI * fac * x[i])); + * (1. - FastMath.cos(2. * FastMath.PI * fac * x[i])); } return f; } @@ -621,7 +622,7 @@ public class BOBYQAOptimizerTest { for (sp = 0., k = 0; k < DIM; ++k) sp += basis[i][k] * basis[i][k]; /* squared norm */ for (k = 0; k < DIM; ++k) - basis[i][k] /= Math.sqrt(sp); + basis[i][k] /= FastMath.sqrt(sp); } } }