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