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.math3.optimization.univariate;
019    
020    import org.apache.commons.math3.util.Incrementor;
021    import org.apache.commons.math3.exception.MaxCountExceededException;
022    import org.apache.commons.math3.exception.TooManyEvaluationsException;
023    import org.apache.commons.math3.exception.NullArgumentException;
024    import org.apache.commons.math3.analysis.UnivariateFunction;
025    import org.apache.commons.math3.optimization.GoalType;
026    import org.apache.commons.math3.optimization.ConvergenceChecker;
027    
028    /**
029     * Provide a default implementation for several functions useful to generic
030     * optimizers.
031     *
032     * @version $Id: BaseAbstractUnivariateOptimizer.java 1422230 2012-12-15 12:11:13Z erans $
033     * @deprecated As of 3.1 (to be removed in 4.0).
034     * @since 2.0
035     */
036    @Deprecated
037    public abstract class BaseAbstractUnivariateOptimizer
038        implements UnivariateOptimizer {
039        /** Convergence checker. */
040        private final ConvergenceChecker<UnivariatePointValuePair> checker;
041        /** Evaluations counter. */
042        private final Incrementor evaluations = new Incrementor();
043        /** Optimization type */
044        private GoalType goal;
045        /** Lower end of search interval. */
046        private double searchMin;
047        /** Higher end of search interval. */
048        private double searchMax;
049        /** Initial guess . */
050        private double searchStart;
051        /** Function to optimize. */
052        private UnivariateFunction function;
053    
054        /**
055         * @param checker Convergence checking procedure.
056         */
057        protected BaseAbstractUnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
058            this.checker = checker;
059        }
060    
061        /** {@inheritDoc} */
062        public int getMaxEvaluations() {
063            return evaluations.getMaximalCount();
064        }
065    
066        /** {@inheritDoc} */
067        public int getEvaluations() {
068            return evaluations.getCount();
069        }
070    
071        /**
072         * @return the optimization type.
073         */
074        public GoalType getGoalType() {
075            return goal;
076        }
077        /**
078         * @return the lower end of the search interval.
079         */
080        public double getMin() {
081            return searchMin;
082        }
083        /**
084         * @return the higher end of the search interval.
085         */
086        public double getMax() {
087            return searchMax;
088        }
089        /**
090         * @return the initial guess.
091         */
092        public double getStartValue() {
093            return searchStart;
094        }
095    
096        /**
097         * Compute the objective function value.
098         *
099         * @param point Point at which the objective function must be evaluated.
100         * @return the objective function value at specified point.
101         * @throws TooManyEvaluationsException if the maximal number of evaluations
102         * is exceeded.
103         */
104        protected double computeObjectiveValue(double point) {
105            try {
106                evaluations.incrementCount();
107            } catch (MaxCountExceededException e) {
108                throw new TooManyEvaluationsException(e.getMax());
109            }
110            return function.value(point);
111        }
112    
113        /** {@inheritDoc} */
114        public UnivariatePointValuePair optimize(int maxEval, UnivariateFunction f,
115                                                 GoalType goalType,
116                                                 double min, double max,
117                                                 double startValue) {
118            // Checks.
119            if (f == null) {
120                throw new NullArgumentException();
121            }
122            if (goalType == null) {
123                throw new NullArgumentException();
124            }
125    
126            // Reset.
127            searchMin = min;
128            searchMax = max;
129            searchStart = startValue;
130            goal = goalType;
131            function = f;
132            evaluations.setMaximalCount(maxEval);
133            evaluations.resetCount();
134    
135            // Perform computation.
136            return doOptimize();
137        }
138    
139        /** {@inheritDoc} */
140        public UnivariatePointValuePair optimize(int maxEval,
141                                                 UnivariateFunction f,
142                                                 GoalType goalType,
143                                                 double min, double max){
144            return optimize(maxEval, f, goalType, min, max, min + 0.5 * (max - min));
145        }
146    
147        /**
148         * {@inheritDoc}
149         */
150        public ConvergenceChecker<UnivariatePointValuePair> getConvergenceChecker() {
151            return checker;
152        }
153    
154        /**
155         * Method for implementing actual optimization algorithms in derived
156         * classes.
157         *
158         * @return the optimum and its corresponding function value.
159         * @throws TooManyEvaluationsException if the maximal number of evaluations
160         * is exceeded.
161         */
162        protected abstract UnivariatePointValuePair doOptimize();
163    }