1dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/*
2dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Licensed to the Apache Software Foundation (ASF) under one or more
3dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * contributor license agreements.  See the NOTICE file distributed with
4dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * this work for additional information regarding copyright ownership.
5dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * The ASF licenses this file to You under the Apache License, Version 2.0
6dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * (the "License"); you may not use this file except in compliance with
7dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * the License.  You may obtain a copy of the License at
8dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
9dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *      http://www.apache.org/licenses/LICENSE-2.0
10dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond *
11dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Unless required by applicable law or agreed to in writing, software
12dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * distributed under the License is distributed on an "AS IS" BASIS,
13dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * See the License for the specific language governing permissions and
15dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * limitations under the License.
16dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
17dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
18dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpackage org.apache.commons.math.optimization;
19dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
20dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.FastMath;
21dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondimport org.apache.commons.math.util.MathUtils;
22dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
23dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond/**
24dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Simple implementation of the {@link RealConvergenceChecker} interface using
25dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * only point coordinates.
26dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * <p>
27dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * Convergence is considered to have been reached if either the relative
28dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * difference between each point coordinate are smaller than a threshold
29dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * or if either the absolute difference between the point coordinates are
30dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * smaller than another threshold.
31dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * </p>
32dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @version $Revision: 990655 $ $Date: 2010-08-29 23:49:40 +0200 (dim. 29 août 2010) $
33dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond * @since 2.0
34dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond */
35dee0849a9704d532af0b550146cbafbaa6ee1d19Raymondpublic class SimpleRealPointChecker implements RealConvergenceChecker {
36dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
37dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Default relative threshold. */
38dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double DEFAULT_RELATIVE_THRESHOLD = 100 * MathUtils.EPSILON;
39dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
40dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Default absolute threshold. */
41dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private static final double DEFAULT_ABSOLUTE_THRESHOLD = 100 * MathUtils.SAFE_MIN;
42dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
43dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Relative tolerance threshold. */
44dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double relativeThreshold;
45dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
46dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Absolute tolerance threshold. */
47dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    private final double absoluteThreshold;
48dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
49dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond   /** Build an instance with default threshold.
50dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
51dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SimpleRealPointChecker() {
52dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.relativeThreshold = DEFAULT_RELATIVE_THRESHOLD;
53dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.absoluteThreshold = DEFAULT_ABSOLUTE_THRESHOLD;
54dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
55dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
56dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** Build an instance with a specified threshold.
57dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * <p>
58dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * In order to perform only relative checks, the absolute tolerance
59dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * must be set to a negative value. In order to perform only absolute
60dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * checks, the relative tolerance must be set to a negative value.
61dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * </p>
62dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param relativeThreshold relative tolerance threshold
63dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     * @param absoluteThreshold absolute tolerance threshold
64dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond     */
65dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public SimpleRealPointChecker(final double relativeThreshold,
66dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                                 final double absoluteThreshold) {
67dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.relativeThreshold = relativeThreshold;
68dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        this.absoluteThreshold = absoluteThreshold;
69dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
70dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
71dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    /** {@inheritDoc} */
72dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    public boolean converged(final int iteration,
73dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                             final RealPointValuePair previous,
74dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                             final RealPointValuePair current) {
75dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double[] p        = previous.getPoint();
76dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        final double[] c        = current.getPoint();
77dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        for (int i = 0; i < p.length; ++i) {
78dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double difference = FastMath.abs(p[i] - c[i]);
79dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            final double size       = FastMath.max(FastMath.abs(p[i]), FastMath.abs(c[i]));
80dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            if ((difference > (size * relativeThreshold)) && (difference > absoluteThreshold)) {
81dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond                return false;
82dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond            }
83dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        }
84dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond        return true;
85dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond    }
86dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond
87dee0849a9704d532af0b550146cbafbaa6ee1d19Raymond}
88