SimpleValueChecker.java

  1. /*
  2.  * Licensed to the Apache Software Foundation (ASF) under one or more
  3.  * contributor license agreements.  See the NOTICE file distributed with
  4.  * this work for additional information regarding copyright ownership.
  5.  * The ASF licenses this file to You under the Apache License, Version 2.0
  6.  * (the "License"); you may not use this file except in compliance with
  7.  * the License.  You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */

  17. package org.apache.commons.math3.optimization;

  18. import org.apache.commons.math3.util.FastMath;
  19. import org.apache.commons.math3.exception.NotStrictlyPositiveException;

  20. /**
  21.  * Simple implementation of the {@link ConvergenceChecker} interface using
  22.  * only objective function values.
  23.  *
  24.  * Convergence is considered to have been reached if either the relative
  25.  * difference between the objective function values is smaller than a
  26.  * threshold or if either the absolute difference between the objective
  27.  * function values is smaller than another threshold.
  28.  * <br/>
  29.  * The {@link #converged(int,PointValuePair,PointValuePair) converged}
  30.  * method will also return {@code true} if the number of iterations has been set
  31.  * (see {@link #SimpleValueChecker(double,double,int) this constructor}).
  32.  *
  33.  * @deprecated As of 3.1 (to be removed in 4.0).
  34.  * @since 3.0
  35.  */
  36. @Deprecated
  37. public class SimpleValueChecker
  38.     extends AbstractConvergenceChecker<PointValuePair> {
  39.     /**
  40.      * If {@link #maxIterationCount} is set to this value, the number of
  41.      * iterations will never cause
  42.      * {@link #converged(int,PointValuePair,PointValuePair)}
  43.      * to return {@code true}.
  44.      */
  45.     private static final int ITERATION_CHECK_DISABLED = -1;
  46.     /**
  47.      * Number of iterations after which the
  48.      * {@link #converged(int,PointValuePair,PointValuePair)} method
  49.      * will return true (unless the check is disabled).
  50.      */
  51.     private final int maxIterationCount;

  52.     /**
  53.      * Build an instance with default thresholds.
  54.      * @deprecated See {@link AbstractConvergenceChecker#AbstractConvergenceChecker()}
  55.      */
  56.     @Deprecated
  57.     public SimpleValueChecker() {
  58.         maxIterationCount = ITERATION_CHECK_DISABLED;
  59.     }

  60.     /** Build an instance with specified thresholds.
  61.      *
  62.      * In order to perform only relative checks, the absolute tolerance
  63.      * must be set to a negative value. In order to perform only absolute
  64.      * checks, the relative tolerance must be set to a negative value.
  65.      *
  66.      * @param relativeThreshold relative tolerance threshold
  67.      * @param absoluteThreshold absolute tolerance threshold
  68.      */
  69.     public SimpleValueChecker(final double relativeThreshold,
  70.                               final double absoluteThreshold) {
  71.         super(relativeThreshold, absoluteThreshold);
  72.         maxIterationCount = ITERATION_CHECK_DISABLED;
  73.     }

  74.     /**
  75.      * Builds an instance with specified thresholds.
  76.      *
  77.      * In order to perform only relative checks, the absolute tolerance
  78.      * must be set to a negative value. In order to perform only absolute
  79.      * checks, the relative tolerance must be set to a negative value.
  80.      *
  81.      * @param relativeThreshold relative tolerance threshold
  82.      * @param absoluteThreshold absolute tolerance threshold
  83.      * @param maxIter Maximum iteration count.
  84.      * @throws NotStrictlyPositiveException if {@code maxIter <= 0}.
  85.      *
  86.      * @since 3.1
  87.      */
  88.     public SimpleValueChecker(final double relativeThreshold,
  89.                               final double absoluteThreshold,
  90.                               final int maxIter) {
  91.         super(relativeThreshold, absoluteThreshold);

  92.         if (maxIter <= 0) {
  93.             throw new NotStrictlyPositiveException(maxIter);
  94.         }
  95.         maxIterationCount = maxIter;
  96.     }

  97.     /**
  98.      * Check if the optimization algorithm has converged considering the
  99.      * last two points.
  100.      * This method may be called several time from the same algorithm
  101.      * iteration with different points. This can be detected by checking the
  102.      * iteration number at each call if needed. Each time this method is
  103.      * called, the previous and current point correspond to points with the
  104.      * same role at each iteration, so they can be compared. As an example,
  105.      * simplex-based algorithms call this method for all points of the simplex,
  106.      * not only for the best or worst ones.
  107.      *
  108.      * @param iteration Index of current iteration
  109.      * @param previous Best point in the previous iteration.
  110.      * @param current Best point in the current iteration.
  111.      * @return {@code true} if the algorithm has converged.
  112.      */
  113.     @Override
  114.     public boolean converged(final int iteration,
  115.                              final PointValuePair previous,
  116.                              final PointValuePair current) {
  117.         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
  118.             return true;
  119.         }

  120.         final double p = previous.getValue();
  121.         final double c = current.getValue();
  122.         final double difference = FastMath.abs(p - c);
  123.         final double size = FastMath.max(FastMath.abs(p), FastMath.abs(c));
  124.         return difference <= size * getRelativeThreshold() ||
  125.             difference <= getAbsoluteThreshold();
  126.     }
  127. }