List of usage examples for org.apache.commons.math.optimization.univariate BrentOptimizer optimize
public double optimize(UnivariateRealFunction f, GoalType goal, double min, double max) throws MaxIterationsExceededException, FunctionEvaluationException
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 w w w .ja v a 2s.c om } }; 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: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 ww w . j a va 2 s. c o m * 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 ww. j a va 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); } }