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.geometry.euclidean.threed.Plane;
30  import org.hipparchus.geometry.euclidean.threed.Vector3D;
31  import org.hipparchus.linear.SingularValueDecomposer;
32  import org.hipparchus.optim.SimpleVectorValueChecker;
33  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
34  import org.hipparchus.util.FastMath;
35  import org.junit.Assert;
36  import org.junit.Test;
37  
38  /**
39   * <p>Some of the unit tests are re-implementations of the MINPACK <a
40   * href="http://www.netlib.org/minpack/ex/file17">file17</a> and <a
41   * href="http://www.netlib.org/minpack/ex/file22">file22</a> test files.
42   * The redistribution policy for MINPACK is available <a
43   * href="http://www.netlib.org/minpack/disclaimer">here</a>/
44   *
45   */
46  public class GaussNewtonOptimizerWithSVDTest
47      extends AbstractLeastSquaresOptimizerAbstractTest {
48  
49      @Override
50      public int getMaxIterations() {
51          return 1000;
52      }
53  
54      @Override
55      public LeastSquaresOptimizer getOptimizer() {
56          return new GaussNewtonOptimizer(new SingularValueDecomposer(), false);
57      }
58  
59      @Test
60      public void testMaxEvaluations() throws Exception {
61          try{
62          CircleVectorial circle = new CircleVectorial();
63          circle.addPoint( 30.0,  68.0);
64          circle.addPoint( 50.0,  -6.0);
65          circle.addPoint(110.0, -20.0);
66          circle.addPoint( 35.0,  15.0);
67          circle.addPoint( 45.0,  97.0);
68  
69          LeastSquaresProblem lsp = builder(circle)
70                  .checkerPair(new SimpleVectorValueChecker(1e-30, 1e-30))
71                  .maxIterations(Integer.MAX_VALUE)
72                  .start(new double[]{98.680, 47.345})
73                  .build();
74  
75          optimizer.optimize(lsp);
76  
77              fail(optimizer);
78          }catch (MathIllegalStateException e){
79              //expected
80          }
81      }
82  
83      @Override
84      @Test
85      public void testCircleFittingBadInit() {
86          /*
87           * This test converged to the wrong solution with this optimizer.
88           * It seems that the state becomes so large that the convergence
89           * checker's relative tolerance test passes.
90           */
91          try {
92              super.testCircleFittingBadInit();
93              fail(optimizer);
94          } catch (AssertionError e) {
95              //expected
96          }
97      }
98  
99      @Override
100     @Test
101     public void testHahn1() throws IOException {
102         try {
103             /*
104              * When NOT FORMING normal equations, the optimizer diverges and hit max evaluations.
105              * When FORMING normal equations, the optimizer converges,
106              * but the results are very bad
107              */
108             super.testHahn1();
109             fail(optimizer);
110         } catch (MathIllegalStateException e) {
111             Assert.assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
112         }
113     }
114 
115     @Test
116     @Override
117     public void testGetIterations() {
118         /* this diverges with SVD and no normal equations */
119         try {
120             super.testGetIterations();
121             fail(optimizer);
122         } catch (MathIllegalStateException e) {
123             Assert.assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED,
124                                 e.getSpecifier());
125         }
126     }
127 
128     @Test
129     @Override
130     public void testNonInvertible() throws Exception {
131         /*  SVD can compute a solution to singular problems.
132          *  In this case the target vector, b, is not in the
133          *  span of the jacobian matrix, A. The closest point
134          *  to b on the plane spanned by A is computed.
135          */
136         LinearProblem problem = new LinearProblem(new double[][]{
137                 {1, 2, -3},
138                 {2, 1, 3},
139                 {-3, 0, -9}
140         }, new double[]{1, 1, 1});
141 
142         Optimum optimum = optimizer.optimize(problem.getBuilder().build());
143 
144         Plane span = new Plane(Vector3D.ZERO, new Vector3D(1, 2, -3), new Vector3D(2, 1, 0), TOl);
145         double expected = FastMath.abs(span.getOffset(new Vector3D(1, 1, 1)));
146         double actual = optimum.getResiduals().getNorm();
147 
148         //verify
149         Assert.assertEquals(expected, actual, TOl);
150     }
151 
152 }