This commit is contained in:
Gilles Sadowski 2021-06-09 16:18:57 +02:00
parent a77999bb84
commit a5b1aa0294
2 changed files with 31 additions and 32 deletions

View File

@ -55,7 +55,7 @@ import static org.hamcrest.CoreMatchers.sameInstance;
public abstract class AbstractLeastSquaresOptimizerAbstractTest { public abstract class AbstractLeastSquaresOptimizerAbstractTest {
/** default absolute tolerance of comparisons */ /** default absolute tolerance of comparisons */
public static final double TOl = 1e-10; public static final double TOL = 1e-10;
public LeastSquaresBuilder base() { public LeastSquaresBuilder base() {
return new LeastSquaresBuilder() return new LeastSquaresBuilder()
@ -160,9 +160,9 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(ls); Optimum optimum = optimizer.optimize(ls);
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 1.5); assertEquals(TOL, optimum.getPoint(), 1.5);
Assert.assertEquals(0.0, optimum.getResiduals().getEntry(0), TOl); Assert.assertEquals(0.0, optimum.getResiduals().getEntry(0), TOL);
} }
@Test @Test
@ -173,9 +173,9 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 7, 3); assertEquals(TOL, optimum.getPoint(), 7, 3);
assertEquals(TOl, optimum.getResiduals(), 0, 0, 0); assertEquals(TOL, optimum.getResiduals(), 0, 0, 0);
} }
@Test @Test
@ -191,9 +191,9 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
for (int i = 0; i < problem.target.length; ++i) { for (int i = 0; i < problem.target.length; ++i) {
Assert.assertEquals(0.55 * i, optimum.getPoint().getEntry(i), TOl); Assert.assertEquals(0.55 * i, optimum.getPoint().getEntry(i), TOL);
} }
} }
@ -207,8 +207,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 1, 2, 3); assertEquals(TOL, optimum.getPoint(), 1, 2, 3);
} }
@Test @Test
@ -225,8 +225,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 3, 4, -1, -2, 1 + epsilon, 1 - epsilon); assertEquals(TOL, optimum.getPoint(), 3, 4, -1, -2, 1 + epsilon, 1 - epsilon);
} }
@Test @Test
@ -259,8 +259,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer Optimum optimum = optimizer
.optimize(problem1.getBuilder().start(start).build()); .optimize(problem1.getBuilder().start(start).build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 1, 1, 1, 1); assertEquals(TOL, optimum.getPoint(), 1, 1, 1, 1);
LinearProblem problem2 = new LinearProblem(new double[][]{ LinearProblem problem2 = new LinearProblem(new double[][]{
{10.00, 7.00, 8.10, 7.20}, {10.00, 7.00, 8.10, 7.20},
@ -271,7 +271,7 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
optimum = optimizer.optimize(problem2.getBuilder().start(start).build()); optimum = optimizer.optimize(problem2.getBuilder().start(start).build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(1e-8, optimum.getPoint(), -81, 137, -34, 22); assertEquals(1e-8, optimum.getPoint(), -81, 137, -34, 22);
} }
@ -286,7 +286,7 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer Optimum optimum = optimizer
.optimize(problem.getBuilder().start(new double[]{7, 6, 5, 4}).build()); .optimize(problem.getBuilder().start(new double[]{7, 6, 5, 4}).build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
} }
@Test @Test
@ -302,13 +302,13 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize( Optimum optimum = optimizer.optimize(
problem.getBuilder().start(new double[]{2, 2, 2, 2, 2, 2}).build()); problem.getBuilder().start(new double[]{2, 2, 2, 2, 2, 2}).build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
RealVector point = optimum.getPoint(); RealVector point = optimum.getPoint();
//the first two elements are under constrained //the first two elements are under constrained
//check first two elements obey the constraint: sum to 3 //check first two elements obey the constraint: sum to 3
Assert.assertEquals(3, point.getEntry(0) + point.getEntry(1), TOl); Assert.assertEquals(3, point.getEntry(0) + point.getEntry(1), TOL);
//#constrains = #states fro the last 4 elements //#constrains = #states fro the last 4 elements
assertEquals(TOl, point.getSubVector(2, 4), 3, 4, 5, 6); assertEquals(TOL, point.getSubVector(2, 4), 3, 4, 5, 6);
} }
@Test @Test
@ -322,8 +322,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer Optimum optimum = optimizer
.optimize(problem.getBuilder().start(new double[]{1, 1}).build()); .optimize(problem.getBuilder().start(new double[]{1, 1}).build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), 2, 1); assertEquals(TOL, optimum.getPoint(), 2, 1);
} }
@Test @Test
@ -352,8 +352,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
//TODO why is this part here? hasn't it been tested already? //TODO why is this part here? hasn't it been tested already?
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), -1, 1); assertEquals(TOL, optimum.getPoint(), -1, 1);
//TODO move to builder test //TODO move to builder test
optimizer.optimize( optimizer.optimize(
@ -374,8 +374,8 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Assert.assertEquals(0, optimum.getRMS(), TOl); Assert.assertEquals(0, optimum.getRMS(), TOL);
assertEquals(TOl, optimum.getPoint(), -1, 1); assertEquals(TOL, optimum.getPoint(), -1, 1);
//TODO move to builder test //TODO move to builder test
optimizer.optimize( optimizer.optimize(
@ -406,7 +406,7 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
Assert.assertTrue(optimum.getEvaluations() < 10); Assert.assertTrue(optimum.getEvaluations() < 10);
double rms = optimum.getRMS(); double rms = optimum.getRMS();
Assert.assertEquals(1.768262623567235, AccurateMath.sqrt(circle.getN()) * rms, TOl); Assert.assertEquals(1.768262623567235, AccurateMath.sqrt(circle.getN()) * rms, TOL);
Vector2D center = Vector2D.of(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1)); Vector2D center = Vector2D.of(optimum.getPoint().getEntry(0), optimum.getPoint().getEntry(1));
Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6); Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
@ -553,7 +553,7 @@ public abstract class AbstractLeastSquaresOptimizerAbstractTest {
previous.getPoint(), previous.getPoint(),
not(sameInstance(current.getPoint()))); not(sameInstance(current.getPoint())));
Assert.assertArrayEquals(new double[3], previous.getPoint().toArray(), 0); Assert.assertArrayEquals(new double[3], previous.getPoint().toArray(), 0);
Assert.assertArrayEquals(new double[] {1, 2, 3}, current.getPoint().toArray(), TOl); Assert.assertArrayEquals(new double[] {1, 2, 3}, current.getPoint().toArray(), TOL);
checked[0] = true; checked[0] = true;
return true; return true;
} }

View File

@ -17,10 +17,10 @@
package org.apache.commons.math4.legacy.fitting.leastsquares; package org.apache.commons.math4.legacy.fitting.leastsquares;
import org.apache.commons.numbers.core.Precision;
import org.apache.commons.geometry.euclidean.threed.Plane; import org.apache.commons.geometry.euclidean.threed.Plane;
import org.apache.commons.geometry.euclidean.threed.Planes; import org.apache.commons.geometry.euclidean.threed.Planes;
import org.apache.commons.geometry.euclidean.threed.Vector3D; import org.apache.commons.geometry.euclidean.threed.Vector3D;
import org.apache.commons.geometry.core.precision.EpsilonDoublePrecisionContext;
import org.apache.commons.math4.legacy.exception.ConvergenceException; import org.apache.commons.math4.legacy.exception.ConvergenceException;
import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException; import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
import org.apache.commons.math4.legacy.fitting.leastsquares.GaussNewtonOptimizer.Decomposition; import org.apache.commons.math4.legacy.fitting.leastsquares.GaussNewtonOptimizer.Decomposition;
@ -140,12 +140,11 @@ public class GaussNewtonOptimizerWithSVDTest
Optimum optimum = optimizer.optimize(problem.getBuilder().build()); Optimum optimum = optimizer.optimize(problem.getBuilder().build());
Plane span = Planes.fromPoints(Vector3D.ZERO, Vector3D.of(1, 2, -3), Vector3D.of(2, 1, 0), Plane span = Planes.fromPoints(Vector3D.ZERO, Vector3D.of(1, 2, -3), Vector3D.of(2, 1, 0),
new EpsilonDoublePrecisionContext(TOl)); Precision.doubleEquivalenceOfEpsilon(TOL));
double expected = AccurateMath.abs(span.offset(Vector3D.of(1, 1, 1))); double expected = AccurateMath.abs(span.offset(Vector3D.of(1, 1, 1)));
double actual = optimum.getResiduals().getNorm(); double actual = optimum.getResiduals().getNorm();
//verify //verify
Assert.assertEquals(expected, actual, TOl); Assert.assertEquals(expected, actual, TOL);
} }
} }