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.math.optimization.general;
019    
020    import org.apache.commons.math.analysis.DifferentiableMultivariateRealFunction;
021    import org.apache.commons.math.analysis.MultivariateVectorialFunction;
022    import org.apache.commons.math.optimization.DifferentiableMultivariateRealOptimizer;
023    import org.apache.commons.math.optimization.GoalType;
024    import org.apache.commons.math.optimization.ConvergenceChecker;
025    import org.apache.commons.math.optimization.RealPointValuePair;
026    import org.apache.commons.math.optimization.direct.BaseAbstractScalarOptimizer;
027    
028    /**
029     * Base class for implementing optimizers for multivariate scalar
030     * differentiable functions.
031     * It contains boiler-plate code for dealing with gradient evaluation.
032     *
033     * @version $Id: AbstractScalarDifferentiableOptimizer.java 1166311 2011-09-07 18:48:06Z luc $
034     * @since 2.0
035     */
036    public abstract class AbstractScalarDifferentiableOptimizer
037        extends BaseAbstractScalarOptimizer<DifferentiableMultivariateRealFunction>
038        implements DifferentiableMultivariateRealOptimizer {
039        /**
040         * Objective function gradient.
041         */
042        private MultivariateVectorialFunction gradient;
043    
044        /**
045         * Simple constructor with default settings.
046         * The convergence check is set to a
047         * {@link org.apache.commons.math.optimization.SimpleScalarValueChecker
048         * SimpleScalarValueChecker}.
049         */
050        protected AbstractScalarDifferentiableOptimizer() {}
051        /**
052         * @param checker Convergence checker.
053         */
054        protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker<RealPointValuePair> checker) {
055            super(checker);
056        }
057    
058        /**
059         * Compute the gradient vector.
060         *
061         * @param evaluationPoint Point at which the gradient must be evaluated.
062         * @return the gradient at the specified point.
063         * @throws org.apache.commons.math.exception.TooManyEvaluationsException
064         * if the allowed number of evaluations is exceeded.
065         */
066        protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
067            return gradient.value(evaluationPoint);
068        }
069    
070        /** {@inheritDoc} */
071        @Override
072        public RealPointValuePair optimize(int maxEval,
073                                           final DifferentiableMultivariateRealFunction f,
074                                           final GoalType goalType,
075                                           final double[] startPoint) {
076            // Store optimization problem characteristics.
077            gradient = f.gradient();
078    
079            return super.optimize(maxEval, f, goalType, startPoint);
080        }
081    }