Java Code Examples for org.apache.commons.math3.optim.PointVectorValuePair#getPointRef()

The following examples show how to use org.apache.commons.math3.optim.PointVectorValuePair#getPointRef() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example 2
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example 3
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example 4
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example 5
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example 6
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void checkTheoreticalMinParams(PointVectorValuePair optimum) {
    double[] params = optimum.getPointRef();
    if (theoreticalMinParams != null) {
        for (int i = 0; i < theoreticalMinParams.length; ++i) {
            double mi = theoreticalMinParams[i];
            double vi = params[i];
            Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi)));
        }
    }
}
 
Example 7
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fit a curve.
 * This method compute the coefficients of the curve that best
 * fit the sample of observed points previously given through calls
 * to the {@link #addObservedPoint(WeightedObservedPoint)
 * addObservedPoint} method.
 *
 * @param f parametric function to fit.
 * @param initialGuess first guess of the function parameters.
 * @param maxEval Maximum number of function evaluations.
 * @return the fitted parameters.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of allowed evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @since 3.0
 */
public double[] fit(int maxEval, T f,
                    final double[] initialGuess) {
    // Prepare least squares problem.
    double[] target  = new double[observations.size()];
    double[] weights = new double[observations.size()];
    int i = 0;
    for (WeightedObservedPoint point : observations) {
        target[i]  = point.getY();
        weights[i] = point.getWeight();
        ++i;
    }

    // Input to the optimizer: the model and its Jacobian.
    final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f);

    // Perform the fit.
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(maxEval),
                             model.getModelFunction(),
                             model.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(initialGuess));
    // Extract the coefficients.
    return optimum.getPointRef();
}
 
Example 8
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fit a curve.
 * This method compute the coefficients of the curve that best
 * fit the sample of observed points previously given through calls
 * to the {@link #addObservedPoint(WeightedObservedPoint)
 * addObservedPoint} method.
 *
 * @param f parametric function to fit.
 * @param initialGuess first guess of the function parameters.
 * @param maxEval Maximum number of function evaluations.
 * @return the fitted parameters.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of allowed evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @since 3.0
 */
public double[] fit(int maxEval, T f,
                    final double[] initialGuess) {
    // Prepare least squares problem.
    double[] target  = new double[observations.size()];
    double[] weights = new double[observations.size()];
    int i = 0;
    for (WeightedObservedPoint point : observations) {
        target[i]  = point.getY();
        weights[i] = point.getWeight();
        ++i;
    }

    // Input to the optimizer: the model and its Jacobian.
    final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f);

    // Perform the fit.
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(maxEval),
                             model.getModelFunction(),
                             model.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(initialGuess));
    // Extract the coefficients.
    return optimum.getPointRef();
}
 
Example 9
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void checkTheoreticalMinParams(PointVectorValuePair optimum) {
    double[] params = optimum.getPointRef();
    if (theoreticalMinParams != null) {
        for (int i = 0; i < theoreticalMinParams.length; ++i) {
            double mi = theoreticalMinParams[i];
            double vi = params[i];
            Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi)));
        }
    }
}
 
Example 10
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fit a curve.
 * This method compute the coefficients of the curve that best
 * fit the sample of observed points previously given through calls
 * to the {@link #addObservedPoint(WeightedObservedPoint)
 * addObservedPoint} method.
 *
 * @param f parametric function to fit.
 * @param initialGuess first guess of the function parameters.
 * @param maxEval Maximum number of function evaluations.
 * @return the fitted parameters.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of allowed evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @since 3.0
 */
public double[] fit(int maxEval, T f,
                    final double[] initialGuess) {
    // Prepare least squares problem.
    double[] target  = new double[observations.size()];
    double[] weights = new double[observations.size()];
    int i = 0;
    for (WeightedObservedPoint point : observations) {
        target[i]  = point.getY();
        weights[i] = point.getWeight();
        ++i;
    }

    // Input to the optimizer: the model and its Jacobian.
    final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f);

    // Perform the fit.
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(maxEval),
                             model.getModelFunction(),
                             model.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(initialGuess));
    // Extract the coefficients.
    return optimum.getPointRef();
}
 
Example 11
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void checkTheoreticalMinParams(PointVectorValuePair optimum) {
    double[] params = optimum.getPointRef();
    if (theoreticalMinParams != null) {
        for (int i = 0; i < theoreticalMinParams.length; ++i) {
            double mi = theoreticalMinParams[i];
            double vi = params[i];
            Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi)));
        }
    }
}
 
Example 12
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fit a curve.
 * This method compute the coefficients of the curve that best
 * fit the sample of observed points previously given through calls
 * to the {@link #addObservedPoint(WeightedObservedPoint)
 * addObservedPoint} method.
 *
 * @param f parametric function to fit.
 * @param initialGuess first guess of the function parameters.
 * @param maxEval Maximum number of function evaluations.
 * @return the fitted parameters.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of allowed evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @since 3.0
 */
public double[] fit(int maxEval, T f,
                    final double[] initialGuess) {
    // Prepare least squares problem.
    double[] target  = new double[observations.size()];
    double[] weights = new double[observations.size()];
    int i = 0;
    for (WeightedObservedPoint point : observations) {
        target[i]  = point.getY();
        weights[i] = point.getWeight();
        ++i;
    }

    // Input to the optimizer: the model and its Jacobian.
    final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f);

    // Perform the fit.
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(maxEval),
                             model.getModelFunction(),
                             model.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(initialGuess));
    // Extract the coefficients.
    return optimum.getPointRef();
}
 
Example 13
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void checkTheoreticalMinParams(PointVectorValuePair optimum) {
    double[] params = optimum.getPointRef();
    if (theoreticalMinParams != null) {
        for (int i = 0; i < theoreticalMinParams.length; ++i) {
            double mi = theoreticalMinParams[i];
            double vi = params[i];
            Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi)));
        }
    }
}
 
Example 14
Source File: CurveFitter.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Fit a curve.
 * This method compute the coefficients of the curve that best
 * fit the sample of observed points previously given through calls
 * to the {@link #addObservedPoint(WeightedObservedPoint)
 * addObservedPoint} method.
 *
 * @param f parametric function to fit.
 * @param initialGuess first guess of the function parameters.
 * @param maxEval Maximum number of function evaluations.
 * @return the fitted parameters.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of allowed evaluations is exceeded.
 * @throws org.apache.commons.math3.exception.DimensionMismatchException
 * if the start point dimension is wrong.
 * @since 3.0
 */
public double[] fit(int maxEval, T f,
                    final double[] initialGuess) {
    // Prepare least squares problem.
    double[] target  = new double[observations.size()];
    double[] weights = new double[observations.size()];
    int i = 0;
    for (WeightedObservedPoint point : observations) {
        target[i]  = point.getY();
        weights[i] = point.getWeight();
        ++i;
    }

    // Input to the optimizer: the model and its Jacobian.
    final TheoreticalValuesFunction model = new TheoreticalValuesFunction(f);

    // Perform the fit.
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(maxEval),
                             model.getModelFunction(),
                             model.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(initialGuess));
    // Extract the coefficients.
    return optimum.getPointRef();
}
 
Example 15
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
public void checkTheoreticalMinParams(PointVectorValuePair optimum) {
    double[] params = optimum.getPointRef();
    if (theoreticalMinParams != null) {
        for (int i = 0; i < theoreticalMinParams.length; ++i) {
            double mi = theoreticalMinParams[i];
            double vi = params[i];
            Assert.assertEquals(mi, vi, paramsAccuracy * (1.0 + FastMath.abs(mi)));
        }
    }
}
 
Example 16
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 17
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 18
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 19
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}
 
Example 20
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30,  68);
    circle.addPoint( 50,  -6);
    circle.addPoint(110, -20);
    circle.addPoint( 35,  15);
    circle.addPoint( 45,  97);
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(new double[] { 0, 0, 0, 0, 0 }),
                             new Weight(new double[] { 1, 1, 1, 1, 1 }),
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertTrue(optimizer.getEvaluations() < 10);
    double rms = optimizer.getRMS();
    Assert.assertEquals(1.768262623567235,  FastMath.sqrt(circle.getN()) * rms,  1e-10);
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.96016176931406, circle.getRadius(center), 1e-6);
    Assert.assertEquals(96.07590211815305, center.getX(),            1e-6);
    Assert.assertEquals(48.13516790438953, center.getY(),            1e-6);
    double[][] cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(1.839, cov[0][0], 0.001);
    Assert.assertEquals(0.731, cov[0][1], 0.001);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.786, cov[1][1], 0.001);

    // add perfect measurements and check errors are reduced
    double  r = circle.getRadius(center);
    for (double d= 0; d < 2 * FastMath.PI; d += 0.01) {
        circle.addPoint(center.getX() + r * FastMath.cos(d), center.getY() + r * FastMath.sin(d));
    }
    double[] target = new double[circle.getN()];
    Arrays.fill(target, 0);
    double[] weights = new double[circle.getN()];
    Arrays.fill(weights, 2);
    optimum = optimizer.optimize(new MaxEval(100),
                                 circle.getModelFunction(),
                                 circle.getModelFunctionJacobian(),
                                 new Target(target),
                                 new Weight(weights),
                                 new InitialGuess(new double[] { 98.680, 47.345 }));
    cov = optimizer.computeCovariances(optimum.getPoint(), 1e-14);
    Assert.assertEquals(0.0016, cov[0][0], 0.001);
    Assert.assertEquals(3.2e-7, cov[0][1], 1e-9);
    Assert.assertEquals(cov[0][1], cov[1][0], 1e-14);
    Assert.assertEquals(0.0016, cov[1][1], 0.001);
}