View Javadoc
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    *      http://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.apache.commons.math4.legacy.fitting.leastsquares;
18  
19  import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresProblem.Evaluation;
20  import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
21  import org.apache.commons.numbers.core.Precision;
22  
23  /**
24   * Check if an optimization has converged based on the change in computed RMS.
25   *
26   * @since 3.4
27   */
28  public class EvaluationRmsChecker implements ConvergenceChecker<Evaluation> {
29  
30      /** relative tolerance for comparisons. */
31      private final double relTol;
32      /** absolute tolerance for comparisons. */
33      private final double absTol;
34  
35      /**
36       * Create a convergence checker for the RMS with the same relative and absolute
37       * tolerance.
38       *
39       * <p>Convenience constructor for when the relative and absolute tolerances are the
40       * same. Same as {@code new EvaluationRmsChecker(tol, tol)}.
41       *
42       * @param tol the relative and absolute tolerance.
43       * @see #EvaluationRmsChecker(double, double)
44       */
45      public EvaluationRmsChecker(final double tol) {
46          this(tol, tol);
47      }
48  
49      /**
50       * Create a convergence checker for the RMS with a relative and absolute tolerance.
51       *
52       * <p>The optimization has converged when the RMS of consecutive evaluations are equal
53       * to within the given relative tolerance or absolute tolerance.
54       *
55       * @param relTol the relative tolerance.
56       * @param absTol the absolute tolerance.
57       * @see Precision#equals(double, double, double)
58       * @see Precision#equalsWithRelativeTolerance(double, double, double)
59       */
60      public EvaluationRmsChecker(final double relTol, final double absTol) {
61          this.relTol = relTol;
62          this.absTol = absTol;
63      }
64  
65      /** {@inheritDoc} */
66      @Override
67      public boolean converged(final int iteration,
68                               final Evaluation previous,
69                               final Evaluation current) {
70          final double prevRms = previous.getRMS();
71          final double currRms = current.getRMS();
72          return Precision.equals(prevRms, currRms, this.absTol) ||
73                  Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol);
74      }
75  }