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.numbers.core.Precision;
21  import org.apache.commons.geometry.euclidean.threed.Plane;
22  import org.apache.commons.geometry.euclidean.threed.Planes;
23  import org.apache.commons.geometry.euclidean.threed.Vector3D;
24  import org.apache.commons.math4.legacy.exception.ConvergenceException;
25  import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
26  import org.apache.commons.math4.legacy.fitting.leastsquares.GaussNewtonOptimizer.Decomposition;
27  import org.apache.commons.math4.legacy.fitting.leastsquares.LeastSquaresOptimizer.Optimum;
28  import org.apache.commons.math4.legacy.optim.SimpleVectorValueChecker;
29  import org.apache.commons.math4.core.jdkmath.JdkMath;
30  import org.junit.Assert;
31  import org.junit.Test;
32  
33  import java.io.IOException;
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 GaussNewtonOptimizerWithSVDTest
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(Decomposition.SVD);
54      }
55  
56      @Test
57      public void testMaxEvaluations() throws Exception {
58          try{
59          CircleVectorial circle = new CircleVectorial();
60          circle.addPoint( 30.0,  68.0);
61          circle.addPoint( 50.0,  -6.0);
62          circle.addPoint(110.0, -20.0);
63          circle.addPoint( 35.0,  15.0);
64          circle.addPoint( 45.0,  97.0);
65  
66          LeastSquaresProblem lsp = builder(circle)
67                  .checkerPair(new SimpleVectorValueChecker(1e-30, 1e-30))
68                  .maxIterations(Integer.MAX_VALUE)
69                  .start(new double[]{98.680, 47.345})
70                  .build();
71  
72          optimizer.optimize(lsp);
73  
74              fail(optimizer);
75          }catch (TooManyEvaluationsException e){
76              //expected
77          }
78      }
79  
80      @Override
81      @Test
82      public void testCircleFittingBadInit() {
83          /*
84           * This test converged to the wrong solution with this optimizer.
85           * It seems that the state becomes so large that the convergence
86           * checker's relative tolerance test passes.
87           */
88          try {
89              super.testCircleFittingBadInit();
90              fail(optimizer);
91          } catch (AssertionError e) {
92              //expected
93          }
94      }
95  
96      @Override
97      @Test
98      public void testHahn1()
99          throws IOException {
100         /*
101          * TODO This test leads to a singular problem with the Gauss-Newton
102          * optimizer. This should be inquired.
103          */
104         try{
105             super.testHahn1();
106             fail(optimizer);
107         } catch (ConvergenceException e){
108             //expected for LU
109         } catch (TooManyEvaluationsException e){
110             //expected for QR
111         }
112     }
113 
114     @Test
115     @Override
116     public void testGetIterations() {
117         /* this diverges with SVD */
118         try {
119             super.testGetIterations();
120             fail(optimizer);
121         } catch (TooManyEvaluationsException e) {
122             //expected
123         }
124     }
125 
126     @Test
127     @Override
128     public void testNonInvertible() throws Exception {
129         /*  SVD can compute a solution to singular problems.
130          *  In this case the target vector, b, is not in the
131          *  span of the jacobian matrix, A. The closes point
132          *  to b on the plane spanned by A is computed.
133          */
134         LinearProblem problem = new LinearProblem(new double[][]{
135                 {1, 2, -3},
136                 {2, 1, 3},
137                 {-3, 0, -9}
138         }, new double[]{1, 1, 1});
139 
140         Optimum optimum = optimizer.optimize(problem.getBuilder().build());
141 
142         Plane span = Planes.fromPoints(Vector3D.ZERO, Vector3D.of(1, 2, -3), Vector3D.of(2, 1, 0),
143                                        Precision.doubleEquivalenceOfEpsilon(TOL));
144         double expected = JdkMath.abs(span.offset(Vector3D.of(1, 1, 1)));
145         double actual = optimum.getResiduals().getNorm();
146 
147         //verify
148         Assert.assertEquals(expected, actual, TOL);
149     }
150 }