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