SimpleUnivariateValueChecker.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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.univariate;

  22. import org.hipparchus.exception.LocalizedCoreFormats;
  23. import org.hipparchus.exception.MathIllegalArgumentException;
  24. import org.hipparchus.optim.AbstractConvergenceChecker;
  25. import org.hipparchus.util.FastMath;

  26. /**
  27.  * Simple implementation of the
  28.  * {@link org.hipparchus.optim.ConvergenceChecker} interface
  29.  * that uses only objective function values.
  30.  *
  31.  * Convergence is considered to have been reached if either the relative
  32.  * difference between the objective function values is smaller than a
  33.  * threshold or if either the absolute difference between the objective
  34.  * function values is smaller than another threshold.
  35.  * <br>
  36.  * The {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)
  37.  * converged} method will also return {@code true} if the number of iterations
  38.  * has been set (see {@link #SimpleUnivariateValueChecker(double,double,int)
  39.  * this constructor}).
  40.  *
  41.  */
  42. public class SimpleUnivariateValueChecker
  43.     extends AbstractConvergenceChecker<UnivariatePointValuePair> {
  44.     /**
  45.      * If {@link #maxIterationCount} is set to this value, the number of
  46.      * iterations will never cause
  47.      * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
  48.      * to return {@code true}.
  49.      */
  50.     private static final int ITERATION_CHECK_DISABLED = -1;
  51.     /**
  52.      * Number of iterations after which the
  53.      * {@link #converged(int,UnivariatePointValuePair,UnivariatePointValuePair)}
  54.      * method will return true (unless the check is disabled).
  55.      */
  56.     private final int maxIterationCount;

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

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

  88.         if (maxIter <= 0) {
  89.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
  90.                                                    maxIter, 0);
  91.         }
  92.         maxIterationCount = maxIter;
  93.     }

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

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