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.optim;
019
020import org.apache.commons.math3.util.FastMath;
021import org.apache.commons.math3.exception.NotStrictlyPositiveException;
022
023/**
024 * Simple implementation of the {@link ConvergenceChecker} interface using
025 * only objective function values.
026 *
027 * Convergence is considered to have been reached if either the relative
028 * difference between the objective function values is smaller than a
029 * threshold or if either the absolute difference between the objective
030 * function values is smaller than another threshold for all vectors elements.
031 * <br/>
032 * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged}
033 * method will also return {@code true} if the number of iterations has been set
034 * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}).
035 *
036 * @since 3.0
037 */
038public class SimpleVectorValueChecker
039    extends AbstractConvergenceChecker<PointVectorValuePair> {
040    /**
041     * If {@link #maxIterationCount} is set to this value, the number of
042     * iterations will never cause
043     * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)}
044     * to return {@code true}.
045     */
046    private static final int ITERATION_CHECK_DISABLED = -1;
047    /**
048     * Number of iterations after which the
049     * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)} method
050     * will return true (unless the check is disabled).
051     */
052    private final int maxIterationCount;
053
054    /**
055     * Build an instance with specified thresholds.
056     *
057     * In order to perform only relative checks, the absolute tolerance
058     * must be set to a negative value. In order to perform only absolute
059     * checks, the relative tolerance must be set to a negative value.
060     *
061     * @param relativeThreshold relative tolerance threshold
062     * @param absoluteThreshold absolute tolerance threshold
063     */
064    public SimpleVectorValueChecker(final double relativeThreshold,
065                                    final double absoluteThreshold) {
066        super(relativeThreshold, absoluteThreshold);
067        maxIterationCount = ITERATION_CHECK_DISABLED;
068    }
069
070    /**
071     * Builds an instance with specified tolerance thresholds and
072     * iteration count.
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 SimpleVectorValueChecker(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 times 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 arguments satify the convergence criterion.
111     */
112    @Override
113    public boolean converged(final int iteration,
114                             final PointVectorValuePair previous,
115                             final PointVectorValuePair current) {
116        if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
117            return true;
118        }
119
120        final double[] p = previous.getValueRef();
121        final double[] c = current.getValueRef();
122        for (int i = 0; i < p.length; ++i) {
123            final double pi         = p[i];
124            final double ci         = c[i];
125            final double difference = FastMath.abs(pi - ci);
126            final double size       = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
127            if (difference > size * getRelativeThreshold() &&
128                difference > getAbsoluteThreshold()) {
129                return false;
130            }
131        }
132        return true;
133    }
134}