List of usage examples for org.apache.commons.math3.optim.univariate SearchInterval SearchInterval
public SearchInterval(double lo, double hi, double init)
From source file:eu.crisis_economics.abm.algorithms.optimization.BrentLineSearch.java
/** * Perform a line search minimization. This function accepts as input: * (a) a starting point (a vector),//from w ww. ja va 2 s .com * (b) a direction in which to travel (a vector), * (c) limits on the total distance to travel along (b). * * With these inputs the function attempts to find the minimum of a * scalar-valued multivariate function along the line starting at * (a) and pointing in the direction of (b). * * @param function * A scalar-valued multivariate function to minimize, * @param startingPoint * A vector starting point from which to begin the minimization (P), * @param vectorDirection * A vector direction along which to travel from P, (V) * @param maximumDistanceToTravel * The maximum distance to travel in the direction of V, * @param maximumEvaluations * The maximum number of function evaluations to identify the minimum, * @param relativeErrorGoal * The relative error target of the minimization, * @param absoluteErrorGoal * The absolute error target of the minimization. * @return * A lightweight immutable struct containing the vector solution and * the evaluation of the function at this point. */ static public LineSearchResult doLineSearch(final MultivariateFunction function, final double[] startingPoint, final double[] vectorDirection, final double maximumDistanceToTravel, final int maximumEvaluations, final double relativeErrorGoal, final double absoluteErrorGoal) { Preconditions.checkArgument(maximumEvaluations > 0); Preconditions.checkArgument(relativeErrorGoal > 0. || absoluteErrorGoal > 0.); Preconditions.checkArgument(maximumDistanceToTravel > 0.); final LineSearchObjectiveFunction lineSearcher = new LineSearchObjectiveFunction(function, startingPoint, vectorDirection); final UnivariateOptimizer optimizer = new BrentOptimizer(relativeErrorGoal, absoluteErrorGoal); UnivariatePointValuePair result = optimizer.optimize(new MaxEval(maximumEvaluations), new UnivariateObjectiveFunction(lineSearcher), GoalType.MINIMIZE, new SearchInterval(0, maximumDistanceToTravel, 0)); final double[] vectorSolution = new double[startingPoint.length]; for (int i = 0; i < vectorDirection.length; ++i) vectorSolution[i] = lineSearcher.startingPoint[i] + lineSearcher.normalizedDirection[i] * result.getPoint(); final LineSearchResult solution = new LineSearchResult(vectorSolution, result.getValue()); return solution; }
From source file:com.wwidesigner.optimization.ObjectiveFunctionOptimizer.java
protected static UnivariatePointValuePair runBrent(BrentOptimizer optimizer, BaseObjectiveFunction objective, double[] startPoint) throws TooManyEvaluationsException { UnivariatePointValuePair outcome;/*from w ww . j ava 2s .c o m*/ outcome = optimizer.optimize(GoalType.MINIMIZE, new UnivariateObjectiveFunction(objective), new MaxEval(objective.getMaxEvaluations()), MaxIter.unlimited(), new SearchInterval(objective.getLowerBounds()[0], objective.getUpperBounds()[0], startPoint[0])); return outcome; }
From source file:com.wwidesigner.modelling.PlayingRange.java
/** * Find fmin for a playing range, given fmax. * fmin is the highest frequency <= fmax that satisfies * either gain(fmin) == MinimumGain//from w w w.j ava 2s . c o m * or fmin is a local minimum of Im(Z)/Re(Z). * @param fmax - maximum frequency, as returned by findFmax(). */ public double findFmin(double fmax) { final double stepSize = fmax * Granularity; // Step size for search. // Upper bound on fmin is fmax. // findFmax ensures Im(Z(fmax)) == 0.0. double lowerFreq = fmax; Complex z_lo = calculator.calcZ(fmax, fingering); double g_lo = calculator.calcGain(lowerFreq, z_lo); double ratio = z_lo.getImaginary() / z_lo.getReal(); double minRatio = ratio + 1.0; if (g_lo < MinimumGain) { // Loop gain is too small, even at fmax. // There is no playing range here. throw new NoPlayingRange(fmax); } // Lower bound on fmin either has gain < MinimumGain // or is past a local minimum of Im(Z)/Re(Z). while (g_lo >= MinimumGain && ratio < minRatio) { minRatio = ratio; lowerFreq -= stepSize; if (lowerFreq < fmax / SearchBoundRatio) { throw new NoPlayingRange(fmax); } z_lo = calculator.calcZ(lowerFreq, fingering); g_lo = calculator.calcGain(lowerFreq, z_lo); ratio = z_lo.getImaginary() / z_lo.getReal(); } double freqGain; // Frequency at which gain == MinimumGain. double freqRatio; // Frequency of local minimum of Im(Z)/Re(Z). if (g_lo < MinimumGain) { // Find the point at which gain == MinimumGain. try { freqGain = solver.solve(50, gainOne, lowerFreq, fmax); } catch (Exception e) { System.out.println("Exception solving for fmin (gain): " + e.getMessage()); // e.printStackTrace(); throw new NoPlayingRange(fmax); } } else { freqGain = lowerFreq; } // Find the local minimum of Im(Z)/Re(Z). try { UnivariatePointValuePair minimum; minimum = optimizer.optimize(GoalType.MINIMIZE, new UnivariateObjectiveFunction(zRatio), new MaxEval(50), MaxIter.unlimited(), new SearchInterval(lowerFreq, fmax, 0.5 * (lowerFreq + fmax))); freqRatio = minimum.getPoint(); } catch (Exception e) { System.out.println("Exception solving for fmin (ratio): " + e.getMessage()); // e.printStackTrace(); throw new NoPlayingRange(fmax); } if (freqRatio > freqGain) { return freqRatio; } return freqGain; }