/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** Increment the iterations counter by 1. * @exception OptimizationException if the maximal number * of iterations is exceeded */ protected void incrementIterationsCounter() throws OptimizationException { if (++iterations > maxIterations) { throw new OptimizationException(new MaxIterationsExceededException(maxIterations)); } }
/** * Guess the errors in optimized parameters. * <p>Guessing is covariance-based, it only gives rough order of magnitude.</p> * @return errors in optimized parameters * @exception FunctionEvaluationException if the function jacobian cannot b evaluated * @exception OptimizationException if the covariances matrix cannot be computed * or the number of degrees of freedom is not positive (number of measurements * lesser or equal to number of parameters) */ public double[] guessParametersErrors() throws FunctionEvaluationException, OptimizationException { if (rows <= cols) { throw new OptimizationException( "no degrees of freedom ({0} measurements, {1} parameters)", rows, cols); } double[] errors = new double[cols]; final double c = Math.sqrt(getChiSquare() / (rows - cols)); double[][] covar = getCovariances(); for (int i = 0; i < errors.length; ++i) { errors[i] = Math.sqrt(covar[i][i]) * c; } return errors; }
/** * Find the upper bound b ensuring bracketing of a root between a and b * @param f function whose root must be bracketed * @param a lower bound of the interval * @param h initial step to try * @return b such that f(a) and f(b) have opposite signs * @exception FunctionEvaluationException if the function cannot be computed * @exception OptimizationException if no bracket can be found */ private double findUpperBound(final UnivariateRealFunction f, final double a, final double h) throws FunctionEvaluationException, OptimizationException { final double yA = f.value(a); double yB = yA; for (double step = h; step < Double.MAX_VALUE; step *= Math.max(2, yA / yB)) { final double b = a + step; yB = f.value(b); if (yA * yB <= 0) { return b; } } throw new OptimizationException("unable to bracket optimum in line search"); }
/** * Find the upper bound b ensuring bracketing of a root between a and b * @param f function whose root must be bracketed * @param a lower bound of the interval * @param h initial step to try * @return b such that f(a) and f(b) have opposite signs * @exception FunctionEvaluationException if the function cannot be computed * @exception OptimizationException if no bracket can be found */ private double findUpperBound(final UnivariateRealFunction f, final double a, final double h) throws FunctionEvaluationException, OptimizationException { final double yA = f.value(a); double yB = yA; for (double step = h; step < Double.MAX_VALUE; step *= FastMath.max(2, yA / yB)) { final double b = a + step; yB = f.value(b); if (yA * yB <= 0) { return b; } } throw new OptimizationException(LocalizedFormats.UNABLE_TO_BRACKET_OPTIMUM_IN_LINE_SEARCH); }
/** * Guess the errors in optimized parameters. * <p>Guessing is covariance-based, it only gives rough order of magnitude.</p> * @return errors in optimized parameters * @exception FunctionEvaluationException if the function jacobian cannot b evaluated * @exception OptimizationException if the covariances matrix cannot be computed * or the number of degrees of freedom is not positive (number of measurements * lesser or equal to number of parameters) */ public double[] guessParametersErrors() throws FunctionEvaluationException, OptimizationException { if (rows <= cols) { throw new OptimizationException( LocalizedFormats.NO_DEGREES_OF_FREEDOM, rows, cols); } double[] errors = new double[cols]; final double c = FastMath.sqrt(getChiSquare() / (rows - cols)); double[][] covar = getCovariances(); for (int i = 0; i < errors.length; ++i) { errors[i] = FastMath.sqrt(covar[i][i]) * c; } return errors; }
/** {@inheritDoc} */ public VectorialPointValuePair optimize(final DifferentiableMultivariateVectorialFunction f, final double[] target, final double[] weights, final double[] startPoint) throws FunctionEvaluationException, OptimizationException, IllegalArgumentException { if (target.length != weights.length) { throw new OptimizationException("dimension mismatch {0} != {1}", target.length, weights.length); } // reset counters iterations = 0; objectiveEvaluations = 0; jacobianEvaluations = 0; // store least squares problem characteristics function = f; jF = f.jacobian(); targetValues = target.clone(); residualsWeights = weights.clone(); this.point = startPoint.clone(); this.residuals = new double[target.length]; // arrays shared with the other private methods rows = target.length; cols = point.length; jacobian = new double[rows][cols]; cost = Double.POSITIVE_INFINITY; return doOptimize(); }
/** {@inheritDoc} */ public VectorialPointValuePair optimize(final DifferentiableMultivariateVectorialFunction f, final double[] target, final double[] weights, final double[] startPoint) throws FunctionEvaluationException, OptimizationException, IllegalArgumentException { if (target.length != weights.length) { throw new OptimizationException(LocalizedFormats.DIMENSIONS_MISMATCH_SIMPLE, target.length, weights.length); } // reset counters iterations = 0; objectiveEvaluations = 0; jacobianEvaluations = 0; // store least squares problem characteristics function = f; jF = f.jacobian(); targetValues = target.clone(); residualsWeights = weights.clone(); this.point = startPoint.clone(); this.residuals = new double[target.length]; // arrays shared with the other private methods rows = target.length; cols = point.length; jacobian = new double[rows][cols]; wjacobian = new double[rows][cols]; wresiduals = new double[rows]; cost = Double.POSITIVE_INFINITY; return doOptimize(); }
throw new OptimizationException( LocalizedFormats.NO_CONVERGENCE_WITH_ANY_START_POINT, starts);
return inverse.getData(); } catch (InvalidMatrixException ime) { throw new OptimizationException(LocalizedFormats.UNABLE_TO_COMPUTE_COVARIANCE_SINGULAR_PROBLEM);
/** Fit an harmonic function to the observed points. * @return harmonic function best fitting the observed points * @throws OptimizationException if the sample is too short or if * the first guess cannot be computed */ public HarmonicFunction fit() throws OptimizationException { // shall we compute the first guess of the parameters ourselves ? if (parameters == null) { final WeightedObservedPoint[] observations = fitter.getObservations(); if (observations.length < 4) { throw new OptimizationException(LocalizedFormats.INSUFFICIENT_OBSERVED_POINTS_IN_SAMPLE, observations.length, 4); } HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations); guesser.guess(); parameters = new double[] { guesser.getGuessedAmplitude(), guesser.getGuessedPulsation(), guesser.getGuessedPhase() }; } try { double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters); return new HarmonicFunction(fitted[0], fitted[1], fitted[2]); } catch (FunctionEvaluationException fee) { // should never happen throw new RuntimeException(fee); } }
return inverse.getData(); } catch (InvalidMatrixException ime) { throw new OptimizationException("unable to compute covariances: singular problem");
valueAtOptimum = optim.getFunctionValue(); } catch (MaxIterationsExceededException e) { throw new OptimizationException(e);
/** Fit an harmonic function to the observed points. * @return harmonic function best fitting the observed points * @throws OptimizationException if the sample is too short or if * the first guess cannot be computed */ public HarmonicFunction fit() throws OptimizationException { try { // shall we compute the first guess of the parameters ourselves ? if (parameters == null) { final WeightedObservedPoint[] observations = fitter.getObservations(); if (observations.length < 4) { throw new OptimizationException("sample contains {0} observed points, at least {1} are required", observations.length, 4); } HarmonicCoefficientsGuesser guesser = new HarmonicCoefficientsGuesser(observations); guesser.guess(); parameters = new double[] { guesser.getGuessedAmplitude(), guesser.getGuessedPulsation(), guesser.getGuessedPhase() }; } double[] fitted = fitter.fit(new ParametricHarmonicFunction(), parameters); return new HarmonicFunction(fitted[0], fitted[1], fitted[2]); } catch (FunctionEvaluationException fee) { // this should never happen throw MathRuntimeException.createInternalError(fee); } }