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