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 2012/04/25 16:49:45 UTC
svn commit: r1330321 - in /commons/proper/math/trunk/src:
main/java/org/apache/commons/math3/optimization/univariate/
test/java/org/apache/commons/math3/optimization/univariate/
Author: erans
Date: Wed Apr 25 14:49:44 2012
New Revision: 1330321
URL: http://svn.apache.org/viewvc?rev=1330321&view=rev
Log:
MATH-782
Moved incorrectly placed block of code (user-defined stopping criterion).
Added unit test.
New utility class for simple stopping criterion based on (univariate)
function values.
Added:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java (with props)
Modified:
commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.java
commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.java
Modified: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.java?rev=1330321&r1=1330320&r2=1330321&view=diff
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.java (original)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/BrentOptimizer.java Wed Apr 25 14:49:44 2012
@@ -233,6 +233,16 @@ public class BrentOptimizer extends Base
fw = fx;
x = u;
fx = fu;
+
+ // User-defined convergence checker.
+ previous = current;
+ current = new UnivariatePointValuePair(x, isMinim ? fx : -fx);
+
+ if (checker != null) {
+ if (checker.converged(iter, previous, current)) {
+ return current;
+ }
+ }
} else {
if (u < x) {
a = u;
@@ -252,16 +262,6 @@ public class BrentOptimizer extends Base
fv = fu;
}
}
-
- previous = current;
- current = new UnivariatePointValuePair(x, isMinim ? fx : -fx);
-
- // User-defined convergence checker.
- if (checker != null) {
- if (checker.converged(iter, previous, current)) {
- return current;
- }
- }
} else { // Default termination (Brent's criterion).
return current;
}
Added: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java?rev=1330321&view=auto
==============================================================================
--- commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java (added)
+++ commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java Wed Apr 25 14:49:44 2012
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.commons.math3.optimization.univariate;
+
+import org.apache.commons.math3.util.FastMath;
+import org.apache.commons.math3.optimization.AbstractConvergenceChecker;
+
+/**
+ * Simple implementation of the
+ * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface
+ * that uses only objective function values.
+ *
+ * Convergence is considered to have been reached if either the relative
+ * difference between the objective function values is smaller than a
+ * threshold or if either the absolute difference between the objective
+ * function values is smaller than another threshold.
+ *
+ * @version $Id$
+ * @since 3.1
+ */
+public class SimpleUnivariateValueChecker
+ extends AbstractConvergenceChecker<UnivariatePointValuePair> {
+ /**
+ * Build an instance with default thresholds.
+ */
+ public SimpleUnivariateValueChecker() {}
+
+ /** Build an instance with specified thresholds.
+ *
+ * In order to perform only relative checks, the absolute tolerance
+ * must be set to a negative value. In order to perform only absolute
+ * checks, the relative tolerance must be set to a negative value.
+ *
+ * @param relativeThreshold relative tolerance threshold
+ * @param absoluteThreshold absolute tolerance threshold
+ */
+ public SimpleUnivariateValueChecker(final double relativeThreshold,
+ final double absoluteThreshold) {
+ super(relativeThreshold, absoluteThreshold);
+ }
+
+ /**
+ * Check if the optimization algorithm has converged considering the
+ * last two points.
+ * This method may be called several time from the same algorithm
+ * iteration with different points. This can be detected by checking the
+ * iteration number at each call if needed. Each time this method is
+ * called, the previous and current point correspond to points with the
+ * same role at each iteration, so they can be compared. As an example,
+ * simplex-based algorithms call this method for all points of the simplex,
+ * not only for the best or worst ones.
+ *
+ * @param iteration Index of current iteration
+ * @param previous Best point in the previous iteration.
+ * @param current Best point in the current iteration.
+ * @return {@code true} if the algorithm has converged.
+ */
+ @Override
+ public boolean converged(final int iteration,
+ final UnivariatePointValuePair previous,
+ final UnivariatePointValuePair current) {
+ final double p = previous.getValue();
+ final double c = current.getValue();
+ final double difference = FastMath.abs(p - c);
+ final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
+ return difference <= size * getRelativeThreshold() ||
+ difference <= getAbsoluteThreshold();
+ }
+}
Propchange: commons/proper/math/trunk/src/main/java/org/apache/commons/math3/optimization/univariate/SimpleUnivariateValueChecker.java
------------------------------------------------------------------------------
svn:eol-style = native
Modified: commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.java
URL: http://svn.apache.org/viewvc/commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.java?rev=1330321&r1=1330320&r2=1330321&view=diff
==============================================================================
--- commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.java (original)
+++ commons/proper/math/trunk/src/test/java/org/apache/commons/math3/optimization/univariate/BrentOptimizerTest.java Wed Apr 25 14:49:44 2012
@@ -24,6 +24,7 @@ import org.apache.commons.math3.analysis
import org.apache.commons.math3.analysis.SinFunction;
import org.apache.commons.math3.analysis.UnivariateFunction;
import org.apache.commons.math3.optimization.GoalType;
+import org.apache.commons.math3.optimization.ConvergenceChecker;
import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
import org.junit.Assert;
import org.junit.Test;
@@ -52,6 +53,18 @@ public final class BrentOptimizerTest {
}
@Test
+ public void testSinMinWithValueChecker() {
+ final UnivariateFunction f = new SinFunction();
+ final ConvergenceChecker checker = new SimpleUnivariateValueChecker(1e-5, 1e-14);
+ // The default stopping criterion of Brent's algorithm should not
+ // pass, but the search will stop at the given relative tolerance
+ // for the function value.
+ final UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14, checker);
+ final UnivariatePointValuePair result = optimizer.optimize(200, f, GoalType.MINIMIZE, 4, 5);
+ Assert.assertEquals(3 * Math.PI / 2, result.getPoint(), 1e-3);
+ }
+
+ @Test
public void testBoundaries() {
final double lower = -1.0;
final double upper = +1.0;