MATH-389
Duplicate code in "MultivariateRealOptimizer" and "DifferentiableMultivariateRealOptimizer" moved to new interface "BaseMultivariateRealOptimizer". New "AbstractScalarOptimizer" superclass allow implementations that do not use derivatives. Deprecated "protected" fields. "NonLinearConjugateGradientOptimizer" changed to make use of the newly added accessors instead of the "protected" fields. git-svn-id: https://svn.apache.org/repos/asf/commons/proper/math/trunk@967288 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
92f005ad13
commit
1a637a4458
|
@ -0,0 +1,115 @@
|
|||
/*
|
||||
* 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.math.optimization;
|
||||
|
||||
import org.apache.commons.math.FunctionEvaluationException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
|
||||
/**
|
||||
* Optimization algorithms find the input point set that either {@link GoalType
|
||||
* maximize or minimize} an objective function.
|
||||
* This interface is mainly intended to enforce the internal coherence of
|
||||
* Commons-Math. Users of the API are advised to base their code on
|
||||
* {@link MultivariateRealOptimizer} or on
|
||||
* {@link DifferentiableMultivariateRealOptimizer}.
|
||||
*
|
||||
* @see MultivariateRealOptimizer
|
||||
* @see DifferentiableMultivariateRealOptimizer
|
||||
* @version $Revision$ $Date$
|
||||
* @since 2.2
|
||||
*/
|
||||
public interface BaseMultivariateRealOptimizer<T extends MultivariateRealFunction> {
|
||||
/**
|
||||
* Set the maximal number of iterations of the algorithm.
|
||||
*
|
||||
* @param maxIterations Maximal number of algorithm iterations.
|
||||
*/
|
||||
void setMaxIterations(int maxIterations);
|
||||
|
||||
/**
|
||||
* Get the maximal number of iterations of the algorithm.
|
||||
*
|
||||
* @return the maximal number of iterations.
|
||||
*/
|
||||
int getMaxIterations();
|
||||
|
||||
/**
|
||||
* Set the maximal number of functions evaluations.
|
||||
*
|
||||
* @param maxEvaluations Maximal number of function evaluations.
|
||||
*/
|
||||
void setMaxEvaluations(int maxEvaluations);
|
||||
|
||||
/**
|
||||
* Get the maximal number of functions evaluations.
|
||||
*
|
||||
* @return the maximal number of functions evaluations.
|
||||
*/
|
||||
int getMaxEvaluations();
|
||||
|
||||
/**
|
||||
* Get the number of iterations realized by the algorithm.
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(MultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
*
|
||||
* @return the number of iterations.
|
||||
*/
|
||||
int getIterations();
|
||||
|
||||
/**
|
||||
* Get the number of evaluations of the objective function.
|
||||
*
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(T, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
*
|
||||
* @return the number of evaluations of the objective function.
|
||||
*/
|
||||
int getEvaluations();
|
||||
|
||||
/**
|
||||
* Set the convergence checker.
|
||||
*
|
||||
* @param checker Object to use to check for convergence.
|
||||
*/
|
||||
void setConvergenceChecker(RealConvergenceChecker checker);
|
||||
|
||||
/**
|
||||
* Get the convergence checker.
|
||||
*
|
||||
* @return the object used to check for convergence.
|
||||
*/
|
||||
RealConvergenceChecker getConvergenceChecker();
|
||||
|
||||
/**
|
||||
* 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.
|
||||
* @return the point/value pair giving the optimal value for objective function.
|
||||
* @throws FunctionEvaluationException if the objective function throws one during
|
||||
* the search.
|
||||
* @throws OptimizationException if the algorithm failed to converge.
|
||||
* @throws IllegalArgumentException if the start point dimension is wrong.
|
||||
*/
|
||||
RealPointValuePair optimize(T f, GoalType goalType, double[] startPoint)
|
||||
throws FunctionEvaluationException, OptimizationException;
|
||||
}
|
|
@ -30,82 +30,15 @@ import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
|
|||
* @version $Revision$ $Date$
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface DifferentiableMultivariateRealOptimizer {
|
||||
|
||||
/** Set the maximal number of iterations of the algorithm.
|
||||
* @param maxIterations maximal number of function calls
|
||||
*/
|
||||
void setMaxIterations(int maxIterations);
|
||||
|
||||
/** Get the maximal number of iterations of the algorithm.
|
||||
* @return maximal number of iterations
|
||||
*/
|
||||
int getMaxIterations();
|
||||
|
||||
/** Get the number of iterations realized by the algorithm.
|
||||
* <p>
|
||||
public interface DifferentiableMultivariateRealOptimizer
|
||||
extends BaseMultivariateRealOptimizer<DifferentiableMultivariateRealFunction> {
|
||||
/**
|
||||
* Get the number of evaluations of the objective function gradient.
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
* </p>
|
||||
* @return number of iterations
|
||||
*/
|
||||
int getIterations();
|
||||
|
||||
/** Set the maximal number of functions evaluations.
|
||||
* @param maxEvaluations maximal number of function evaluations
|
||||
*/
|
||||
void setMaxEvaluations(int maxEvaluations);
|
||||
|
||||
/** Get the maximal number of functions evaluations.
|
||||
* @return maximal number of functions evaluations
|
||||
*/
|
||||
int getMaxEvaluations();
|
||||
|
||||
/** Get the number of evaluations of the objective function.
|
||||
* <p>
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
* </p>
|
||||
* @return number of evaluations of the objective function
|
||||
*/
|
||||
int getEvaluations();
|
||||
|
||||
/** Get the number of evaluations of the objective function gradient.
|
||||
* <p>
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(DifferentiableMultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
* </p>
|
||||
* @return number of evaluations of the objective function gradient
|
||||
*
|
||||
* @return the number of evaluations of the objective function gradient.
|
||||
*/
|
||||
int getGradientEvaluations();
|
||||
|
||||
/** Set the convergence checker.
|
||||
* @param checker object to use to check for convergence
|
||||
*/
|
||||
void setConvergenceChecker(RealConvergenceChecker checker);
|
||||
|
||||
/** Get the convergence checker.
|
||||
* @return object used to check for convergence
|
||||
*/
|
||||
RealConvergenceChecker getConvergenceChecker();
|
||||
|
||||
/** Optimizes an objective function.
|
||||
* @param f objective function
|
||||
* @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
|
||||
* or {@link GoalType#MINIMIZE}
|
||||
* @param startPoint the start point for optimization
|
||||
* @return the point/value pair giving the optimal value for objective function
|
||||
* @exception FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @exception OptimizationException if the algorithm failed to converge
|
||||
* @exception IllegalArgumentException if the start point dimension is wrong
|
||||
*/
|
||||
RealPointValuePair optimize(DifferentiableMultivariateRealFunction f,
|
||||
GoalType goalType,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
|
||||
|
||||
}
|
||||
|
|
|
@ -30,72 +30,5 @@ import org.apache.commons.math.analysis.MultivariateRealFunction;
|
|||
* @version $Revision$ $Date$
|
||||
* @since 2.0
|
||||
*/
|
||||
public interface MultivariateRealOptimizer {
|
||||
|
||||
/** Set the maximal number of iterations of the algorithm.
|
||||
* @param maxIterations maximal number of algorithm iterations
|
||||
*/
|
||||
void setMaxIterations(int maxIterations);
|
||||
|
||||
/** Get the maximal number of iterations of the algorithm.
|
||||
* @return maximal number of iterations
|
||||
*/
|
||||
int getMaxIterations();
|
||||
|
||||
/** Set the maximal number of functions evaluations.
|
||||
* @param maxEvaluations maximal number of function evaluations
|
||||
*/
|
||||
void setMaxEvaluations(int maxEvaluations);
|
||||
|
||||
/** Get the maximal number of functions evaluations.
|
||||
* @return maximal number of functions evaluations
|
||||
*/
|
||||
int getMaxEvaluations();
|
||||
|
||||
/** Get the number of iterations realized by the algorithm.
|
||||
* <p>
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(MultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
* </p>
|
||||
* @return number of iterations
|
||||
*/
|
||||
int getIterations();
|
||||
|
||||
/** Get the number of evaluations of the objective function.
|
||||
* <p>
|
||||
* The number of evaluations corresponds to the last call to the
|
||||
* {@link #optimize(MultivariateRealFunction, GoalType, double[]) optimize}
|
||||
* method. It is 0 if the method has not been called yet.
|
||||
* </p>
|
||||
* @return number of evaluations of the objective function
|
||||
*/
|
||||
int getEvaluations();
|
||||
|
||||
/** Set the convergence checker.
|
||||
* @param checker object to use to check for convergence
|
||||
*/
|
||||
void setConvergenceChecker(RealConvergenceChecker checker);
|
||||
|
||||
/** Get the convergence checker.
|
||||
* @return object used to check for convergence
|
||||
*/
|
||||
RealConvergenceChecker getConvergenceChecker();
|
||||
|
||||
/** Optimizes an objective function.
|
||||
* @param f objective function
|
||||
* @param goalType type of optimization goal: either {@link GoalType#MAXIMIZE}
|
||||
* or {@link GoalType#MINIMIZE}
|
||||
* @param startPoint the start point for optimization
|
||||
* @return the point/value pair giving the optimal value for objective function
|
||||
* @exception FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @exception OptimizationException if the algorithm failed to converge
|
||||
* @exception IllegalArgumentException if the start point dimension is wrong
|
||||
*/
|
||||
RealPointValuePair optimize(MultivariateRealFunction f,
|
||||
GoalType goalType,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
|
||||
|
||||
}
|
||||
public interface MultivariateRealOptimizer
|
||||
extends BaseMultivariateRealOptimizer<MultivariateRealFunction> {}
|
||||
|
|
|
@ -30,89 +30,55 @@ import org.apache.commons.math.optimization.RealPointValuePair;
|
|||
import org.apache.commons.math.optimization.SimpleScalarValueChecker;
|
||||
|
||||
/**
|
||||
* Base class for implementing optimizers for multivariate scalar functions.
|
||||
* <p>This base class handles the boilerplate methods associated to thresholds
|
||||
* settings, iterations and evaluations counting.</p>
|
||||
* Base class for implementing optimizers for multivariate scalar
|
||||
* differentiable functions.
|
||||
* It contains boiler-plate code for dealing with gradient evaluation.
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
* @since 2.0
|
||||
*/
|
||||
public abstract class AbstractScalarDifferentiableOptimizer
|
||||
extends BaseAbstractScalarOptimizer<DifferentiableMultivariateRealFunction>
|
||||
implements DifferentiableMultivariateRealOptimizer {
|
||||
|
||||
/** Default maximal number of iterations allowed. */
|
||||
public static final int DEFAULT_MAX_ITERATIONS = 100;
|
||||
|
||||
/** Convergence checker. */
|
||||
protected RealConvergenceChecker checker;
|
||||
|
||||
/**
|
||||
* Type of optimization.
|
||||
* @since 2.1
|
||||
*/
|
||||
protected GoalType goal;
|
||||
|
||||
/** Current point set. */
|
||||
protected double[] point;
|
||||
|
||||
/** Maximal number of iterations allowed. */
|
||||
private int maxIterations;
|
||||
|
||||
/** Number of iterations already performed. */
|
||||
private int iterations;
|
||||
|
||||
/** Maximal number of evaluations allowed. */
|
||||
private int maxEvaluations;
|
||||
|
||||
/** Number of evaluations already performed. */
|
||||
private int evaluations;
|
||||
|
||||
/** Number of gradient evaluations. */
|
||||
private int gradientEvaluations;
|
||||
|
||||
/** Objective function. */
|
||||
private DifferentiableMultivariateRealFunction function;
|
||||
|
||||
/** Objective function gradient. */
|
||||
private MultivariateVectorialFunction gradient;
|
||||
|
||||
/** Simple constructor with default settings.
|
||||
* <p>The convergence check is set to a {@link SimpleScalarValueChecker}
|
||||
* and the maximal number of evaluation is set to its default value.</p>
|
||||
/** Convergence checker.
|
||||
* @deprecated in 2.2 (to be removed in 3.0). Please use the accessor
|
||||
* {@link BaseAbstractScalarOptimizer#getConvergenceChecker()} instead.
|
||||
*/
|
||||
protected AbstractScalarDifferentiableOptimizer() {
|
||||
setConvergenceChecker(new SimpleScalarValueChecker());
|
||||
setMaxIterations(DEFAULT_MAX_ITERATIONS);
|
||||
setMaxEvaluations(Integer.MAX_VALUE);
|
||||
}
|
||||
protected RealConvergenceChecker checker;
|
||||
/**
|
||||
* Type of optimization.
|
||||
* @since 2.1
|
||||
* @deprecated in 2.2 (to be removed in 3.0). Please use the accessor
|
||||
* {@link BaseAbstractScalarOptimizer#getGoalType()} instead.
|
||||
*/
|
||||
protected GoalType goal;
|
||||
/** Current point set.
|
||||
* @deprecated in 2.2 (to be removed in 3.0).
|
||||
*/
|
||||
protected double[] point;
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setMaxIterations(int maxIterations) {
|
||||
this.maxIterations = maxIterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getMaxIterations() {
|
||||
return maxIterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getIterations() {
|
||||
return iterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setMaxEvaluations(int maxEvaluations) {
|
||||
this.maxEvaluations = maxEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getMaxEvaluations() {
|
||||
return maxEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getEvaluations() {
|
||||
return evaluations;
|
||||
/**
|
||||
* Simple constructor with default settings.
|
||||
* The convergence check is set to a {@link SimpleScalarValueChecker},
|
||||
* the allowed number of iterations and evaluations are set to their
|
||||
* default values.
|
||||
*/
|
||||
protected AbstractScalarDifferentiableOptimizer() {}
|
||||
/**
|
||||
* @param convergenceChecker Convergence checker.
|
||||
* @param maxIterations Maximum number of iterations.
|
||||
* @param maxEvaluations Maximum number of evaluations.
|
||||
*/
|
||||
protected AbstractScalarDifferentiableOptimizer(RealConvergenceChecker checker,
|
||||
int maxIterations,
|
||||
int maxEvaluations) {
|
||||
super(checker, maxIterations, maxEvaluations);
|
||||
this.checker = checker; // Do not use (deprecated).
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
|
@ -120,27 +86,6 @@ public abstract class AbstractScalarDifferentiableOptimizer
|
|||
return gradientEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setConvergenceChecker(RealConvergenceChecker convergenceChecker) {
|
||||
this.checker = convergenceChecker;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealConvergenceChecker getConvergenceChecker() {
|
||||
return checker;
|
||||
}
|
||||
|
||||
/** Increment the iterations counter by 1.
|
||||
* @exception OptimizationException if the maximal number
|
||||
* of iterations is exceeded
|
||||
*/
|
||||
protected void incrementIterationsCounter()
|
||||
throws OptimizationException {
|
||||
if (++iterations > maxIterations) {
|
||||
throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the gradient vector.
|
||||
* @param evaluationPoint point at which the gradient must be evaluated
|
||||
|
@ -153,52 +98,21 @@ public abstract class AbstractScalarDifferentiableOptimizer
|
|||
return gradient.value(evaluationPoint);
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the objective function value.
|
||||
* @param evaluationPoint point at which the objective function must be evaluated
|
||||
* @return objective function value at specified point
|
||||
* @exception FunctionEvaluationException if the function cannot be evaluated
|
||||
* or its dimension doesn't match problem dimension or the maximal number
|
||||
* of iterations is exceeded
|
||||
*/
|
||||
protected double computeObjectiveValue(final double[] evaluationPoint)
|
||||
throws FunctionEvaluationException {
|
||||
if (++evaluations > maxEvaluations) {
|
||||
throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
|
||||
evaluationPoint);
|
||||
}
|
||||
return function.value(evaluationPoint);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealPointValuePair optimize(final DifferentiableMultivariateRealFunction f,
|
||||
final GoalType goalType,
|
||||
final double[] startPoint)
|
||||
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {
|
||||
|
||||
final GoalType goalType,
|
||||
final double[] startPoint)
|
||||
throws FunctionEvaluationException,
|
||||
OptimizationException {
|
||||
// reset counters
|
||||
iterations = 0;
|
||||
evaluations = 0;
|
||||
gradientEvaluations = 0;
|
||||
|
||||
// store optimization problem characteristics
|
||||
function = f;
|
||||
gradient = f.gradient();
|
||||
goal = goalType;
|
||||
point = startPoint.clone();
|
||||
|
||||
return doOptimize();
|
||||
goal = goalType; // Do not use (deprecated).
|
||||
point = startPoint.clone(); // Do not use (deprecated).
|
||||
|
||||
return super.optimize(f, goalType, startPoint);
|
||||
}
|
||||
|
||||
/** Perform the bulk of optimization algorithm.
|
||||
* @return the point/value pair giving the optimal value for objective function
|
||||
* @exception FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @exception OptimizationException if the algorithm failed to converge
|
||||
* @exception IllegalArgumentException if the start point dimension is wrong
|
||||
*/
|
||||
protected abstract RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException;
|
||||
|
||||
}
|
||||
|
|
|
@ -0,0 +1,60 @@
|
|||
/*
|
||||
* 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.math.optimization.general;
|
||||
|
||||
import org.apache.commons.math.FunctionEvaluationException;
|
||||
import org.apache.commons.math.MaxEvaluationsExceededException;
|
||||
import org.apache.commons.math.MaxIterationsExceededException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
|
||||
import org.apache.commons.math.optimization.GoalType;
|
||||
import org.apache.commons.math.optimization.OptimizationException;
|
||||
import org.apache.commons.math.optimization.RealConvergenceChecker;
|
||||
import org.apache.commons.math.optimization.BaseMultivariateRealOptimizer;
|
||||
import org.apache.commons.math.optimization.MultivariateRealOptimizer;
|
||||
import org.apache.commons.math.optimization.RealPointValuePair;
|
||||
import org.apache.commons.math.optimization.SimpleScalarValueChecker;
|
||||
|
||||
/**
|
||||
* Base class for implementing optimizers for multivariate (not necessarily
|
||||
* differentiable) scalar functions.
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
* @since 2.2
|
||||
*/
|
||||
public abstract class AbstractScalarOptimizer
|
||||
extends BaseAbstractScalarOptimizer<MultivariateRealFunction>
|
||||
implements MultivariateRealOptimizer {
|
||||
/**
|
||||
* Simple constructor with default settings.
|
||||
* The convergence check is set to a {@link SimpleScalarValueChecker},
|
||||
* the allowed number of iterations and evaluations are set to their
|
||||
* default values.
|
||||
*/
|
||||
protected AbstractScalarOptimizer() {}
|
||||
/**
|
||||
* @param convergenceChecker Convergence checker.
|
||||
* @param maxIterations Maximum number of iterations.
|
||||
* @param maxEvaluations Maximum number of evaluations.
|
||||
*/
|
||||
protected AbstractScalarOptimizer(RealConvergenceChecker checker,
|
||||
int maxIterations,
|
||||
int maxEvaluations) {
|
||||
super(checker, maxIterations, maxEvaluations);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,204 @@
|
|||
/*
|
||||
* 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.math.optimization.general;
|
||||
|
||||
import org.apache.commons.math.FunctionEvaluationException;
|
||||
import org.apache.commons.math.MaxEvaluationsExceededException;
|
||||
import org.apache.commons.math.MaxIterationsExceededException;
|
||||
import org.apache.commons.math.analysis.MultivariateRealFunction;
|
||||
import org.apache.commons.math.analysis.MultivariateVectorialFunction;
|
||||
import org.apache.commons.math.optimization.GoalType;
|
||||
import org.apache.commons.math.optimization.OptimizationException;
|
||||
import org.apache.commons.math.optimization.RealConvergenceChecker;
|
||||
import org.apache.commons.math.optimization.BaseMultivariateRealOptimizer;
|
||||
import org.apache.commons.math.optimization.RealPointValuePair;
|
||||
import org.apache.commons.math.optimization.SimpleScalarValueChecker;
|
||||
|
||||
/**
|
||||
* Base class for implementing optimizers for multivariate scalar functions.
|
||||
* This base class handles the boiler-plate methods associated to thresholds
|
||||
* settings, iterations and evaluations counting.
|
||||
* This class is mainly intended to enforce the internal coherence of
|
||||
* Commons-Math.
|
||||
* A class that implements an optimization algorithm should inherit from
|
||||
* {@link AbstractScalarOptimizer} or from
|
||||
* {@link AbstractScalarDifferentiableOptimizer}.
|
||||
*
|
||||
* @version $Revision$ $Date$
|
||||
* @since 2.2
|
||||
*/
|
||||
public abstract class BaseAbstractScalarOptimizer<T extends MultivariateRealFunction>
|
||||
implements BaseMultivariateRealOptimizer<T> {
|
||||
/** Default maximal number of iterations allowed ({@value}). */
|
||||
public static final int DEFAULT_MAX_ITERATIONS = 1000;
|
||||
/** Default maximal number of iterations allowed ({@value}). */
|
||||
public static final int DEFAULT_MAX_EVALUATIONS = 10000;
|
||||
|
||||
/** Convergence checker. */
|
||||
private RealConvergenceChecker checker;
|
||||
/** Type of optimization. */
|
||||
private GoalType goal;
|
||||
/** Initial guess. */
|
||||
private double[] start;
|
||||
/** Maximal number of iterations allowed. */
|
||||
private int maxIterations;
|
||||
/** Number of iterations already performed. */
|
||||
private int iterations;
|
||||
/** Maximal number of evaluations allowed. */
|
||||
private int maxEvaluations;
|
||||
/** Number of evaluations already performed. */
|
||||
private int evaluations;
|
||||
/** Objective function. */
|
||||
private MultivariateRealFunction function;
|
||||
|
||||
/**
|
||||
* Simple constructor with default settings.
|
||||
* The convergence check is set to a {@link SimpleScalarValueChecker},
|
||||
* the allowed number of iterations and evaluations are set to their
|
||||
* default values.
|
||||
*/
|
||||
protected BaseAbstractScalarOptimizer() {
|
||||
this(new SimpleScalarValueChecker(),
|
||||
DEFAULT_MAX_ITERATIONS,
|
||||
DEFAULT_MAX_EVALUATIONS);
|
||||
}
|
||||
/**
|
||||
* @param convergenceChecker Convergence checker.
|
||||
* @param maxIterations Maximum number of iterations.
|
||||
* @param maxEvaluations Maximum number of evaluations.
|
||||
*/
|
||||
protected BaseAbstractScalarOptimizer(RealConvergenceChecker checker,
|
||||
int maxIterations,
|
||||
int maxEvaluations) {
|
||||
this.checker = checker;
|
||||
this.maxIterations = maxIterations;
|
||||
this.maxEvaluations = maxEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setMaxIterations(int maxIterations) {
|
||||
this.maxIterations = maxIterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getMaxIterations() {
|
||||
return maxIterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getIterations() {
|
||||
return iterations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setMaxEvaluations(int maxEvaluations) {
|
||||
this.maxEvaluations = maxEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getMaxEvaluations() {
|
||||
return maxEvaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public int getEvaluations() {
|
||||
return evaluations;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public void setConvergenceChecker(RealConvergenceChecker checker) {
|
||||
this.checker = checker;
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealConvergenceChecker getConvergenceChecker() {
|
||||
return checker;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increment the iterations counter by 1.
|
||||
* @throws OptimizationException if the maximal number
|
||||
* of iterations is exceeded
|
||||
*/
|
||||
protected void incrementIterationsCounter()
|
||||
throws OptimizationException {
|
||||
if (++iterations > maxIterations) {
|
||||
throw new OptimizationException(new MaxIterationsExceededException(maxIterations));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Compute the objective function value.
|
||||
* @param evaluationPoint point at which the objective function must be evaluated
|
||||
* @return objective function value at specified point
|
||||
* @throws FunctionEvaluationException if the function cannot be evaluated
|
||||
* or its dimension doesn't match problem dimension or the maximal number
|
||||
* of iterations is exceeded
|
||||
*/
|
||||
protected double computeObjectiveValue(double[] evaluationPoint)
|
||||
throws FunctionEvaluationException {
|
||||
if (++evaluations > maxEvaluations) {
|
||||
throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
|
||||
evaluationPoint);
|
||||
}
|
||||
return function.value(evaluationPoint);
|
||||
}
|
||||
|
||||
/** {@inheritDoc} */
|
||||
public RealPointValuePair optimize(T f,
|
||||
GoalType goalType,
|
||||
double[] startPoint)
|
||||
throws FunctionEvaluationException, OptimizationException {
|
||||
|
||||
// reset counters
|
||||
iterations = 0;
|
||||
evaluations = 0;
|
||||
|
||||
// store optimization problem characteristics
|
||||
function = f;
|
||||
goal = goalType;
|
||||
start = startPoint.clone();
|
||||
|
||||
return doOptimize();
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the optimization type.
|
||||
*/
|
||||
public GoalType getGoalType() {
|
||||
return goal;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return the initial guess.
|
||||
*/
|
||||
public double[] getStartPoint() {
|
||||
return start.clone();
|
||||
}
|
||||
|
||||
/**
|
||||
* Perform the bulk of optimization algorithm.
|
||||
* @return the point/value pair giving the optimal value for objective function
|
||||
* @throws FunctionEvaluationException if the objective function throws one during
|
||||
* the search
|
||||
* @throws OptimizationException if the algorithm failed to converge
|
||||
* @throws IllegalArgumentException if the start point dimension is wrong
|
||||
*/
|
||||
protected abstract RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException, OptimizationException;
|
||||
}
|
|
@ -42,20 +42,17 @@ import org.apache.commons.math.util.LocalizedFormats;
|
|||
*/
|
||||
|
||||
public class NonLinearConjugateGradientOptimizer
|
||||
extends AbstractScalarDifferentiableOptimizer
|
||||
implements DifferentiableMultivariateRealOptimizer {
|
||||
|
||||
extends AbstractScalarDifferentiableOptimizer {
|
||||
/** Update formula for the beta parameter. */
|
||||
private final ConjugateGradientFormula updateFormula;
|
||||
|
||||
/** Preconditioner (may be null). */
|
||||
private Preconditioner preconditioner;
|
||||
|
||||
/** solver to use in the line search (may be null). */
|
||||
private UnivariateRealSolver solver;
|
||||
|
||||
/** Initial step used to bracket the optimum in line search. */
|
||||
private double initialStep;
|
||||
/** Current point. */
|
||||
private double[] point;
|
||||
|
||||
/** Simple constructor with default settings.
|
||||
* <p>The convergence check is set to a {@link
|
||||
|
@ -115,7 +112,6 @@ public class NonLinearConjugateGradientOptimizer
|
|||
protected RealPointValuePair doOptimize()
|
||||
throws FunctionEvaluationException, OptimizationException, IllegalArgumentException {
|
||||
try {
|
||||
|
||||
// initialization
|
||||
if (preconditioner == null) {
|
||||
preconditioner = new IdentityPreconditioner();
|
||||
|
@ -123,6 +119,8 @@ public class NonLinearConjugateGradientOptimizer
|
|||
if (solver == null) {
|
||||
solver = new BrentSolver();
|
||||
}
|
||||
point = getStartPoint();
|
||||
final GoalType goal = getGoalType();
|
||||
final int n = point.length;
|
||||
double[] r = computeObjectiveGradient(point);
|
||||
if (goal == GoalType.MINIMIZE) {
|
||||
|
@ -147,7 +145,7 @@ public class NonLinearConjugateGradientOptimizer
|
|||
RealPointValuePair previous = current;
|
||||
current = new RealPointValuePair(point, objective);
|
||||
if (previous != null) {
|
||||
if (checker.converged(getIterations(), previous, current)) {
|
||||
if (getConvergenceChecker().converged(getIterations(), previous, current)) {
|
||||
// we have found an optimum
|
||||
return current;
|
||||
}
|
||||
|
|
|
@ -52,6 +52,17 @@ The <action> type attribute can be add,update,fix,remove.
|
|||
If the output is not quite correct, check for invisible trailing spaces!
|
||||
-->
|
||||
<release version="2.2" date="TBD" description="TBD">
|
||||
<action dev="erans" type="update" issue="MATH-389">
|
||||
Refactoring of some classes in package "optimization.general" to remove
|
||||
code duplication and to allow the implementation of optimization algorithms
|
||||
that do not use derivatives.
|
||||
Created interface "BaseMultivariateRealOptimizer".
|
||||
Created classes "BaseAbstractScalarOptimizer" and "AbstractScalarOptimizer".
|
||||
"MultivariateRealOptimizer" and "DifferentiableMultivariateRealOptimizer"
|
||||
both extend "BaseMultivariateRealOptimizer".
|
||||
"AbstractScalarOptimizer" extends "BaseAbstractScalarOptimizer" and
|
||||
"AbstractScalarDifferentiableOptimizer" inherits from "AbstractScalarOptimizer".
|
||||
</action>
|
||||
<action dev="luc" type="add" issue="MATH-388">
|
||||
Added a feature allowing error estimation to be computed only on a subset of
|
||||
Ordinary Differential Equations, considered as the main set, the remaining equations
|
||||
|
|
Loading…
Reference in New Issue