SimplePointChecker.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. import org.hipparchus.util.Pair;

  26. /**
  27.  * Simple implementation of the {@link ConvergenceChecker} interface using
  28.  * only point coordinates.
  29.  *
  30.  * Convergence is considered to have been reached if either the relative
  31.  * difference between each point coordinate are smaller than a threshold
  32.  * or if either the absolute difference between the point coordinates are
  33.  * smaller than another threshold.
  34.  * <br>
  35.  * The {@link #converged(int,Pair,Pair) converged} method will also return
  36.  * {@code true} if the number of iterations has been set (see
  37.  * {@link #SimplePointChecker(double,double,int) this constructor}).
  38.  *
  39.  * @param <P> Type of the (point, value) pair.
  40.  * The type of the "value" part of the pair (not used by this class).
  41.  *
  42.  */
  43. public class SimplePointChecker<P extends Pair<double[], ? extends Object>>
  44.     extends AbstractConvergenceChecker<P> {
  45.     /**
  46.      * If {@link #maxIterationCount} is set to this value, the number of
  47.      * iterations will never cause {@link #converged(int, Pair, Pair)}
  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, Pair, Pair)} method
  54.      * will return true (unless the check is disabled).
  55.      */
  56.     private final int maxIterationCount;

  57.     /**
  58.      * Build an instance with specified thresholds.
  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 SimplePointChecker(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.      * In order to perform only relative checks, the absolute tolerance
  74.      * must be set to a negative value. In order to perform only absolute
  75.      * checks, the relative tolerance must be set to a negative value.
  76.      *
  77.      * @param relativeThreshold Relative tolerance threshold.
  78.      * @param absoluteThreshold Absolute tolerance threshold.
  79.      * @param maxIter Maximum iteration count.
  80.      * @throws MathIllegalArgumentException if {@code maxIter <= 0}.
  81.      *
  82.      */
  83.     public SimplePointChecker(final double relativeThreshold,
  84.                               final double absoluteThreshold,
  85.                               final int maxIter) {
  86.         super(relativeThreshold, absoluteThreshold);

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

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

  116.         final double[] p = previous.getKey();
  117.         final double[] c = current.getKey();
  118.         for (int i = 0; i < p.length; ++i) {
  119.             final double pi = p[i];
  120.             final double ci = c[i];
  121.             final double difference = FastMath.abs(pi - ci);
  122.             final double size = FastMath.max(FastMath.abs(pi), FastMath.abs(ci));
  123.             if (difference > size * getRelativeThreshold() &&
  124.                 difference > getAbsoluteThreshold()) {
  125.                 return false;
  126.             }
  127.         }
  128.         return true;
  129.     }
  130. }