MATH-697
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:
parent
f7be1dc6ac
commit
481e8c1729
|
@ -137,6 +137,16 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
|
||||||
public RealPointValuePair optimize(int maxEval, final FUNC f,
|
public RealPointValuePair optimize(int maxEval, final FUNC f,
|
||||||
final GoalType goal,
|
final GoalType goal,
|
||||||
double[] startPoint) {
|
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;
|
maxEvaluations = maxEval;
|
||||||
RuntimeException lastException = null;
|
RuntimeException lastException = null;
|
||||||
optima = new RealPointValuePair[starts];
|
optima = new RealPointValuePair[starts];
|
||||||
|
@ -147,7 +157,8 @@ public class BaseMultiStartMultivariateRealOptimizer<FUNC extends MultivariateRe
|
||||||
// CHECKSTYLE: stop IllegalCatch
|
// CHECKSTYLE: stop IllegalCatch
|
||||||
try {
|
try {
|
||||||
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
|
optima[i] = optimizer.optimize(maxEval - totalEvaluations, f, goal,
|
||||||
i == 0 ? startPoint : generator.nextVector());
|
i == 0 ? startPoint : generator.nextVector(),
|
||||||
|
lowerBound, upperBound);
|
||||||
} catch (RuntimeException mue) {
|
} catch (RuntimeException mue) {
|
||||||
lastException = mue;
|
lastException = mue;
|
||||||
optima[i] = null;
|
optima[i] = null;
|
||||||
|
|
|
@ -54,4 +54,27 @@ public interface BaseMultivariateRealOptimizer<FUNC extends MultivariateRealFunc
|
||||||
*/
|
*/
|
||||||
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
|
RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
|
||||||
double[] startPoint);
|
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);
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,6 +48,10 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
||||||
private GoalType goal;
|
private GoalType goal;
|
||||||
/** Initial guess. */
|
/** Initial guess. */
|
||||||
private double[] start;
|
private double[] start;
|
||||||
|
/** Lower bounds. */
|
||||||
|
private double[] lowerBound;
|
||||||
|
/** Upper bounds. */
|
||||||
|
private double[] upperBound;
|
||||||
/** Objective function. */
|
/** Objective function. */
|
||||||
private MultivariateRealFunction function;
|
private MultivariateRealFunction function;
|
||||||
|
|
||||||
|
@ -101,6 +105,13 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
||||||
/** {@inheritDoc} */
|
/** {@inheritDoc} */
|
||||||
public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
|
public RealPointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
|
||||||
double[] startPoint) {
|
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.
|
// Checks.
|
||||||
if (f == null) {
|
if (f == null) {
|
||||||
throw new NullArgumentException();
|
throw new NullArgumentException();
|
||||||
|
@ -120,6 +131,23 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
||||||
function = f;
|
function = f;
|
||||||
goal = goalType;
|
goal = goalType;
|
||||||
start = startPoint.clone();
|
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.
|
// Perform computation.
|
||||||
return doOptimize();
|
return doOptimize();
|
||||||
|
@ -139,6 +167,20 @@ public abstract class BaseAbstractScalarOptimizer<FUNC extends MultivariateRealF
|
||||||
return start.clone();
|
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.
|
* Perform the bulk of the optimization algorithm.
|
||||||
*
|
*
|
||||||
|
|
|
@ -73,9 +73,21 @@ public abstract class AbstractScalarDifferentiableOptimizer
|
||||||
final DifferentiableMultivariateRealFunction f,
|
final DifferentiableMultivariateRealFunction f,
|
||||||
final GoalType goalType,
|
final GoalType goalType,
|
||||||
final double[] startPoint) {
|
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.
|
// Store optimization problem characteristics.
|
||||||
gradient = f.gradient();
|
gradient = f.gradient();
|
||||||
|
|
||||||
return super.optimize(maxEval, f, goalType, startPoint);
|
return super.optimize(maxEval, f, goalType,
|
||||||
|
startPoint,
|
||||||
|
lowerBound, upperBound);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue