View Javadoc
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  
19  import org.hipparchus.linear.RealMatrix;
20  import org.hipparchus.linear.RealVector;
21  import org.hipparchus.optim.ConvergenceChecker;
22  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
23  import org.junit.Assert;
24  import org.junit.Test;
25  
26  /** Unit tests for {@link EvaluationRmsChecker}. */
27  public class EvaluationRmsCheckerTest {
28  
29      /** check {@link ConvergenceChecker#converged(int, Object, Object)}. */
30      @Test
31      public void testConverged() {
32          //setup
33          ConvergenceChecker<Evaluation> checker = new EvaluationRmsChecker(0.1, 1);
34          Evaluation e200 = mockEvaluation(200);
35          Evaluation e1 = mockEvaluation(1);
36  
37          //action + verify
38          //just matches rel tol
39          Assert.assertEquals(true, checker.converged(0, e200, mockEvaluation(210)));
40          //just matches abs tol
41          Assert.assertEquals(true, checker.converged(0, e1, mockEvaluation(1.9)));
42          //matches both
43          Assert.assertEquals(true, checker.converged(0, e1, mockEvaluation(1.01)));
44          //matches neither
45          Assert.assertEquals(false, checker.converged(0, e200, mockEvaluation(300)));
46      }
47  
48      /**
49       * Create a mock {@link Evaluation}.
50       *
51       * @param rms the evaluation's rms.
52       * @return a new mock evaluation.
53       */
54      private static Evaluation mockEvaluation(final double rms) {
55          return new Evaluation() {
56              public RealMatrix getCovariances(double threshold) {
57                  return null;
58              }
59  
60              public RealVector getSigma(double covarianceSingularityThreshold) {
61                  return null;
62              }
63  
64              public double getRMS() {
65                  return rms;
66              }
67  
68              public RealMatrix getJacobian() {
69                  return null;
70              }
71  
72              public double getCost() {
73                  return 0;
74              }
75  
76              public double getChiSquare() {
77                  return 0;
78              }
79  
80              public double getReducedChiSquare(int n) {
81                  return 0;
82              }
83  
84              public RealVector getResiduals() {
85                  return null;
86              }
87  
88              public RealVector getPoint() {
89                  return null;
90              }
91          };
92      }
93  
94  }