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

The following examples show how to use org.apache.commons.math3.optim.InitialGuess. 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: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMoreEstimatedParametersSimple() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 3.0, 2.0,  0.0, 0.0 },
            { 0.0, 1.0, -1.0, 1.0 },
            { 2.0, 0.0,  1.0, 0.0 }
    }, new double[] { 7.0, 3.0, 5.0 });

    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6));
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 7, 6, 5, 4 }));
    Assert.assertEquals(0, optimum.getValue(), 1.0e-10);

}
 
Example #2
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #3
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testColumnsPermutation() {
    LinearProblem problem
        = new LinearProblem(new double[][] { { 1.0, -1.0 }, { 0.0, 2.0 }, { 1.0, -2.0 } },
                            new double[] { 4.0, 6.0, 1.0 });

    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6),
                                                  1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0 }));
    Assert.assertEquals(7.0, optimum.getPoint()[0], 1.0e-10);
    Assert.assertEquals(3.0, optimum.getPoint()[1], 1.0e-10);
    Assert.assertEquals(0.0, optimum.getValue(), 1.0e-10);

}
 
Example #4
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 #5
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath283() {
    // fails because MultiDirectional.iterateSimplex is looping forever
    // the while(true) should be replaced with a convergence check
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-14, 1e-14);
    final Gaussian2D function = new Gaussian2D(0, 0, 1);
    PointValuePair estimate = optimizer.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(function),
                                                 GoalType.MAXIMIZE,
                                                 new InitialGuess(function.getMaximumPosition()),
                                                 new MultiDirectionalSimplex(2));
    final double EPSILON = 1e-5;
    final double expectedMaximum = function.getMaximum();
    final double actualMaximum = estimate.getValue();
    Assert.assertEquals(expectedMaximum, actualMaximum, EPSILON);

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
}
 
Example #6
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 #7
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test(expected=ConvergenceException.class)
public void testNonInvertible() throws Exception {

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

    AbstractLeastSquaresOptimizer optimizer = createOptimizer();

    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 }));
}
 
Example #8
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param fLineTol Tolerance (relative error on the objective function)
 * for the internal line search algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double fLineTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
Example #9
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }
}
 
Example #10
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRosenbrock() {

    Rosenbrock rosenbrock = new Rosenbrock();
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-3);
    PointValuePair optimum
    = optimizer.optimize(new MaxEval(100),
                         new ObjectiveFunction(rosenbrock),
                         GoalType.MINIMIZE,
                         new InitialGuess(new double[] { -1.2, 1 }),
                            new NelderMeadSimplex(new double[][] {
                                    { -1.2,  1 },
                                    { 0.9, 1.2 },
                                    {  3.5, -2.3 } }));

    Assert.assertEquals(rosenbrock.getCount(), optimizer.getEvaluations());
    Assert.assertTrue(optimizer.getEvaluations() > 40);
    Assert.assertTrue(optimizer.getEvaluations() < 50);
    Assert.assertTrue(optimum.getValue() < 8e-4);
}
 
Example #11
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneSet() {

    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, 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.assertEquals(0, optimizer.getRMS(), 1e-10);
    Assert.assertEquals(1, optimum.getPoint()[0], 1e-10);
    Assert.assertEquals(2, optimum.getPoint()[1], 1e-10);
    Assert.assertEquals(3, optimum.getPoint()[2], 1e-10);
}
 
Example #12
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares2() {
    final RealMatrix factors
        = new Array2DRowRealMatrix(new double[][] {
                { 1, 0 },
                { 0, 1 }
            }, false);
    LeastSquaresConverter ls = new LeastSquaresConverter(new MultivariateVectorFunction() {
            public double[] value(double[] variables) {
                return factors.operate(variables);
            }
        }, new double[] { 2, -3 }, new double[] { 10, 0.1 });
    SimplexOptimizer optimizer = new SimplexOptimizer(-1, 1e-6);
    PointValuePair optimum =
        optimizer.optimize(new MaxEval(200),
                           new ObjectiveFunction(ls),
                           GoalType.MINIMIZE,
                           new InitialGuess(new double[] { 10, 10 }),
                           new NelderMeadSimplex(2));
    Assert.assertEquals( 2, optimum.getPointRef()[0], 5e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 8e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1e-6);
}
 
Example #13
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 #14
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped
        = new MultivariateFunctionMappingAdapter(biQuadratic,
                                                 biQuadratic.getLower(),
                                                 biQuadratic.getUpper());

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] {
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
            wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
        });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(wrapped),
                             simplex,
                             GoalType.MINIMIZE,
                             new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })));
    final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
}
 
Example #15
Source File: NonLinearConjugateGradientOptimizerTest.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.0, 1.1, 2.2, 3.3, 4.4, 5.5 });
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6),
                                                  1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0, 0, 0, 0 }));
    for (int i = 0; i < problem.target.length; ++i) {
        Assert.assertEquals(0.55 * i, optimum.getPoint()[i], 1.0e-10);
    }
}
 
Example #16
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNonInversible() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6),
                                                  1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(optimum.getValue() > 0.5);
}
 
Example #17
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param fLineTol Tolerance (relative error on the objective function)
 * for the internal line search algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double fLineTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
Example #18
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStartSimplexInsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped
        = new MultivariateFunctionMappingAdapter(biQuadratic,
                                                 biQuadratic.getLower(),
                                                 biQuadratic.getUpper());

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] {
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
            wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
        });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(300),
                             new ObjectiveFunction(wrapped),
                             simplex,
                             GoalType.MINIMIZE,
                             new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })));
    final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
}
 
Example #19
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 #20
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinimize1() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { -3, 0 }),
                             new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 4e-6);
    Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 8e-13);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #21
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStartSimplexOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped
          = new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                   biQuadratic.getLower(),
                                                   biQuadratic.getUpper(),
                                                   1000.0, new double[] { 100.0, 100.0 });

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(300),
                             new ObjectiveFunction(wrapped),
                             simplex,
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { -1.5, 4.0 }));

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
 
Example #22
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 #23
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 #24
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param fLineTol Tolerance (relative error on the objective function)
 * for the internal line search algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double fLineTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d),
                                                      fLineTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }

    Assert.assertTrue(optim.getIterations() > 0);
}
 
Example #25
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStartSimplexInsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped
        = new MultivariateFunctionMappingAdapter(biQuadratic,
                                                 biQuadratic.getLower(),
                                                 biQuadratic.getUpper());

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] {
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
            wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
        });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(300),
                             new ObjectiveFunction(wrapped),
                             simplex,
                             GoalType.MINIMIZE,
                             new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })));
    final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
}
 
Example #26
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOneSet() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1,  0, 0 },
            { -1,  1, 0 },
            {  0, -1, 1 }
    }, new double[] { 1, 1, 1});
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6),
                                                  1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertEquals(1.0, optimum.getPoint()[0], 1.0e-10);
    Assert.assertEquals(2.0, optimum.getPoint()[1], 1.0e-10);
    Assert.assertEquals(3.0, optimum.getPoint()[2], 1.0e-10);

}
 
Example #27
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinimize2() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-11, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 1, 0 }),
                             new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 2e-8);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 120);
    Assert.assertTrue(optimizer.getEvaluations() < 150);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #28
Source File: NonLinearConjugateGradientOptimizerTest.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.0, 1.0,  0.0,  0.0, 0.0,  0.0 },
             { 0.0, 0.0,  1.0,  1.0, 1.0,  0.0 },
             { 0.0, 0.0,  0.0,  0.0, 1.0, -1.0 },
             { 0.0, 0.0, -1.0,  1.0, 0.0,  1.0 },
             { 0.0, 0.0,  0.0, -1.0, 1.0,  0.0 }
    }, new double[] { 3.0, 12.0, -1.0, 7.0, 1.0 });
    NonLinearConjugateGradientOptimizer optimizer
       = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                 new SimpleValueChecker(1e-6, 1e-6),
                                                 1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 2, 2, 2, 2, 2, 2 }));
    Assert.assertEquals(0, optimum.getValue(), 1.0e-10);
}
 
Example #29
Source File: PowellOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param optimum Expected optimum.
 * @param init Starting point.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance (relative error on the objective function) for
 * "Powell" algorithm.
 * @param pointTol Tolerance for checking that the optimum is correct.
 */
private void doTest(MultivariateFunction func,
                    double[] optimum,
                    double[] init,
                    GoalType goal,
                    double fTol,
                    double pointTol) {
    final PowellOptimizer optim = new PowellOptimizer(fTol, Math.ulp(1d));

    final PointValuePair result = optim.optimize(new MaxEval(1000),
                                                 new ObjectiveFunction(func),
                                                 goal,
                                                 new InitialGuess(init));
    final double[] point = result.getPoint();

    for (int i = 0, dim = optimum.length; i < dim; i++) {
        Assert.assertEquals("found[" + i + "]=" + point[i] + " value=" + result.getValue(),
                            optimum[i], point[i], pointTol);
    }
}
 
Example #30
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped
        = new MultivariateFunctionMappingAdapter(biQuadratic,
                                                 biQuadratic.getLower(),
                                                 biQuadratic.getUpper());

    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[][] {
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.75 }),
            wrapped.boundedToUnbounded(new double[] { 1.5, 2.95 }),
            wrapped.boundedToUnbounded(new double[] { 1.7, 2.90 })
        });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(wrapped),
                             simplex,
                             GoalType.MINIMIZE,
                             new InitialGuess(wrapped.boundedToUnbounded(new double[] { 1.5, 2.25 })));
    final double[] bounded = wrapped.unboundedToBounded(optimum.getPoint());

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
}