29
<section name="13 Optimization">
30
<subsection name="13.1 Overview" href="overview">
32
The optimization package provides simplex-based direct search optimization algorithms.
35
The aim of this package is similar to the aim of the estimation package, but the
36
algorithms are entirely differents as:
29
<section name="12 Optimization">
30
<subsection name="12.1 Overview" href="overview">
32
The optimization package provides algorithms to optimize (i.e. either minimize
33
or maximize) some objective or cost function. The package is split in several
34
sub-packages dedicated to different kind of functions or algorithms.
39
they do not need the partial derivatives of the measurements
40
with respect to the free parameters
43
they do not rely on residuals-based quadratic cost functions but
44
handle any cost functions, including non-continuous ones!
36
<li>the univariate package handles univariate scalar functions,</li>
37
<li>the linear package handles multivariate vector linear functions
38
with linear constraints,</li>
39
<li>the direct package handles multivariate scalar functions
40
using direct search methods (i.e. not using derivatives),</li>
41
<li>the general package handles multivariate scalar or vector functions
42
using derivatives.</li>
43
<li>the fitting package handles curve fitting by univariate real functions</li>
49
<subsection name="13.2 Direct Methods" href="direct">
47
The top level optimization package provides common interfaces for the optimization
48
algorithms provided in sub-packages. The main interfaces defines defines optimizers
49
and convergence checkers. The functions that are optimized by the algorithms provided
50
by this package and its sub-packages are a subset of the one defined in the
51
<code>analysis</code> package, namely the real and vector valued functions. These
52
functions are called objective function here. When the goal is to minimize, the
53
functions are often called cost function, this name is not used in this package.
56
The type of goal, i.e. minimization or maximization, is defined by the enumerated
57
<a href="../apidocs/org/apache/commons/math/optimization/GoalType.html">
58
GoalType</a> which has only two values: <code>MAXIMIZE</code> and <code>MINIMIZE</code>.
61
Optimizers are the algorithms that will either minimize or maximize, the objective
62
function by changing its input variables set until an optimal set is found. There
63
are only four interfaces defining the common behavior of optimizers, one for each
64
supported type of objective function:
66
<li><a href="../apidocs/org/apache/commons/math/optimization/UnivariateRealOptimizer.html">
67
UnivariateRealOptimizer</a> for <a
68
href="../apidocs/org/apache/commons/math/analysis/UnivariateRealFunction.html">
69
univariate real functions</a></li>
70
<li><a href="../apidocs/org/apache/commons/math/optimization/MultivariateRealOptimizer.html">
71
MultivariateRealOptimizer</a> for <a
72
href="../apidocs/org/apache/commons/math/analysis/MultivariateRealFunction.html">
73
multivariate real functions</a></li>
74
<li><a href="../apidocs/org/apache/commons/math/optimization/DifferentiableMultivariateRealOptimizer.html">
75
DifferentiableMultivariateRealOptimizer</a> for <a
76
href="../apidocs/org/apache/commons/math/analysis/DifferentiableMultivariateRealFunction.html">
77
differentiable multivariate real functions</a></li>
78
<li><a href="../apidocs/org/apache/commons/math/optimization/DifferentiableMultivariateVectorialOptimizer.html">
79
DifferentiableMultivariateVectorialOptimizer</a> for <a
80
href="../apidocs/org/apache/commons/math/analysis/DifferentiableMultivariateVectorialFunction.html">
81
differentiable multivariate vectorial functions</a></li>
86
Despite there are only four types of supported optimizers, it is possible to optimize
88
href="../apidocs/org/apache/commons/math/analysis/MultivariateVectorialFunction.html">
89
non-differentiable multivariate vectorial function</a> by converting it to a <a
90
href="../apidocs/org/apache/commons/math/analysis/MultivariateRealFunction.html">
91
non-differentiable multivariate real function</a> thanks to the <a
92
href="../apidocs/org/apache/commons/math/optimization/LeastSquaresConverter.html">
93
LeastSquaresConverter</a> helper class. The transformed function can be optimized using
94
any implementation of the <a
95
href="../apidocs/org/apache/commons/math/optimization/MultivariateRealOptimizer.html">
96
MultivariateRealOptimizer</a> interface.
100
For each of the four types of supported optimizers, there is a special implementation
101
which wraps a classical optimizer in order to add it a multi-start feature. This feature
102
call the underlying optimizer several times in sequence with different starting points
103
and returns the best optimum found or all optima if desired. This is a classical way to
104
prevent being trapped into a local extremum when looking for a global one.
107
<subsection name="12.2 Univariate Functions" href="univariate">
109
A <a href="../apidocs/org/apache/commons/math/optimization/UnivariateRealOptimizer.html">
110
UnivariateRealOptimizer</a> is used to find the minimal values of a univariate real-valued
111
function <code>f</code>.
114
These algorithms usage is very similar to root-finding algorithms usage explained
115
in the analysis package. The main difference is that the <code>solve</code> methods in root
116
finding algorithms is replaced by <code>optimize</code> methods.
119
<subsection name="12.3 Linear Programming" href="linear">
121
This package provides an implementation of George Dantzig's simplex algorithm
122
for solving linear optimization problems with linear equality and inequality
126
<subsection name="12.4 Direct Methods" href="direct">
51
128
Direct search methods only use cost function values, they don't
52
129
need derivatives and don't either try to compute approximation of
77
154
already provided by the <code>minimizes</code> method).
80
The package provides two solvers. The first one is the classical
81
<a href="../apidocs/org/apache/commons/math/optimization/NelderMead.html">
157
The <code>direct</code> package provides two solvers. The first one is the classical
158
<a href="../apidocs/org/apache/commons/math/optimization/direct/NelderMead.html">
82
159
Nelder-Mead</a> method. The second one is Virginia Torczon's
83
<a href="../apidocs/org/apache/commons/math/optimization/MultiDirectional.html">
160
<a href="../apidocs/org/apache/commons/math/optimization/direct/MultiDirectional.html">
84
161
multi-directional</a> method.
164
<subsection name="12.5 General Case" href="general">
166
The general package deals with non-linear vectorial optimization problems when
167
the partial derivatives of the objective function are available.
170
One important class of estimation problems is weighted least
171
squares problems. They basically consist in finding the values
172
for some parameters p<sub>k</sub> such that a cost function
173
J = sum(w<sub>i</sub>(mes<sub>i</sub> - mod<sub>i</sub>)<sup>2</sup>) is
174
minimized. The various (target<sub>i</sub> - model<sub>i</sub>(p<sub>k</sub>))
175
terms are called residuals. They represent the deviation between a set of
176
target values target<sub>i</sub> and theoretical values computed from
177
models model<sub>i</sub> depending on free parameters p<sub>k</sub>.
178
The w<sub>i</sub> factors are weights. One classical use case is when the
179
target values are experimental observations or measurements.
182
Solving a least-squares problem is finding the free parameters p<sub>k</sub>
183
of the theoretical models such that they are close to the target values, i.e.
184
when the residual are small.
187
Two optimizers are available in the general package, both devoted to least-squares
188
problems. The first one is based on the <a
189
href="../apidocs/org/apache/commons/math/optimization/general/GaussNewtonOptimizer.html">
190
Gauss-Newton</a> method. The second one is the <a
191
href="../apidocs/org/apache/commons/math/optimization/general/LevenbergMarquardtOptimizer.html">
192
Levenberg-Marquardt</a> method.
195
In order to solve a vectorial optimization problem, the user must provide it as
196
an object implementing the <a
197
href="../apidocs/org/apache/commons/math/analysis/DifferentiableMultivariateVectorialFunction.html">
198
DifferentiableMultivariateVectorialFunction</a> interface. The object will be provided to
199
the <code>estimate</code> method of the optimizer, along with the target and weight arrays,
200
thus allowing the optimizer to compute the residuals at will. The last parameter to the
201
<code>estimate</code> method is the point from which the optimizer will start its
202
search for the optimal point.
205
In addition to least squares solving, the <a
206
href="../apidocs/org/apache/commons/math/optimization/general/NonLinearConjugateGradientOptimizer.html">
207
NonLinearConjugateGradientOptimizer</a> class provides a non-linear conjugate gradient algorithm
209
href="../apidocs/org/apache/commons/math/optimization/DifferentiableMultivariateRealFunction.html">
210
DifferentiableMultivariateRealFunction</a>. Both the Fletcher-Reeves and the Polak-Ribière
211
search direction update methods are supported. It is also possible to set up a preconditioner
212
or to change the line-search algorithm of the inner loop if desired (the default one is a Brent
216
<subsection name="12.6 Curve Fitting" href="fitting">
218
The fitting package deals with curve fitting for univariate real functions.
219
When a univariate real function y = f(x) does depend on some unknown parameters
220
p<sub>0</sub>, p<sub>1</sub> ... p<sub>n-1</sub>, curve fitting can be used to
221
find these parameters. It does this by <em>fitting</em> the curve so it remains
222
very close to a set of observed points (x<sub>0</sub>, y<sub>0</sub>),
223
(x<sub>1</sub>, y<sub>1</sub>) ... (x<sub>k-1</sub>, y<sub>k-1</sub>). This
224
fitting is done by finding the parameters values that minimizes the objective
225
function sum(y<sub>i</sub>-f(x<sub>i</sub>))<sup>2</sup>. This is really a least
229
For all provided curve fitters, the operating principle is the same. Users must first
230
create an instance of the fitter, then add the observed points and once the complete
231
sample of observed points has been added they must call the <code>fit</code> method
232
which will compute the parameters that best fit the sample. A weight is associated
233
with each observed point, this allows to take into account uncertainty on some points
234
when they come from loosy measurements for example. If no such information exist and
235
all points should be treated the same, it is safe to put 1.0 as the weight for all points.
239
href="../apidocs/org/apache/commons/math/optimization/fitting/CurveFitter.html">
240
CurveFitter</a> class provides curve fitting for general curves. Users must
241
provide their own implementation of the curve template as a class implementing
243
href="../apidocs/org/apache/commons/math/optimization/fitting/ParametricRealFunction.html">
244
ParametricRealFunction</a> interface and they must provide the initial guess of the
245
parameters. The more specialized <a
246
href="../apidocs/org/apache/commons/math/optimization/fitting/PolynomialFitter.html">
247
PolynomialFitter</a> and <a
248
href="../apidocs/org/apache/commons/math/optimization/fitting/HarmonicFitter.html">
249
HarmonicFitter</a> classes require neither an implementation of the parametric real function
250
not an initial guess as they are able to compute them by themselves.
253
An example of fitting a polynomial is given here:
255
<source>PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer());
256
fitter.addObservedPoint(-1.00, 2.021170021833143);
257
fitter.addObservedPoint(-0.99 2.221135431136975);
258
fitter.addObservedPoint(-0.98 2.09985277659314);
259
fitter.addObservedPoint(-0.97 2.0211192647627025);
260
// lots of lines ommitted
261
fitter.addObservedPoint( 0.99, -2.4345814727089854);
262
PolynomialFunction fitted = fitter.fit();