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 */
017package org.apache.commons.math4.legacy.optim.univariate;
018
019import org.apache.commons.math4.legacy.exception.NotStrictlyPositiveException;
020import org.apache.commons.math4.legacy.optim.AbstractConvergenceChecker;
021import org.apache.commons.math4.core.jdkmath.JdkMath;
022
023/**
024 * Simple implementation of the
025 * {@link org.apache.commons.math4.legacy.optim.ConvergenceChecker} interface
026 * that uses only objective function values.
027 *
028 * Convergence is considered to have been reached if either the relative
029 * difference between the objective function values is smaller than a
030 * threshold or if either the absolute difference between the objective
031 * function values is smaller than another threshold.
032 * <br>
033 * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
034 * converged} method will also return {@code true} if the number of iterations
035 * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
036 * this constructor}).
037 *
038 * @since 3.1
039 */
040public class SimpleUnivariateValueChecker
041    extends AbstractConvergenceChecker<UnivariatePointValuePair> {
042    /**
043     * If {@link #maxIterationCount} is set to this value, the number of
044     * iterations will never cause
045     * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
046     * to return {@code true}.
047     */
048    private static final int ITERATION_CHECK_DISABLED = -1;
049    /**
050     * Number of iterations after which the
051     * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
052     * method will return true (unless the check is disabled).
053     */
054    private final int maxIterationCount;
055
056    /** Build an instance with specified thresholds.
057     *
058     * In order to perform only relative checks, the absolute tolerance
059     * must be set to a negative value. In order to perform only absolute
060     * checks, the relative tolerance must be set to a negative value.
061     *
062     * @param relativeThreshold relative tolerance threshold
063     * @param absoluteThreshold absolute tolerance threshold
064     */
065    public SimpleUnivariateValueChecker(final double relativeThreshold,
066                                        final double absoluteThreshold) {
067        super(relativeThreshold, absoluteThreshold);
068        maxIterationCount = ITERATION_CHECK_DISABLED;
069    }
070
071    /**
072     * Builds an instance with specified thresholds.
073     *
074     * In order to perform only relative checks, the absolute tolerance
075     * must be set to a negative value. In order to perform only absolute
076     * checks, the relative tolerance must be set to a negative value.
077     *
078     * @param relativeThreshold relative tolerance threshold
079     * @param absoluteThreshold absolute tolerance threshold
080     * @param maxIter Maximum iteration count.
081     * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
082     *
083     * @since 3.1
084     */
085    public SimpleUnivariateValueChecker(final double relativeThreshold,
086                                        final double absoluteThreshold,
087                                        final int maxIter) {
088        super(relativeThreshold, absoluteThreshold);
089
090        if (maxIter <= 0) {
091            throw new NotStrictlyPositiveException(maxIter);
092        }
093        maxIterationCount = maxIter;
094    }
095
096    /**
097     * Check if the optimization algorithm has converged considering the
098     * last two points.
099     * This method may be called several time from the same algorithm
100     * iteration with different points. This can be detected by checking the
101     * iteration number at each call if needed. Each time this method is
102     * called, the previous and current point correspond to points with the
103     * same role at each iteration, so they can be compared. As an example,
104     * simplex-based algorithms call this method for all points of the simplex,
105     * not only for the best or worst ones.
106     *
107     * @param iteration Index of current iteration
108     * @param previous Best point in the previous iteration.
109     * @param current Best point in the current iteration.
110     * @return {@code true} if the algorithm has converged.
111     */
112    @Override
113    public boolean converged(final int iteration,
114                             final UnivariatePointValuePair previous,
115                             final UnivariatePointValuePair current) {
116        if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
117            return true;
118        }
119
120        final double p = previous.getValue();
121        final double c = current.getValue();
122        final double difference = JdkMath.abs(p - c);
123        final double size = JdkMath.max(JdkMath.abs(p), JdkMath.abs(c));
124        return difference <= size * getRelativeThreshold() ||
125            difference <= getAbsoluteThreshold();
126    }
127}