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 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
37
38
39
40
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
91
92
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
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
118
119
120
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
136 assertEquals(expected, actual, TOl);
137 }
138
139 }