org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction Java Examples

The following examples show how to use org.apache.commons.math3.optim.nonlinear.scalar.ObjectiveFunction. 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: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize2() {
    SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30));
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MAXIMIZE,
                             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.yP, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #2
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize1() {
    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.MAXIMIZE,
                             new InitialGuess(new double[] { -3.0, 0.0 }),
                             new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
    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 #3
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 #4
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize1() {
    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.MAXIMIZE,
                             new InitialGuess(new double[] { -3.0, 0.0 }),
                             new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
    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 #5
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 #6
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 #7
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {
    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.0, -3.0 });
    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], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #8
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 #9
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {
    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.0, -3.0 });
    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], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #10
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 #11
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 #12
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize1() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MAXIMIZE,
                             new InitialGuess(new double[] { -3, 0 }),
                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 1e-5);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 3e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 90);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #13
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 #14
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 #15
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 #16
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 #17
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 #18
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 #19
Source File: SimplexOptimizerNelderMeadTest.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-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 1, 0 }),
                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 5e-6);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 6e-6);
    Assert.assertEquals(fourExtrema.valueXpYm, optimum.getValue(), 1e-11);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 90);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #20
Source File: SimplexOptimizerNelderMeadTest.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-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { -3, 0 }),
                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 2e-7);
    Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 2e-5);
    Assert.assertEquals(fourExtrema.valueXmYp, optimum.getValue(), 6e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 90);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #21
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 #22
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 #23
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize1() {
    SimplexOptimizer optimizer = new SimplexOptimizer(1e-10, 1e-30);
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MAXIMIZE,
                             new InitialGuess(new double[] { -3, 0 }),
                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 1e-5);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 3e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 90);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #24
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testLeastSquares1() {
    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.0, -3.0 });
    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], 3e-5);
    Assert.assertEquals(-3, optimum.getPointRef()[1], 4e-4);
    Assert.assertTrue(optimizer.getEvaluations() > 60);
    Assert.assertTrue(optimizer.getEvaluations() < 80);
    Assert.assertTrue(optimum.getValue() < 1.0e-6);
}
 
Example #25
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 #26
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 #27
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize2() {
    SimplexOptimizer optimizer = new SimplexOptimizer(new SimpleValueChecker(1e-15, 1e-30));
    final FourExtrema fourExtrema = new FourExtrema();

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             new ObjectiveFunction(fourExtrema),
                             GoalType.MAXIMIZE,
                             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.yP, optimum.getPoint()[1], 3e-6);
    Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 2e-12);
    Assert.assertTrue(optimizer.getEvaluations() > 180);
    Assert.assertTrue(optimizer.getEvaluations() < 220);

    // Check that the number of iterations is updated (MATH-949).
    Assert.assertTrue(optimizer.getIterations() > 0);
}
 
Example #28
Source File: SimplexOptimizerMultiDirectionalTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize1() {
    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.MAXIMIZE,
                             new InitialGuess(new double[] { -3.0, 0.0 }),
                             new MultiDirectionalSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xM, optimum.getPoint()[0], 7e-7);
    Assert.assertEquals(fourExtrema.yM, optimum.getPoint()[1], 3e-7);
    Assert.assertEquals(fourExtrema.valueXmYm, optimum.getValue(), 2e-14);
    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 #29
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 #30
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);
}