Fixed bug in check that the initial value is within bounds.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1194874 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-10-29 11:19:30 +00:00
parent aecf3993c4
commit d8e1809529
2 changed files with 27 additions and 17 deletions

View File

@ -73,8 +73,10 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
* if the maximal number of evaluations is exceeded. * if the maximal number of evaluations is exceeded.
* @throws org.apache.commons.math.exception.NullArgumentException if * @throws org.apache.commons.math.exception.NullArgumentException if
* {@code f}, {@code goalType} or {@code startPoint} is {@code null}. * {@code f}, {@code goalType} or {@code startPoint} is {@code null}.
* @throws org.apache.commons.math.exception.OutOfRangeException if any * @throws org.apache.commons.math.exception.NumberIsTooSmallException if any
* of the initial values is out of bounds. * of the initial values is less than its lower bound.
* @throws org.apache.commons.math.exception.NumberIsTooLargeException if any
* of the initial values is greater than its upper bound.
*/ */
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType, RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint, double[] startPoint,

View File

@ -22,7 +22,8 @@ import org.apache.commons.math.exception.MaxCountExceededException;
import org.apache.commons.math.exception.TooManyEvaluationsException; import org.apache.commons.math.exception.TooManyEvaluationsException;
import org.apache.commons.math.exception.NullArgumentException; import org.apache.commons.math.exception.NullArgumentException;
import org.apache.commons.math.exception.DimensionMismatchException; import org.apache.commons.math.exception.DimensionMismatchException;
import org.apache.commons.math.exception.OutOfRangeException; import org.apache.commons.math.exception.NumberIsTooSmallException;
import org.apache.commons.math.exception.NumberIsTooLargeException;
import org.apache.commons.math.analysis.MultivariateRealFunction; import org.apache.commons.math.analysis.MultivariateRealFunction;
import org.apache.commons.math.optimization.BaseMultivariateRealOptimizer; import org.apache.commons.math.optimization.BaseMultivariateRealOptimizer;
import org.apache.commons.math.optimization.GoalType; import org.apache.commons.math.optimization.GoalType;
@ -125,21 +126,28 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
throw new NullArgumentException(); throw new NullArgumentException();
} }
final int dim = startPoint.length; final int dim = startPoint.length;
if (lower != null && if (lower != null) {
lower.length != dim) { if (lower.length != dim) {
throw new DimensionMismatchException(lower.length, dim); throw new DimensionMismatchException(lower.length, dim);
} }
if (upper != null &&
upper.length != dim) {
throw new DimensionMismatchException(upper.length, dim);
}
for (int i = 0; i < dim; i++) { for (int i = 0; i < dim; i++) {
final double v = startPoint[i]; final double v = startPoint[i];
final double lo = lower[i]; final double lo = lower[i];
if (v < lo) {
throw new NumberIsTooSmallException(v, lo, true);
}
}
}
if (upper != null) {
if (upper.length != dim) {
throw new DimensionMismatchException(upper.length, dim);
}
for (int i = 0; i < dim; i++) {
final double v = startPoint[i];
final double hi = upper[i]; final double hi = upper[i];
if (v < lo || if (v > hi) {
v > hi) { throw new NumberIsTooLargeException(v, hi, true);
throw new OutOfRangeException(v, lo, hi); }
} }
} }