001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.math4.legacy.fitting.leastsquares;
018
019import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresProblem.Evaluation;
020import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
021import org.apache.commons.numbers.core.Precision;
022
023/**
024 * Check if an optimization has converged based on the change in computed RMS.
025 *
026 * @since 3.4
027 */
028public class EvaluationRmsChecker implements ConvergenceChecker<Evaluation> {
029
030    /** relative tolerance for comparisons. */
031    private final double relTol;
032    /** absolute tolerance for comparisons. */
033    private final double absTol;
034
035    /**
036     * Create a convergence checker for the RMS with the same relative and absolute
037     * tolerance.
038     *
039     * <p>Convenience constructor for when the relative and absolute tolerances are the
040     * same. Same as {@code new EvaluationRmsChecker(tol, tol)}.
041     *
042     * @param tol the relative and absolute tolerance.
043     * @see #EvaluationRmsChecker(double, double)
044     */
045    public EvaluationRmsChecker(final double tol) {
046        this(tol, tol);
047    }
048
049    /**
050     * Create a convergence checker for the RMS with a relative and absolute tolerance.
051     *
052     * <p>The optimization has converged when the RMS of consecutive evaluations are equal
053     * to within the given relative tolerance or absolute tolerance.
054     *
055     * @param relTol the relative tolerance.
056     * @param absTol the absolute tolerance.
057     * @see Precision#equals(double, double, double)
058     * @see Precision#equalsWithRelativeTolerance(double, double, double)
059     */
060    public EvaluationRmsChecker(final double relTol, final double absTol) {
061        this.relTol = relTol;
062        this.absTol = absTol;
063    }
064
065    /** {@inheritDoc} */
066    @Override
067    public boolean converged(final int iteration,
068                             final Evaluation previous,
069                             final Evaluation current) {
070        final double prevRms = previous.getRMS();
071        final double currRms = current.getRMS();
072        return Precision.equals(prevRms, currRms, this.absTol) ||
073                Precision.equalsWithRelativeTolerance(prevRms, currRms, this.relTol);
074    }
075}