View Javadoc
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.DifferentiableMultivariateFunction;
21  import org.apache.commons.math3.analysis.MultivariateVectorFunction;
22  import org.apache.commons.math3.analysis.FunctionUtils;
23  import org.apache.commons.math3.analysis.differentiation.MultivariateDifferentiableFunction;
24  import org.apache.commons.math3.optimization.DifferentiableMultivariateOptimizer;
25  import org.apache.commons.math3.optimization.GoalType;
26  import org.apache.commons.math3.optimization.ConvergenceChecker;
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 2.0
37   */
38  @Deprecated
39  public abstract class AbstractScalarDifferentiableOptimizer
40      extends BaseAbstractMultivariateOptimizer<DifferentiableMultivariateFunction>
41      implements DifferentiableMultivariateOptimizer {
42      /**
43       * Objective function gradient.
44       */
45      private MultivariateVectorFunction gradient;
46  
47      /**
48       * Simple constructor with default settings.
49       * The convergence check is set to a
50       * {@link org.apache.commons.math3.optimization.SimpleValueChecker
51       * SimpleValueChecker}.
52       * @deprecated See {@link org.apache.commons.math3.optimization.SimpleValueChecker#SimpleValueChecker()}
53       */
54      @Deprecated
55      protected AbstractScalarDifferentiableOptimizer() {}
56  
57      /**
58       * @param checker Convergence checker.
59       */
60      protected AbstractScalarDifferentiableOptimizer(ConvergenceChecker<PointValuePair> checker) {
61          super(checker);
62      }
63  
64      /**
65       * Compute the gradient vector.
66       *
67       * @param evaluationPoint Point at which the gradient must be evaluated.
68       * @return the gradient at the specified point.
69       * @throws org.apache.commons.math3.exception.TooManyEvaluationsException
70       * if the allowed number of evaluations is exceeded.
71       */
72      protected double[] computeObjectiveGradient(final double[] evaluationPoint) {
73          return gradient.value(evaluationPoint);
74      }
75  
76      /** {@inheritDoc} */
77      @Override
78      protected PointValuePair optimizeInternal(int maxEval,
79                                                final DifferentiableMultivariateFunction f,
80                                                final GoalType goalType,
81                                                final double[] startPoint) {
82          // Store optimization problem characteristics.
83          gradient = f.gradient();
84  
85          return super.optimizeInternal(maxEval, f, goalType, startPoint);
86      }
87  
88      /**
89       * Optimize an objective function.
90       *
91       * @param f Objective function.
92       * @param goalType Type of optimization goal: either
93       * {@link GoalType#MAXIMIZE} or {@link GoalType#MINIMIZE}.
94       * @param startPoint Start point for optimization.
95       * @param maxEval Maximum number of function evaluations.
96       * @return the point/value pair giving the optimal value for objective
97       * function.
98       * @throws org.apache.commons.math3.exception.DimensionMismatchException
99       * 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 }