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