List of usage examples for org.apache.commons.math3.exception TooManyEvaluationsException getMessage
@Override
public String getMessage()
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
protected static PointValuePair doSingleStart(BaseObjectiveFunction objective, double[] startPoint, int maxEvaluations, double[] nextStart) { singleRunEvaluations = 0;//from w w w . j a v a2 s . com PointValuePair result = null; try { int numVariables = objective.getNrDimensions(); if (numVariables > 1) // Use BOBYQA { double trustRegion = objective.getInitialTrustRegionRadius(nextStart); double stoppingTrustRegion = objective.getStoppingTrustRegionRadius(); BOBYQAOptimizer optimizer = new BOBYQAOptimizer(objective.getNrInterpolations(), trustRegion, stoppingTrustRegion); result = runBobyqa(optimizer, objective, nextStart, maxEvaluations); singleRunEvaluations = optimizer.getEvaluations(); } else // Use Brent { BrentOptimizer optimizer = new BrentOptimizer(1.e-6, 1.e-14); UnivariatePointValuePair outcome = runBrent(optimizer, objective, startPoint); result = new PointValuePair(new double[] { outcome.getPoint() }, outcome.getValue()); singleRunEvaluations = optimizer.getEvaluations(); } double value = result.getValue(); if (value == Double.POSITIVE_INFINITY) { System.out.print("no valid solution found"); } else { System.out.print("optimum " + result.getValue()); } } catch (TooManyEvaluationsException e) { System.out.println("Exception: " + e.getMessage()); } // Thrown by BOBYQA for no apparent reason: a bug? catch (NoSuchElementException e) { System.out.println("no valid solution found"); } catch (OperationCancelledException e) { // Restore starting point. objective.setGeometryPoint(startPoint); // Re-throw the exception to give up the whole multi-start // optimization. throw new OperationCancelledException(e.getMessage()); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); // e.printStackTrace(); } finally { System.out.println(" at start point " + Arrays.toString(nextStart)); } return result; }
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
/** * Use a specified optimizer type to optimize a specified objective * function./*from w w w . j a v a2 s. c o m*/ * * @param objective * - objective function to optimize * @param optimizerType * - type of optimizer to use */ public static boolean optimizeObjectiveFunction(BaseObjectiveFunction objective, BaseObjectiveFunction.OptimizerType optimizerType) { System.out.print("\nSystem has "); System.out.print(objective.getNrDimensions()); System.out.print(" optimization variables and "); System.out.print(objective.getNrNotes()); System.out.print(" target notes."); long startTime = System.currentTimeMillis(); double[] startPoint = objective.getInitialPoint(); double[] errorVector = objective.getErrorVector(startPoint); initialNorm = objective.calcNorm(errorVector); System.out.println(); printErrors("Initial error: ", initialNorm, errorVector); finalNorm = initialNorm; try { if (objective.isMultiStart()) { // Make the startPoint from the optimization result of a // DIRECT/BOBYQA run. PointValuePair outcome; MultivariateOptimizer optimizer = new DIRECTCOptimizer(6.0e-6); outcome = runDirect(optimizer, objective, startPoint); System.out.println("After global optimizer, error: " + outcome.getValue()); outcome = runBobyqa(objective, outcome.getPoint()); System.out.println("Refined start, error: " + outcome.getValue()); outcome = optimizeMultiStart(objective, outcome.getPoint()); if (outcome == null) { // Restore starting point. objective.setGeometryPoint(startPoint); } else { objective.setGeometryPoint(outcome.getPoint()); } } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.BrentOptimizer)) { // Univariate optimization. UnivariatePointValuePair outcome = runBrent(objective, startPoint); double[] geometry = new double[1]; geometry[0] = outcome.getPoint(); objective.setGeometryPoint(geometry); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.PowellOptimizer)) { // Multivariate optimization, without bounds. PointValuePair outcome = runPowell(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.SimplexOptimizer)) { // Multivariate optimization, without bounds. PointValuePair outcome = runSimplex(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.CMAESOptimizer)) { // Multivariate optimization, with bounds. PointValuePair outcome = runCmaes(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } else if (optimizerType.equals(BaseObjectiveFunction.OptimizerType.DIRECTOptimizer)) { // Multivariate DIRECT optimization, with bounds. // Convergence threshold about 3^-15. MultivariateOptimizer optimizer = new DIRECTCOptimizer(7.0e-8); PointValuePair outcome = runDirect(optimizer, objective, startPoint); System.out.println("After " + objective.getNumberOfEvaluations() + " evaluations, global optimizer found optimum " + outcome.getValue()); // Use BOBYQA to refine global optimum found. PointValuePair outcome2 = runBobyqa(objective, outcome.getPoint()); if (outcome.getValue() < outcome2.getValue()) { // Don't use second-stage optimum if it isn't better. System.out.println("Second-stage optimizer found optimum " + outcome2.getValue()); objective.setGeometryPoint(outcome.getPoint()); } else { objective.setGeometryPoint(outcome2.getPoint()); } } else { // Multivariate BOBYQA optimization, with bounds. PointValuePair outcome = runBobyqa(objective, startPoint); objective.setGeometryPoint(outcome.getPoint()); } } catch (TooManyEvaluationsException e) { System.out.println("Exception: " + e.getMessage()); } catch (OperationCancelledException e) { if (objective.isMultiStart()) { System.out.println("\nOptimization cancelled.\n"); return false; } System.out.println("\nOptimization cancelled.\nPartially-optimized result returned.\n"); } catch (Exception e) { System.out.println("Exception: " + e.getMessage()); e.printStackTrace(); return false; } System.out.print("Performed "); System.out.print(objective.getNumberOfTunings()); System.out.print(" tuning calculations in "); System.out.print(objective.getNumberOfEvaluations()); System.out.println(" error norm evaluations."); errorVector = objective.getErrorVector(objective.getInitialPoint()); finalNorm = objective.calcNorm(errorVector); printErrors("Final error: ", finalNorm, errorVector); System.out.print("Residual error ratio: "); System.out.println(finalNorm / initialNorm); long elapsedTime = System.currentTimeMillis() - startTime; double elapsedSeconds = 0.001 * (double) elapsedTime; System.out.print("Elapsed time: "); System.out.printf("%3.1f", elapsedSeconds); System.out.println(" seconds."); return true; }
From source file:gdsc.smlm.fitting.BinomialFitter.java
/** * Fit the binomial distribution (n,p) to the cumulative histogram. Performs fitting assuming a fixed n value and * attempts to optimise p./*from w w w .j a v a2s. c o m*/ * * @param histogram * The input histogram * @param mean * The histogram mean (used to estimate p). Calculated if NaN. * @param n * The n to evaluate * @param zeroTruncated * True if the model should ignore n=0 (zero-truncated binomial) * @return The best fit (n, p) * @throws IllegalArgumentException * If any of the input data values are negative * @throws IllegalArgumentException * If any fitting a zero truncated binomial and there are no values above zero */ public PointValuePair fitBinomial(double[] histogram, double mean, int n, boolean zeroTruncated) { if (Double.isNaN(mean)) mean = getMean(histogram); if (zeroTruncated && histogram[0] > 0) { log("Fitting zero-truncated histogram but there are zero values - Renormalising to ignore zero"); double cumul = 0; for (int i = 1; i < histogram.length; i++) cumul += histogram[i]; if (cumul == 0) throw new IllegalArgumentException( "Fitting zero-truncated histogram but there are no non-zero values"); histogram[0] = 0; for (int i = 1; i < histogram.length; i++) histogram[i] /= cumul; } int nFittedPoints = Math.min(histogram.length, n + 1) - ((zeroTruncated) ? 1 : 0); if (nFittedPoints < 1) { log("No points to fit (%d): Histogram.length = %d, n = %d, zero-truncated = %b", nFittedPoints, histogram.length, n, zeroTruncated); return null; } // The model is only fitting the probability p // For a binomial n*p = mean => p = mean/n double[] initialSolution = new double[] { FastMath.min(mean / n, 1) }; // Create the function BinomialModelFunction function = new BinomialModelFunction(histogram, n, zeroTruncated); double[] lB = new double[1]; double[] uB = new double[] { 1 }; SimpleBounds bounds = new SimpleBounds(lB, uB); // Fit // CMAESOptimizer or BOBYQAOptimizer support bounds // CMAESOptimiser based on Matlab code: // https://www.lri.fr/~hansen/cmaes.m // Take the defaults from the Matlab documentation int maxIterations = 2000; double stopFitness = 0; //Double.NEGATIVE_INFINITY; boolean isActiveCMA = true; int diagonalOnly = 0; int checkFeasableCount = 1; RandomGenerator random = new Well19937c(); boolean generateStatistics = false; ConvergenceChecker<PointValuePair> checker = new SimpleValueChecker(1e-6, 1e-10); // The sigma determines the search range for the variables. It should be 1/3 of the initial search region. OptimizationData sigma = new CMAESOptimizer.Sigma(new double[] { (uB[0] - lB[0]) / 3 }); OptimizationData popSize = new CMAESOptimizer.PopulationSize((int) (4 + Math.floor(3 * Math.log(2)))); try { PointValuePair solution = null; boolean noRefit = maximumLikelihood; if (n == 1 && zeroTruncated) { // No need to fit solution = new PointValuePair(new double[] { 1 }, 0); noRefit = true; } else { GoalType goalType = (maximumLikelihood) ? GoalType.MAXIMIZE : GoalType.MINIMIZE; // Iteratively fit CMAESOptimizer opt = new CMAESOptimizer(maxIterations, stopFitness, isActiveCMA, diagonalOnly, checkFeasableCount, random, generateStatistics, checker); for (int iteration = 0; iteration <= fitRestarts; iteration++) { try { // Start from the initial solution PointValuePair result = opt.optimize(new InitialGuess(initialSolution), new ObjectiveFunction(function), goalType, bounds, sigma, popSize, new MaxIter(maxIterations), new MaxEval(maxIterations * 2)); //System.out.printf("CMAES Iter %d initial = %g (%d)\n", iteration, result.getValue(), // opt.getEvaluations()); if (solution == null || result.getValue() < solution.getValue()) { solution = result; } } catch (TooManyEvaluationsException e) { } catch (TooManyIterationsException e) { } if (solution == null) continue; try { // Also restart from the current optimum PointValuePair result = opt.optimize(new InitialGuess(solution.getPointRef()), new ObjectiveFunction(function), goalType, bounds, sigma, popSize, new MaxIter(maxIterations), new MaxEval(maxIterations * 2)); //System.out.printf("CMAES Iter %d restart = %g (%d)\n", iteration, result.getValue(), // opt.getEvaluations()); if (result.getValue() < solution.getValue()) { solution = result; } } catch (TooManyEvaluationsException e) { } catch (TooManyIterationsException e) { } } if (solution == null) return null; } if (noRefit) { // Although we fit the log-likelihood, return the sum-of-squares to allow // comparison across different n double p = solution.getPointRef()[0]; double ss = 0; double[] obs = function.p; double[] exp = function.getP(p); for (int i = 0; i < obs.length; i++) ss += (obs[i] - exp[i]) * (obs[i] - exp[i]); return new PointValuePair(solution.getPointRef(), ss); } // We can do a LVM refit if the number of fitted points is more than 1 else if (nFittedPoints > 1) { // Improve SS fit with a gradient based LVM optimizer LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); try { final BinomialModelFunctionGradient gradientFunction = new BinomialModelFunctionGradient( histogram, n, zeroTruncated); PointVectorValuePair lvmSolution = optimizer.optimize(new MaxIter(3000), new MaxEval(Integer.MAX_VALUE), new ModelFunctionJacobian(new MultivariateMatrixFunction() { public double[][] value(double[] point) throws IllegalArgumentException { return gradientFunction.jacobian(point); } }), new ModelFunction(gradientFunction), new Target(gradientFunction.p), new Weight(gradientFunction.getWeights()), new InitialGuess(solution.getPointRef())); double ss = 0; double[] obs = gradientFunction.p; double[] exp = lvmSolution.getValue(); for (int i = 0; i < obs.length; i++) ss += (obs[i] - exp[i]) * (obs[i] - exp[i]); // Check the pValue is valid since the LVM is not bounded. double p = lvmSolution.getPointRef()[0]; if (ss < solution.getValue() && p <= 1 && p >= 0) { //log("Re-fitting improved the SS from %s to %s (-%s%%)", // Utils.rounded(solution.getValue(), 4), Utils.rounded(ss, 4), // Utils.rounded(100 * (solution.getValue() - ss) / solution.getValue(), 4)); return new PointValuePair(lvmSolution.getPoint(), ss); } } catch (TooManyIterationsException e) { log("Failed to re-fit: Too many iterations (%d)", optimizer.getIterations()); } catch (ConvergenceException e) { log("Failed to re-fit: %s", e.getMessage()); } catch (Exception e) { // Ignore this ... } } return solution; } catch (Exception e) { log("Failed to fit Binomial distribution with N=%d : %s", n, e.getMessage()); } return null; }