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.  *      https://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. /*
  18.  * This is not the original file distributed by the Apache Software Foundation
  19.  * It has been modified by the Hipparchus project
  20.  */
  21. package org.hipparchus.optim.univariate;

  22. import org.hipparchus.analysis.UnivariateFunction;
  23. import org.hipparchus.exception.MathIllegalStateException;
  24. import org.hipparchus.optim.BaseOptimizer;
  25. import org.hipparchus.optim.ConvergenceChecker;
  26. import org.hipparchus.optim.OptimizationData;
  27. import org.hipparchus.optim.nonlinear.scalar.GoalType;

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

  44.     /** Simple constructor.
  45.      * @param checker Convergence checker.
  46.      */
  47.     protected UnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
  48.         super(checker);
  49.     }

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

  71.     /** Get optimization type.
  72.      * @return the optimization type
  73.      */
  74.     public GoalType getGoalType() {
  75.         return goal;
  76.     }

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

  93.         // The existing values (as set by the previous call) are reused if
  94.         // not provided in the argument list.
  95.         for (OptimizationData data : optData) {
  96.             if (data instanceof SearchInterval) {
  97.                 final SearchInterval interval = (SearchInterval) data;
  98.                 min = interval.getMin();
  99.                 max = interval.getMax();
  100.                 start = interval.getStartValue();
  101.                 continue;
  102.             }
  103.             if (data instanceof UnivariateObjectiveFunction) {
  104.                 function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
  105.                 continue;
  106.             }
  107.             if (data instanceof GoalType) {
  108.                 goal = (GoalType) data;
  109.                 continue;
  110.             }
  111.         }
  112.     }

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

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