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.math.optimization;
19
20 import org.apache.commons.math.util.MathUtils;
21
22 /**
23 * Simple implementation of the {@link RealConvergenceChecker} interface using
24 * only point coordinates.
25 * <p>
26 * Convergence is considered to have been reached if either the relative
27 * difference between each point coordinate are smaller than a threshold
28 * or if either the absolute difference between the point coordinates are
29 * smaller than another threshold.
30 * </p>
31 * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
32 * @since 2.0
33 */
34 public class SimpleRealPointChecker implements RealConvergenceChecker {
35
36 /** Default relative threshold. */
37 private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
38
39 /** Default absolute threshold. */
40 private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
41
42 /** Relative tolerance threshold. */
43 private final double relativeThreshold;
44
45 /** Absolute tolerance threshold. */
46 private final double absoluteThreshold;
47
48 /** Build an instance with default threshold.
49 */
50 public SimpleRealPointChecker() {
51 this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
52 this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
53 }
54
55 /** Build an instance with a specified threshold.
56 * <p>
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 * </p>
61 * @param relativeThreshold relative tolerance threshold
62 * @param absoluteThreshold absolute tolerance threshold
63 */
64 public SimpleRealPointChecker(final double relativeThreshold,
65 final double absoluteThreshold) {
66 this.relativeThreshold = relativeThreshold;
67 this.absoluteThreshold = absoluteThreshold;
68 }
69
70 /** {@inheritDoc} */
71 public boolean converged(final int iteration,
72 final RealPointValuePair previous,
73 final RealPointValuePair current) {
74 final double[] p = previous.getPoint();
75 final double[] c = current.getPoint();
76 for (int i = 0; i < p.length; ++i) {
77 final double difference = Math.abs(p[i] - c[i]);
78 final double size = Math.max(Math.abs(p[i]), Math.abs(c[i]));
79 if ((difference > (size * relativeThreshold)) && (difference > absoluteThreshold)) {
80 return false;
81 }
82 }
83 return true;
84 }
85
86 }