Java Code Examples for org.apache.commons.math3.optim.PointValuePair#getPointRef()

The following examples show how to use org.apache.commons.math3.optim.PointValuePair#getPointRef() . 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 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 2
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),
                                                 new BrentSolver(1e-15, 1e-13));
    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-8);
    Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
 
Example 3
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),
                                                 new BrentSolver(1e-15, 1e-13));
    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-8);
    Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
 
Example 4
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),
                                                 new BrentSolver(1e-15, 1e-13));
    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-8);
    Assert.assertEquals(48.135167894, center.getY(), 1.0e-8);
}
 
Example 5
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 6
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}
 
Example 7
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}
 
Example 8
Source File: MultiStartMultivariateOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    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);
    // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for
    // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer
    // will officially be declared as implementing MultivariateDifferentiableOptimizer
    GradientMultivariateOptimizer underlying
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-10, 1e-10));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(753289573253l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                new double[] { 10, 10 },
                                                new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer
        = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             circle.getObjectiveFunction(),
                             circle.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertEquals(200, optimizer.getMaxEvaluations());
    PointValuePair[] optima = optimizer.getOptima();
    for (PointValuePair o : optima) {
        Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
        Assert.assertEquals(69.960161753, circle.getRadius(center), 1e-8);
        Assert.assertEquals(96.075902096, center.getX(), 1e-8);
        Assert.assertEquals(48.135167894, center.getY(), 1e-8);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 70);
    Assert.assertTrue(optimizer.getEvaluations() < 90);
    Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}
 
Example 9
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}
 
Example 10
Source File: MultiStartMultivariateOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    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);
    // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for
    // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer
    // will officially be declared as implementing MultivariateDifferentiableOptimizer
    GradientMultivariateOptimizer underlying
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-10, 1e-10));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(753289573253l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                new double[] { 10, 10 },
                                                new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer
        = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             circle.getObjectiveFunction(),
                             circle.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertEquals(200, optimizer.getMaxEvaluations());
    PointValuePair[] optima = optimizer.getOptima();
    for (PointValuePair o : optima) {
        Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
        Assert.assertEquals(69.960161753, circle.getRadius(center), 1e-8);
        Assert.assertEquals(96.075902096, center.getX(), 1e-8);
        Assert.assertEquals(48.135167894, center.getY(), 1e-8);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 70);
    Assert.assertTrue(optimizer.getEvaluations() < 90);
    Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}
 
Example 11
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}
 
Example 12
Source File: MultiStartMultivariateOptimizerTest.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    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);
    // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for
    // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer
    // will officially be declared as implementing MultivariateDifferentiableOptimizer
    GradientMultivariateOptimizer underlying
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-10, 1e-10));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(753289573253l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                new double[] { 10, 10 },
                                                new GaussianRandomGenerator(g));
    MultiStartMultivariateOptimizer optimizer
        = new MultiStartMultivariateOptimizer(underlying, 10, generator);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(200),
                             circle.getObjectiveFunction(),
                             circle.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertEquals(200, optimizer.getMaxEvaluations());
    PointValuePair[] optima = optimizer.getOptima();
    for (PointValuePair o : optima) {
        Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
        Assert.assertEquals(69.960161753, circle.getRadius(center), 1e-8);
        Assert.assertEquals(96.075902096, center.getX(), 1e-8);
        Assert.assertEquals(48.135167894, center.getY(), 1e-8);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 70);
    Assert.assertTrue(optimizer.getEvaluations() < 90);
    Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}
 
Example 13
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Evaluate all the non-evaluated points of the simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param comparator Comparator to use to sort simplex vertices from best to worst.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
public void evaluate(final MultivariateFunction evaluationFunction,
                     final Comparator<PointValuePair> comparator) {
    // Evaluate the objective function at all non-evaluated simplex points.
    for (int i = 0; i < simplex.length; i++) {
        final PointValuePair vertex = simplex[i];
        final double[] point = vertex.getPointRef();
        if (Double.isNaN(vertex.getValue())) {
            simplex[i] = new PointValuePair(point, evaluationFunction.value(point), false);
        }
    }

    // Sort the simplex from best to worst.
    Arrays.sort(simplex, comparator);
}
 
Example 14
Source File: MultiStartMultivariateOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    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);
    // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for
    // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer
    // will officially be declared as implementing MultivariateDifferentiableOptimizer
    GradientMultivariateOptimizer underlying
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-10, 1e-10));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(753289573253l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                new double[] { 10, 10 },
                                                new GaussianRandomGenerator(g));
    int nbStarts = 10;
    MultiStartMultivariateOptimizer optimizer
        = new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(1000),
                             circle.getObjectiveFunction(),
                             circle.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertEquals(1000, optimizer.getMaxEvaluations());
    PointValuePair[] optima = optimizer.getOptima();
    Assert.assertEquals(nbStarts, optima.length);
    for (PointValuePair o : optima) {
        // we check the results of all intermediate restarts here (there are 10 such results)
        Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
        Assert.assertTrue(69.9592 < circle.getRadius(center));
        Assert.assertTrue(69.9602 > circle.getRadius(center));
        Assert.assertTrue(96.0745 < center.getX());
        Assert.assertTrue(96.0762 > center.getX());
        Assert.assertTrue(48.1344 < center.getY());
        Assert.assertTrue(48.1354 > center.getY());
    }

    Assert.assertTrue(optimizer.getEvaluations() > 850);
    Assert.assertTrue(optimizer.getEvaluations() < 900);

    Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}
 
Example 15
Source File: MultiStartMultivariateOptimizerTest.java    From astor with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testCircleFitting() {
    CircleScalar circle = new CircleScalar();
    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);
    // TODO: the wrapper around NonLinearConjugateGradientOptimizer is a temporary hack for
    // version 3.1 of the library. It should be removed when NonLinearConjugateGradientOptimizer
    // will officially be declared as implementing MultivariateDifferentiableOptimizer
    GradientMultivariateOptimizer underlying
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-10, 1e-10));
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(753289573253l);
    RandomVectorGenerator generator
        = new UncorrelatedRandomVectorGenerator(new double[] { 50, 50 },
                                                new double[] { 10, 10 },
                                                new GaussianRandomGenerator(g));
    int nbStarts = 10;
    MultiStartMultivariateOptimizer optimizer
        = new MultiStartMultivariateOptimizer(underlying, nbStarts, generator);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(1000),
                             circle.getObjectiveFunction(),
                             circle.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 98.680, 47.345 }));
    Assert.assertEquals(1000, optimizer.getMaxEvaluations());
    PointValuePair[] optima = optimizer.getOptima();
    Assert.assertEquals(nbStarts, optima.length);
    for (PointValuePair o : optima) {
        // we check the results of all intermediate restarts here (there are 10 such results)
        Vector2D center = new Vector2D(o.getPointRef()[0], o.getPointRef()[1]);
        Assert.assertTrue(69.9592 < circle.getRadius(center));
        Assert.assertTrue(69.9602 > circle.getRadius(center));
        Assert.assertTrue(96.0745 < center.getX());
        Assert.assertTrue(96.0762 > center.getX());
        Assert.assertTrue(48.1344 < center.getY());
        Assert.assertTrue(48.1354 > center.getY());
    }

    Assert.assertTrue(optimizer.getEvaluations() > 850);
    Assert.assertTrue(optimizer.getEvaluations() < 900);

    Assert.assertEquals(3.1267527, optimum.getValue(), 1e-8);
}