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.math3.optimization;
19  
20  import org.apache.commons.math3.util.FastMath;
21  import org.apache.commons.math3.exception.NotStrictlyPositiveException;
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   * @version $Id: SimpleValueChecker.java 1591835 2014-05-02 09:04:01Z tn $
37   * @deprecated As of 3.1 (to be removed in 4.0).
38   * @since 3.0
39   */
40  @Deprecated
41  public class SimpleValueChecker
42      extends AbstractConvergenceChecker<PointValuePair> {
43      /**
44       * If {@link #maxIterationCount} is set to this value, the number of
45       * iterations will never cause
46       * {@link #converged(int,PointValuePair,PointValuePair)}
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,PointValuePair,PointValuePair)} method
53       * will return true (unless the check is disabled).
54       */
55      private final int maxIterationCount;
56  
57      /**
58       * Build an instance with default thresholds.
59       * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()}
60       */
61      @Deprecated
62      public SimpleValueChecker() {
63          maxIterationCount = ITERATION_CHECK_DISABLED;
64      }
65  
66      /** Build an instance with specified thresholds.
67       *
68       * In order to perform only relative checks, the absolute tolerance
69       * must be set to a negative value. In order to perform only absolute
70       * checks, the relative tolerance must be set to a negative value.
71       *
72       * @param relativeThreshold relative tolerance threshold
73       * @param absoluteThreshold absolute tolerance threshold
74       */
75      public SimpleValueChecker(final double relativeThreshold,
76                                final double absoluteThreshold) {
77          super(relativeThreshold, absoluteThreshold);
78          maxIterationCount = ITERATION_CHECK_DISABLED;
79      }
80  
81      /**
82       * Builds an instance with specified thresholds.
83       *
84       * In order to perform only relative checks, the absolute tolerance
85       * must be set to a negative value. In order to perform only absolute
86       * checks, the relative tolerance must be set to a negative value.
87       *
88       * @param relativeThreshold relative tolerance threshold
89       * @param absoluteThreshold absolute tolerance threshold
90       * @param maxIter Maximum iteration count.
91       * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
92       *
93       * @since 3.1
94       */
95      public SimpleValueChecker(final double relativeThreshold,
96                                final double absoluteThreshold,
97                                final int maxIter) {
98          super(relativeThreshold, absoluteThreshold);
99  
100         if (maxIter <= 0) {
101             throw new NotStrictlyPositiveException(maxIter);
102         }
103         maxIterationCount = maxIter;
104     }
105 
106     /**
107      * Check if the optimization algorithm has converged considering the
108      * last two points.
109      * This method may be called several time from the same algorithm
110      * iteration with different points. This can be detected by checking the
111      * iteration number at each call if needed. Each time this method is
112      * called, the previous and current point correspond to points with the
113      * same role at each iteration, so they can be compared. As an example,
114      * simplex-based algorithms call this method for all points of the simplex,
115      * not only for the best or worst ones.
116      *
117      * @param iteration Index of current iteration
118      * @param previous Best point in the previous iteration.
119      * @param current Best point in the current iteration.
120      * @return {@code true} if the algorithm has converged.
121      */
122     @Override
123     public boolean converged(final int iteration,
124                              final PointValuePair previous,
125                              final PointValuePair current) {
126         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
127             return true;
128         }
129 
130         final double p = previous.getValue();
131         final double c = current.getValue();
132         final double difference = FastMath.abs(p - c);
133         final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
134         return difference <= size * getRelativeThreshold() ||
135             difference <= getAbsoluteThreshold();
136     }
137 }