blob: fbfd30cb75bf073c05d38836f4e91cc50e5936ab [file] [log] [blame]
Raymonddee08492015-04-02 10:43:13 -07001/*
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
18package org.apache.commons.math.optimization;
19
20import org.apache.commons.math.util.FastMath;
21import org.apache.commons.math.util.MathUtils;
22
23/**
24 * Simple implementation of the {@link RealConvergenceChecker} interface using
25 * only objective function values.
26 * <p>
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.
31 * </p>
32 * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
33 * @since 2.0
34 */
35public class SimpleScalarValueChecker implements RealConvergenceChecker {
36
37 /** Default relative threshold. */
38 private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
39
40 /** Default absolute threshold. */
41 private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
42
43 /** Relative tolerance threshold. */
44 private final double relativeThreshold;
45
46 /** Absolute tolerance threshold. */
47 private final double absoluteThreshold;
48
49 /** Build an instance with default threshold.
50 */
51 public SimpleScalarValueChecker() {
52 this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
53 this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
54 }
55
56 /** Build an instance with a specified threshold.
57 * <p>
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 * </p>
62 * @param relativeThreshold relative tolerance threshold
63 * @param absoluteThreshold absolute tolerance threshold
64 */
65 public SimpleScalarValueChecker(final double relativeThreshold,
66 final double absoluteThreshold) {
67 this.relativeThreshold = relativeThreshold;
68 this.absoluteThreshold = absoluteThreshold;
69 }
70
71 /** {@inheritDoc} */
72 public boolean converged(final int iteration,
73 final RealPointValuePair previous,
74 final RealPointValuePair current) {
75 final double p = previous.getValue();
76 final double c = current.getValue();
77 final double difference = FastMath.abs(p - c);
78 final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
79 return (difference <= (size * relativeThreshold)) || (difference <= absoluteThreshold);
80 }
81}