001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package org.apache.commons.math3.optimization.univariate;
019
020import org.apache.commons.math3.util.FastMath;
021import org.apache.commons.math3.exception.NotStrictlyPositiveException;
022import org.apache.commons.math3.optimization.AbstractConvergenceChecker;
023
024/**
025 * Simple implementation of the
026 * {@link org.apache.commons.math3.optimization.ConvergenceChecker} interface
027 * that uses only objective function values.
028 *
029 * Convergence is considered to have been reached if either the relative
030 * difference between the objective function values is smaller than a
031 * threshold or if either the absolute difference between the objective
032 * function values is smaller than another threshold.
033 * <br/>
034 * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
035 * converged} method will also return {@code true} if the number of iterations
036 * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
037 * this constructor}).
038 *
039 * @deprecated As of 3.1 (to be removed in 4.0).
040 * @since 3.1
041 */
042@Deprecated
043public class SimpleUnivariateValueChecker
044    extends AbstractConvergenceChecker<UnivariatePointValuePair> {
045    /**
046     * If {@link #maxIterationCount} is set to this value, the number of
047     * iterations will never cause
048     * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
049     * to return {@code true}.
050     */
051    private static final int ITERATION_CHECK_DISABLED = -1;
052    /**
053     * Number of iterations after which the
054     * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
055     * method will return true (unless the check is disabled).
056     */
057    private final int maxIterationCount;
058
059    /**
060     * Build an instance with default thresholds.
061     * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()}
062     */
063    @Deprecated
064    public SimpleUnivariateValueChecker() {
065        maxIterationCount = ITERATION_CHECK_DISABLED;
066    }
067
068    /** Build an instance with specified thresholds.
069     *
070     * In order to perform only relative checks, the absolute tolerance
071     * must be set to a negative value. In order to perform only absolute
072     * checks, the relative tolerance must be set to a negative value.
073     *
074     * @param relativeThreshold relative tolerance threshold
075     * @param absoluteThreshold absolute tolerance threshold
076     */
077    public SimpleUnivariateValueChecker(final double relativeThreshold,
078                                        final double absoluteThreshold) {
079        super(relativeThreshold, absoluteThreshold);
080        maxIterationCount = ITERATION_CHECK_DISABLED;
081    }
082
083    /**
084     * Builds an instance with specified thresholds.
085     *
086     * In order to perform only relative checks, the absolute tolerance
087     * must be set to a negative value. In order to perform only absolute
088     * checks, the relative tolerance must be set to a negative value.
089     *
090     * @param relativeThreshold relative tolerance threshold
091     * @param absoluteThreshold absolute tolerance threshold
092     * @param maxIter Maximum iteration count.
093     * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
094     *
095     * @since 3.1
096     */
097    public SimpleUnivariateValueChecker(final double relativeThreshold,
098                                        final double absoluteThreshold,
099                                        final int maxIter) {
100        super(relativeThreshold, absoluteThreshold);
101
102        if (maxIter <= 0) {
103            throw new NotStrictlyPositiveException(maxIter);
104        }
105        maxIterationCount = maxIter;
106    }
107
108    /**
109     * Check if the optimization algorithm has converged considering the
110     * last two points.
111     * This method may be called several time from the same algorithm
112     * iteration with different points. This can be detected by checking the
113     * iteration number at each call if needed. Each time this method is
114     * called, the previous and current point correspond to points with the
115     * same role at each iteration, so they can be compared. As an example,
116     * simplex-based algorithms call this method for all points of the simplex,
117     * not only for the best or worst ones.
118     *
119     * @param iteration Index of current iteration
120     * @param previous Best point in the previous iteration.
121     * @param current Best point in the current iteration.
122     * @return {@code true} if the algorithm has converged.
123     */
124    @Override
125    public boolean converged(final int iteration,
126                             final UnivariatePointValuePair previous,
127                             final UnivariatePointValuePair current) {
128        if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
129            return true;
130        }
131
132        final double p = previous.getValue();
133        final double c = current.getValue();
134        final double difference = FastMath.abs(p - c);
135        final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
136        return difference <= size * getRelativeThreshold() ||
137            difference <= getAbsoluteThreshold();
138    }
139}