Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 74FC69727 for ; Wed, 25 Apr 2012 14:50:08 +0000 (UTC) Received: (qmail 50576 invoked by uid 500); 25 Apr 2012 14:50:08 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 50497 invoked by uid 500); 25 Apr 2012 14:50:07 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 50485 invoked by uid 99); 25 Apr 2012 14:50:07 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Apr 2012 14:50:07 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 25 Apr 2012 14:50:05 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 8B52623888CD for ; Wed, 25 Apr 2012 14:49:45 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: 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/ Date: Wed, 25 Apr 2012 14:49:45 -0000 To: commits@commons.apache.org From: erans@apache.org X-Mailer: svnmailer-1.0.8-patched Message-Id: <20120425144945.8B52623888CD@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 { + /** + * 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;