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.CholeskyDecomposer;
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 GaussNewtonOptimizerWithCholeskyTest
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 CholeskyDecomposer(1.0e-11, 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          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         try {
102             optimizer.optimize(lsp);
103             fail(optimizer);
104         } catch (MathIllegalStateException e) {
105             Assert.assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
106         }
107     }
108 
109     @Override
110     @Test
111     public void testCircleFittingBadInit() {
112         try {
113             /*
114              * This test does not converge with this optimizer.
115              */
116             super.testCircleFittingBadInit();
117             fail(optimizer);
118         } catch (MathIllegalStateException e) {
119             Assert.assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
120                                 e.getSpecifier());
121         }
122 
123     }
124 
125     @Override
126     @Test
127     public void testHahn1()
128                     throws IOException {
129         try {
130             /*
131              * TODO This test leads to a singular problem with the Gauss-Newton
132              * optimizer. This should be inquired.
133              */
134             super.testHahn1();
135             fail(optimizer);
136         } catch (MathIllegalStateException e) {
137             Assert.assertEquals(LocalizedOptimFormats.UNABLE_TO_SOLVE_SINGULAR_PROBLEM,
138                                 e.getSpecifier());
139         }
140     }
141 
142 }