You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by er...@apache.org on 2011/12/20 14:30:29 UTC
svn commit: r1221253 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java
site/xdoc/changes.xml
test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java
Author: erans
Date: Tue Dec 20 13:30:29 2011
New Revision: 1221253
URL: http://svn.apache.org/viewvc?rev=1221253&view=rev
Log:
MATH-728
Offset by one bug. Suggested fix provided by Bruce Johnson.
I've added a unit test that exercises the use of more interpolation points;
two previously unexplored code paths are now being traversed.
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java
commons/proper/math/trunk/src/site/xdoc/changes.xml
commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java?rev=1221253&r1=1221252&r2=1221253&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizer.java Tue Dec 20 13:30:29 2011
@@ -1657,10 +1657,12 @@ public class BOBYQAOptimizer
final int tmp2 = jpt;
jpt = ipt - n;
ipt = tmp2;
- throw new PathIsExploredException(); // XXX
+// throw new PathIsExploredException(); // XXX
}
- interpolationPoints.setEntry(nfm, ipt, interpolationPoints.getEntry(ipt, ipt));
- interpolationPoints.setEntry(nfm, jpt, interpolationPoints.getEntry(jpt, jpt));
+ final int iptMinus1 = ipt - 1;
+ final int jptMinus1 = jpt - 1;
+ interpolationPoints.setEntry(nfm, iptMinus1, interpolationPoints.getEntry(ipt, iptMinus1));
+ interpolationPoints.setEntry(nfm, jptMinus1, interpolationPoints.getEntry(jpt, jptMinus1));
}
// Calculate the next value of F. The least function value so far and
@@ -1747,7 +1749,7 @@ public class BOBYQAOptimizer
final int ih = ipt * (ipt - 1) / 2 + jpt - 1;
final double tmp = interpolationPoints.getEntry(nfm, ipt - 1) * interpolationPoints.getEntry(nfm, jpt - 1);
modelSecondDerivativesValues.setEntry(ih, (fbeg - fAtInterpolationPoints.getEntry(ipt) - fAtInterpolationPoints.getEntry(jpt) + f) / tmp);
- throw new PathIsExploredException(); // XXX
+// throw new PathIsExploredException(); // XXX
}
} while (getEvaluations() < npt);
} // prelim
@@ -2462,19 +2464,19 @@ public class BOBYQAOptimizer
private static void printMethod() {
// System.out.println(caller(2));
}
-}
-/**
- * Marker for code paths that are not explored with the current unit tests.
- * If the path becomes explored, it should just be removed from the code.
- */
-class PathIsExploredException extends RuntimeException {
- private static final long serialVersionUID = 745350979634801853L;
+ /**
+ * Marker for code paths that are not explored with the current unit tests.
+ * If the path becomes explored, it should just be removed from the code.
+ */
+ private static class PathIsExploredException extends RuntimeException {
+ private static final long serialVersionUID = 745350979634801853L;
- private static final String PATH_IS_EXPLORED
- = "If this exception is thrown, just remove it from the code";
+ private static final String PATH_IS_EXPLORED
+ = "If this exception is thrown, just remove it from the code";
- PathIsExploredException() {
- super(PATH_IS_EXPLORED);
+ PathIsExploredException() {
+ super(PATH_IS_EXPLORED + " " + BOBYQAOptimizer.caller(3));
+ }
}
}
Modified: commons/proper/math/trunk/src/site/xdoc/changes.xml
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/site/xdoc/changes.xml?rev=1221253&r1=1221252&r2=1221253&view=diff
==============================================================================
--- commons/proper/math/trunk/src/site/xdoc/changes.xml (original)
+++ commons/proper/math/trunk/src/site/xdoc/changes.xml Tue Dec 20 13:30:29 2011
@@ -52,6 +52,9 @@ The <action> type attribute can be add,u
If the output is not quite correct, check for invisible trailing spaces!
-->
<release version="3.0" date="TBD" description="TBD">
+ <action dev="erans" type="fix" issue="MATH-728" due-to="Bruce A. Johnson">
+ Fixed "offset by one" bug in "BOBYQAOptimizer".
+ </action>
<action dev="luc" type="fix" issue="MATH-727">
Check first step size in embedded Runge-Kutta integrators, and truncate it if needed.
</action>
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java?rev=1221253&r1=1221252&r2=1221253&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math/optimization/direct/BOBYQAOptimizerTest.java Tue Dec 20 13:30:29 2011
@@ -231,6 +231,60 @@ public class BOBYQAOptimizerTest {
1e-13, 1e-6, 2000, expected);
}
+ // See MATH-728
+ @Test
+ public void testConstrainedRosenWithMoreInterpolationPoints() {
+ final double[] startPoint = point(DIM, 0.1);
+ final double[][] boundaries = boundaries(DIM, -1, 2);
+ final RealPointValuePair expected = new RealPointValuePair(point(DIM, 1.0), 0.0);
+
+ // This should have been 78 because in the code the hard limit is
+ // said to be
+ // ((DIM + 1) * (DIM + 2)) / 2 - (2 * DIM + 1)
+ // i.e. 78 in this case, but the test fails for 48, 59, 62, 63, 64,
+ // 65, 66, ...
+ final int maxAdditionalPoints = 47;
+
+ for (int num = 1; num <= maxAdditionalPoints; num++) {
+ doTest(new Rosen(), startPoint, boundaries,
+ GoalType.MINIMIZE,
+ 1e-12, 1e-6, 2000,
+ num,
+ expected,
+ "num=" + num);
+ }
+ }
+
+ /**
+ * @param func Function to optimize.
+ * @param startPoint Starting point.
+ * @param boundaries Upper / lower point limit.
+ * @param goal Minimization or maximization.
+ * @param fTol Tolerance relative error on the objective function.
+ * @param pointTol Tolerance for checking that the optimum is correct.
+ * @param maxEvaluations Maximum number of evaluations.
+ * @param expected Expected point / value.
+ */
+ private void doTest(MultivariateFunction func,
+ double[] startPoint,
+ double[][] boundaries,
+ GoalType goal,
+ double fTol,
+ double pointTol,
+ int maxEvaluations,
+ RealPointValuePair expected) {
+ doTest(func,
+ startPoint,
+ boundaries,
+ goal,
+ fTol,
+ pointTol,
+ maxEvaluations,
+ 0,
+ expected,
+ "");
+ }
+
/**
* @param func Function to optimize.
* @param startPoint Starting point.
@@ -239,16 +293,20 @@ public class BOBYQAOptimizerTest {
* @param fTol Tolerance relative error on the objective function.
* @param pointTol Tolerance for checking that the optimum is correct.
* @param maxEvaluations Maximum number of evaluations.
+ * @param additionalInterpolationPoints Number of interpolation to used
+ * in addition to the default (2 * dim + 1).
* @param expected Expected point / value.
*/
private void doTest(MultivariateFunction func,
- double[] startPoint,
- double[][] boundaries,
- GoalType goal,
- double fTol,
- double pointTol,
- int maxEvaluations,
- RealPointValuePair expected) {
+ double[] startPoint,
+ double[][] boundaries,
+ GoalType goal,
+ double fTol,
+ double pointTol,
+ int maxEvaluations,
+ int additionalInterpolationPoints,
+ RealPointValuePair expected,
+ String assertMsg) {
System.out.println(func.getClass().getName() + " BEGIN"); // XXX
@@ -258,17 +316,17 @@ public class BOBYQAOptimizerTest {
// RealPointValuePair result = optim.optimize(100000, func, goal, startPoint);
final double[] lB = boundaries == null ? null : boundaries[0];
final double[] uB = boundaries == null ? null : boundaries[1];
- BOBYQAOptimizer optim = new BOBYQAOptimizer(2 * dim + 1);
+ final int numIterpolationPoints = 2 * dim + 1 + additionalInterpolationPoints;
+ BOBYQAOptimizer optim = new BOBYQAOptimizer(numIterpolationPoints);
RealPointValuePair result = optim.optimize(maxEvaluations, func, goal, startPoint, lB, uB);
// System.out.println(func.getClass().getName() + " = "
// + optim.getEvaluations() + " f(");
// for (double x: result.getPoint()) System.out.print(x + " ");
// System.out.println(") = " + result.getValue());
- Assert.assertEquals(expected.getValue(),
- result.getValue(), fTol);
+ Assert.assertEquals(assertMsg, expected.getValue(), result.getValue(), fTol);
for (int i = 0; i < dim; i++) {
Assert.assertEquals(expected.getPoint()[i],
- result.getPoint()[i], pointTol);
+ result.getPoint()[i], pointTol);
}
System.out.println(func.getClass().getName() + " END"); // XXX