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 java.io.IOException;
26  
27  import org.hipparchus.exception.LocalizedCoreFormats;
28  import org.hipparchus.exception.MathIllegalStateException;
29  import org.hipparchus.linear.LUDecomposer;
30  import org.hipparchus.optim.LocalizedOptimFormats;
31  import org.hipparchus.optim.SimpleVectorValueChecker;
32  import org.junit.Assert;
33  import org.junit.Test;
34  
35  /**
36   * <p>Some of the unit tests are re-implementations of the MINPACK <a
37   * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
38   * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
39   * The redistribution policy for MINPACK is available <a
40   * href="http://www.netlib.org/minpack/disclaimer">here</a>/
41   *
42   */
43  public class GaussNewtonOptimizerWithLUTest
44      extends AbstractLeastSquaresOptimizerAbstractTest {
45  
46      @Override
47      public int getMaxIterations() {
48          return 1000;
49      }
50  
51      @Override
52      public LeastSquaresOptimizer getOptimizer() {
53          return new GaussNewtonOptimizer(new LUDecomposer(1.0e-11), true);
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              fail(optimizer);
65          } catch (MathIllegalStateException e) {
66              Assert.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              fail(optimizer);
80          } catch (MathIllegalStateException e) {
81              Assert.assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
82                                  e.getSpecifier());
83          }
84      }
85  
86      @Test
87      public 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             optimizer.optimize(lsp);
103 
104             fail(optimizer);
105         } catch (MathIllegalStateException e) {
106             Assert.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             fail(optimizer);
119         } catch (MathIllegalStateException e) {
120             Assert.assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
121                                 e.getSpecifier());
122         }
123     }
124 
125     @Override
126     @Test
127     public void testHahn1() throws IOException {
128         try {
129             /*
130              * TODO This test leads to a singular problem with the Gauss-Newton
131              * optimizer. This should be inquired.
132              */
133             super.testHahn1();
134             fail(optimizer);
135         } catch (MathIllegalStateException e) {
136             Assert.assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
137                                 e.getSpecifier());
138         }
139     }
140 
141 }