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    *      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  
18  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  
23  package org.hipparchus.optim.nonlinear.vector.leastsquares;
24  
25  import org.hipparchus.exception.MathIllegalStateException;
26  import org.hipparchus.linear.QRDecomposer;
27  import org.hipparchus.optim.LocalizedOptimFormats;
28  import org.hipparchus.optim.SimpleVectorValueChecker;
29  import org.junit.jupiter.api.Test;
30  
31  import java.io.IOException;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
34  import static org.junit.jupiter.api.Assertions.assertThrows;
35  import static org.junit.jupiter.api.Assertions.fail;
36  
37  /**
38   * <p>Some of the unit tests are re-implementations of the MINPACK <a
39   * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
40   * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
41   * The redistribution policy for MINPACK is available <a
42   * href="http://www.netlib.org/minpack/disclaimer">here</a>/
43   *
44   */
45  public class GaussNewtonOptimizerWithQRNormalTest
46      extends AbstractLeastSquaresOptimizerAbstractTest {
47  
48      @Override
49      public int getMaxIterations() {
50          return 1000;
51      }
52  
53      @Override
54      public LeastSquaresOptimizer getOptimizer() {
55          return new GaussNewtonOptimizer(new QRDecomposer(1e-11), true);
56      }
57  
58      @Override
59      @Test
60      public void testMoreEstimatedParametersUnsorted() {
61          /*
62           * Exception is expected with this optimizer
63           */
64          try {
65              super.testMoreEstimatedParametersUnsorted();
66          } catch (MathIllegalStateException mise) {
67              assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM, mise.getSpecifier());
68          }
69      }
70  
71      @Test
72      void testMaxEvaluations() throws Exception {
73          try{
74          CircleVectorial circle = new CircleVectorial();
75          circle.addPoint( 30.0,  68.0);
76          circle.addPoint( 50.0,  -6.0);
77          circle.addPoint(110.0, -20.0);
78          circle.addPoint( 35.0,  15.0);
79          circle.addPoint( 45.0,  97.0);
80  
81          LeastSquaresProblem lsp = builder(circle)
82                  .checkerPair(new SimpleVectorValueChecker(1e-30, 1e-30))
83                  .maxIterations(Integer.MAX_VALUE)
84                  .start(new double[]{98.680, 47.345})
85                  .build();
86  
87          optimizer.optimize(lsp);
88  
89              customFail(optimizer);
90          }catch (MathIllegalStateException e){
91              //expected
92          }
93      }
94  
95      @Override
96      @Test
97      public void testCircleFittingBadInit() {
98          try {
99              /*
100              * This test does not converge with this optimizer.
101              */
102             super.testCircleFittingBadInit();
103         } catch (MathIllegalStateException mise) {
104             assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM, mise.getSpecifier());
105         }
106     }
107 
108     @Override
109     @Test
110     public void testHahn1() throws IOException {
111         try {
112             /*
113              * TODO This test leads to a singular problem with the Gauss-Newton
114              * optimizer. This should be inquired.
115              */
116             super.testHahn1();
117             fail("Expected Exception with: " + optimizer);
118         } catch (MathIllegalStateException mise) {
119             // pass. Both singular problem, and max iterations is acceptable.
120         }
121     }
122 
123     @Override
124     @Test
125     public void testMoreEstimatedParametersSimple() {
126         assertThrows(MathIllegalStateException.class, () -> {
127             // reduced numerical stability when forming the normal equations
128             super.testMoreEstimatedParametersSimple();
129         });
130     }
131 
132 }