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