UnivariateOptimizer.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.math4.legacy.optim.univariate;

  18. import org.apache.commons.math4.legacy.analysis.UnivariateFunction;
  19. import org.apache.commons.math4.legacy.exception.TooManyEvaluationsException;
  20. import org.apache.commons.math4.legacy.optim.BaseOptimizer;
  21. import org.apache.commons.math4.legacy.optim.ConvergenceChecker;
  22. import org.apache.commons.math4.legacy.optim.OptimizationData;
  23. import org.apache.commons.math4.legacy.optim.nonlinear.scalar.GoalType;

  24. /**
  25.  * Base class for a univariate scalar function optimizer.
  26.  *
  27.  * @since 3.1
  28.  */
  29. public abstract class UnivariateOptimizer
  30.     extends BaseOptimizer<UnivariatePointValuePair> {
  31.     /** Objective function. */
  32.     private UnivariateFunction function;
  33.     /** Type of optimization. */
  34.     private GoalType goal;
  35.     /** Initial guess. */
  36.     private double start;
  37.     /** Lower bound. */
  38.     private double min;
  39.     /** Upper bound. */
  40.     private double max;

  41.     /**
  42.      * @param checker Convergence checker.
  43.      */
  44.     protected UnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
  45.         super(checker);
  46.     }

  47.     /**
  48.      * {@inheritDoc}
  49.      *
  50.      * @param optData Optimization data. In addition to those documented in
  51.      * {@link BaseOptimizer#parseOptimizationData(OptimizationData[])
  52.      * BaseOptimizer}, this method will register the following data:
  53.      * <ul>
  54.      *  <li>{@link GoalType}</li>
  55.      *  <li>{@link SearchInterval}</li>
  56.      *  <li>{@link UnivariateObjectiveFunction}</li>
  57.      * </ul>
  58.      * @return {@inheritDoc}
  59.      * @throws TooManyEvaluationsException if the maximal number of
  60.      * evaluations is exceeded.
  61.      */
  62.     @Override
  63.     public UnivariatePointValuePair optimize(OptimizationData... optData)
  64.         throws TooManyEvaluationsException {
  65.         // Perform computation.
  66.         return super.optimize(optData);
  67.     }

  68.     /**
  69.      * @return the optimization type.
  70.      */
  71.     public GoalType getGoalType() {
  72.         return goal;
  73.     }

  74.     /**
  75.      * Scans the list of (required and optional) optimization data that
  76.      * characterize the problem.
  77.      *
  78.      * @param optData Optimization data.
  79.      * The following data will be looked for:
  80.      * <ul>
  81.      *  <li>{@link GoalType}</li>
  82.      *  <li>{@link SearchInterval}</li>
  83.      *  <li>{@link UnivariateObjectiveFunction}</li>
  84.      * </ul>
  85.      */
  86.     @Override
  87.     protected void parseOptimizationData(OptimizationData... optData) {
  88.         // Allow base class to register its own data.
  89.         super.parseOptimizationData(optData);

  90.         // The existing values (as set by the previous call) are reused if
  91.         // not provided in the argument list.
  92.         for (OptimizationData data : optData) {
  93.             if (data instanceof SearchInterval) {
  94.                 final SearchInterval interval = (SearchInterval) data;
  95.                 min = interval.getMin();
  96.                 max = interval.getMax();
  97.                 start = interval.getStartValue();
  98.                 continue;
  99.             }
  100.             if (data instanceof UnivariateObjectiveFunction) {
  101.                 final UnivariateFunction delegate = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
  102.                 function = new UnivariateFunction() {
  103.                         @Override
  104.                         public double value(double point) {
  105.                             incrementEvaluationCount();
  106.                             return delegate.value(point);
  107.                         }
  108.                     };
  109.                 continue;
  110.             }
  111.             if (data instanceof GoalType) {
  112.                 goal = (GoalType) data;
  113.                 continue;
  114.             }
  115.         }
  116.     }

  117.     /**
  118.      * @return the initial guess.
  119.      */
  120.     public double getStartValue() {
  121.         return start;
  122.     }
  123.     /**
  124.      * @return the lower bounds.
  125.      */
  126.     public double getMin() {
  127.         return min;
  128.     }
  129.     /**
  130.      * @return the upper bounds.
  131.      */
  132.     public double getMax() {
  133.         return max;
  134.     }

  135.     /**
  136.      * @return a wrapper that delegates to the user-supplied function,
  137.      * and counts the number of evaluations.
  138.      */
  139.     protected UnivariateFunction getObjectiveFunction() {
  140.         return function;
  141.     }

  142.     /**
  143.      * Computes the objective function value.
  144.      * This method <em>must</em> be called by subclasses to enforce the
  145.      * evaluation counter limit.
  146.      *
  147.      * @param x Point at which the objective function must be evaluated.
  148.      * @return the objective function value at the specified point.
  149.      * @throws TooManyEvaluationsException if the maximal number of
  150.      * evaluations is exceeded.
  151.      *
  152.      * @deprecated Use {@link #getObjectiveFunction()} instead.
  153.      */
  154.     @Deprecated
  155.     protected double computeObjectiveValue(double x) {
  156.         return function.value(x);
  157.     }
  158. }