1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
36
37
38
39
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
90
91
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
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
117
118
119
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
135 Assert.assertEquals(expected, actual, TOl);
136 }
137
138 }