SimpleVectorValueChecker.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 for all vectors elements.
  33.  * <br>
  34.  * The {@link #converged(int,PointVectorValuePair,PointVectorValuePair) converged}
  35.  * method will also return {@code true} if the number of iterations has been set
  36.  * (see {@link #SimpleVectorValueChecker(double,double,int) this constructor}).
  37.  *
  38.  */
  39. public class SimpleVectorValueChecker
  40.     extends AbstractConvergenceChecker<PointVectorValuePair> {
  41.     /**
  42.      * If {@link #maxIterationCount} is set to this value, the number of
  43.      * iterations will never cause
  44.      * {@link #converged(int,PointVectorValuePair,PointVectorValuePair)}
  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,PointVectorValuePair,PointVectorValuePair)} method
  51.      * will return true (unless the check is disabled).
  52.      */
  53.     private final int maxIterationCount;

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

  69.     /**
  70.      * Builds an instance with specified tolerance thresholds and
  71.      * iteration count.
  72.      *
  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 SimpleVectorValueChecker(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 PointVectorValuePair previous,
  112.                              final PointVectorValuePair current) {
  113.         if (maxIterationCount != ITERATION_CHECK_DISABLED && iteration >= maxIterationCount) {
  114.             return true;
  115.         }

  116.         final double[] p = previous.getValueRef();
  117.         final double[] c = current.getValueRef();
  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. }