Added "optimize" method to allow passing simple bounds.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1190556 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2011-10-28 20:44:09 +00:00
parent f7be1dc6ac
commit 481e8c1729
4 changed files with 90 additions and 2 deletions

View File

@ -137,6 +137,16 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
public RealPointValuePair optimize(int maxEval, final FUNC f,
final GoalType goal,
double[] startPoint) {
return optimize(maxEval, f, goal, startPoint, null, null);
}
/**
* {@inheritDoc}
*/
public RealPointValuePair optimize(int maxEval, final FUNC f,
final GoalType goal,
double[] startPoint,
double[] lowerBound, double[] upperBound) {
maxEvaluations = maxEval;
RuntimeException lastException = null;
optima = new RealPointValuePair[starts];
@ -147,7 +157,8 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
// CHECKSTYLE: stop IllegalCatch
try {
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
i == 0 ? startPoint : generator.nextVector());
i == 0 ? startPoint : generator.nextVector(),
lowerBound, upperBound);
} catch (RuntimeException mue) {
lastException = mue;
optima[i] = null;

View File

@ -54,4 +54,27 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
*/
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint);
/**
* Optimize an objective function.
*
* @param f Objective function.
* @param goalType Type of optimization goal: either
* {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
* @param startPoint Start point for optimization.
* @param maxEval Maximum number of function evaluations.
* @param lowerBound Lower bound for each of the parameters.
* @param upperBound Upper bound for each of the parameters.
* @return the point/value pair giving the optimal value for objective
* function.
* @throws org.apache.commons.math.exception.DimensionMismatchException
* if the array sizes are wrong.
* @throws org.apache.commons.math.exception.TooManyEvaluationsException
* if the maximal number of evaluations is exceeded.
* @throws org.apache.commons.math.exception.NullArgumentException if
* {@code f}, {@code goalType} or {@code startPoint} is {@code null}.
*/
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint,
double[] lowerBound, double[] upperBound);
}

View File

@ -48,6 +48,10 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
private GoalType goal;
/** Initial guess. */
private double[] start;
/** Lower bounds. */
private double[] lowerBound;
/** Upper bounds. */
private double[] upperBound;
/** Objective function. */
private MultivariateRealFunction function;
@ -101,6 +105,13 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
/** {@inheritDoc} */
public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint) {
return optimize(maxEval, f, goalType, startPoint, null, null);
}
/** {@inheritDoc} */
public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint,
double[] lower, double[] upper) {
// Checks.
if (f == null) {
throw new NullArgumentException();
@ -120,6 +131,23 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
function = f;
goal = goalType;
start = startPoint.clone();
final int dim = startPoint.length;
if (lower == null) {
lowerBound = new double[dim];
for (int i = 0; i < dim; i++) {
lowerBound[i] = Double.NEGATIVE_INFINITY;
}
} else {
lowerBound = lower.clone();
}
if (upper == null) {
upperBound = new double[dim];
for (int i = 0; i < dim; i++) {
upperBound[i] = Double.POSITIVE_INFINITY;
}
} else {
upperBound = upper.clone();
}
// Perform computation.
return doOptimize();
@ -139,6 +167,20 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
return start.clone();
}
/**
* @return the lower bounds.
*/
public double[] getLowerBound() {
return lowerBound.clone();
}
/**
* @return the upper bounds.
*/
public double[] getUpperBound() {
return upperBound.clone();
}
/**
* Perform the bulk of the optimization algorithm.
*

View File

@ -73,9 +73,21 @@ public abstract class AbstractScalarDifferentiableOptimizer
final DifferentiableMultivariateRealFunction f,
final GoalType goalType,
final double[] startPoint) {
return optimize(maxEval, f, goalType, startPoint, null, null);
}
/** {@inheritDoc} */
@Override
public RealPointValuePair optimize(int maxEval,
final DifferentiableMultivariateRealFunction f,
final GoalType goalType,
final double[] startPoint,
double[] lowerBound, double[] upperBound) {
// Store optimization problem characteristics.
gradient = f.gradient();
return super.optimize(maxEval, f, goalType, startPoint);
return super.optimize(maxEval, f, goalType,
startPoint,
lowerBound, upperBound);
}
}