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