org.apache.commons.math3.optim.PointVectorValuePair Java Examples

The following examples show how to use org.apache.commons.math3.optim.PointVectorValuePair. 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 testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1, 1, 1, 1 }),
                           new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1e-10);
    }
}
 
Example #2
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #3
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 testInconsistentSizes1() {
    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(),
                       problem.getTarget(),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0, 0 }));
}
 
Example #4
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 5 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1 }),
                           new InitialGuess(new double[] { 1, 1 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(2, optimum.getPointRef()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPointRef()[1], 1e-10);
}
 
Example #5
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 #6
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 #7
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 #8
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #9
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRedundantEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1,  1 },
            { 1, -1 },
            { 1,  3 }
    }, new double[] { 3, 1, 5 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1 }),
                           new InitialGuess(new double[] { 1, 1 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(2, optimum.getPointRef()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPointRef()[1], 1e-10);
}
 
Example #10
Source File: MultiStartMultivariateVectorOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return a comparator for sorting the optima.
 */
private Comparator<PointVectorValuePair> getPairComparator() {
    return new Comparator<PointVectorValuePair>() {
        private final RealVector target = new ArrayRealVector(optimizer.getTarget(), false);
        private final RealMatrix weight = optimizer.getWeight();

        public int compare(final PointVectorValuePair o1,
                           final PointVectorValuePair o2) {
            if (o1 == null) {
                return (o2 == null) ? 0 : 1;
            } else if (o2 == null) {
                return -1;
            }
            return Double.compare(weightedResidual(o1),
                                  weightedResidual(o2));
        }

        private double weightedResidual(final PointVectorValuePair pv) {
            final RealVector v = new ArrayRealVector(pv.getValueRef(), false);
            final RealVector r = target.subtract(v);
            return r.dotProduct(weight.operate(r));
        }
    };
}
 
Example #11
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQRColumnsPermutation() {

    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

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

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1, 1, 1 }),
                           new InitialGuess(new double[] { 2, 2, 2, 2, 2, 2 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(3, optimum.getPointRef()[2], 1e-10);
    Assert.assertEquals(4, optimum.getPointRef()[3], 1e-10);
    Assert.assertEquals(5, optimum.getPointRef()[4], 1e-10);
    Assert.assertEquals(6, optimum.getPointRef()[5], 1e-10);
}
 
Example #14
Source File: LevenbergMarquardtOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Override
@Test(expected=SingularMatrixException.class)
public void testNonInvertible() {
    /*
     * Overrides the method from parent class, since the default singularity
     * threshold (1e-14) does not trigger the expected exception.
     */
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1, 1, 1 }),
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(FastMath.sqrt(optimizer.getTargetSize()) * optimizer.getRMS() > 0.6);

    optimizer.computeCovariances(optimum.getPoint(), 1.5e-14);
}
 
Example #15
Source File: MultiStartMultivariateVectorOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return a comparator for sorting the optima.
 */
private Comparator<PointVectorValuePair> getPairComparator() {
    return new Comparator<PointVectorValuePair>() {
        private final RealVector target = new ArrayRealVector(optimizer.getTarget(), false);
        private final RealMatrix weight = optimizer.getWeight();

        public int compare(final PointVectorValuePair o1,
                           final PointVectorValuePair o2) {
            if (o1 == null) {
                return (o2 == null) ? 0 : 1;
            } else if (o2 == null) {
                return -1;
            }
            return Double.compare(weightedResidual(o1),
                                  weightedResidual(o2));
        }

        private double weightedResidual(final PointVectorValuePair pv) {
            final RealVector v = new ArrayRealVector(pv.getValueRef(), false);
            final RealVector r = target.subtract(v);
            return r.dotProduct(weight.operate(r));
        }
    };
}
 
Example #16
Source File: MultiStartMultivariateVectorOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return a comparator for sorting the optima.
 */
private Comparator<PointVectorValuePair> getPairComparator() {
    return new Comparator<PointVectorValuePair>() {
        private final RealVector target = new ArrayRealVector(optimizer.getTarget(), false);
        private final RealMatrix weight = optimizer.getWeight();

        public int compare(final PointVectorValuePair o1,
                           final PointVectorValuePair o2) {
            if (o1 == null) {
                return (o2 == null) ? 0 : 1;
            } else if (o2 == null) {
                return -1;
            }
            return Double.compare(weightedResidual(o1),
                                  weightedResidual(o2));
        }

        private double weightedResidual(final PointVectorValuePair pv) {
            final RealVector v = new ArrayRealVector(pv.getValueRef(), false);
            final RealVector r = target.subtract(v);
            return r.dotProduct(weight.operate(r));
        }
    };
}
 
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 testQRColumnsPermutation() {

    LinearProblem problem
        = new LinearProblem(new double[][] { { 1, -1 }, { 0, 2 }, { 1, -2 } },
                            new double[] { 4, 6, 1 });

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

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1 }),
                           new InitialGuess(new double[] { 1, 1 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(2, optimum.getPointRef()[0], 1e-10);
    Assert.assertEquals(1, optimum.getPointRef()[1], 1e-10);
}
 
Example #22
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNoDependency() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 2, 0, 0, 0, 0, 0 },
            { 0, 2, 0, 0, 0, 0 },
            { 0, 0, 2, 0, 0, 0 },
            { 0, 0, 0, 2, 0, 0 },
            { 0, 0, 0, 0, 2, 0 },
            { 0, 0, 0, 0, 0, 2 }
    }, new double[] { 0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum =
        optimizer.optimize(new MaxEval(100),
                           problem.getModelFunction(),
                           problem.getModelFunctionJacobian(),
                           problem.getTarget(),
                           new Weight(new double[] { 1, 1, 1, 1, 1, 1 }),
                           new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1e-10);
    }
}
 
Example #23
Source File: MultiStartMultivariateVectorOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    JacobianMultivariateVectorOptimizer underlyingOptimizer
        = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(16069223052l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartMultivariateVectorOptimizer optimizer
        = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);

    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1 }),
                             new InitialGuess(new double[] { 0 }));
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
    PointVectorValuePair[] optima = optimizer.getOptima();
    Assert.assertEquals(10, optima.length);
    for (int i = 0; i < optima.length; i++) {
        Assert.assertEquals(1.5, optima[i].getPoint()[0], 1e-10);
        Assert.assertEquals(3.0, optima[i].getValue()[0], 1e-10);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 20);
    Assert.assertTrue(optimizer.getEvaluations() < 50);
    Assert.assertEquals(100, optimizer.getMaxEvaluations());
}
 
Example #24
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 #25
Source File: MultiStartMultivariateVectorOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testIssue914() {
    LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    JacobianMultivariateVectorOptimizer underlyingOptimizer =
            new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)) {
        public PointVectorValuePair optimize(OptimizationData... optData) {
            // filter out simple bounds, as they are not supported
            // by the underlying optimizer, and we don't really care for this test
            OptimizationData[] filtered = optData.clone();
            for (int i = 0; i < filtered.length; ++i) {
                if (filtered[i] instanceof SimpleBounds) {
                    filtered[i] = null;
                }
            }
            return super.optimize(filtered);
        }
    };
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(16069223052l);
    RandomVectorGenerator generator =
            new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartMultivariateVectorOptimizer optimizer =
            new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       problem.getTarget(),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0 }),
                       new SimpleBounds(new double[] { -1.0e-10 }, new double[] {  1.0e-10 }));
    PointVectorValuePair[] optima = optimizer.getOptima();
    // only the first start should have succeeded
    Assert.assertEquals(1, optima.length);

}
 
Example #26
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 #27
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 #28
Source File: MultiStartMultivariateVectorOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testTrivial() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    JacobianMultivariateVectorOptimizer underlyingOptimizer
        = new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(16069223052l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartMultivariateVectorOptimizer optimizer
        = new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);

    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             problem.getTarget(),
                             new Weight(new double[] { 1 }),
                             new InitialGuess(new double[] { 0 }));
    Assert.assertEquals(1.5, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(3.0, optimum.getValue()[0], 1e-10);
    PointVectorValuePair[] optima = optimizer.getOptima();
    Assert.assertEquals(10, optima.length);
    for (int i = 0; i < optima.length; i++) {
        Assert.assertEquals(1.5, optima[i].getPoint()[0], 1e-10);
        Assert.assertEquals(3.0, optima[i].getValue()[0], 1e-10);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 20);
    Assert.assertTrue(optimizer.getEvaluations() < 50);
    Assert.assertEquals(100, optimizer.getMaxEvaluations());
}
 
Example #29
Source File: AbstractLeastSquaresOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testComputeSigma() 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);

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    final PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(1),
                             problem.getModelFunction(),
                             problem.getModelFunctionJacobian(),
                             new Target(y),
                             new Weight(w),
                             new InitialGuess(a));

    final double[] sig = optimizer.computeSigma(optimum.getPoint(), 1e-14);

    final int dof = y.length - a.length;
    final double[] expected = dataset.getParametersStandardDeviations();
    for (int i = 0; i < sig.length; i++) {
        final double actual = FastMath.sqrt(optimizer.getChiSquare() / dof) * sig[i];
        Assert.assertEquals(dataset.getName() + ", parameter #" + i,
                            expected[i], actual, 1e-6 * expected[i]);
    }
}
 
Example #30
Source File: MultiStartMultivariateVectorOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testIssue914() {
    LinearProblem problem = new LinearProblem(new double[][] { { 2 } }, new double[] { 3 });
    JacobianMultivariateVectorOptimizer underlyingOptimizer =
            new GaussNewtonOptimizer(true, new SimpleVectorValueChecker(1e-6, 1e-6)) {
        @Override
        public PointVectorValuePair optimize(OptimizationData... optData) {
            // filter out simple bounds, as they are not supported
            // by the underlying optimizer, and we don't really care for this test
            OptimizationData[] filtered = optData.clone();
            for (int i = 0; i < filtered.length; ++i) {
                if (filtered[i] instanceof SimpleBounds) {
                    filtered[i] = null;
                }
            }
            return super.optimize(filtered);
        }
    };
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(16069223052l);
    RandomVectorGenerator generator =
            new UncorrelatedRandomVectorGenerator(1, new GaussianRandomGenerator(g));
    MultiStartMultivariateVectorOptimizer optimizer =
            new MultiStartMultivariateVectorOptimizer(underlyingOptimizer, 10, generator);

    optimizer.optimize(new MaxEval(100),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       problem.getTarget(),
                       new Weight(new double[] { 1 }),
                       new InitialGuess(new double[] { 0 }),
                       new SimpleBounds(new double[] { -1.0e-10 }, new double[] {  1.0e-10 }));
    PointVectorValuePair[] optima = optimizer.getOptima();
    // only the first start should have succeeded
    Assert.assertEquals(1, optima.length);

}