org.apache.commons.math3.optim.nonlinear.vector.Target Java Examples

The following examples show how to use org.apache.commons.math3.optim.nonlinear.vector.Target. 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(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1 }),
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[1], 1e-10);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(new double[] { 1 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #2
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    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[] { 0, 0 }));
    Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum.getPointRef()[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.getRMS(),       1e-8);
}
 
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(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1 }),
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[1], 1e-10);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(new double[] { 1 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #5
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer
        = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));

    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 }));
}
 
Example #6
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer
        = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));

    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 }));
}
 
Example #7
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1 }),
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[1], 1e-10);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(new double[] { 1 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #8
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
    LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16),
                                          FastMath.sqrt(2.22044604926e-16),
                                          2.22044604926e-16);
    try {
        PointVectorValuePair optimum
            = optimizer.optimize(new MaxEval(400 * (function.getN() + 1)),
                                 function.getModelFunction(),
                                 function.getModelFunctionJacobian(),
                                 new Target(function.getTarget()),
                                 new Weight(function.getWeight()),
                                 new InitialGuess(function.getStartPoint()));
        Assert.assertFalse(exceptionExpected);
        function.checkTheoreticalMinCost(optimizer.getRMS());
        function.checkTheoreticalMinParams(optimum);
    } catch (TooManyEvaluationsException e) {
        Assert.assertTrue(exceptionExpected);
    }
}
 
Example #9
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example #10
Source File: LSQFitter.java    From thunderstorm with GNU General Public License v3.0 6 votes vote down vote up
protected Molecule fit(ILsqFunctions functions) {
    // init
    double[] weights = functions.calcWeights(useWeighting);
    double[] observations = functions.getObservations();

    // fit
    LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(
            new SimplePointChecker<PointVectorValuePair>(10e-10, 10e-10, maxIter));

    PointVectorValuePair pv;
    pv = optimizer.optimize(
            MaxEval.unlimited(),
            new MaxIter(MAX_ITERATIONS + 1),
            new ModelFunction(functions.getValueFunction()),
            new ModelFunctionJacobian(functions.getJacobianFunction()),
            new Target(observations),
            new InitialGuess(psfModel.transformParametersInverse(functions.getInitialParams())),
            new Weight(weights));

    // estimate background and return an instance of the `Molecule`
    fittedParameters = pv.getPointRef();
    if (bkgStdColumn >= 0) {
        fittedParameters[bkgStdColumn] = VectorMath.stddev(sub(observations, functions.getValueFunction().value(fittedParameters)));
    }
    return psfModel.newInstanceFromParams(psfModel.transformParameters(fittedParameters), functions.getImageUnits(), true);
}
 
Example #11
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetChiSquare() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1.0);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    optimizer.optimize(new MaxEval(1),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(y),
                       new Weight(w),
                       new InitialGuess(a));
    final double expected = dataset.getResidualSumOfSquares();
    final double actual = optimizer.getChiSquare();
    Assert.assertEquals(dataset.getName(), expected, actual,
                        1E-11 * expected);
}
 
Example #12
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
    LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16),
                                          FastMath.sqrt(2.22044604926e-16),
                                          2.22044604926e-16);
    try {
        PointVectorValuePair optimum
            = optimizer.optimize(new MaxEval(400 * (function.getN() + 1)),
                                 function.getModelFunction(),
                                 function.getModelFunctionJacobian(),
                                 new Target(function.getTarget()),
                                 new Weight(function.getWeight()),
                                 new InitialGuess(function.getStartPoint()));
        Assert.assertFalse(exceptionExpected);
        function.checkTheoreticalMinCost(optimizer.getRMS());
        function.checkTheoreticalMinParams(optimum);
    } catch (TooManyEvaluationsException e) {
        Assert.assertTrue(exceptionExpected);
    }
}
 
Example #13
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example #14
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer
        = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));

    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 }));
}
 
Example #15
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 #16
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    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[] { 0, 0 }));
    Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum.getPointRef()[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.getRMS(),       1e-8);
}
 
Example #17
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1 }),
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[1], 1e-10);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(new double[] { 1 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #18
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 #19
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    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[] { 0, 0 }));
    Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum.getPointRef()[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.getRMS(),       1e-8);
}
 
Example #20
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
public void doTestStRD(final StatisticalReferenceDataset dataset,
                       final double errParams,
                       final double errParamsSd) {
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] w = new double[dataset.getNumObservations()];
    Arrays.fill(w, 1);

    final double[][] data = dataset.getData();
    final double[] initial = dataset.getStartingPoint(0);
    final StatisticalReferenceDataset.LeastSquaresProblem problem = dataset.getLeastSquaresProblem();
    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(data[1]),
                             new Weight(w),
                             new InitialGuess(initial));

    final double[] actual = optimum.getPoint();
    for (int i = 0; i < actual.length; i++) {
        double expected = dataset.getParameter(i);
        double delta = FastMath.abs(errParams * expected);
        Assert.assertEquals(dataset.getName() + ", param #" + i,
                            expected, actual[i], delta);
    }
}
 
Example #21
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
    LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16),
                                          FastMath.sqrt(2.22044604926e-16),
                                          2.22044604926e-16);
    try {
        PointVectorValuePair optimum
            = optimizer.optimize(new MaxEval(400 * (function.getN() + 1)),
                                 function.getModelFunction(),
                                 function.getModelFunctionJacobian(),
                                 new Target(function.getTarget()),
                                 new Weight(function.getWeight()),
                                 new InitialGuess(function.getStartPoint()));
        Assert.assertFalse(exceptionExpected);
        function.checkTheoreticalMinCost(optimizer.getRMS());
        function.checkTheoreticalMinParams(optimum);
    } catch (TooManyEvaluationsException e) {
        Assert.assertTrue(exceptionExpected);
    }
}
 
Example #22
Source File: AbstractLeastSquaresOptimizerTestValidation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return the normalized chi-square.
 */
private double getChi2N(AbstractLeastSquaresOptimizer optim,
                        StraightLineProblem problem,
                        double[] params) {
    final double[] t = problem.target();
    final double[] w = problem.weight();

    optim.optimize(new MaxEval(Integer.MAX_VALUE),
                   problem.getModelFunction(),
                   problem.getModelFunctionJacobian(),
                   new Target(t),
                   new Weight(w),
                   new InitialGuess(params));

    return optim.getChiSquare() / (t.length - params.length);
}
 
Example #23
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer
        = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));

    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 }));
}
 
Example #24
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetChiSquare() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1.0);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    optimizer.optimize(new MaxEval(1),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(y),
                       new Weight(w),
                       new InitialGuess(a));
    final double expected = dataset.getResidualSumOfSquares();
    final double actual = optimizer.getChiSquare();
    Assert.assertEquals(dataset.getName(), expected, actual,
                        1E-11 * expected);
}
 
Example #25
Source File: GaussNewtonOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=TooManyEvaluationsException.class)
public void testMaxEvaluations() throws Exception {
    CircleVectorial circle = new CircleVectorial();
    circle.addPoint( 30.0,  68.0);
    circle.addPoint( 50.0,  -6.0);
    circle.addPoint(110.0, -20.0);
    circle.addPoint( 35.0,  15.0);
    circle.addPoint( 45.0,  97.0);

    GaussNewtonOptimizer optimizer
        = new GaussNewtonOptimizer(new SimpleVectorValueChecker(1e-30, 1e-30));

    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 }));
}
 
Example #26
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testGetChiSquare() throws IOException {
    final StatisticalReferenceDataset dataset
        = StatisticalReferenceDatasetFactory.createKirby2();
    final AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    final double[] a = dataset.getParameters();
    final double[] y = dataset.getData()[1];
    final double[] w = new double[y.length];
    Arrays.fill(w, 1.0);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    optimizer.optimize(new MaxEval(1),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(y),
                       new Weight(w),
                       new InitialGuess(a));
    final double expected = dataset.getResidualSumOfSquares();
    final double actual = optimizer.getChiSquare();
    Assert.assertEquals(dataset.getName(), expected, actual,
                        1E-11 * expected);
}
 
Example #27
Source File: AbstractLeastSquaresOptimizerTestValidation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return the normalized chi-square.
 */
private double getChi2N(AbstractLeastSquaresOptimizer optim,
                        StraightLineProblem problem,
                        double[] params) {
    final double[] t = problem.target();
    final double[] w = problem.weight();

    optim.optimize(new MaxEval(Integer.MAX_VALUE),
                   problem.getModelFunction(),
                   problem.getModelFunctionJacobian(),
                   new Target(t),
                   new Weight(w),
                   new InitialGuess(params));

    return optim.getChiSquare() / (t.length - params.length);
}
 
Example #28
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=DimensionMismatchException.class)
public void testInconsistentSizes2() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, 0 }, { 0, 1 } },
                            new double[] { -1, 1 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1 }),
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(-1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[1], 1e-10);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(new double[] { 1 }),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #29
Source File: MinpackTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
private void minpackTest(MinpackFunction function, boolean exceptionExpected) {
    LevenbergMarquardtOptimizer optimizer
        = new LevenbergMarquardtOptimizer(FastMath.sqrt(2.22044604926e-16),
                                          FastMath.sqrt(2.22044604926e-16),
                                          2.22044604926e-16);
    try {
        PointVectorValuePair optimum
            = optimizer.optimize(new MaxEval(400 * (function.getN() + 1)),
                                 function.getModelFunction(),
                                 function.getModelFunctionJacobian(),
                                 new Target(function.getTarget()),
                                 new Weight(function.getWeight()),
                                 new InitialGuess(function.getStartPoint()));
        Assert.assertFalse(exceptionExpected);
        function.checkTheoreticalMinCost(optimizer.getRMS());
        function.checkTheoreticalMinParams(optimum);
    } catch (TooManyEvaluationsException e) {
        Assert.assertTrue(exceptionExpected);
    }
}
 
Example #30
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingGoodInit() {
    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[] { 0, 0 }));
    Assert.assertEquals(-0.1517383071957963, optimum.getPointRef()[0], 1e-6);
    Assert.assertEquals(0.2074999736353867,  optimum.getPointRef()[1], 1e-6);
    Assert.assertEquals(0.04268731682389561, optimizer.getRMS(),       1e-8);
}