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;
23  
24  import org.hipparchus.exception.LocalizedCoreFormats;
25  import org.hipparchus.exception.MathIllegalArgumentException;
26  import org.hipparchus.util.FastMath;
27  import org.hipparchus.util.Pair;
28  
29  /**
30   * Simple implementation of the {@link ConvergenceChecker} interface using
31   * only point coordinates.
32   *
33   * Convergence is considered to have been reached if either the relative
34   * difference between each point coordinate are smaller than a threshold
35   * or if either the absolute difference between the point coordinates are
36   * smaller than another threshold.
37   * <br>
38   * The {@link #converged(int,Pair,Pair) converged} method will also return
39   * {@code true} if the number of iterations has been set (see
40   * {@link #SimplePointChecker(double,double,int) this constructor}).
41   *
42   * @param <P> Type of the (point, value) pair.
43   * The type of the "value" part of the pair (not used by this class).
44   *
45   */
46  public class SimplePointChecker<P extends Pair<double[], ? extends Object>>
47      extends AbstractConvergenceChecker<P> {
48      /**
49       * If {@link #maxIterationCount} is set to this value, the number of
50       * iterations will never cause {@link #converged(int, Pair, Pair)}
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, Pair, Pair)} method
57       * will return true (unless the check is disabled).
58       */
59      private final int maxIterationCount;
60  
61      /**
62       * Build an instance with specified thresholds.
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 SimplePointChecker(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       * In order to perform only relative checks, the absolute tolerance
79       * must be set to a negative value. In order to perform only absolute
80       * checks, the relative tolerance must be set to a negative value.
81       *
82       * @param relativeThreshold Relative tolerance threshold.
83       * @param absoluteThreshold Absolute tolerance threshold.
84       * @param maxIter Maximum iteration count.
85       * @throws MathIllegalArgumentException if {@code maxIter <= 0}.
86       *
87       */
88      public SimplePointChecker(final double relativeThreshold,
89                                final double absoluteThreshold,
90                                final int maxIter) {
91          super(relativeThreshold, absoluteThreshold);
92  
93          if (maxIter <= 0) {
94              throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
95                                                     maxIter, 0);
96          }
97          maxIterationCount = maxIter;
98      }
99  
100     /**
101      * Check if the optimization algorithm has converged considering the
102      * last two points.
103      * This method may be called several times from the same algorithm
104      * iteration with different points. This can be detected by checking the
105      * iteration number at each call if needed. Each time this method is
106      * called, the previous and current point correspond to points with the
107      * same role at each iteration, so they can be compared. As an example,
108      * simplex-based algorithms call this method for all points of the simplex,
109      * not only for the best or worst ones.
110      *
111      * @param iteration Index of current iteration
112      * @param previous Best point in the previous iteration.
113      * @param current Best point in the current iteration.
114      * @return {@code true} if the arguments satify the convergence criterion.
115      */
116     @Override
117     public boolean converged(final int iteration,
118                              final P previous,
119                              final P current) {
120         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
121             return true;
122         }
123 
124         final double[] p = previous.getKey();
125         final double[] c = current.getKey();
126         for (int i = 0; i < p.length; ++i) {
127             final double pi = p[i];
128             final double ci = c[i];
129             final double difference = FastMath.abs(pi - ci);
130             final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
131             if (difference > size * getRelativeThreshold() &&
132                 difference > getAbsoluteThreshold()) {
133                 return false;
134             }
135         }
136         return true;
137     }
138 }