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 org.hipparchus.exception.LocalizedCoreFormats;
21  import org.hipparchus.exception.MathIllegalStateException;
22  import org.hipparchus.geometry.euclidean.threed.Plane;
23  import org.hipparchus.geometry.euclidean.threed.Vector3D;
24  import org.hipparchus.linear.SingularValueDecomposer;
25  import org.hipparchus.optim.SimpleVectorValueChecker;
26  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
27  import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresProblem.Evaluation;
28  import org.hipparchus.util.FastMath;
29  import org.junit.jupiter.api.Test;
30  
31  import java.io.IOException;
32  
33  import static org.junit.jupiter.api.Assertions.assertEquals;
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 SequentialGaussNewtonOptimizerWithSVDTest
44      extends AbstractSequentialLeastSquaresOptimizerAbstractTest {
45  
46      @Override
47      public int getMaxIterations() {
48          return 1000;
49      }
50  
51      @Override
52      public void defineOptimizer(Evaluation evaluation) {
53          this.optimizer = new SequentialGaussNewtonOptimizer().
54                           withDecomposer(new SingularValueDecomposer()).
55                           withFormNormalEquations(false).
56                           withEvaluation(evaluation);
57      }
58  
59      @Test
60      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              defineOptimizer(null);
76              optimizer.optimize(lsp);
77  
78              customFail(optimizer);
79          } catch (MathIllegalStateException e) {
80              assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
81          }
82      }
83  
84  
85      @Override
86      @Test
87      public void testHahn1() throws IOException {
88          try {
89              /*
90               * When NOT FORMING normal equations, the optimizer diverges and hit max evaluations.
91               * When FORMING normal equations, the optimizer converges,
92               * but the results are very bad
93               */
94              super.testHahn1();
95              customFail(optimizer);
96          } catch (MathIllegalStateException e) {
97              assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
98          }
99      }
100 
101     @Test
102     @Override
103     public void testGetIterations() {
104         /* this diverges with SVD and no normal equations */
105         try {
106             super.testGetIterations();
107             customFail(optimizer);
108         } catch (MathIllegalStateException e) {
109             assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED,
110                                 e.getSpecifier());
111         }
112     }
113 
114     @Test
115     @Override
116     public void testNonInvertible() throws Exception {
117         /*  SVD can compute a solution to singular problems.
118          *  In this case the target vector, b, is not in the
119          *  span of the jacobian matrix, A. The closest point
120          *  to b on the plane spanned by A is computed.
121          */
122         LinearProblem problem = new LinearProblem(new double[][]{
123                 {1, 2, -3},
124                 {2, 1, 3},
125                 {-3, 0, -9}
126         }, new double[]{1, 1, 1});
127 
128         defineOptimizer(null);
129         Optimum optimum = optimizer.optimize(problem.getBuilder().build());
130 
131         Plane span = new Plane(Vector3D.ZERO, new Vector3D(1, 2, -3), new Vector3D(2, 1, 0), TOl);
132         double expected = FastMath.abs(span.getOffset(new Vector3D(1, 1, 1)));
133         double actual = optimum.getResiduals().getNorm();
134 
135         //verify
136         assertEquals(expected, actual, TOl);
137     }
138 
139 }