View Javadoc
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  /*
19   * This is not the original file distributed by the Apache Software Foundation
20   * It has been modified by the Hipparchus project
21   */
22  package org.hipparchus.optim.univariate;
23  
24  import org.hipparchus.analysis.UnivariateFunction;
25  import org.hipparchus.exception.MathIllegalStateException;
26  import org.hipparchus.optim.BaseOptimizer;
27  import org.hipparchus.optim.ConvergenceChecker;
28  import org.hipparchus.optim.OptimizationData;
29  import org.hipparchus.optim.nonlinear.scalar.GoalType;
30  
31  /**
32   * Base class for a univariate scalar function optimizer.
33   *
34   */
35  public abstract class UnivariateOptimizer
36      extends BaseOptimizer<UnivariatePointValuePair> {
37      /** Objective function. */
38      private UnivariateFunction function;
39      /** Type of optimization. */
40      private GoalType goal;
41      /** Initial guess. */
42      private double start;
43      /** Lower bound. */
44      private double min;
45      /** Upper bound. */
46      private double max;
47  
48      /** Simple constructor.
49       * @param checker Convergence checker.
50       */
51      protected UnivariateOptimizer(ConvergenceChecker<UnivariatePointValuePair> checker) {
52          super(checker);
53      }
54  
55      /**
56       * {@inheritDoc}
57       *
58       * @param optData Optimization data. In addition to those documented in
59       * {@link BaseOptimizer#parseOptimizationData(OptimizationData[])
60       * BaseOptimizer}, this method will register the following data:
61       * <ul>
62       *  <li>{@link GoalType}</li>
63       *  <li>{@link SearchInterval}</li>
64       *  <li>{@link UnivariateObjectiveFunction}</li>
65       * </ul>
66       * @return {@inheritDoc}
67       * @throws MathIllegalStateException if the maximal number of
68       * evaluations is exceeded.
69       */
70      @Override
71      public UnivariatePointValuePair optimize(OptimizationData... optData)
72          throws MathIllegalStateException {
73          // Perform computation.
74          return super.optimize(optData);
75      }
76  
77      /** Get optimization type.
78       * @return the optimization type
79       */
80      public GoalType getGoalType() {
81          return goal;
82      }
83  
84      /**
85       * Scans the list of (required and optional) optimization data that
86       * characterize the problem.
87       *
88       * @param optData Optimization data.
89       * The following data will be looked for:
90       * <ul>
91       *  <li>{@link GoalType}</li>
92       *  <li>{@link SearchInterval}</li>
93       *  <li>{@link UnivariateObjectiveFunction}</li>
94       * </ul>
95       */
96      @Override
97      protected void parseOptimizationData(OptimizationData... optData) {
98          // Allow base class to register its own data.
99          super.parseOptimizationData(optData);
100 
101         // The existing values (as set by the previous call) are reused if
102         // not provided in the argument list.
103         for (OptimizationData data : optData) {
104             if (data instanceof SearchInterval) {
105                 final SearchInterval interval = (SearchInterval) data;
106                 min = interval.getMin();
107                 max = interval.getMax();
108                 start = interval.getStartValue();
109                 continue;
110             }
111             if (data instanceof UnivariateObjectiveFunction) {
112                 function = ((UnivariateObjectiveFunction) data).getObjectiveFunction();
113                 continue;
114             }
115             if (data instanceof GoalType) {
116                 goal = (GoalType) data;
117                 continue;
118             }
119         }
120     }
121 
122     /** Get initial guess.
123      * @return the initial guess
124      */
125     public double getStartValue() {
126         return start;
127     }
128     /** Get lower bounds.
129      * @return the lower bounds
130      */
131     public double getMin() {
132         return min;
133     }
134     /** Get upper bounds.
135      * @return the upper bounds
136      */
137     public double getMax() {
138         return max;
139     }
140 
141     /**
142      * Computes the objective function value.
143      * This method <em>must</em> be called by subclasses to enforce the
144      * evaluation counter limit.
145      *
146      * @param x Point at which the objective function must be evaluated.
147      * @return the objective function value at the specified point.
148      * @throws MathIllegalStateException if the maximal number of
149      * evaluations is exceeded.
150      */
151     protected double computeObjectiveValue(double x) {
152         super.incrementEvaluationCount();
153         return function.value(x);
154     }
155 }