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