New "optimize" method.


git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@1397758 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Gilles Sadowski 2012-10-13 01:09:02 +00:00
parent 91be760415
commit 8bc689a30c
3 changed files with 154 additions and 18 deletions

View File

@ -0,0 +1,47 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.optimization;
/**
* Starting point (first guess) of the optimization procedure.
* <br/>
* Immutable class.
*
* @version $Id$
* @since 3.1
*/
public class InitialGuess implements OptimizationData {
/** Initial guess. */
private final double[] init;
/**
* @param startPoint Initial guess.
*/
public InitialGuess(double[] startPoint) {
init = startPoint.clone();
}
/**
* Gets the initial guess.
*
* @return the initial guess.
*/
public double[] getInitialGuess() {
return init.clone();
}
}

View File

@ -0,0 +1,29 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.commons.math3.optimization;
/**
* Marker interface.
* Implementations will provide functionality (optional or required) needed
* by the optimizers, and those will need to check the actual type of the
* arguments and perform the appropriate cast in order to access the data
* they need.
*
* @version $Id$
* @since 3.1
*/
public interface OptimizationData {}

View File

@ -20,10 +20,11 @@ package org.apache.commons.math3.optimization.direct;
import org.apache.commons.math3.util.Incrementor;
import org.apache.commons.math3.exception.MaxCountExceededException;
import org.apache.commons.math3.exception.TooManyEvaluationsException;
import org.apache.commons.math3.exception.NullArgumentException;
import org.apache.commons.math3.analysis.MultivariateFunction;
import org.apache.commons.math3.optimization.BaseMultivariateOptimizer;
import org.apache.commons.math3.optimization.OptimizationData;
import org.apache.commons.math3.optimization.GoalType;
import org.apache.commons.math3.optimization.InitialGuess;
import org.apache.commons.math3.optimization.ConvergenceChecker;
import org.apache.commons.math3.optimization.PointValuePair;
import org.apache.commons.math3.optimization.SimpleValueChecker;
@ -99,12 +100,38 @@ public abstract class BaseAbstractMultivariateOptimizer<FUNC extends Multivariat
return function.value(point);
}
/** {@inheritDoc} */
/**
* {@inheritDoc}
*
* @deprecated As of 3.1. Please use
* {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])}
* instead.
*/
public PointValuePair optimize(int maxEval, FUNC f, GoalType goalType,
double[] startPoint) {
double[] startPoint) {
return optimizeInternal(maxEval, f, goalType, startPoint);
}
/**
* Optimize an objective function.
*
* @param maxEval Allowed number of evaluations of the objective function.
* @param f Objective function.
* @param goalType Optimization type.
* @param optData Optimization data. The following data will be looked for:
* <ul>
* <li>{@link InitialGuess}</li>
* </ul>
* @return the point/value pair giving the optimal value of the objective
* function.
*/
public PointValuePair optimize(int maxEval,
FUNC f,
GoalType goalType,
OptimizationData... optData) {
return optimizeInternal(maxEval, f, goalType, optData);
}
/**
* Optimize an objective function.
*
@ -121,33 +148,66 @@ public abstract class BaseAbstractMultivariateOptimizer<FUNC extends Multivariat
* if the maximal number of evaluations is exceeded.
* @throws org.apache.commons.math3.exception.NullArgumentException if
* any argument is {@code null}.
* @deprecated As of 3.1. Please use
* {@link #optimize(int,MultivariateFunction,GoalType,OptimizationData[])}
* instead.
*/
@Deprecated
protected PointValuePair optimizeInternal(int maxEval, MultivariateFunction f, GoalType goalType,
double[] startPoint) {
// Checks.
if (f == null) {
throw new NullArgumentException();
}
if (goalType == null) {
throw new NullArgumentException();
}
if (startPoint == null) {
throw new NullArgumentException();
}
return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
}
// Reset.
/**
* Optimize an objective function.
*
* @param maxEval Allowed number of evaluations of the objective function.
* @param f Objective function.
* @param goalType Optimization type.
* @param optData Optimization data. The following data will be looked for:
* <ul>
* <li>{@link InitialGuess}</li>
* </ul>
* @return the point/value pair giving the optimal value of the objective
* function.
* @throws TooManyEvaluationsException if the maximal number of
* evaluations is exceeded.
*/
protected PointValuePair optimizeInternal(int maxEval,
MultivariateFunction f,
GoalType goalType,
OptimizationData... optData)
throws TooManyEvaluationsException {
evaluations.setMaximalCount(maxEval);
evaluations.resetCount();
// Store optimization problem characteristics.
function = f;
goal = goalType;
start = startPoint.clone();
parseOptimizationData(optData);
// Perform computation.
return doOptimize();
}
/**
* Scans the list of (required and optional) optimization data that
* characterize the problem.
*
* @param optData Optimization data. The following data will be looked for:
* <ul>
* <li>{@link InitialGuess}</li>
* </ul>
*/
private void parseOptimizationData(OptimizationData... optData) {
// The existing values (as set by the previous call) are reused if
// not provided in the argument list.
for (OptimizationData data : optData) {
if (data instanceof InitialGuess) {
start = ((InitialGuess) data).getInitialGuess();
continue;
}
}
}
/**
* @return the optimization type.
*/
@ -165,7 +225,7 @@ public abstract class BaseAbstractMultivariateOptimizer<FUNC extends Multivariat
/**
* Perform the bulk of the optimization algorithm.
*
* @return the point/value pair giving the optimal value for the
* @return the point/value pair giving the optimal value of the
* objective function.
*/
protected abstract PointValuePair doOptimize();