1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements.  See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License.  You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 package org.apache.commons.math3.optimization.general;
19 
20 import org.apache.commons.math3.analysis.MultivariateVectorFunction;
21 import org.apache.commons.math3.analysis.differentiation.GradientFunction;
22 import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
23 import org.apache.commons.math3.optimization.ConvergenceChecker;
24 import org.apache.commons.math3.optimization.GoalType;
25 import org.apache.commons.math3.optimization.OptimizationData;
26 import org.apache.commons.math3.optimization.InitialGuess;
27 import org.apache.commons.math3.optimization.PointValuePair;
28 import org.apache.commons.math3.optimization.direct.BaseAbstractMultivariateOptimizer;
29 
30 /**
31  * Base class for implementing optimizers for multivariate scalar
32  * differentiable functions.
33  * It contains boiler-plate code for dealing with gradient evaluation.
34  *
35  * @deprecated As of 3.1 (to be removed in 4.0).
36  * @since 3.1
37  */
38 @Deprecated
39 public abstract class AbstractDifferentiableOptimizer
40     extends BaseAbstractMultivariateOptimizer<MultivariateDifferentiableFunction> {
41     /**
42      * Objective function gradient.
43      */
44     private MultivariateVectorFunction gradient;
45 
46     /**
47      * @param checker Convergence checker.
48      */
AbstractDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker)49     protected AbstractDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
50         super(checker);
51     }
52 
53     /**
54      * Compute the gradient vector.
55      *
56      * @param evaluationPoint Point at which the gradient must be evaluated.
57      * @return the gradient at the specified point.
58      */
computeObjectiveGradient(final double[] evaluationPoint)59     protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
60         return gradient.value(evaluationPoint);
61     }
62 
63     /**
64      * {@inheritDoc}
65      *
66      * @deprecated In 3.1. Please use
67      * {@link #optimizeInternal(int,MultivariateDifferentiableFunction,GoalType,OptimizationData[])}
68      * instead.
69      */
70     @Override@Deprecated
optimizeInternal(final int maxEval, final MultivariateDifferentiableFunction f, final GoalType goalType, final double[] startPoint)71     protected PointValuePair optimizeInternal(final int maxEval,
72                                               final MultivariateDifferentiableFunction f,
73                                               final GoalType goalType,
74                                               final double[] startPoint) {
75         return optimizeInternal(maxEval, f, goalType, new InitialGuess(startPoint));
76     }
77 
78     /** {@inheritDoc} */
79     @Override
optimizeInternal(final int maxEval, final MultivariateDifferentiableFunction f, final GoalType goalType, final OptimizationData... optData)80     protected PointValuePair optimizeInternal(final int maxEval,
81                                               final MultivariateDifferentiableFunction f,
82                                               final GoalType goalType,
83                                               final OptimizationData... optData) {
84         // Store optimization problem characteristics.
85         gradient = new GradientFunction(f);
86 
87         // Perform optimization.
88         return super.optimizeInternal(maxEval, f, goalType, optData);
89     }
90 }
91