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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
package org.apache.commons.math.optimization.univariate;
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;
28
* Provide a default implementation for several functions useful to generic
31
* @version $Revision: 799857 $ $Date: 2009-08-01 09:07:12 -0400 (Sat, 01 Aug 2009) $
34
public abstract class AbstractUnivariateRealOptimizer
35
extends ConvergingAlgorithmImpl implements UnivariateRealOptimizer {
37
/** Indicates where a root has been computed. */
38
protected boolean resultComputed;
40
/** The last computed root. */
41
protected double result;
43
/** Value of the function at the last computed result. */
44
protected double functionValue;
46
/** Maximal number of evaluations allowed. */
47
private int maxEvaluations;
49
/** Number of evaluations already performed. */
50
private int evaluations;
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
60
protected AbstractUnivariateRealOptimizer(final int defaultMaximalIterationCount,
61
final double defaultAbsoluteAccuracy) {
62
super(defaultMaximalIterationCount, defaultAbsoluteAccuracy);
63
resultComputed = false;
64
setMaxEvaluations(Integer.MAX_VALUE);
67
/** Check if a result has been computed.
68
* @exception IllegalStateException if no result has been computed
70
protected void checkResultComputed() throws IllegalStateException {
71
if (!resultComputed) {
72
throw MathRuntimeException.createIllegalStateException("no result available");
77
public double getResult() {
78
checkResultComputed();
83
public double getFunctionValue() {
84
checkResultComputed();
89
* Convenience function for implementations.
91
* @param x the result to set
92
* @param fx the result to set
93
* @param iterationCount the iteration count to set
95
protected final void setResult(final double x, final double fx,
96
final int iterationCount) {
98
this.functionValue = fx;
99
this.iterationCount = iterationCount;
100
this.resultComputed = true;
104
* Convenience function for implementations.
106
protected final void clearResult() {
107
this.resultComputed = false;
111
public void setMaxEvaluations(int maxEvaluations) {
112
this.maxEvaluations = maxEvaluations;
116
public int getMaxEvaluations() {
117
return maxEvaluations;
121
public int getEvaluations() {
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
133
protected double computeObjectiveValue(final UnivariateRealFunction f,
135
throws FunctionEvaluationException {
136
if (++evaluations > maxEvaluations) {
137
throw new FunctionEvaluationException(new MaxEvaluationsExceededException(maxEvaluations),
140
return f.value(point);