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
018package org.apache.commons.math3.optimization.univariate;
019
020import org.apache.commons.math3.util.Incrementor;
021import org.apache.commons.math3.exception.MaxCountExceededException;
022import org.apache.commons.math3.exception.TooManyEvaluationsException;
023import org.apache.commons.math3.exception.NullArgumentException;
024import org.apache.commons.math3.analysis.UnivariateFunction;
025import org.apache.commons.math3.optimization.GoalType;
026import org.apache.commons.math3.optimization.ConvergenceChecker;
027
028/**
029 * Provide a default implementation for several functions useful to generic
030 * optimizers.
031 *
032 * @deprecated As of 3.1 (to be removed in 4.0).
033 * @since 2.0
034 */
035@Deprecated
036public abstract class BaseAbstractUnivariateOptimizer
037    implements UnivariateOptimizer {
038    /** Convergence checker. */
039    private final ConvergenceChecker<UnivariatePointValuePair> checker;
040    /** Evaluations counter. */
041    private final Incrementor evaluations = new Incrementor();
042    /** Optimization type */
043    private GoalType goal;
044    /** Lower end of search interval. */
045    private double searchMin;
046    /** Higher end of search interval. */
047    private double searchMax;
048    /** Initial guess . */
049    private double searchStart;
050    /** Function to optimize. */
051    private UnivariateFunction function;
052
053    /**
054     * @param checker Convergence checking procedure.
055     */
056    protected BaseAbstractUnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
057        this.checker = checker;
058    }
059
060    /** {@inheritDoc} */
061    public int getMaxEvaluations() {
062        return evaluations.getMaximalCount();
063    }
064
065    /** {@inheritDoc} */
066    public int getEvaluations() {
067        return evaluations.getCount();
068    }
069
070    /**
071     * @return the optimization type.
072     */
073    public GoalType getGoalType() {
074        return goal;
075    }
076    /**
077     * @return the lower end of the search interval.
078     */
079    public double getMin() {
080        return searchMin;
081    }
082    /**
083     * @return the higher end of the search interval.
084     */
085    public double getMax() {
086        return searchMax;
087    }
088    /**
089     * @return the initial guess.
090     */
091    public double getStartValue() {
092        return searchStart;
093    }
094
095    /**
096     * Compute the objective function value.
097     *
098     * @param point Point at which the objective function must be evaluated.
099     * @return the objective function value at specified point.
100     * @throws TooManyEvaluationsException if the maximal number of evaluations
101     * is exceeded.
102     */
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} */
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} */
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     */
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     */
161    protected abstract UnivariatePointValuePair doOptimize();
162}