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    *      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.nonlinear.scalar;
18  
19  import org.apache.commons.math3.analysis.MultivariateFunction;
20  import org.apache.commons.math3.optim.BaseMultivariateOptimizer;
21  import org.apache.commons.math3.optim.OptimizationData;
22  import org.apache.commons.math3.optim.ConvergenceChecker;
23  import org.apache.commons.math3.optim.PointValuePair;
24  import org.apache.commons.math3.exception.TooManyEvaluationsException;
25  
26  /**
27   * Base class for a multivariate scalar function optimizer.
28   *
29   * @since 3.1
30   */
31  public abstract class MultivariateOptimizer
32      extends BaseMultivariateOptimizer<PointValuePair> {
33      /** Objective function. */
34      private MultivariateFunction function;
35      /** Type of optimization. */
36      private GoalType goal;
37  
38      /**
39       * @param checker Convergence checker.
40       */
41      protected MultivariateOptimizer(ConvergenceChecker<PointValuePair> checker) {
42          super(checker);
43      }
44  
45      /**
46       * {@inheritDoc}
47       *
48       * @param optData Optimization data. In addition to those documented in
49       * {@link BaseMultivariateOptimizer#parseOptimizationData(OptimizationData[])
50       * BaseMultivariateOptimizer}, this method will register the following data:
51       * <ul>
52       *  <li>{@link ObjectiveFunction}</li>
53       *  <li>{@link GoalType}</li>
54       * </ul>
55       * @return {@inheritDoc}
56       * @throws TooManyEvaluationsException if the maximal number of
57       * evaluations is exceeded.
58       */
59      @Override
60      public PointValuePair optimize(OptimizationData... optData)
61          throws TooManyEvaluationsException {
62          // Set up base class and perform computation.
63          return super.optimize(optData);
64      }
65  
66      /**
67       * Scans the list of (required and optional) optimization data that
68       * characterize the problem.
69       *
70       * @param optData Optimization data.
71       * The following data will be looked for:
72       * <ul>
73       *  <li>{@link ObjectiveFunction}</li>
74       *  <li>{@link GoalType}</li>
75       * </ul>
76       */
77      @Override
78      protected void parseOptimizationData(OptimizationData... optData) {
79          // Allow base class to register its own data.
80          super.parseOptimizationData(optData);
81  
82          // The existing values (as set by the previous call) are reused if
83          // not provided in the argument list.
84          for (OptimizationData data : optData) {
85              if (data instanceof GoalType) {
86                  goal = (GoalType) data;
87                  continue;
88              }
89              if (data instanceof ObjectiveFunction) {
90                  function = ((ObjectiveFunction) data).getObjectiveFunction();
91                  continue;
92              }
93          }
94      }
95  
96      /**
97       * @return the optimization type.
98       */
99      public GoalType getGoalType() {
100         return goal;
101     }
102 
103     /**
104      * Computes the objective function value.
105      * This method <em>must</em> be called by subclasses to enforce the
106      * evaluation counter limit.
107      *
108      * @param params Point at which the objective function must be evaluated.
109      * @return the objective function value at the specified point.
110      * @throws TooManyEvaluationsException if the maximal number of
111      * evaluations is exceeded.
112      */
113     public double computeObjectiveValue(double[] params) {
114         super.incrementEvaluationCount();
115         return function.value(params);
116     }
117 }