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.math3.optim.univariate;

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

  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.                 function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
  102.                 continue;
  103.             }
  104.             if (data instanceof GoalType) {
  105.                 goal = (GoalType) data;
  106.                 continue;
  107.             }
  108.         }
  109.     }

  110.     /**
  111.      * @return the initial guess.
  112.      */
  113.     public double getStartValue() {
  114.         return start;
  115.     }
  116.     /**
  117.      * @return the lower bounds.
  118.      */
  119.     public double getMin() {
  120.         return min;
  121.     }
  122.     /**
  123.      * @return the upper bounds.
  124.      */
  125.     public double getMax() {
  126.         return max;
  127.     }

  128.     /**
  129.      * Computes the objective function value.
  130.      * This method <em>must</em> be called by subclasses to enforce the
  131.      * evaluation counter limit.
  132.      *
  133.      * @param x Point at which the objective function must be evaluated.
  134.      * @return the objective function value at the specified point.
  135.      * @throws TooManyEvaluationsException if the maximal number of
  136.      * evaluations is exceeded.
  137.      */
  138.     protected double computeObjectiveValue(double x) {
  139.         super.incrementEvaluationCount();
  140.         return function.value(x);
  141.     }
  142. }