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.  *      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;

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

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

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

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

  85.         if (maxIter <= 0) {
  86.             throw new MathIllegalArgumentException(LocalizedCoreFormats.NUMBER_TOO_SMALL_BOUND_EXCLUDED,
  87.                                                    maxIter, 0);
  88.         }
  89.         maxIterationCount = maxIter;
  90.     }

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

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