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.univariate;
19 
20 import org.apache.commons.math3.util.Incrementor;
21 import org.apache.commons.math3.exception.MaxCountExceededException;
22 import org.apache.commons.math3.exception.TooManyEvaluationsException;
23 import org.apache.commons.math3.exception.NullArgumentException;
24 import org.apache.commons.math3.analysis.UnivariateFunction;
25 import org.apache.commons.math3.optimization.GoalType;
26 import org.apache.commons.math3.optimization.ConvergenceChecker;
27 
28 /**
29  * Provide a default implementation for several functions useful to generic
30  * optimizers.
31  *
32  * @deprecated As of 3.1 (to be removed in 4.0).
33  * @since 2.0
34  */
35 @Deprecated
36 public abstract class BaseAbstractUnivariateOptimizer
37     implements UnivariateOptimizer {
38     /** Convergence checker. */
39     private final ConvergenceChecker<UnivariatePointValuePair> checker;
40     /** Evaluations counter. */
41     private final Incrementor evaluations = new Incrementor();
42     /** Optimization type */
43     private GoalType goal;
44     /** Lower end of search interval. */
45     private double searchMin;
46     /** Higher end of search interval. */
47     private double searchMax;
48     /** Initial guess . */
49     private double searchStart;
50     /** Function to optimize. */
51     private UnivariateFunction function;
52 
53     /**
54      * @param checker Convergence checking procedure.
55      */
BaseAbstractUnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker)56     protected BaseAbstractUnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
57         this.checker = checker;
58     }
59 
60     /** {@inheritDoc} */
getMaxEvaluations()61     public int getMaxEvaluations() {
62         return evaluations.getMaximalCount();
63     }
64 
65     /** {@inheritDoc} */
getEvaluations()66     public int getEvaluations() {
67         return evaluations.getCount();
68     }
69 
70     /**
71      * @return the optimization type.
72      */
getGoalType()73     public GoalType getGoalType() {
74         return goal;
75     }
76     /**
77      * @return the lower end of the search interval.
78      */
getMin()79     public double getMin() {
80         return searchMin;
81     }
82     /**
83      * @return the higher end of the search interval.
84      */
getMax()85     public double getMax() {
86         return searchMax;
87     }
88     /**
89      * @return the initial guess.
90      */
getStartValue()91     public double getStartValue() {
92         return searchStart;
93     }
94 
95     /**
96      * Compute the objective function value.
97      *
98      * @param point Point at which the objective function must be evaluated.
99      * @return the objective function value at specified point.
100      * @throws TooManyEvaluationsException if the maximal number of evaluations
101      * is exceeded.
102      */
computeObjectiveValue(double point)103     protected double computeObjectiveValue(double point) {
104         try {
105             evaluations.incrementCount();
106         } catch (MaxCountExceededException e) {
107             throw new TooManyEvaluationsException(e.getMax());
108         }
109         return function.value(point);
110     }
111 
112     /** {@inheritDoc} */
optimize(int maxEval, UnivariateFunction f, GoalType goalType, double min, double max, double startValue)113     public UnivariatePointValuePair optimize(int maxEval, UnivariateFunction f,
114                                              GoalType goalType,
115                                              double min, double max,
116                                              double startValue) {
117         // Checks.
118         if (f == null) {
119             throw new NullArgumentException();
120         }
121         if (goalType == null) {
122             throw new NullArgumentException();
123         }
124 
125         // Reset.
126         searchMin = min;
127         searchMax = max;
128         searchStart = startValue;
129         goal = goalType;
130         function = f;
131         evaluations.setMaximalCount(maxEval);
132         evaluations.resetCount();
133 
134         // Perform computation.
135         return doOptimize();
136     }
137 
138     /** {@inheritDoc} */
optimize(int maxEval, UnivariateFunction f, GoalType goalType, double min, double max)139     public UnivariatePointValuePair optimize(int maxEval,
140                                              UnivariateFunction f,
141                                              GoalType goalType,
142                                              double min, double max){
143         return optimize(maxEval, f, goalType, min, max, min + 0.5 * (max - min));
144     }
145 
146     /**
147      * {@inheritDoc}
148      */
getConvergenceChecker()149     public ConvergenceChecker<UnivariatePointValuePair> getConvergenceChecker() {
150         return checker;
151     }
152 
153     /**
154      * Method for implementing actual optimization algorithms in derived
155      * classes.
156      *
157      * @return the optimum and its corresponding function value.
158      * @throws TooManyEvaluationsException if the maximal number of evaluations
159      * is exceeded.
160      */
doOptimize()161     protected abstract UnivariatePointValuePair doOptimize();
162 }
163