View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.math3.optim.univariate;
18  
19  import org.apache.commons.math3.util.FastMath;
20  import org.apache.commons.math3.exception.NotStrictlyPositiveException;
21  import org.apache.commons.math3.optim.AbstractConvergenceChecker;
22  
23  /**
24   * Simple implementation of the
25   * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface
26   * that uses only objective function values.
27   *
28   * Convergence is considered to have been reached if either the relative
29   * difference between the objective function values is smaller than a
30   * threshold or if either the absolute difference between the objective
31   * function values is smaller than another threshold.
32   * <br/>
33   * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
34   * converged} method will also return {@code true} if the number of iterations
35   * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
36   * this constructor}).
37   *
38   * @version $Id: SimpleUnivariateValueChecker.java 1462503 2013-03-29 15:48:27Z luc $
39   * @since 3.1
40   */
41  public class SimpleUnivariateValueChecker
42      extends AbstractConvergenceChecker<UnivariatePointValuePair> {
43      /**
44       * If {@link #maxIterationCount} is set to this value, the number of
45       * iterations will never cause
46       * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
47       * to return {@code true}.
48       */
49      private static final int ITERATION_CHECK_DISABLED = -1;
50      /**
51       * Number of iterations after which the
52       * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
53       * method will return true (unless the check is disabled).
54       */
55      private final int maxIterationCount;
56  
57      /** Build an instance with specified thresholds.
58       *
59       * In order to perform only relative checks, the absolute tolerance
60       * must be set to a negative value. In order to perform only absolute
61       * checks, the relative tolerance must be set to a negative value.
62       *
63       * @param relativeThreshold relative tolerance threshold
64       * @param absoluteThreshold absolute tolerance threshold
65       */
66      public SimpleUnivariateValueChecker(final double relativeThreshold,
67                                          final double absoluteThreshold) {
68          super(relativeThreshold, absoluteThreshold);
69          maxIterationCount = ITERATION_CHECK_DISABLED;
70      }
71  
72      /**
73       * Builds an instance with specified thresholds.
74       *
75       * In order to perform only relative checks, the absolute tolerance
76       * must be set to a negative value. In order to perform only absolute
77       * checks, the relative tolerance must be set to a negative value.
78       *
79       * @param relativeThreshold relative tolerance threshold
80       * @param absoluteThreshold absolute tolerance threshold
81       * @param maxIter Maximum iteration count.
82       * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
83       *
84       * @since 3.1
85       */
86      public SimpleUnivariateValueChecker(final double relativeThreshold,
87                                          final double absoluteThreshold,
88                                          final int maxIter) {
89          super(relativeThreshold, absoluteThreshold);
90  
91          if (maxIter <= 0) {
92              throw new NotStrictlyPositiveException(maxIter);
93          }
94          maxIterationCount = maxIter;
95      }
96  
97      /**
98       * Check if the optimization algorithm has converged considering the
99       * last two points.
100      * This method may be called several time from the same algorithm
101      * iteration with different points. This can be detected by checking the
102      * iteration number at each call if needed. Each time this method is
103      * called, the previous and current point correspond to points with the
104      * same role at each iteration, so they can be compared. As an example,
105      * simplex-based algorithms call this method for all points of the simplex,
106      * not only for the best or worst ones.
107      *
108      * @param iteration Index of current iteration
109      * @param previous Best point in the previous iteration.
110      * @param current Best point in the current iteration.
111      * @return {@code true} if the algorithm has converged.
112      */
113     @Override
114     public boolean converged(final int iteration,
115                              final UnivariatePointValuePair previous,
116                              final UnivariatePointValuePair current) {
117         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
118             return true;
119         }
120 
121         final double p = previous.getValue();
122         final double c = current.getValue();
123         final double difference = FastMath.abs(p - c);
124         final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
125         return difference <= size * getRelativeThreshold() ||
126             difference <= getAbsoluteThreshold();
127     }
128 }