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.LocalizedCoreFormats;
26  import org.hipparchus.exception.MathIllegalStateException;
27  import org.hipparchus.linear.CholeskyDecomposer;
28  import org.hipparchus.optim.LocalizedOptimFormats;
29  import org.hipparchus.optim.SimpleVectorValueChecker;
30  import org.junit.jupiter.api.Test;
31  
32  import java.io.IOException;
33  
34  import static org.junit.jupiter.api.Assertions.assertEquals;
35  
36  /**
37   * <p>Some of the unit tests are re-implementations of the MINPACK <a
38   * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
39   * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
40   * The redistribution policy for MINPACK is available <a
41   * href="http://www.netlib.org/minpack/disclaimer">here</a>/
42   *
43   */
44  public class GaussNewtonOptimizerWithCholeskyTest
45      extends AbstractLeastSquaresOptimizerAbstractTest {
46  
47      @Override
48      public int getMaxIterations() {
49          return 1000;
50      }
51  
52      @Override
53      public LeastSquaresOptimizer getOptimizer() {
54          return new GaussNewtonOptimizer(new CholeskyDecomposer(1.0e-11, 1.0e-11), true);
55      }
56  
57      @Override
58      @Test
59      public void testMoreEstimatedParametersSimple() {
60          try {
61              /*
62               * Exception is expected with this optimizer
63               */
64              super.testMoreEstimatedParametersSimple();
65              customFail(optimizer);
66          } catch (MathIllegalStateException e) {
67              assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
68                                  e.getSpecifier());
69          }
70      }
71  
72      @Override
73      @Test
74      public void testMoreEstimatedParametersUnsorted() {
75          try {
76              /*
77               * Exception is expected with this optimizer
78               */
79              super.testMoreEstimatedParametersUnsorted();
80              customFail(optimizer);
81          } catch (MathIllegalStateException e) {
82              assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
83                                  e.getSpecifier());
84          }
85      }
86  
87      @Test
88      void testMaxEvaluations() throws Exception {
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         try {
103             optimizer.optimize(lsp);
104             customFail(optimizer);
105         } catch (MathIllegalStateException e) {
106             assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
107         }
108     }
109 
110     @Override
111     @Test
112     public void testCircleFittingBadInit() {
113         try {
114             /*
115              * This test does not converge with this optimizer.
116              */
117             super.testCircleFittingBadInit();
118             customFail(optimizer);
119         } catch (MathIllegalStateException e) {
120             assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
121                                 e.getSpecifier());
122         }
123 
124     }
125 
126     @Override
127     @Test
128     public void testHahn1()
129                     throws IOException {
130         try {
131             /*
132              * TODO This test leads to a singular problem with the Gauss-Newton
133              * optimizer. This should be inquired.
134              */
135             super.testHahn1();
136             customFail(optimizer);
137         } catch (MathIllegalStateException e) {
138             assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
139                                 e.getSpecifier());
140         }
141     }
142 
143 }