List of usage examples for org.apache.commons.math3.optim.nonlinear.scalar.noderiv MultiDirectionalSimplex MultiDirectionalSimplex
public MultiDirectionalSimplex(final double[][] referenceSimplex)
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
protected static PointValuePair runSimplex(BaseObjectiveFunction objective, double[] startPoint) throws TooManyEvaluationsException { // Rely on relative difference to test convergence, // to allow for vast differences in absolute error scale // between objective functions. ConvergenceChecker<PointValuePair> convergenceChecker = new SimpleValueChecker(1.e-6, 1.e-14); SimplexOptimizer optimizer = new SimplexOptimizer(convergenceChecker); MultiDirectionalSimplex simplex = new MultiDirectionalSimplex(objective.getSimplexStepSize()); return optimizer.optimize(GoalType.MINIMIZE, new ObjectiveFunction(objective), new MaxEval(objective.getMaxEvaluations()), MaxIter.unlimited(), new InitialGuess(startPoint), simplex);/*from ww w . java 2 s . c o m*/ }
From source file:org.hawkular.datamining.forecast.models.performance.OptimizationAlgorithmsTests.java
private long executeMultidirectionalSimplex(ModelData modelData) { long start = System.nanoTime(); SimplexOptimizer optimizer = new SimplexOptimizer(0.00001, 0.00001); PointValuePair unbounded = optimizer.optimize(GoalType.MINIMIZE, new MaxIter(MAX_ITER), new MaxEval(MAX_EVAL), new InitialGuess(INITIAL_GUESS), new ObjectiveFunction(objectiveFunction), new MultiDirectionalSimplex(2)); long executionTime = System.nanoTime() - start; printOptimizationResult(objectiveFunction, unbounded.getPoint(), modelData); return executionTime; }
From source file:smlm.fitting.FittingClassical.java
@SuppressWarnings("unused") @Override/* w w w . j a v a 2 s . c o m*/ public boolean FitThis() { // Initial estimates (your initial x) // Calculating the centroid ImageStatistics stat = roi.getStatistics(); double x0 = stat.xCenterOfMass; double y0 = stat.yCenterOfMass; double[] start = { x0, y0, param.psfSigma, param.psfSigma, i0Max, backgroundLevel }; PointValuePair solutionMult = null; MultivariateFunctionMappingAdapter fitFunc = null; if (param.fitting != Params.Fitting.CentroidFit) { // initial step sizes (take a good guess) double[] step = { 1, 1, 0.1, 0.1, stdBackground, stdBackground }; // convergence tolerance double ftol = 0.0001;// 0.000001; pixelPrecision = 3;// 5; if (param.fitting == Params.Fitting.FastGaussianFit) { ftol = 0.1; pixelPrecision = 3; } SimplexOptimizer Fit = new SimplexOptimizer(ftol, ftol * ftol); double[] low = new double[] { 0, 0, 0, 0, 0, 0 }; double[] up = new double[] { roi.getWidth(), roi.getHeight(), Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY, Double.POSITIVE_INFINITY }; fitFunc = new MultivariateFunctionMappingAdapter(new LLH(), low, up); // maximal number of iterations int maxIter = 5000; // Nelder and Mead maximisation procedure // x0 e [0, xmax] // Fit.addConstraint(0, -1, 0); // Fit.addConstraint(0, 1, roi.getWidth()); // y0 e [0, ymax] // Fit.addConstraint(1, -1, 0); // Fit.addConstraint(1, 1, roi.getHeight()); /* * // sigmax e [PSFSigma/3, 3*PSFSigma] Fit.addConstraint(2, -1, * PSFSigmaInt-PSFSigmaInt/2); Fit.addConstraint(2, 1, * 2*PSFSigmaInt); // sigmay e [PSFSigma/3, 3*PSFSigma] * Fit.addConstraint(3, -1, PSFSigmaInt-PSFSigmaInt/2); * Fit.addConstraint(3, 1, 2*PSFSigmaInt); // I0 e [StdBackground, * 50*Intensities[i]] Fit.addConstraint(4, -1, StdBackground); * Fit.addConstraint(4, 1, 50*XY[3][i]); // PoissonNoise e * [BackgroundLevel/3, 3*BackgroundLevel] Fit.addConstraint(5, -1, * BackgroundLevel/3); Fit.addConstraint(5, 1, 3*BackgroundLevel); */ solutionMult = Fit.optimize(new MaxEval(maxIter), new ObjectiveFunction(fitFunc), GoalType.MAXIMIZE, new InitialGuess(fitFunc.boundedToUnbounded(start)), new MultiDirectionalSimplex(step)); } // Result of minimisation // Save the fit results this.fit.incrementCounter(); if (param.fitting == Params.Fitting.CentroidFit) { results = start; } else { results = fitFunc.unboundedToBounded(solutionMult.getPoint()); } for (int i = 0; i < isArgumentFixed.length; i++) { if (isArgumentFixed[i]) { results[i] = fixedArguments[i]; } } if (cycle != -1) this.fit.addValue(ResultsTableMt.CYCLE, cycle); this.fit.addValue(ResultsTableMt.FRAME, realFrame); this.fit.addValue(ResultsTableMt.X0, results[0] + xMax - ((double) roiWidth * param.psfSigmaInt)); this.fit.addValue(ResultsTableMt.Y0, results[1] + yMax - ((double) roiWidth * param.psfSigmaInt)); this.fit.addValue(ResultsTableMt.SIGMAX, results[2]); this.fit.addValue(ResultsTableMt.SIGMAY, results[3]); this.fit.addValue(ResultsTableMt.I0, results[4]); this.fit.addValue(ResultsTableMt.NOISE, results[5]); if (param.fitting == Params.Fitting.CentroidFit || true) { this.fit.addValue(ResultsTableMt.IS_FITTED, 1); if (param.fitting != Params.Fitting.CentroidFit) { this.fit.addValue("MinFit", solutionMult.getValue()); } } else this.fit.addValue(ResultsTableMt.IS_FITTED, 0); // Save results if (param.debug) { DrawInitialRoi(true); DrawFit(true); } return true; }
From source file:uk.ac.diamond.scisoft.analysis.diffraction.FittingUtils.java
/** * Optimize given function/*from www. j a v a 2s.co m*/ * @param f * @param opt * @param min * @return residual */ public static double optimize(FitFunction f, MultivariateOptimizer opt, double min) { double res = Double.NaN; try { PointValuePair result; if (opt instanceof BOBYQAOptimizer) { result = opt.optimize(new InitialGuess(f.getInitial()), GoalType.MINIMIZE, new ObjectiveFunction(f), new MaxEval(MAX_EVAL), f.getBounds()); } else if (opt instanceof CMAESOptimizer) { int p = (int) Math.ceil(4 + Math.log(f.getN())) + 1; logger.trace("Population size: {}", p); result = opt.optimize(new InitialGuess(f.getInitial()), GoalType.MINIMIZE, new ObjectiveFunction(f), new CMAESOptimizer.Sigma(f.getSigma()), new CMAESOptimizer.PopulationSize(p), new MaxEval(MAX_EVAL), f.getBounds()); } else { int n = f.getN(); double offset = 1e12; double[] scale = new double[n]; for (int i = 0; i < n; i++) { scale[i] = offset * 0.25; } SimpleBounds bnds = f.getBounds(); MultivariateFunctionPenaltyAdapter of = new MultivariateFunctionPenaltyAdapter(f, bnds.getLower(), bnds.getUpper(), offset, scale); result = opt.optimize(new InitialGuess(f.getInitial()), GoalType.MINIMIZE, new ObjectiveFunction(of), new MaxEval(MAX_EVAL), new MultiDirectionalSimplex(n)); // new NelderMeadSimplex(n)); } // logger.info("Q-space fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare()); double ires = f.value(opt.getStartPoint()); logger.trace("Residual: {} from {}", result.getValue(), ires); res = result.getValue(); if (res < min) f.setParameters(result.getPoint()); logger.trace("Used {} evals and {} iters", opt.getEvaluations(), opt.getIterations()); // System.err.printf("Used %d evals and %d iters\n", opt.getEvaluations(), opt.getIterations()); // logger.info("Q-space fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare()); } catch (IllegalArgumentException e) { logger.error("Start point has wrong dimension", e); // should not happen! } catch (TooManyEvaluationsException e) { throw new IllegalArgumentException("Could not fit as optimizer did not converge"); // logger.error("Convergence problem: max iterations ({}) exceeded", opt.getMaxIterations()); } return res; }
From source file:uk.ac.diamond.scisoft.analysis.optimize.ApacheOptimizer.java
private void internalScalarOptimize() { MultivariateOptimizer opt = createOptimizer(); SimpleBounds bd = createBounds();/*from w w w . java 2 s . com*/ double offset = 1e12; double[] scale = new double[n]; for (int i = 0; i < n; i++) { scale[i] = offset * 0.25; } MultivariateFunction fn = createFunction(); if (optimizer == Optimizer.SIMPLEX_MD || optimizer == Optimizer.SIMPLEX_NM) { fn = new MultivariateFunctionPenaltyAdapter(fn, bd.getLower(), bd.getUpper(), offset, scale); } ObjectiveFunction of = new ObjectiveFunction(fn); InitialGuess ig = new InitialGuess(getParameterValues()); MaxEval me = new MaxEval(MAX_EVAL); double min = Double.POSITIVE_INFINITY; double res = Double.NaN; try { PointValuePair result; switch (optimizer) { case CONJUGATE_GRADIENT: // af = new MultivariateFunctionPenaltyAdapter(fn, bd.getLower(), bd.getUpper(), offset, scale); result = opt.optimize(ig, GoalType.MINIMIZE, of, me, new ObjectiveFunctionGradient(createGradientFunction())); break; case BOBYQA: result = opt.optimize(ig, GoalType.MINIMIZE, of, me, bd); break; case CMAES: double[] sigma = new double[n]; for (int i = 0; i < n; i++) { IParameter p = params.get(i); double v = p.getValue(); double r = Math.max(p.getUpperLimit() - v, v - p.getLowerLimit()); sigma[i] = r * 0.05; // 5% of range } int p = (int) Math.ceil(4 + Math.log(n)) + 1; logger.trace("Population size: {}", p); result = opt.optimize(ig, GoalType.MINIMIZE, of, me, bd, new CMAESOptimizer.Sigma(sigma), new CMAESOptimizer.PopulationSize(p)); break; case SIMPLEX_MD: result = opt.optimize(ig, GoalType.MINIMIZE, of, me, new MultiDirectionalSimplex(n)); break; case SIMPLEX_NM: result = opt.optimize(ig, GoalType.MINIMIZE, of, me, new NelderMeadSimplex(n)); break; default: throw new IllegalStateException("Should not be called"); } // logger.info("Q-space fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare()); double ires = calculateResidual(opt.getStartPoint()); logger.trace("Residual: {} from {}", result.getValue(), ires); res = result.getValue(); if (res < min) setParameterValues(result.getPoint()); logger.trace("Used {} evals and {} iters", opt.getEvaluations(), opt.getIterations()); // System.err.printf("Used %d evals and %d iters\n", opt.getEvaluations(), opt.getIterations()); // logger.info("Q-space fit: rms = {}, x^2 = {}", opt.getRMS(), opt.getChiSquare()); } catch (IllegalArgumentException e) { logger.error("Start point has wrong dimension", e); // should not happen! } catch (TooManyEvaluationsException e) { throw new IllegalArgumentException("Could not fit as optimizer did not converge"); // logger.error("Convergence problem: max iterations ({}) exceeded", opt.getMaxIterations()); } }