EvaluationRmsChecker.java

  1. /*
  2.  * Licensed to the Hipparchus project 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. package org.hipparchus.optim.nonlinear.vector.leastsquares;

  18. import org.hipparchus.optim.ConvergenceChecker;
  19. import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
  20. import org.hipparchus.util.Precision;

  21. /**
  22.  * Check if an optimization has converged based on the change in computed RMS.
  23.  *
  24.  */
  25. public class EvaluationRmsChecker implements ConvergenceChecker<Evaluation> {

  26.     /** relative tolerance for comparisons. */
  27.     private final double relTol;
  28.     /** absolute tolerance for comparisons. */
  29.     private final double absTol;

  30.     /**
  31.      * Create a convergence checker for the RMS with the same relative and absolute
  32.      * tolerance.
  33.      *
  34.      * <p>Convenience constructor for when the relative and absolute tolerances are the
  35.      * same. Same as {@code new EvaluationRmsChecker(tol, tol)}.
  36.      *
  37.      * @param tol the relative and absolute tolerance.
  38.      * @see #EvaluationRmsChecker(double, double)
  39.      */
  40.     public EvaluationRmsChecker(final double tol) {
  41.         this(tol, tol);
  42.     }

  43.     /**
  44.      * Create a convergence checker for the RMS with a relative and absolute tolerance.
  45.      *
  46.      * <p>The optimization has converged when the RMS of consecutive evaluations are equal
  47.      * to within the given relative tolerance or absolute tolerance.
  48.      *
  49.      * @param relTol the relative tolerance.
  50.      * @param absTol the absolute tolerance.
  51.      * @see Precision#equals(double, double, double)
  52.      * @see Precision#equalsWithRelativeTolerance(double, double, double)
  53.      */
  54.     public EvaluationRmsChecker(final double relTol, final double absTol) {
  55.         this.relTol = relTol;
  56.         this.absTol = absTol;
  57.     }

  58.     /** {@inheritDoc} */
  59.     @Override
  60.     public boolean converged(final int iteration,
  61.                              final Evaluation previous,
  62.                              final Evaluation current) {
  63.         final double prevRms = previous.getRMS();
  64.         final double currRms = current.getRMS();
  65.         return Precision.equals(prevRms, currRms, this.absTol) ||
  66.                 Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol);
  67.     }

  68. }