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