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
018package org.apache.commons.math3.optimization.general;
019
020import org.apache.commons.math3.analysis.MultivariateVectorFunction;
021import org.apache.commons.math3.analysis.differentiation.GradientFunction;
022import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
023import org.apache.commons.math3.optimization.ConvergenceChecker;
024import org.apache.commons.math3.optimization.GoalType;
025import org.apache.commons.math3.optimization.OptimizationData;
026import org.apache.commons.math3.optimization.InitialGuess;
027import org.apache.commons.math3.optimization.PointValuePair;
028import 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 * @deprecated As of 3.1 (to be removed in 4.0).
036 * @since 3.1
037 */
038@Deprecated
039public abstract class AbstractDifferentiableOptimizer
040    extends BaseAbstractMultivariateOptimizer<MultivariateDifferentiableFunction> {
041    /**
042     * Objective function gradient.
043     */
044    private MultivariateVectorFunction gradient;
045
046    /**
047     * @param checker Convergence checker.
048     */
049    protected AbstractDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
050        super(checker);
051    }
052
053    /**
054     * Compute the gradient vector.
055     *
056     * @param evaluationPoint Point at which the gradient must be evaluated.
057     * @return the gradient at the specified point.
058     */
059    protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
060        return gradient.value(evaluationPoint);
061    }
062
063    /**
064     * {@inheritDoc}
065     *
066     * @deprecated In 3.1. Please use
067     * {@link #optimizeInternal(int,MultivariateDifferentiableFunction,GoalType,OptimizationData[])}
068     * instead.
069     */
070    @Override@Deprecated
071    protected PointValuePair optimizeInternal(final int maxEval,
072                                              final MultivariateDifferentiableFunction f,
073                                              final GoalType goalType,
074                                              final double[] startPoint) {
075        return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
076    }
077
078    /** {@inheritDoc} */
079    @Override
080    protected PointValuePair optimizeInternal(final int maxEval,
081                                              final MultivariateDifferentiableFunction f,
082                                              final GoalType goalType,
083                                              final OptimizationData... optData) {
084        // Store optimization problem characteristics.
085        gradient = new GradientFunction(f);
086
087        // Perform optimization.
088        return super.optimizeInternal(maxEval, f, goalType, optData);
089    }
090}