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