1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.hipparchus.optim.nonlinear.vector.leastsquares;
24
25 import org.hipparchus.exception.LocalizedCoreFormats;
26 import org.hipparchus.exception.MathIllegalStateException;
27 import org.hipparchus.geometry.euclidean.threed.Plane;
28 import org.hipparchus.geometry.euclidean.threed.Vector3D;
29 import org.hipparchus.linear.SingularValueDecomposer;
30 import org.hipparchus.optim.SimpleVectorValueChecker;
31 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
32 import org.hipparchus.util.FastMath;
33 import org.junit.jupiter.api.Test;
34
35 import java.io.IOException;
36
37 import static org.junit.jupiter.api.Assertions.assertEquals;
38
39
40
41
42
43
44
45
46
47 public class GaussNewtonOptimizerWithSVDTest
48 extends AbstractLeastSquaresOptimizerAbstractTest {
49
50 @Override
51 public int getMaxIterations() {
52 return 1000;
53 }
54
55 @Override
56 public LeastSquaresOptimizer getOptimizer() {
57 return new GaussNewtonOptimizer(new SingularValueDecomposer(), false);
58 }
59
60 @Test
61 void testMaxEvaluations() throws Exception {
62 try{
63 CircleVectorial circle = new CircleVectorial();
64 circle.addPoint( 30.0, 68.0);
65 circle.addPoint( 50.0, -6.0);
66 circle.addPoint(110.0, -20.0);
67 circle.addPoint( 35.0, 15.0);
68 circle.addPoint( 45.0, 97.0);
69
70 LeastSquaresProblem lsp = builder(circle)
71 .checkerPair(new SimpleVectorValueChecker(1e-30, 1e-30))
72 .maxIterations(Integer.MAX_VALUE)
73 .start(new double[]{98.680, 47.345})
74 .build();
75
76 optimizer.optimize(lsp);
77
78 customFail(optimizer);
79 }catch (MathIllegalStateException e){
80
81 }
82 }
83
84 @Override
85 @Test
86 public void testCircleFittingBadInit() {
87
88
89
90
91
92 try {
93 super.testCircleFittingBadInit();
94 customFail(optimizer);
95 } catch (AssertionError e) {
96
97 }
98 }
99
100 @Override
101 @Test
102 public void testHahn1() throws IOException {
103 try {
104
105
106
107
108
109 super.testHahn1();
110 customFail(optimizer);
111 } catch (MathIllegalStateException e) {
112 assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
113 }
114 }
115
116 @Test
117 @Override
118 public void testGetIterations() {
119
120 try {
121 super.testGetIterations();
122 customFail(optimizer);
123 } catch (MathIllegalStateException e) {
124 assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED,
125 e.getSpecifier());
126 }
127 }
128
129 @Test
130 @Override
131 public void testNonInvertible() throws Exception {
132
133
134
135
136
137 LinearProblem problem = new LinearProblem(new double[][]{
138 {1, 2, -3},
139 {2, 1, 3},
140 {-3, 0, -9}
141 }, new double[]{1, 1, 1});
142
143 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
144
145 Plane span = new Plane(Vector3D.ZERO, new Vector3D(1, 2, -3), new Vector3D(2, 1, 0), TOl);
146 double expected = FastMath.abs(span.getOffset(new Vector3D(1, 1, 1)));
147 double actual = optimum.getResiduals().getNorm();
148
149
150 assertEquals(expected, actual, TOl);
151 }
152
153 }