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    
018    package org.apache.commons.math3.optimization;
019    
020    import org.apache.commons.math3.util.FastMath;
021    import 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.
031     * <br/>
032     * The {@link #converged(int,PointValuePair,PointValuePair) converged}
033     * method will also return {@code true} if the number of iterations has been set
034     * (see {@link #SimpleValueChecker(double,double,int) this constructor}).
035     *
036     * @version $Id: SimpleValueChecker.java 1422230 2012-12-15 12:11:13Z erans $
037     * @deprecated As of 3.1 (to be removed in 4.0).
038     * @since 3.0
039     */
040    @Deprecated
041    public class SimpleValueChecker
042        extends AbstractConvergenceChecker<PointValuePair> {
043        /**
044         * If {@link #maxIterationCount} is set to this value, the number of
045         * iterations will never cause
046         * {@link #converged(int,PointValuePair,PointValuePair)}
047         * to return {@code true}.
048         */
049        private static final int ITERATION_CHECK_DISABLED = -1;
050        /**
051         * Number of iterations after which the
052         * {@link #converged(int,PointValuePair,PointValuePair)} method
053         * will return true (unless the check is disabled).
054         */
055        private final int maxIterationCount;
056    
057        /**
058         * Build an instance with default thresholds.
059         * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()}
060         */
061        @Deprecated
062        public SimpleValueChecker() {
063            maxIterationCount = ITERATION_CHECK_DISABLED;
064        }
065    
066        /** Build an instance with specified thresholds.
067         *
068         * In order to perform only relative checks, the absolute tolerance
069         * must be set to a negative value. In order to perform only absolute
070         * checks, the relative tolerance must be set to a negative value.
071         *
072         * @param relativeThreshold relative tolerance threshold
073         * @param absoluteThreshold absolute tolerance threshold
074         */
075        public SimpleValueChecker(final double relativeThreshold,
076                                  final double absoluteThreshold) {
077            super(relativeThreshold, absoluteThreshold);
078            maxIterationCount = ITERATION_CHECK_DISABLED;
079        }
080    
081        /**
082         * Builds an instance with specified thresholds.
083         *
084         * In order to perform only relative checks, the absolute tolerance
085         * must be set to a negative value. In order to perform only absolute
086         * checks, the relative tolerance must be set to a negative value.
087         *
088         * @param relativeThreshold relative tolerance threshold
089         * @param absoluteThreshold absolute tolerance threshold
090         * @param maxIter Maximum iteration count.
091         * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
092         *
093         * @since 3.1
094         */
095        public SimpleValueChecker(final double relativeThreshold,
096                                  final double absoluteThreshold,
097                                  final int maxIter) {
098            super(relativeThreshold, absoluteThreshold);
099    
100            if (maxIter <= 0) {
101                throw new NotStrictlyPositiveException(maxIter);
102            }
103            maxIterationCount = maxIter;
104        }
105    
106        /**
107         * Check if the optimization algorithm has converged considering the
108         * last two points.
109         * This method may be called several time from the same algorithm
110         * iteration with different points. This can be detected by checking the
111         * iteration number at each call if needed. Each time this method is
112         * called, the previous and current point correspond to points with the
113         * same role at each iteration, so they can be compared. As an example,
114         * simplex-based algorithms call this method for all points of the simplex,
115         * not only for the best or worst ones.
116         *
117         * @param iteration Index of current iteration
118         * @param previous Best point in the previous iteration.
119         * @param current Best point in the current iteration.
120         * @return {@code true} if the algorithm has converged.
121         */
122        @Override
123        public boolean converged(final int iteration,
124                                 final PointValuePair previous,
125                                 final PointValuePair current) {
126            if (maxIterationCount != ITERATION_CHECK_DISABLED) {
127                if (iteration >= maxIterationCount) {
128                    return true;
129                }
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    }