BaseAbstractUnivariateOptimizer.java

  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. package org.apache.commons.math3.optimization.univariate;

  18. import org.apache.commons.math3.util.Incrementor;
  19. import org.apache.commons.math3.exception.MaxCountExceededException;
  20. import org.apache.commons.math3.exception.TooManyEvaluationsException;
  21. import org.apache.commons.math3.exception.NullArgumentException;
  22. import org.apache.commons.math3.analysis.UnivariateFunction;
  23. import org.apache.commons.math3.optimization.GoalType;
  24. import org.apache.commons.math3.optimization.ConvergenceChecker;

  25. /**
  26.  * Provide a default implementation for several functions useful to generic
  27.  * optimizers.
  28.  *
  29.  * @deprecated As of 3.1 (to be removed in 4.0).
  30.  * @since 2.0
  31.  */
  32. @Deprecated
  33. public abstract class BaseAbstractUnivariateOptimizer
  34.     implements UnivariateOptimizer {
  35.     /** Convergence checker. */
  36.     private final ConvergenceChecker<UnivariatePointValuePair> checker;
  37.     /** Evaluations counter. */
  38.     private final Incrementor evaluations = new Incrementor();
  39.     /** Optimization type */
  40.     private GoalType goal;
  41.     /** Lower end of search interval. */
  42.     private double searchMin;
  43.     /** Higher end of search interval. */
  44.     private double searchMax;
  45.     /** Initial guess . */
  46.     private double searchStart;
  47.     /** Function to optimize. */
  48.     private UnivariateFunction function;

  49.     /**
  50.      * @param checker Convergence checking procedure.
  51.      */
  52.     protected BaseAbstractUnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
  53.         this.checker = checker;
  54.     }

  55.     /** {@inheritDoc} */
  56.     public int getMaxEvaluations() {
  57.         return evaluations.getMaximalCount();
  58.     }

  59.     /** {@inheritDoc} */
  60.     public int getEvaluations() {
  61.         return evaluations.getCount();
  62.     }

  63.     /**
  64.      * @return the optimization type.
  65.      */
  66.     public GoalType getGoalType() {
  67.         return goal;
  68.     }
  69.     /**
  70.      * @return the lower end of the search interval.
  71.      */
  72.     public double getMin() {
  73.         return searchMin;
  74.     }
  75.     /**
  76.      * @return the higher end of the search interval.
  77.      */
  78.     public double getMax() {
  79.         return searchMax;
  80.     }
  81.     /**
  82.      * @return the initial guess.
  83.      */
  84.     public double getStartValue() {
  85.         return searchStart;
  86.     }

  87.     /**
  88.      * Compute the objective function value.
  89.      *
  90.      * @param point Point at which the objective function must be evaluated.
  91.      * @return the objective function value at specified point.
  92.      * @throws TooManyEvaluationsException if the maximal number of evaluations
  93.      * is exceeded.
  94.      */
  95.     protected double computeObjectiveValue(double point) {
  96.         try {
  97.             evaluations.incrementCount();
  98.         } catch (MaxCountExceededException e) {
  99.             throw new TooManyEvaluationsException(e.getMax());
  100.         }
  101.         return function.value(point);
  102.     }

  103.     /** {@inheritDoc} */
  104.     public UnivariatePointValuePair optimize(int maxEval, UnivariateFunction f,
  105.                                              GoalType goalType,
  106.                                              double min, double max,
  107.                                              double startValue) {
  108.         // Checks.
  109.         if (f == null) {
  110.             throw new NullArgumentException();
  111.         }
  112.         if (goalType == null) {
  113.             throw new NullArgumentException();
  114.         }

  115.         // Reset.
  116.         searchMin = min;
  117.         searchMax = max;
  118.         searchStart = startValue;
  119.         goal = goalType;
  120.         function = f;
  121.         evaluations.setMaximalCount(maxEval);
  122.         evaluations.resetCount();

  123.         // Perform computation.
  124.         return doOptimize();
  125.     }

  126.     /** {@inheritDoc} */
  127.     public UnivariatePointValuePair optimize(int maxEval,
  128.                                              UnivariateFunction f,
  129.                                              GoalType goalType,
  130.                                              double min, double max){
  131.         return optimize(maxEval, f, goalType, min, max, min + 0.5 * (max - min));
  132.     }

  133.     /**
  134.      * {@inheritDoc}
  135.      */
  136.     public ConvergenceChecker<UnivariatePointValuePair> getConvergenceChecker() {
  137.         return checker;
  138.     }

  139.     /**
  140.      * Method for implementing actual optimization algorithms in derived
  141.      * classes.
  142.      *
  143.      * @return the optimum and its corresponding function value.
  144.      * @throws TooManyEvaluationsException if the maximal number of evaluations
  145.      * is exceeded.
  146.      */
  147.     protected abstract UnivariatePointValuePair doOptimize();
  148. }