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