Program: 57378
Project: 57378
Buggy source:
/** 
 * Find the minimum of the function   {@code f} within the interval {@code (a, b)}. If the function   {@code f} is defined on the interval {@code (a, b)}, then this method finds an approximation   {@code x} to the point at which {@code f}attains its minimum.<br/>  {@code t} and {@code eps} define a tolerance {@code tol = eps |x| + t} and{@code f} is never evaluated at two points closer together than {@code tol}.  {@code eps} should be no smaller than <em>2 macheps</em> and preferable notmuch less than <em>sqrt(macheps)</em>, where <em>macheps</em> is the relative machine precision.  {@code t} should be positive.
 * @param f the function to solve
 * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}or   {@link GoalType#MINIMIZE}
 * @param a Lower bound of the interval
 * @param b Higher bound of the interval
 * @param eps Relative accuracy
 * @param t Absolute accuracy
 * @return the point at which the function is minimal.
 * @throws MaxIterationsExceededException if the maximum iteration countis exceeded.
 * @throws FunctionEvaluationException if an error occurs evaluatingthe function.
 */
private double localMin(final UnivariateRealFunction f,final GoalType goalType,double a,double b,final double eps,final double t) throws MaxIterationsExceededException, FunctionEvaluationException {
  double x=a + GOLDEN_SECTION * (b - a);
  double v=x;
  double w=x;
  double e=0;
  double fx=computeObjectiveValue(f,x);
  if (goalType == GoalType.MAXIMIZE) {
    fx=-fx;
  }
  double fv=fx;
  double fw=fx;
  int count=0;
  while (count < maximalIterationCount) {
    double m=0.5 * (a + b);
    double tol=eps * Math.abs(x) + t;
    double t2=2 * tol;
    if (Math.abs(x - m) > t2 - 0.5 * (b - a)) {
      double p=0;
      double q=0;
      double r=0;
      double d=0;
      double u=0;
      if (Math.abs(e) > tol) {
        r=(x - w) * (fx - fv);
        q=(x - v) * (fx - fw);
        p=(x - v) * q - (x - w) * r;
        q=2 * (q - r);
        if (q > 0) {
          p=-p;
        }
 else {
          q=-q;
        }
        r=e;
        e=d;
      }
      if (Math.abs(p) < Math.abs(0.5 * q * r) && (p < q * (a - x)) && (p < q * (b - x))) {
        d=p / q;
        u=x + d;
        if (((u - a) < t2) || ((b - u) < t2)) {
          d=(x < m) ? tol : -tol;
        }
      }
 else {
        e=((x < m) ? b : a) - x;
        d=GOLDEN_SECTION * e;
      }
      u=x + ((Math.abs(d) > tol) ? d : ((d > 0) ? tol : -tol));
      double fu=computeObjectiveValue(f,u);
      if (goalType == GoalType.MAXIMIZE) {
        fu=-fu;
      }
      if (fu <= fx) {
        if (u < x) {
          b=x;
        }
 else {
          a=x;
        }
        v=w;
        fv=fw;
        w=x;
        fw=fx;
        x=u;
        fx=fu;
      }
 else {
        if (u < x) {
          a=u;
        }
 else {
          b=u;
        }
        if ((fu <= fw) || (w == x)) {
          v=w;
          fv=fw;
          w=u;
          fw=fu;
        }
 else         if ((fu <= fv) || (v == x) || (v == w)) {
          v=u;
          fv=fu;
        }
      }
    }
 else {
      setResult(x,(goalType == GoalType.MAXIMIZE) ? -fx : fx,count);
      return x;
    }
    ++count;
  }
  throw new MaxIterationsExceededException(maximalIterationCount);
}

Fixed version (ground truth):
/** 
 * Find the minimum of the function   {@code f} within the interval {@code (a, b)}. If the function   {@code f} is defined on the interval {@code (a, b)}, then this method finds an approximation   {@code x} to the point at which {@code f}attains its minimum.<br/>  {@code t} and {@code eps} define a tolerance {@code tol = eps |x| + t} and{@code f} is never evaluated at two points closer together than {@code tol}.  {@code eps} should be no smaller than <em>2 macheps</em> and preferable notmuch less than <em>sqrt(macheps)</em>, where <em>macheps</em> is the relative machine precision.  {@code t} should be positive.
 * @param f the function to solve.
 * @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}or   {@link GoalType#MINIMIZE}.
 * @param lo Lower bound of the interval.
 * @param mid Point inside the interval {@code [lo, hi]}.
 * @param hi Higher bound of the interval.
 * @param eps Relative accuracy.
 * @param t Absolute accuracy.
 * @return the optimum point.
 * @throws MaxIterationsExceededException if the maximum iteration countis exceeded.
 * @throws FunctionEvaluationException if an error occurs evaluatingthe function.
 */
private double localMin(UnivariateRealFunction f,GoalType goalType,double lo,double mid,double hi,double eps,double t) throws MaxIterationsExceededException, FunctionEvaluationException {
  if (eps <= 0) {
    throw new NotStrictlyPositiveException(eps);
  }
  if (t <= 0) {
    throw new NotStrictlyPositiveException(t);
  }
  double a, b;
  if (lo < hi) {
    a=lo;
    b=hi;
  }
 else {
    a=hi;
    b=lo;
  }
  double x=mid;
  double v=x;
  double w=x;
  double e=0;
  double fx=computeObjectiveValue(f,x);
  if (goalType == GoalType.MAXIMIZE) {
    fx=-fx;
  }
  double fv=fx;
  double fw=fx;
  int count=0;
  while (count < maximalIterationCount) {
    double m=0.5 * (a + b);
    final double tol1=eps * Math.abs(x) + t;
    final double tol2=2 * tol1;
    if (Math.abs(x - m) > tol2 - 0.5 * (b - a)) {
      double p=0;
      double q=0;
      double r=0;
      double d=0;
      double u=0;
      if (Math.abs(e) > tol1) {
        r=(x - w) * (fx - fv);
        q=(x - v) * (fx - fw);
        p=(x - v) * q - (x - w) * r;
        q=2 * (q - r);
        if (q > 0) {
          p=-p;
        }
 else {
          q=-q;
        }
        r=e;
        e=d;
        if (p > q * (a - x) && p < q * (b - x) && Math.abs(p) < Math.abs(0.5 * q * r)) {
          d=p / q;
          u=x + d;
          if (u - a < tol2 || b - u < tol2) {
            if (x <= m) {
              d=tol1;
            }
 else {
              d=-tol1;
            }
          }
        }
 else {
          if (x < m) {
            e=b - x;
          }
 else {
            e=a - x;
          }
          d=GOLDEN_SECTION * e;
        }
      }
 else {
        if (x < m) {
          e=b - x;
        }
 else {
          e=a - x;
        }
        d=GOLDEN_SECTION * e;
      }
      if (Math.abs(d) < tol1) {
        if (d >= 0) {
          u=x + tol1;
        }
 else {
          u=x - tol1;
        }
      }
 else {
        u=x + d;
      }
      double fu=computeObjectiveValue(f,u);
      if (goalType == GoalType.MAXIMIZE) {
        fu=-fu;
      }
      if (fu <= fx) {
        if (u < x) {
          b=x;
        }
 else {
          a=x;
        }
        v=w;
        fv=fw;
        w=x;
        fw=fx;
        x=u;
        fx=fu;
      }
 else {
        if (u < x) {
          a=u;
        }
 else {
          b=u;
        }
        if (fu <= fw || w == x) {
          v=w;
          fv=fw;
          w=u;
          fw=fu;
        }
 else         if (fu <= fv || v == x || v == w) {
          v=u;
          fv=fu;
        }
      }
    }
 else {
      setResult(x,(goalType == GoalType.MAXIMIZE) ? -fx : fx,count);
      return x;
    }
    ++count;
  }
  throw new MaxIterationsExceededException(maximalIterationCount);
}