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