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 VectorialConvergenceChecker} 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: 795972 $ $Date: 2009-07-20 15:34:06 -0400 (Mon, 20 Jul 2009) $
32 * @since 2.0
33 */
34 public class SimpleVectorialPointChecker implements VectorialConvergenceChecker {
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 SimpleVectorialPointChecker() {
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 SimpleVectorialPointChecker(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 VectorialPointValuePair previous,
73 final VectorialPointValuePair current) {
74 final double[] p = previous.getPointRef();
75 final double[] c = current.getPointRef();
76 for (int i = 0; i < p.length; ++i) {
77 final double pi = p[i];
78 final double ci = c[i];
79 final double difference = Math.abs(pi - ci);
80 final double size = Math.max(Math.abs(pi), Math.abs(ci));
81 if ((difference > (size * relativeThreshold)) &&
82 (difference > absoluteThreshold)) {
83 return false;
84 }
85 }
86 return true;
87 }
88
89 }