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