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 java.io.IOException;
26
27 import org.hipparchus.exception.LocalizedCoreFormats;
28 import org.hipparchus.exception.MathIllegalStateException;
29 import org.hipparchus.geometry.euclidean.threed.Plane;
30 import org.hipparchus.geometry.euclidean.threed.Vector3D;
31 import org.hipparchus.linear.SingularValueDecomposer;
32 import org.hipparchus.optim.SimpleVectorValueChecker;
33 import org.hipparchus.optim.nonlinear.vector.leastsquares.LeastSquaresOptimizer.Optimum;
34 import org.hipparchus.util.FastMath;
35 import org.junit.Assert;
36 import org.junit.Test;
37
38
39
40
41
42
43
44
45
46 public class GaussNewtonOptimizerWithSVDTest
47 extends AbstractLeastSquaresOptimizerAbstractTest {
48
49 @Override
50 public int getMaxIterations() {
51 return 1000;
52 }
53
54 @Override
55 public LeastSquaresOptimizer getOptimizer() {
56 return new GaussNewtonOptimizer(new SingularValueDecomposer(), false);
57 }
58
59 @Test
60 public 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 optimizer.optimize(lsp);
76
77 fail(optimizer);
78 }catch (MathIllegalStateException e){
79
80 }
81 }
82
83 @Override
84 @Test
85 public void testCircleFittingBadInit() {
86
87
88
89
90
91 try {
92 super.testCircleFittingBadInit();
93 fail(optimizer);
94 } catch (AssertionError e) {
95
96 }
97 }
98
99 @Override
100 @Test
101 public void testHahn1() throws IOException {
102 try {
103
104
105
106
107
108 super.testHahn1();
109 fail(optimizer);
110 } catch (MathIllegalStateException e) {
111 Assert.assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED, e.getSpecifier());
112 }
113 }
114
115 @Test
116 @Override
117 public void testGetIterations() {
118
119 try {
120 super.testGetIterations();
121 fail(optimizer);
122 } catch (MathIllegalStateException e) {
123 Assert.assertEquals(LocalizedCoreFormats.MAX_COUNT_EXCEEDED,
124 e.getSpecifier());
125 }
126 }
127
128 @Test
129 @Override
130 public void testNonInvertible() throws Exception {
131
132
133
134
135
136 LinearProblem problem = new LinearProblem(new double[][]{
137 {1, 2, -3},
138 {2, 1, 3},
139 {-3, 0, -9}
140 }, new double[]{1, 1, 1});
141
142 Optimum optimum = optimizer.optimize(problem.getBuilder().build());
143
144 Plane span = new Plane(Vector3D.ZERO, new Vector3D(1, 2, -3), new Vector3D(2, 1, 0), TOl);
145 double expected = FastMath.abs(span.getOffset(new Vector3D(1, 1, 1)));
146 double actual = optimum.getResiduals().getNorm();
147
148
149 Assert.assertEquals(expected, actual, TOl);
150 }
151
152 }