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  
18  package org.apache.commons.math4.legacy.optim;
19  
20  import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
22  
23  /**
24   * Simple implementation of the {@link ConvergenceChecker} interface using
25   * only objective function values.
26   *
27   * Convergence is considered to have been reached if either the relative
28   * difference between the objective function values is smaller than a
29   * threshold or if either the absolute difference between the objective
30   * function values is smaller than another threshold.
31   * <br>
32   * The {@link #converged(int,PointValuePair,PointValuePair) converged}
33   * method will also return {@code true} if the number of iterations has been set
34   * (see {@link #SimpleValueChecker(double,double,int) this constructor}).
35   *
36   * @since 3.0
37   */
38  public class SimpleValueChecker
39      extends AbstractConvergenceChecker<PointValuePair> {
40      /**
41       * If {@link #maxIterationCount} is set to this value, the number of
42       * iterations will never cause
43       * {@link #converged(int,PointValuePair,PointValuePair)}
44       * to return {@code true}.
45       */
46      private static final int ITERATION_CHECK_DISABLED = -1;
47      /**
48       * Number of iterations after which the
49       * {@link #converged(int,PointValuePair,PointValuePair)} method
50       * will return true (unless the check is disabled).
51       */
52      private final int maxIterationCount;
53  
54      /** Build an instance with specified thresholds.
55       *
56       * In order to perform only relative checks, the absolute tolerance
57       * must be set to a negative value. In order to perform only absolute
58       * checks, the relative tolerance must be set to a negative value.
59       *
60       * @param relativeThreshold relative tolerance threshold
61       * @param absoluteThreshold absolute tolerance threshold
62       */
63      public SimpleValueChecker(final double relativeThreshold,
64                                final double absoluteThreshold) {
65          super(relativeThreshold, absoluteThreshold);
66          maxIterationCount = ITERATION_CHECK_DISABLED;
67      }
68  
69      /**
70       * Builds an instance with specified thresholds.
71       *
72       * In order to perform only relative checks, the absolute tolerance
73       * must be set to a negative value. In order to perform only absolute
74       * checks, the relative tolerance must be set to a negative value.
75       *
76       * @param relativeThreshold relative tolerance threshold
77       * @param absoluteThreshold absolute tolerance threshold
78       * @param maxIter Maximum iteration count.
79       * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
80       *
81       * @since 3.1
82       */
83      public SimpleValueChecker(final double relativeThreshold,
84                                final double absoluteThreshold,
85                                final int maxIter) {
86          super(relativeThreshold, absoluteThreshold);
87  
88          if (maxIter <= 0) {
89              throw new NotStrictlyPositiveException(maxIter);
90          }
91          maxIterationCount = maxIter;
92      }
93  
94      /**
95       * Check if the optimization algorithm has converged considering the
96       * last two points.
97       * This method may be called several time from the same algorithm
98       * iteration with different points. This can be detected by checking the
99       * iteration number at each call if needed. Each time this method is
100      * called, the previous and current point correspond to points with the
101      * same role at each iteration, so they can be compared. As an example,
102      * simplex-based algorithms call this method for all points of the simplex,
103      * not only for the best or worst ones.
104      *
105      * @param iteration Index of current iteration
106      * @param previous Best point in the previous iteration.
107      * @param current Best point in the current iteration.
108      * @return {@code true} if the algorithm has converged.
109      */
110     @Override
111     public boolean converged(final int iteration,
112                              final PointValuePair previous,
113                              final PointValuePair current) {
114         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
115             return true;
116         }
117 
118         final double p = previous.getValue();
119         final double c = current.getValue();
120         final double difference = JdkMath.abs(p - c);
121         final double size = JdkMath.max(JdkMath.abs(p), JdkMath.abs(c));
122         return difference <= size * getRelativeThreshold() ||
123             difference <= getAbsoluteThreshold();
124     }
125 }