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.DifferentiableMultivariateFunction;
021import org.apache.commons.math3.analysis.MultivariateVectorFunction;
022import org.apache.commons.math3.analysis.FunctionUtils;
023import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
024import org.apache.commons.math3.optimization.DifferentiableMultivariateOptimizer;
025import org.apache.commons.math3.optimization.GoalType;
026import org.apache.commons.math3.optimization.ConvergenceChecker;
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 2.0
037 */
038@Deprecated
039public abstract class AbstractScalarDifferentiableOptimizer
040    extends BaseAbstractMultivariateOptimizer<DifferentiableMultivariateFunction>
041    implements DifferentiableMultivariateOptimizer {
042    /**
043     * Objective function gradient.
044     */
045    private MultivariateVectorFunction gradient;
046
047    /**
048     * Simple constructor with default settings.
049     * The convergence check is set to a
050     * {@link org.apache.commons.math3.optimization.SimpleValueChecker
051     * SimpleValueChecker}.
052     * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()}
053     */
054    @Deprecated
055    protected AbstractScalarDifferentiableOptimizer() {}
056
057    /**
058     * @param checker Convergence checker.
059     */
060    protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
061        super(checker);
062    }
063
064    /**
065     * Compute the gradient vector.
066     *
067     * @param evaluationPoint Point at which the gradient must be evaluated.
068     * @return the gradient at the specified point.
069     * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
070     * if the allowed number of evaluations is exceeded.
071     */
072    protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
073        return gradient.value(evaluationPoint);
074    }
075
076    /** {@inheritDoc} */
077    @Override
078    protected PointValuePair optimizeInternal(int maxEval,
079                                              final DifferentiableMultivariateFunction f,
080                                              final GoalType goalType,
081                                              final double[] startPoint) {
082        // Store optimization problem characteristics.
083        gradient = f.gradient();
084
085        return super.optimizeInternal(maxEval, f, goalType, startPoint);
086    }
087
088    /**
089     * Optimize an objective function.
090     *
091     * @param f Objective function.
092     * @param goalType Type of optimization goal: either
093     * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
094     * @param startPoint Start point for optimization.
095     * @param maxEval Maximum number of function evaluations.
096     * @return the point/value pair giving the optimal value for objective
097     * function.
098     * @throws org.apache.commons.math3.exception.DimensionMismatchException
099     * if the start point dimension is wrong.
100     * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
101     * if the maximal number of evaluations is exceeded.
102     * @throws org.apache.commons.math3.exception.NullArgumentException if
103     * any argument is {@code null}.
104     */
105    public PointValuePair optimize(final int maxEval,
106                                   final MultivariateDifferentiableFunction f,
107                                   final GoalType goalType,
108                                   final double[] startPoint) {
109        return optimizeInternal(maxEval,
110                                FunctionUtils.toDifferentiableMultivariateFunction(f),
111                                goalType,
112                                startPoint);
113    }
114}