Skip to content

Commit

Permalink
fixed files form Math #66
Browse files Browse the repository at this point in the history
  • Loading branch information
tdurieux committed Mar 7, 2017
1 parent 32c6b74 commit 1687e1a
Showing 1 changed file with 11 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,10 @@ public class BrentOptimizer extends AbstractUnivariateRealOptimizer {
* Construct a solver.
*/
public BrentOptimizer() {
setMaxEvaluations(Integer.MAX_VALUE);
setMaxEvaluations(1000);
setMaximalIterationCount(100);
setAbsoluteAccuracy(1E-10);
setRelativeAccuracy(1.0e-14);
setAbsoluteAccuracy(1e-11);
setRelativeAccuracy(1e-9);
}

/**
Expand All @@ -54,17 +54,10 @@ public BrentOptimizer() {
*/
protected double doOptimize()
throws MaxIterationsExceededException, FunctionEvaluationException {
throw new UnsupportedOperationException();
}
public double optimize(final UnivariateRealFunction f, final GoalType goalType, final double min, final double max, final double startValue) throws MaxIterationsExceededException, FunctionEvaluationException {
clearResult();
return localMin(getGoalType() == GoalType.MINIMIZE,
f, goalType, min, startValue, max,
getMin(), getStartValue(), getMax(),
getRelativeAccuracy(), getAbsoluteAccuracy());
}
public double optimize(final UnivariateRealFunction f, final GoalType goalType, final double min, final double max) throws MaxIterationsExceededException, FunctionEvaluationException {
return optimize(f, goalType, min, max, min + GOLDEN_SECTION * (max - min));
}

/**
* Find the minimum of the function within the interval {@code (lo, hi)}.
Expand All @@ -91,8 +84,6 @@ public double optimize(final UnivariateRealFunction f, final GoalType goalType,
* the function.
*/
private double localMin(boolean isMinim,
UnivariateRealFunction f,
GoalType goalType,
double lo, double mid, double hi,
double eps, double t)
throws MaxIterationsExceededException, FunctionEvaluationException {
Expand All @@ -116,15 +107,14 @@ private double localMin(boolean isMinim,
double w = x;
double d = 0;
double e = 0;
double fx = computeObjectiveValue(f, x);
if (goalType == GoalType.MAXIMIZE) {
double fx = computeObjectiveValue(x);
if (!isMinim) {
fx = -fx;
}
double fv = fx;
double fw = fx;

int count = 0;
while (count < maximalIterationCount) {
while (true) {
double m = 0.5 * (a + b);
final double tol1 = eps * Math.abs(x) + t;
final double tol2 = 2 * tol1;
Expand Down Expand Up @@ -197,8 +187,8 @@ private double localMin(boolean isMinim,
u = x + d;
}

double fu = computeObjectiveValue(f, u);
if (goalType == GoalType.MAXIMIZE) {
double fu = computeObjectiveValue(u);
if (!isMinim) {
fu = -fu;
}

Expand Down Expand Up @@ -235,11 +225,10 @@ private double localMin(boolean isMinim,
}
}
} else { // termination
setResult(x, (goalType == GoalType.MAXIMIZE) ? -fx : fx, count);
setFunctionValue(isMinim ? fx : -fx);
return x;
}
++count;
incrementIterationsCounter();
}
throw new MaxIterationsExceededException(maximalIterationCount);
}
}

0 comments on commit 1687e1a

Please sign in to comment.