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

The following examples show how to use org.apache.commons.math3.optim.nonlinear.scalar.GoalType. 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: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath272() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 2, 1 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 1, 0 }, Relationship.GEQ,  1));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 1 }, Relationship.GEQ,  1));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0 }, Relationship.GEQ,  1));

    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MINIMIZE, new NonNegativeConstraint(true));

    Assert.assertEquals(0.0, solution.getPoint()[0], .0000001);
    Assert.assertEquals(1.0, solution.getPoint()[1], .0000001);
    Assert.assertEquals(1.0, solution.getPoint()[2], .0000001);
    Assert.assertEquals(3.0, solution.getValue(), .0000001);
}
 
Example #2
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 #3
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 #4
Source File: BOBYQAOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
    final double[] lowerBound = getLowerBound();
    final double[] upperBound = getUpperBound();

    // Validity checks.
    setup(lowerBound, upperBound);

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa(lowerBound, upperBound);

    return new PointValuePair(currentBest.getDataRef(),
                              isMinimize ? value : -value);
}
 
Example #5
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 #6
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 #7
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 #8
Source File: NonLinearConjugateGradientOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testNonInversible() {
    LinearProblem problem = new LinearProblem(new double[][] {
            {  1, 2, -3 },
            {  2, 1,  3 },
            { -3, 0, -9 }
    }, new double[] { 1, 1, 1 });
    NonLinearConjugateGradientOptimizer optimizer
        = new NonLinearConjugateGradientOptimizer(NonLinearConjugateGradientOptimizer.Formula.POLAK_RIBIERE,
                                                  new SimpleValueChecker(1e-6, 1e-6),
                                                  1e-3, 1e-3, 1);
    PointValuePair optimum
        = optimizer.optimize(new MaxEval(100),
                             problem.getObjectiveFunction(),
                             problem.getObjectiveFunctionGradient(),
                             GoalType.MINIMIZE,
                             new InitialGuess(new double[] { 0, 0, 0 }));
    Assert.assertTrue(optimum.getValue() > 0.5);
}
 
Example #9
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),
                                                  1e-3, 1e-3, 1);
    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 #10
Source File: BOBYQAOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/** {@inheritDoc} */
@Override
protected PointValuePair doOptimize() {
    final double[] lowerBound = getLowerBound();
    final double[] upperBound = getUpperBound();

    // Validity checks.
    setup(lowerBound, upperBound);

    isMinimize = (getGoalType() == GoalType.MINIMIZE);
    currentBest = new ArrayRealVector(getStartPoint());

    final double value = bobyqa(lowerBound, upperBound);

    return new PointValuePair(currentBest.getDataRef(),
                              isMinimize ? value : -value);
}
 
Example #11
Source File: BrentOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testQuinticMin() {
    // The function has local minima at -0.27195613 and 0.82221643.
    UnivariateFunction f = new QuinticFunction();
    UnivariateOptimizer optimizer = new BrentOptimizer(1e-10, 1e-14);
    Assert.assertEquals(-0.27195613, optimizer.optimize(new MaxEval(200),
                                                        new UnivariateObjectiveFunction(f),
                                                        GoalType.MINIMIZE,
                                                        new SearchInterval(-0.3, -0.2)).getPoint(), 1.0e-8);
    Assert.assertEquals( 0.82221643, optimizer.optimize(new MaxEval(200),
                                                        new UnivariateObjectiveFunction(f),
                                                        GoalType.MINIMIZE,
                                                        new SearchInterval(0.3,  0.9)).getPoint(), 1.0e-8);
    Assert.assertTrue(optimizer.getEvaluations() <= 50);

    // search in a large interval
    Assert.assertEquals(-0.27195613, optimizer.optimize(new MaxEval(200),
                                                        new UnivariateObjectiveFunction(f),
                                                        GoalType.MINIMIZE,
                                                        new SearchInterval(-1.0, 0.2)).getPoint(), 1.0e-8);
    Assert.assertTrue(optimizer.getEvaluations() <= 50);
}
 
Example #12
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 #13
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath781() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 2, 6, 7 }, 0);

    ArrayList<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 2, 1 }, Relationship.LEQ, 2));
    constraints.add(new LinearConstraint(new double[] { -1, 1, 1 }, Relationship.LEQ, -1));
    constraints.add(new LinearConstraint(new double[] { 2, -3, 1 }, Relationship.LEQ, -1));

    double epsilon = 1e-6;
    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MAXIMIZE, new NonNegativeConstraint(false));

    Assert.assertTrue(Precision.compareTo(solution.getPoint()[0], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[1], 0.0d, epsilon) > 0);
    Assert.assertTrue(Precision.compareTo(solution.getPoint()[2], 0.0d, epsilon) < 0);
    Assert.assertEquals(2.0d, solution.getValue(), epsilon);
}
 
Example #14
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 #15
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 #16
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
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: 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 #19
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 #20
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 #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: 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 #23
Source File: BracketFinderTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testCubicMax() {
    final BracketFinder bFind = new BracketFinder();
    final UnivariateFunction func = new UnivariateFunction() {
            public double value(double x) {
                if (x < -2) {
                    return value(-2);
                }
                else  {
                    return -(x - 1) * (x + 2) * (x + 3);
                }
            }
        };

    bFind.search(func, GoalType.MAXIMIZE, -2 , -1);
    final double tol = 1e-15;
    Assert.assertEquals(-2, bFind.getLo(), tol);
    Assert.assertEquals(-1, bFind.getMid(), tol);
    Assert.assertEquals(0.61803399999999997, bFind.getHi(), tol);
}
 
Example #24
Source File: UnivariateOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Scans the list of (required and optional) optimization data that
 * characterize the problem.
 *
 * @param optData Optimization data.
 * The following data will be looked for:
 * <ul>
 *  <li>{@link GoalType}</li>
 *  <li>{@link SearchInterval}</li>
 *  <li>{@link UnivariateObjectiveFunction}</li>
 * </ul>
 */
@Override
protected void parseOptimizationData(OptimizationData... optData) {
    // Allow base class to register its own data.
    super.parseOptimizationData(optData);

    // The existing values (as set by the previous call) are reused if
    // not provided in the argument list.
    for (OptimizationData data : optData) {
        if (data instanceof SearchInterval) {
            final SearchInterval interval = (SearchInterval) data;
            min = interval.getMin();
            max = interval.getMax();
            start = interval.getStartValue();
            continue;
        }
        if (data instanceof UnivariateObjectiveFunction) {
            function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
            continue;
        }
        if (data instanceof GoalType) {
            goal = (GoalType) data;
            continue;
        }
    }
}
 
Example #25
Source File: SimplexOptimizerNelderMeadTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMaximize2() {
    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[] { 1, 0 }),
                             new NelderMeadSimplex(new double[] { 0.2, 0.2 }));
    Assert.assertEquals(fourExtrema.xP, optimum.getPoint()[0], 4e-6);
    Assert.assertEquals(fourExtrema.yP, optimum.getPoint()[1], 5e-6);
    Assert.assertEquals(fourExtrema.valueXpYp, optimum.getValue(), 7e-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 #26
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 #27
Source File: MultiStartUnivariateOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Sort the optima from best to worst, followed by {@code null} elements.
 *
 * @param goal Goal type.
 */
private void sortPairs(final GoalType goal) {
    Arrays.sort(optima, new Comparator<UnivariatePointValuePair>() {
            public int compare(final UnivariatePointValuePair o1,
                               final UnivariatePointValuePair o2) {
                if (o1 == null) {
                    return (o2 == null) ? 0 : 1;
                } else if (o2 == null) {
                    return -1;
                }
                final double v1 = o1.getValue();
                final double v2 = o2.getValue();
                return (goal == GoalType.MINIMIZE) ?
                    Double.compare(v1, v2) : Double.compare(v2, v1);
            }
        });
}
 
Example #28
Source File: MultiStartUnivariateOptimizerTest.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 underlying = new BrentOptimizer(1e-10, 1e-14);
    JDKRandomGenerator g = new JDKRandomGenerator();
    g.setSeed(44428400075l);
    MultiStartUnivariateOptimizer optimizer = new MultiStartUnivariateOptimizer(underlying, 10, g);
    optimizer.optimize(new MaxEval(300),
                       new UnivariateObjectiveFunction(f),
                       GoalType.MINIMIZE,
                       new SearchInterval(-100.0, 100.0));
    UnivariatePointValuePair[] optima = optimizer.getOptima();
    for (int i = 1; i < optima.length; ++i) {
        double d = (optima[i].getPoint() - optima[i-1].getPoint()) / (2 * FastMath.PI);
        Assert.assertTrue(FastMath.abs(d - FastMath.rint(d)) < 1.0e-8);
        Assert.assertEquals(-1.0, f.value(optima[i].getPoint()), 1.0e-10);
        Assert.assertEquals(f.value(optima[i].getPoint()), optima[i].getValue(), 1.0e-10);
    }
    Assert.assertTrue(optimizer.getEvaluations() > 200);
    Assert.assertTrue(optimizer.getEvaluations() < 300);
}
 
Example #29
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testRestrictVariablesToNonNegative() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 409, 523, 70, 204, 339 }, 0);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] {    43,   56, 345,  56,    5 }, Relationship.LEQ,  4567456));
    constraints.add(new LinearConstraint(new double[] {    12,   45,   7,  56,   23 }, Relationship.LEQ,    56454));
    constraints.add(new LinearConstraint(new double[] {     8,  768,   0,  34, 7456 }, Relationship.LEQ,  1923421));
    constraints.add(new LinearConstraint(new double[] { 12342, 2342,  34, 678, 2342 }, Relationship.GEQ,     4356));
    constraints.add(new LinearConstraint(new double[] {    45,  678,  76,  52,   23 }, Relationship.EQ,    456356));

    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MAXIMIZE, new NonNegativeConstraint(true));
    Assert.assertEquals(2902.92783505155, solution.getPoint()[0], .0000001);
    Assert.assertEquals(480.419243986254, solution.getPoint()[1], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[2], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[3], .0000001);
    Assert.assertEquals(0.0, solution.getPoint()[4], .0000001);
    Assert.assertEquals(1438556.7491409, solution.getValue(), .0000001);
}
 
Example #30
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath828Cycle() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(
            new double[] { 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0}, 0.0);
    
    ArrayList <LinearConstraint>constraints = new ArrayList<LinearConstraint>();

    constraints.add(new LinearConstraint(new double[] {0.0, 16.0, 14.0, 69.0, 1.0, 85.0, 52.0, 43.0, 64.0, 97.0, 14.0, 74.0, 89.0, 28.0, 94.0, 58.0, 13.0, 22.0, 21.0, 17.0, 30.0, 25.0, 1.0, 59.0, 91.0, 78.0, 12.0, 74.0, 56.0, 3.0, 88.0,}, Relationship.GEQ, 91.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 60.0, 40.0, 81.0, 71.0, 72.0, 46.0, 45.0, 38.0, 48.0, 40.0, 17.0, 33.0, 85.0, 64.0, 32.0, 84.0, 3.0, 54.0, 44.0, 71.0, 67.0, 90.0, 95.0, 54.0, 99.0, 99.0, 29.0, 52.0, 98.0, 9.0,}, Relationship.GEQ, 54.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 41.0, 12.0, 86.0, 90.0, 61.0, 31.0, 41.0, 23.0, 89.0, 17.0, 74.0, 44.0, 27.0, 16.0, 47.0, 80.0, 32.0, 11.0, 56.0, 68.0, 82.0, 11.0, 62.0, 62.0, 53.0, 39.0, 16.0, 48.0, 1.0, 63.0,}, Relationship.GEQ, 62.0));
    constraints.add(new LinearConstraint(new double[] {83.0, -76.0, -94.0, -19.0, -15.0, -70.0, -72.0, -57.0, -63.0, -65.0, -22.0, -94.0, -22.0, -88.0, -86.0, -89.0, -72.0, -16.0, -80.0, -49.0, -70.0, -93.0, -95.0, -17.0, -83.0, -97.0, -31.0, -47.0, -31.0, -13.0, -23.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {41.0, -96.0, -41.0, -48.0, -70.0, -43.0, -43.0, -43.0, -97.0, -37.0, -85.0, -70.0, -45.0, -67.0, -87.0, -69.0, -94.0, -54.0, -54.0, -92.0, -79.0, -10.0, -35.0, -20.0, -41.0, -41.0, -65.0, -25.0, -12.0, -8.0, -46.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {27.0, -42.0, -65.0, -49.0, -53.0, -42.0, -17.0, -2.0, -61.0, -31.0, -76.0, -47.0, -8.0, -93.0, -86.0, -62.0, -65.0, -63.0, -22.0, -43.0, -27.0, -23.0, -32.0, -74.0, -27.0, -63.0, -47.0, -78.0, -29.0, -95.0, -73.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {15.0, -46.0, -41.0, -83.0, -98.0, -99.0, -21.0, -35.0, -7.0, -14.0, -80.0, -63.0, -18.0, -42.0, -5.0, -34.0, -56.0, -70.0, -16.0, -18.0, -74.0, -61.0, -47.0, -41.0, -15.0, -79.0, -18.0, -47.0, -88.0, -68.0, -55.0,}, Relationship.GEQ, 0.0));
    
    double epsilon = 1e-6;
    PointValuePair solution = new SimplexSolver().optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                                           GoalType.MINIMIZE, new NonNegativeConstraint(true));
    Assert.assertEquals(1.0d, solution.getValue(), epsilon);
    Assert.assertTrue(validSolution(solution, constraints, epsilon));        
}