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.math4.legacy.optim;
19  
20  import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
21  import org.apache.commons.math4.core.jdkmath.JdkMath;
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 for all vectors elements.
31   * <br>
32   * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged}
33   * method will also return {@code true} if the number of iterations has been set
34   * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}).
35   *
36   * @since 3.0
37   */
38  public class SimpleVectorValueChecker
39      extends AbstractConvergenceChecker<PointVectorValuePair> {
40      /**
41       * If {@link #maxIterationCount} is set to this value, the number of
42       * iterations will never cause
43       * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)}
44       * to return {@code true}.
45       */
46      private static final int ITERATION_CHECK_DISABLED = -1;
47      /**
48       * Number of iterations after which the
49       * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} method
50       * will return true (unless the check is disabled).
51       */
52      private final int maxIterationCount;
53  
54      /**
55       * Build an instance with specified thresholds.
56       *
57       * In order to perform only relative checks, the absolute tolerance
58       * must be set to a negative value. In order to perform only absolute
59       * checks, the relative tolerance must be set to a negative value.
60       *
61       * @param relativeThreshold relative tolerance threshold
62       * @param absoluteThreshold absolute tolerance threshold
63       */
64      public SimpleVectorValueChecker(final double relativeThreshold,
65                                      final double absoluteThreshold) {
66          super(relativeThreshold, absoluteThreshold);
67          maxIterationCount = ITERATION_CHECK_DISABLED;
68      }
69  
70      /**
71       * Builds an instance with specified tolerance thresholds and
72       * iteration count.
73       *
74       * In order to perform only relative checks, the absolute tolerance
75       * must be set to a negative value. In order to perform only absolute
76       * checks, the relative tolerance must be set to a negative value.
77       *
78       * @param relativeThreshold Relative tolerance threshold.
79       * @param absoluteThreshold Absolute tolerance threshold.
80       * @param maxIter Maximum iteration count.
81       * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
82       *
83       * @since 3.1
84       */
85      public SimpleVectorValueChecker(final double relativeThreshold,
86                                      final double absoluteThreshold,
87                                      final int maxIter) {
88          super(relativeThreshold, absoluteThreshold);
89  
90          if (maxIter <= 0) {
91              throw new NotStrictlyPositiveException(maxIter);
92          }
93          maxIterationCount = maxIter;
94      }
95  
96      /**
97       * Check if the optimization algorithm has converged considering the
98       * last two points.
99       * This method may be called several times from the same algorithm
100      * iteration with different points. This can be detected by checking the
101      * iteration number at each call if needed. Each time this method is
102      * called, the previous and current point correspond to points with the
103      * same role at each iteration, so they can be compared. As an example,
104      * simplex-based algorithms call this method for all points of the simplex,
105      * not only for the best or worst ones.
106      *
107      * @param iteration Index of current iteration
108      * @param previous Best point in the previous iteration.
109      * @param current Best point in the current iteration.
110      * @return {@code true} if the arguments satisfy the convergence criterion.
111      */
112     @Override
113     public boolean converged(final int iteration,
114                              final PointVectorValuePair previous,
115                              final PointVectorValuePair current) {
116         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
117             return true;
118         }
119 
120         final double[] p = previous.getValueRef();
121         final double[] c = current.getValueRef();
122         for (int i = 0; i < p.length; ++i) {
123             final double pi         = p[i];
124             final double ci         = c[i];
125             final double difference = JdkMath.abs(pi - ci);
126             final double size       = JdkMath.max(JdkMath.abs(pi), JdkMath.abs(ci));
127             if (difference > size * getRelativeThreshold() &&
128                 difference > getAbsoluteThreshold()) {
129                 return false;
130             }
131         }
132         return true;
133     }
134 }