001 /*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018 package org.apache.commons.math3.optimization.general;
019
020 import org.apache.commons.math3.analysis.MultivariateVectorFunction;
021 import org.apache.commons.math3.analysis.differentiation.GradientFunction;
022 import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
023 import org.apache.commons.math3.optimization.ConvergenceChecker;
024 import org.apache.commons.math3.optimization.GoalType;
025 import org.apache.commons.math3.optimization.OptimizationData;
026 import org.apache.commons.math3.optimization.InitialGuess;
027 import org.apache.commons.math3.optimization.PointValuePair;
028 import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer;
029
030 /**
031 * Base class for implementing optimizers for multivariate scalar
032 * differentiable functions.
033 * It contains boiler-plate code for dealing with gradient evaluation.
034 *
035 * @version $Id: AbstractDifferentiableOptimizer.java 1422230 2012-12-15 12:11:13Z erans $
036 * @deprecated As of 3.1 (to be removed in 4.0).
037 * @since 3.1
038 */
039 @Deprecated
040 public abstract class AbstractDifferentiableOptimizer
041 extends BaseAbstractMultivariateOptimizer<MultivariateDifferentiableFunction> {
042 /**
043 * Objective function gradient.
044 */
045 private MultivariateVectorFunction gradient;
046
047 /**
048 * @param checker Convergence checker.
049 */
050 protected AbstractDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
051 super(checker);
052 }
053
054 /**
055 * Compute the gradient vector.
056 *
057 * @param evaluationPoint Point at which the gradient must be evaluated.
058 * @return the gradient at the specified point.
059 */
060 protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
061 return gradient.value(evaluationPoint);
062 }
063
064 /**
065 * {@inheritDoc}
066 *
067 * @deprecated In 3.1. Please use
068 * {@link #optimizeInternal(int,MultivariateDifferentiableFunction,GoalType,OptimizationData[])}
069 * instead.
070 */
071 @Override@Deprecated
072 protected PointValuePair optimizeInternal(final int maxEval,
073 final MultivariateDifferentiableFunction f,
074 final GoalType goalType,
075 final double[] startPoint) {
076 return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
077 }
078
079 /** {@inheritDoc} */
080 @Override
081 protected PointValuePair optimizeInternal(final int maxEval,
082 final MultivariateDifferentiableFunction f,
083 final GoalType goalType,
084 final OptimizationData... optData) {
085 // Store optimization problem characteristics.
086 gradient = new GradientFunction(f);
087
088 // Perform optimization.
089 return super.optimizeInternal(maxEval, f, goalType, optData);
090 }
091 }