org.apache.commons.math3.optim.univariate.UnivariatePointValuePair Java Examples

The following examples show how to use org.apache.commons.math3.optim.univariate.UnivariatePointValuePair. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Math_6_PowellOptimizer_t.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
Example #2
Source File: Math_6_PowellOptimizer_s.java    From coming with MIT License 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
Example #3
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
Example #4
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
Example #5
Source File: PowellOptimizer.java    From astor with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Find the minimum of the function {@code f(p + alpha * d)}.
 *
 * @param p Starting point.
 * @param d Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] p, final double[] d) {
    final int n = p.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = p[i] + alpha * d[i];
                }
                final double obj = PowellOptimizer.this.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = PowellOptimizer.this.getGoalType();
    bracket.search(f, goal, 0, 1);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return optimize(new MaxEval(Integer.MAX_VALUE),
                    new UnivariateObjectiveFunction(f),
                    goal,
                    new SearchInterval(bracket.getLo(),
                                       bracket.getHi(),
                                       bracket.getMid()));
}
 
Example #6
Source File: LineSearch.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds the number {@code alpha} that optimizes
 * {@code f(startPoint + alpha * direction)}.
 *
 * @param startPoint Starting point.
 * @param direction Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] startPoint,
                                       final double[] direction) {
    final int n = startPoint.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = startPoint[i] + alpha * direction[i];
                }
                final double obj = mainOptimizer.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = mainOptimizer.getGoalType();
    bracket.search(f, goal, 0, initialBracketingRange);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return lineOptimizer.optimize(new MaxEval(Integer.MAX_VALUE),
                                  new UnivariateObjectiveFunction(f),
                                  goal,
                                  new SearchInterval(bracket.getLo(),
                                                     bracket.getHi(),
                                                     bracket.getMid()));
}
 
Example #7
Source File: LineSearch.java    From astor with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Finds the number {@code alpha} that optimizes
 * {@code f(startPoint + alpha * direction)}.
 *
 * @param startPoint Starting point.
 * @param direction Search direction.
 * @return the optimum.
 * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
 * if the number of evaluations is exceeded.
 */
public UnivariatePointValuePair search(final double[] startPoint,
                                       final double[] direction) {
    final int n = startPoint.length;
    final UnivariateFunction f = new UnivariateFunction() {
            public double value(double alpha) {
                final double[] x = new double[n];
                for (int i = 0; i < n; i++) {
                    x[i] = startPoint[i] + alpha * direction[i];
                }
                final double obj = mainOptimizer.computeObjectiveValue(x);
                return obj;
            }
        };

    final GoalType goal = mainOptimizer.getGoalType();
    bracket.search(f, goal, 0, initialBracketingRange);
    // Passing "MAX_VALUE" as a dummy value because it is the enclosing
    // class that counts the number of evaluations (and will eventually
    // generate the exception).
    return lineOptimizer.optimize(new MaxEval(Integer.MAX_VALUE),
                                  new UnivariateObjectiveFunction(f),
                                  goal,
                                  new SearchInterval(bracket.getLo(),
                                                     bracket.getHi(),
                                                     bracket.getMid()));
}
 
Example #8
Source File: ContaminationModel.java    From gatk with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private static double calculateContamination(final double errorRate, final List<List<PileupSummary>> segments, final List<Double> mafs) {
    final DoubleUnaryOperator objective = c -> modelLogLikelihood(segments, c, errorRate, mafs);

    final List<UnivariatePointValuePair> optima = CONTAMINATION_INITIAL_GUESSES.stream()
            .map(initial -> OptimizationUtils.max(objective, 0, 0.5, initial, 1.0e-4, 1.0e-4, 30))
            .collect(Collectors.toList());

    return Collections.max(optima, Comparator.comparingDouble(UnivariatePointValuePair::getValue)).getPoint();
}
 
Example #9
Source File: Solver.java    From dataflow-java with Apache License 2.0 3 votes vote down vote up
/**
 * Maximizes a univariate function using a grid search followed by Brent's algorithm.
 *
 * @param fn the likelihood function to minimize
 * @param gridStart the lower bound for the grid search
 * @param gridEnd the upper bound for the grid search
 * @param gridStep step size for the grid search
 * @param relErr relative error tolerance for Brent's algorithm
 * @param absErr absolute error tolerance for Brent's algorithm
 * @param maxIter maximum # of iterations to perform in Brent's algorithm
 * @param maxEval maximum # of Likelihood function evaluations in Brent's algorithm
 *
 * @return the value of the parameter that maximizes the function
 */
public static double maximize(UnivariateFunction fn, double gridStart, double gridEnd,
    double gridStep, double relErr, double absErr, int maxIter, int maxEval) {
  Interval interval = gridSearch(fn, gridStart, gridEnd, gridStep);
  BrentOptimizer bo = new BrentOptimizer(relErr, absErr);
  UnivariatePointValuePair max = bo.optimize(
      new MaxIter(maxIter),
      new MaxEval(maxEval),
      new SearchInterval(interval.getInf(), interval.getSup()),
      new UnivariateObjectiveFunction(fn),
      GoalType.MAXIMIZE);
  return max.getPoint();
}