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