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

The following examples show how to use org.apache.commons.math3.optim.MaxEval. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFittingBadInit() {
    CircleVectorial circle = new CircleVectorial();
    double[][] points = circlePoints;
    double[] target = new double[points.length];
    Arrays.fill(target, 0);
    double[] weights = new double[points.length];
    Arrays.fill(weights, 2);
    for (int i = 0; i < points.length; ++i) {
        circle.addPoint(points[i][0], points[i][1]);
    }
    AbstractLeastSquaresOptimizer optimizer = createOptimizer();
    PointVectorValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             circle.getModelFunction(),
                             circle.getModelFunctionJacobian(),
                             new Target(target),
                             new Weight(weights),
                             new InitialGuess(new double[] { -12, -12 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertTrue(optimizer.getEvaluations() < 25);
    Assert.assertEquals( 0.043, optimizer.getRMS(), 1e-3);
    Assert.assertEquals( 0.292235,  circle.getRadius(center), 1e-6);
    Assert.assertEquals(-0.151738,  center.getX(),            1e-6);
    Assert.assertEquals( 0.2075001, center.getY(),            1e-6);
}
 
Example #2
Source File: AbstractLeastSquaresOptimizerAbstractTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void 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 #3
Source File: AbstractLeastSquaresOptimizerTestValidation.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @return the normalized chi-square.
 */
private double getChi2N(AbstractLeastSquaresOptimizer optim,
                        StraightLineProblem problem,
                        double[] params) {
    final double[] t = problem.target();
    final double[] w = problem.weight();

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

    return optim.getChiSquare() / (t.length - params.length);
}
 
Example #4
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 #5
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 #6
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar problem = new CircleScalar();
    problem.addPoint( 30.0,  68.0);
    problem.addPoint( 50.0,  -6.0);
    problem.addPoint(110.0, -20.0);
    problem.addPoint( 35.0,  15.0);
    problem.addPoint( 45.0,  97.0);
    NonLinearConjugateGradientOptimizer optimizer
       = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                 new SimpleValueChecker(1e-30, 1e-30),
                                                 1e-15, 1e-13, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Vector2D center = new Vector2D(optimum.getPointRef()[0], optimum.getPointRef()[1]);
    Assert.assertEquals(69.960161753, problem.getRadius(center), 1.0e-8);
    Assert.assertEquals(96.075902096, center.getX(), 1.0e-7);
    Assert.assertEquals(48.135167894, center.getY(), 1.0e-6);
}
 
Example #7
Source File: AbstractLeastSquaresOptimizerAbstractTest.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, 2,  0, 0 },
            { 0, 1, -1, 1 },
            { 2, 0,  1, 0 }
    }, new double[] { 7, 3, 5 });

    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[] { 7, 6, 5, 4 }));
    Assert.assertEquals(0, optimizer.getRMS(), 1e-10);
}
 
Example #8
Source File: NonLinearConjugateGradientOptimizerTest.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.0,  1.0 },
            { 1.0, -1.0 },
            { 1.0,  3.0 }
    }, new double[] { 3.0, 1.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[] { 1, 1 }));
    Assert.assertEquals(2.0, optimum.getPoint()[0], 1.0e-8);
    Assert.assertEquals(1.0, optimum.getPoint()[1], 1.0e-8);

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

    StatisticalReferenceDataset.LeastSquaresProblem problem
        = dataset.getLeastSquaresProblem();

    optimizer.optimize(new MaxEval(1),
                       problem.getModelFunction(),
                       problem.getModelFunctionJacobian(),
                       new Target(y),
                       new Weight(w),
                       new InitialGuess(a));
    final double expected = dataset.getResidualSumOfSquares();
    final double actual = optimizer.getChiSquare();
    Assert.assertEquals(dataset.getName(), expected, actual,
                        1E-11 * expected);
}
 
Example #10
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 #11
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 #12
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath832() {
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double x) {
                final double sqrtX = FastMath.sqrt(x);
                final double a = 1e2 * sqrtX;
                final double b = 1e6 / x;
                final double c = 1e4 / sqrtX;

                return a + b + c;
            }
        };

    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-8);
    final double result = optimizer.optimize(new MaxEval(1483),
                                             new UnivariateObjectiveFunction(f),
                                             GoalType.MINIMIZE,
                                             new SearchInterval(Double.MIN_VALUE,
                                                                Double.MAX_VALUE)).getPoint();

    Assert.assertEquals(804.9355825, result, 1e-6);
}
 
Example #13
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 #14
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testInconsistentEquations() {
    LinearProblem problem = new LinearProblem(new double[][] {
            { 1.0,  1.0 },
            { 1.0, -1.0 },
            { 1.0,  3.0 }
    }, new double[] { 3.0, 1.0, 4.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[] { 1, 1 }));
    Assert.assertTrue(optimum.getValue() > 0.1);

}
 
Example #15
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 #16
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 #17
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 #18
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSinMin() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14);
    Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(new MaxEval(200),
                                                            new UnivariateObjectiveFunction(f),
                                                            GoalType.MINIMIZE,
                                                            new SearchInterval(4, 5)).getPoint(), 1e-8);
    Assert.assertTrue(optimizer.getEvaluations() <= 50);
    Assert.assertEquals(200, optimizer.getMaxEvaluations());
    Assert.assertEquals(3 * Math.PI / 2, optimizer.optimize(new MaxEval(200),
                                                            new UnivariateObjectiveFunction(f),
                                                            GoalType.MINIMIZE,
                                                            new SearchInterval(1, 5)).getPoint(), 1e-8);
    Assert.assertTrue(optimizer.getEvaluations() <= 100);
    Assert.assertTrue(optimizer.getEvaluations() >= 15);
    try {
        optimizer.optimize(new MaxEval(10),
                           new UnivariateObjectiveFunction(f),
                           GoalType.MINIMIZE,
                           new SearchInterval(4, 5));
        Assert.fail("an exception should have been thrown");
    } catch (TooManyEvaluationsException fee) {
        // expected
    }
}
 
Example #19
Source File: MultivariateFunctionPenaltyAdapterTest.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 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, 2.25 }));

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), optimum.getPoint()[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), optimum.getPoint()[1], 2e-7);
}
 
Example #20
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 #21
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 #22
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 #23
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 #24
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 #25
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMinEndpoints() {
    UnivariateFunction f = new Sin();
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-8, 1e-14);

    // endpoint is minimum
    double result = optimizer.optimize(new MaxEval(50),
                                       new UnivariateObjectiveFunction(f),
                                       GoalType.MINIMIZE,
                                       new SearchInterval(3 * Math.PI / 2, 5)).getPoint();
    Assert.assertEquals(3 * Math.PI / 2, result, 1e-6);

    result = optimizer.optimize(new MaxEval(50),
                                new UnivariateObjectiveFunction(f),
                                GoalType.MINIMIZE,
                                new SearchInterval(4, 3 * Math.PI / 2)).getPoint();
    Assert.assertEquals(3 * Math.PI / 2, result, 1e-6);
}
 
Example #26
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testUnbounded() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0,
                                                    Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY,
                                                    Double.NEGATIVE_INFINITY, Double.POSITIVE_INFINITY);
    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 #27
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 #28
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 #29
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 #30
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);
}