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

The following examples show how to use org.apache.commons.math3.optim.PointValuePair. 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: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testHalfBounded() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 4.0,
                                                    1.0, Double.POSITIVE_INFINITY,
                                                    Double.NEGATIVE_INFINITY, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped
          = new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                   biQuadratic.getLower(),
                                                   biQuadratic.getUpper(),
                                                   1000.0, new double[] { 100.0, 100.0 });

    SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-10, 1.0e-20));
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(400),
                             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 #2
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath286() {
    LinearObjectiveFunction f = new LinearObjectiveFunction(new double[] { 0.8, 0.2, 0.7, 0.3, 0.6, 0.4 }, 0 );
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 0, 1, 0, 1, 0 }, Relationship.EQ, 23.0));
    constraints.add(new LinearConstraint(new double[] { 0, 1, 0, 1, 0, 1 }, Relationship.EQ, 23.0));
    constraints.add(new LinearConstraint(new double[] { 1, 0, 0, 0, 0, 0 }, Relationship.GEQ, 10.0));
    constraints.add(new LinearConstraint(new double[] { 0, 0, 1, 0, 0, 0 }, Relationship.GEQ, 8.0));
    constraints.add(new LinearConstraint(new double[] { 0, 0, 0, 0, 1, 0 }, Relationship.GEQ, 5.0));

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

    Assert.assertEquals(25.8, solution.getValue(), .0000001);
    Assert.assertEquals(23.0, solution.getPoint()[0] + solution.getPoint()[2] + solution.getPoint()[4], 0.0000001);
    Assert.assertEquals(23.0, solution.getPoint()[1] + solution.getPoint()[3] + solution.getPoint()[5], 0.0000001);
    Assert.assertTrue(solution.getPoint()[0] >= 10.0 - 0.0000001);
    Assert.assertTrue(solution.getPoint()[2] >= 8.0 - 0.0000001);
    Assert.assertTrue(solution.getPoint()[4] >= 5.0 - 0.0000001);
}
 
Example #3
Source File: Math_6_CMAESOptimizer_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * @param maxIterations Maximal number of iterations.
 * @param stopFitness Whether to stop if objective function value is smaller than
 * {@code stopFitness}.
 * @param isActiveCMA Chooses the covariance matrix update method.
 * @param diagonalOnly Number of initial iterations, where the covariance matrix
 * remains diagonal.
 * @param checkFeasableCount Determines how often new random objective variables are
 * generated in case they are out of bounds.
 * @param random Random generator.
 * @param generateStatistics Whether statistic data is collected.
 * @param checker Convergence checker.
 *
 * @since 3.1
 */
public CMAESOptimizer(int maxIterations,
                      double stopFitness,
                      boolean isActiveCMA,
                      int diagonalOnly,
                      int checkFeasableCount,
                      RandomGenerator random,
                      boolean generateStatistics,
                      ConvergenceChecker<PointValuePair> checker) {
    super(checker);
    this.maxIterations = maxIterations;
    this.stopFitness = stopFitness;
    this.isActiveCMA = isActiveCMA;
    this.diagonalOnly = diagonalOnly;
    this.checkFeasableCount = checkFeasableCount;
    this.random = random;
    this.generateStatistics = generateStatistics;
}
 
Example #4
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 #5
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped
        =  new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                  biQuadratic.getLower(),
                                                  biQuadratic.getUpper(),
                                                  1000.0, new double[] { 100.0, 100.0 });

    SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(600),
                             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 #6
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 #7
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 #8
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 #9
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 #10
Source File: MultivariateFunctionMappingAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStartSimplexInsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionMappingAdapter wrapped
        = new MultivariateFunctionMappingAdapter(biQuadratic,
                                                 biQuadratic.getLower(),
                                                 biQuadratic.getUpper());

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

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

    Assert.assertEquals(biQuadratic.getBoundedXOptimum(), bounded[0], 2e-7);
    Assert.assertEquals(biQuadratic.getBoundedYOptimum(), bounded[1], 2e-7);
}
 
Example #11
Source File: MultiDirectionalSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Compute and evaluate a new simplex.
 *
 * @param evaluationFunction Evaluation function.
 * @param original Original simplex (to be preserved).
 * @param coeff Linear coefficient.
 * @param comparator Comparator to use to sort simplex vertices from best
 * to poorest.
 * @return the best point in the transformed simplex.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the maximal number of evaluations is exceeded.
 */
private PointValuePair evaluateNewSimplex(final MultivariateFunction evaluationFunction,
                                              final PointValuePair[] original,
                                              final double coeff,
                                              final Comparator<PointValuePair> comparator) {
    final double[] xSmallest = original[0].getPointRef();
    // Perform a linear transformation on all the simplex points,
    // except the first one.
    setPoint(0, original[0]);
    final int dim = getDimension();
    for (int i = 1; i < getSize(); i++) {
        final double[] xOriginal = original[i].getPointRef();
        final double[] xTransformed = new double[dim];
        for (int j = 0; j < dim; j++) {
            xTransformed[j] = xSmallest[j] + coeff * (xSmallest[j] - xOriginal[j]);
        }
        setPoint(i, new PointValuePair(xTransformed, Double.NaN, false));
    }

    // Evaluate the simplex.
    evaluate(evaluationFunction, comparator);

    return getPoint(0);
}
 
Example #12
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));        
}
 
Example #13
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 #14
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build an initial simplex.
 *
 * @param startPoint First point of the simplex.
 * @throws DimensionMismatchException if the start point does not match
 * simplex dimension.
 */
public void build(final double[] startPoint) {
    if (dimension != startPoint.length) {
        throw new DimensionMismatchException(dimension, startPoint.length);
    }

    // Set first vertex.
    simplex = new PointValuePair[dimension + 1];
    simplex[0] = new PointValuePair(startPoint, Double.NaN);

    // Set remaining vertices.
    for (int i = 0; i < dimension; i++) {
        final double[] confI = startConfiguration[i];
        final double[] vertexI = new double[dimension];
        for (int k = 0; k < dimension; k++) {
            vertexI[k] = startPoint[k] + confI[k];
        }
        simplex[i + 1] = new PointValuePair(vertexI, Double.NaN);
    }
}
 
Example #15
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testOptimumOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(4.0, 0.0, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped
        =  new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                  biQuadratic.getLower(),
                                                  biQuadratic.getUpper(),
                                                  1000.0, new double[] { 100.0, 100.0 });

    SimplexOptimizer optimizer = new SimplexOptimizer(new SimplePointChecker<PointValuePair>(1.0e-11, 1.0e-20));
    final AbstractSimplex simplex = new NelderMeadSimplex(new double[] { 1.0, 0.5 });

    final PointValuePair optimum
        = optimizer.optimize(new MaxEval(600),
                             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 #16
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * This constructor allows to specify a user-defined convergence checker,
 * in addition to the parameters that control the default convergence
 * checking procedure and the line search tolerances.
 *
 * @param rel Relative threshold for this optimizer.
 * @param abs Absolute threshold for this optimizer.
 * @param lineRel Relative threshold for the internal line search optimizer.
 * @param lineAbs Absolute threshold for the internal line search optimizer.
 * @param checker Convergence checker.
 * @throws NotStrictlyPositiveException if {@code abs <= 0}.
 * @throws NumberIsTooSmallException if {@code rel < 2 * Math.ulp(1d)}.
 */
public PowellOptimizer(double rel,
                       double abs,
                       double lineRel,
                       double lineAbs,
                       ConvergenceChecker<PointValuePair> checker) {
    super(checker);

    if (rel < MIN_RELATIVE_TOLERANCE) {
        throw new NumberIsTooSmallException(rel, MIN_RELATIVE_TOLERANCE, true);
    }
    if (abs <= 0) {
        throw new NotStrictlyPositiveException(abs);
    }
    relativeThreshold = rel;
    absoluteThreshold = abs;

    // Create the line search optimizer.
    line = new LineSearch(lineRel,
                          lineAbs);
}
 
Example #17
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 #18
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 #19
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 #20
Source File: MultivariateFunctionPenaltyAdapterTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testStartSimplexOutsideRange() {
    final BiQuadratic biQuadratic = new BiQuadratic(2.0, 2.5, 1.0, 3.0, 2.0, 3.0);
    final MultivariateFunctionPenaltyAdapter wrapped
          = new MultivariateFunctionPenaltyAdapter(biQuadratic,
                                                   biQuadratic.getLower(),
                                                   biQuadratic.getUpper(),
                                                   1000.0, new double[] { 100.0, 100.0 });

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

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

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

    final double[] expectedPosition = function.getMaximumPosition();
    final double[] actualPosition = estimate.getPoint();
    Assert.assertEquals(expectedPosition[0], actualPosition[0], EPSILON );
    Assert.assertEquals(expectedPosition[1], actualPosition[1], EPSILON );
}
 
Example #22
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 #23
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testMath828() {
    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);
    
    ArrayList <LinearConstraint>constraints = new ArrayList<LinearConstraint>();

    constraints.add(new LinearConstraint(new double[] {0.0, 39.0, 23.0, 96.0, 15.0, 48.0, 9.0, 21.0, 48.0, 36.0, 76.0, 19.0, 88.0, 17.0, 16.0, 36.0,}, Relationship.GEQ, 15.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 59.0, 93.0, 12.0, 29.0, 78.0, 73.0, 87.0, 32.0, 70.0, 68.0, 24.0, 11.0, 26.0, 65.0, 25.0,}, Relationship.GEQ, 29.0));
    constraints.add(new LinearConstraint(new double[] {0.0, 74.0, 5.0, 82.0, 6.0, 97.0, 55.0, 44.0, 52.0, 54.0, 5.0, 93.0, 91.0, 8.0, 20.0, 97.0,}, Relationship.GEQ, 6.0));
    constraints.add(new LinearConstraint(new double[] {8.0, -3.0, -28.0, -72.0, -8.0, -31.0, -31.0, -74.0, -47.0, -59.0, -24.0, -57.0, -56.0, -16.0, -92.0, -59.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {25.0, -7.0, -99.0, -78.0, -25.0, -14.0, -16.0, -89.0, -39.0, -56.0, -53.0, -9.0, -18.0, -26.0, -11.0, -61.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {33.0, -95.0, -15.0, -4.0, -33.0, -3.0, -20.0, -96.0, -27.0, -13.0, -80.0, -24.0, -3.0, -13.0, -57.0, -76.0,}, Relationship.GEQ, 0.0));
    constraints.add(new LinearConstraint(new double[] {7.0, -95.0, -39.0, -93.0, -7.0, -94.0, -94.0, -62.0, -76.0, -26.0, -53.0, -57.0, -31.0, -76.0, -53.0, -52.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));
}
 
Example #24
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 #25
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 #26
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 #27
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 #28
Source File: AbstractSimplex.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Build an initial simplex.
 *
 * @param startPoint First point of the simplex.
 * @throws DimensionMismatchException if the start point does not match
 * simplex dimension.
 */
public void build(final double[] startPoint) {
    if (dimension != startPoint.length) {
        throw new DimensionMismatchException(dimension, startPoint.length);
    }

    // Set first vertex.
    simplex = new PointValuePair[dimension + 1];
    simplex[0] = new PointValuePair(startPoint, Double.NaN);

    // Set remaining vertices.
    for (int i = 0; i < dimension; i++) {
        final double[] confI = startConfiguration[i];
        final double[] vertexI = new double[dimension];
        for (int k = 0; k < dimension; k++) {
            vertexI[k] = startPoint[k] + confI[k];
        }
        simplex[i + 1] = new PointValuePair(vertexI, Double.NaN);
    }
}
 
Example #29
Source File: SimplexSolverTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
@Test
public void testSimplexSolver() {
    LinearObjectiveFunction f =
        new LinearObjectiveFunction(new double[] { 15, 10 }, 7);
    Collection<LinearConstraint> constraints = new ArrayList<LinearConstraint>();
    constraints.add(new LinearConstraint(new double[] { 1, 0 }, Relationship.LEQ, 2));
    constraints.add(new LinearConstraint(new double[] { 0, 1 }, Relationship.LEQ, 3));
    constraints.add(new LinearConstraint(new double[] { 1, 1 }, Relationship.EQ, 4));

    SimplexSolver solver = new SimplexSolver();
    PointValuePair solution = solver.optimize(DEFAULT_MAX_ITER, f, new LinearConstraintSet(constraints),
                                              GoalType.MAXIMIZE, new NonNegativeConstraint(true));
    Assert.assertEquals(2.0, solution.getPoint()[0], 0.0);
    Assert.assertEquals(2.0, solution.getPoint()[1], 0.0);
    Assert.assertEquals(57.0, solution.getValue(), 0.0);
}
 
Example #30
Source File: BOBYQAOptimizerTest.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * @param func Function to optimize.
 * @param startPoint Starting point.
 * @param boundaries Upper / lower point limit.
 * @param goal Minimization or maximization.
 * @param fTol Tolerance relative error on the objective function.
 * @param pointTol Tolerance for checking that the optimum is correct.
 * @param maxEvaluations Maximum number of evaluations.
 * @param expected Expected point / value.
 */
private void doTest(MultivariateFunction func,
                    double[] startPoint,
                    double[][] boundaries,
                    GoalType goal,
                    double fTol,
                    double pointTol,
                    int maxEvaluations,
                    PointValuePair expected) {
    doTest(func,
           startPoint,
           boundaries,
           goal,
           fTol,
           pointTol,
           maxEvaluations,
           0,
           expected,
           "");
}