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  
18  package org.apache.commons.math.optimization.univariate;
19  
20  import org.apache.commons.math.ConvergingAlgorithmImpl;
21  import org.apache.commons.math.FunctionEvaluationException;
22  import org.apache.commons.math.MathRuntimeException;
23  import org.apache.commons.math.MaxEvaluationsExceededException;
24  import org.apache.commons.math.analysis.UnivariateRealFunction;
25  import org.apache.commons.math.optimization.UnivariateRealOptimizer;
26  
27  /**
28   * Provide a default implementation for several functions useful to generic
29   * optimizers.
30   *  
31   * @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
32   * @since 2.0
33   */
34  public abstract class AbstractUnivariateRealOptimizer
35      extends ConvergingAlgorithmImpl implements UnivariateRealOptimizer {
36  
37      /** Indicates where a root has been computed. */
38      protected boolean resultComputed;
39  
40      /** The last computed root. */
41      protected double result;
42  
43      /** Value of the function at the last computed result. */
44      protected double functionValue;
45  
46      /** Maximal number of evaluations allowed. */
47      private int maxEvaluations;
48  
49      /** Number of evaluations already performed. */
50      private int evaluations;
51  
52      /**
53       * Construct a solver with given iteration count and accuracy.
54       * FunctionEvaluationExceptionFunctionEvaluationException
55       * @param defaultAbsoluteAccuracy maximum absolute error
56       * @param defaultMaximalIterationCount maximum number of iterations
57       * @throws IllegalArgumentException if f is null or the 
58       * defaultAbsoluteAccuracy is not valid
59       */
60      protected AbstractUnivariateRealOptimizer(final int defaultMaximalIterationCount,
61                                                final double defaultAbsoluteAccuracy) {
62          super(defaultMaximalIterationCount, defaultAbsoluteAccuracy);
63          resultComputed = false;
64          setMaxEvaluations(Integer.MAX_VALUE);
65      }
66  
67      /** Check if a result has been computed.
68       * @exception IllegalStateException if no result has been computed
69       */
70      protected void checkResultComputed() throws IllegalStateException {
71          if (!resultComputed) {
72              throw MathRuntimeException.createIllegalStateException("no result available");
73          }
74      }
75  
76      /** {@inheritDoc} */
77      public double getResult() {
78          checkResultComputed();
79          return result;
80      }
81  
82      /** {@inheritDoc} */
83      public double getFunctionValue() {
84          checkResultComputed();
85          return functionValue;
86      }
87  
88      /**
89       * Convenience function for implementations.
90       * 
91       * @param x the result to set
92       * @param fx the result to set
93       * @param iterationCount the iteration count to set
94       */
95      protected final void setResult(final double x, final double fx,
96                                     final int iterationCount) {
97          this.result         = x;
98          this.functionValue  = fx;
99          this.iterationCount = iterationCount;
100         this.resultComputed = true;
101     }
102 
103     /**
104      * Convenience function for implementations.
105      */
106     protected final void clearResult() {
107         this.resultComputed = false;
108     }
109 
110     /** {@inheritDoc} */
111     public void setMaxEvaluations(int maxEvaluations) {
112         this.maxEvaluations = maxEvaluations;
113     }
114 
115     /** {@inheritDoc} */
116     public int getMaxEvaluations() {
117         return maxEvaluations;
118     }
119 
120     /** {@inheritDoc} */
121     public int getEvaluations() {
122         return evaluations;
123     }
124 
125     /** 
126      * Compute the objective function value.
127      * @param f objective function
128      * @param point point at which the objective function must be evaluated
129      * @return objective function value at specified point
130      * @exception FunctionEvaluationException if the function cannot be evaluated
131      * or the maximal number of iterations is exceeded
132      */
133     protected double computeObjectiveValue(final UnivariateRealFunction f,
134                                            final double point)
135         throws FunctionEvaluationException {
136         if (++evaluations > maxEvaluations) {
137             throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
138                                                   point);
139         }
140         return f.value(point);
141     }
142 
143 }