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