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