Example usage for org.apache.commons.math.optimization.univariate BrentOptimizer BrentOptimizer

List of usage examples for org.apache.commons.math.optimization.univariate BrentOptimizer BrentOptimizer

Introduction

In this page you can find the example usage for org.apache.commons.math.optimization.univariate BrentOptimizer BrentOptimizer.

Prototype

public BrentOptimizer() 

Source Link

Document

Construct a solver.

Usage

From source file:OughtaFocus.java

private double runAutofocusAlgorithm() throws Exception {
    UnivariateRealFunction scoreFun = new UnivariateRealFunction() {
        public double value(double d) throws FunctionEvaluationException {
            try {
                return measureFocusScore(d);
            } catch (Exception e) {
                throw new FunctionEvaluationException(e, d);
            }//from ww  w .  ja v  a 2  s. c  o m
        }
    };
    BrentOptimizer brentOptimizer = new BrentOptimizer();
    brentOptimizer.setAbsoluteAccuracy(tolerance);
    imageCount_ = 0;

    CMMCore core = app_.getMMCore();
    double z = core.getPosition(core.getFocusDevice());
    startZUm_ = z;
    //      getCurrentFocusScore();
    double zResult = brentOptimizer.optimize(scoreFun, GoalType.MAXIMIZE, z - searchRange / 2,
            z + searchRange / 2);
    ReportingUtils.logMessage("OughtaFocus Iterations: " + brentOptimizer.getIterationCount() + ", z="
            + TextUtils.FMT2.format(zResult) + ", dz=" + TextUtils.FMT2.format(zResult - startZUm_) + ", t="
            + (System.currentTimeMillis() - startTimeMs_));
    return zResult;
}

From source file:ch.algotrader.simulation.SimulationExecutorImpl.java

/**
 * {@inheritDoc}//from www.  j  ava  2 s . co  m
 */
@Override
public OptimizationResultVO optimizeSingleParam(final StrategyGroup strategyGroup, final String parameter,
        final double min, final double max, final double accuracy) {

    Validate.notEmpty(parameter, "Parameter is empty");

    try {
        UnivariateRealFunction function = new UnivariateFunction(this, strategyGroup, parameter);
        UnivariateRealOptimizer optimizer = new BrentOptimizer();
        optimizer.setAbsoluteAccuracy(accuracy);
        optimizer.optimize(function, GoalType.MAXIMIZE, min, max);
        OptimizationResultVO optimizationResult = new OptimizationResultVO();
        optimizationResult.setParameter(parameter);
        optimizationResult.setResult(optimizer.getResult());
        optimizationResult.setFunctionValue(optimizer.getFunctionValue());
        optimizationResult.setIterations(optimizer.getIterationCount());

        return optimizationResult;
    } catch (MathException ex) {
        throw new SimulationExecutorException(ex);
    }
}

From source file:org.renjin.primitives.optimize.Optimizations.java

/**
 * Searches the interval from lower to upper for a minimum or maximum of the
 * function f with respect to its first argument.
 *
 * <p>This implementation uses the BrentOptimizer from Apache Commons Math, which
 * is the same reference used by the original R:
 *
 * <p>/*from w  w  w.j av a  2s  .com*/
 * Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs N.J.: Prentice-Hall.
 */
public static double fmin(@Current Context context, @Current Environment rho, Function fn, double lower,
        double upper, double tol) {

    BrentOptimizer optimizer = new BrentOptimizer();
    optimizer.setAbsoluteAccuracy(tol);
    try {
        return optimizer.optimize(new UnivariateRealClosure(context, rho, fn), GoalType.MINIMIZE, lower, upper);
    } catch (MaxIterationsExceededException e) {
        throw new EvalException("maximum iterations reached", e);
    } catch (FunctionEvaluationException e) {
        throw new EvalException(e);
    }
}

From source file:org.renjin.stats.internals.optimize.Optimizations.java

/**
 * Searches the interval from lower to upper for a minimum or maximum of the
 * function f with respect to its first argument.
 *
 * <p>This implementation uses the BrentOptimizer from Apache Commons Math, which
 * is the same reference used by the original R:
 *
 * <p>/*from   w  w w . ja v  a  2  s  .c  o m*/
 * Brent, R. (1973) Algorithms for Minimization without Derivatives. Englewood Cliffs N.J.: Prentice-Hall.
 */
@Internal
public static double fmin(@Current Context context, @Current Environment rho, Function fn, double lower,
        double upper, double tol) {

    BrentOptimizer optimizer = new BrentOptimizer();
    optimizer.setAbsoluteAccuracy(tol);
    try {
        return optimizer.optimize(new UnivariateRealClosure(context, rho, fn), GoalType.MINIMIZE, lower, upper);
    } catch (MaxIterationsExceededException e) {
        throw new EvalException("maximum iterations reached", e);
    } catch (FunctionEvaluationException e) {
        throw new EvalException(e);
    }
}