diff --git a/src/site/xdoc/userguide/optimization.xml b/src/site/xdoc/userguide/optimization.xml index b61c2033f..040780de9 100644 --- a/src/site/xdoc/userguide/optimization.xml +++ b/src/site/xdoc/userguide/optimization.xml @@ -63,21 +63,21 @@ are only four interfaces defining the common behavior of optimizers, one for each supported type of objective function:

@@ -85,15 +85,15 @@

Despite there are only four types of supported optimizers, it is possible to optimize a transform a + href="../apidocs/org/apache/commons/math3/analysis/MultivariateVectorFunction.html"> non-differentiable multivariate vectorial function by converting it to a + href="../apidocs/org/apache/commons/math3/analysis/MultivariateFunction.html"> non-differentiable multivariate real function thanks to the LeastSquaresConverter helper class. The transformed function can be optimized using any implementation of the - MultivariateRealOptimizer interface. + href="../apidocs/org/apache/commons/math3/optimization/MultivariateOptimizer.html"> + MultivariateOptimizer interface.

@@ -106,8 +106,8 @@

- A - UnivariateRealOptimizer is used to find the minimal values of a univariate real-valued + A + UnivariateOptimizer is used to find the minimal values of a univariate real-valued function f.

@@ -174,10 +174,10 @@

The first two simplex-based methods do not handle simple bounds constraints by themselves. However there are two adapters( - MultivariateRealFunctionMappingAdapter and - MultivariateRealFunctionPenaltyAdapter) that can be used to wrap the user function in + href="../apidocs/org/apache/commons/math3/optimization/direct/MultivariateFunctionMappingAdapter.html"> + MultivariateFunctionMappingAdapter and + MultivariateFunctionPenaltyAdapter) that can be used to wrap the user function in such a way the wrapped function is unbounded and can be used with these optimizers, despite the fact the underlying function is still bounded and will be called only with feasible points that fulfill the constraints. Note however that using these adapters are only a @@ -238,8 +238,8 @@

In order to solve a vectorial optimization problem, the user must provide it as an object implementing the - DifferentiableMultivariateVectorialFunction interface. The object will be provided to + href="../apidocs/org/apache/commons/math3/analysis/DifferentiableMultivariateVectorFunction.html"> + DifferentiableMultivariateVectorFunction interface. The object will be provided to the estimate method of the optimizer, along with the target and weight arrays, thus allowing the optimizer to compute the residuals at will. The last parameter to the estimate method is the point from which the optimizer will start its @@ -251,9 +251,10 @@

- We are looking to find the best parameters [a, b, c] for the quadratic function f(x)=a*x^2 + b*x + c . - The data set below was generated using [a = 8, b = 10, c = 16]. A random number between zero and one was added - to each y value calculated. + We are looking to find the best parameters [a, b, c] for the quadratic function + f(x) = a x2 + b x + c. + The data set below was generated using [a = 8, b = 10, c = 16]. + A random number between zero and one was added to each y value calculated. @@ -303,7 +304,7 @@

-First we need to implement the interface DifferentiableMultivariateVectorialFunction. +First we need to implement the interface DifferentiableMultivariateVectorFunction. This requires the implementation of the method signatures:

@@ -318,24 +319,23 @@ We'll tackle the implementation of the MultivariateMatrixFunction jacobian In this case the Jacobian is the partial derivative of the function with respect to the parameters a, b and c. These derivatives are computed as follows:

For a quadratic which has three variables the Jacobian Matrix will have three columns, one for each variable, and the number -of rows will equal the number of rows in our data set, which in this case is ten. So for example for [a = 1, b=1, c=1] -the Jacobian Matrix is (Exluding the first column which shows the value of x): +of rows will equal the number of rows in our data set, which in this case is ten. So for example for [a = 1, b = 1, c = 1], the Jacobian Matrix is (excluding the first column which shows the value of x):

- - - + + + @@ -405,8 +405,7 @@ parameter is an ArrayList containing the independent values of the data set):

- private double[][] jacobian(double[] variables) - { + private double[][] jacobian(double[] variables) { double[][] jacobian = new double[x.size()][3]; for (int i = 0; i < jacobian.length; ++i) { jacobian[i][0] = x.get(i) * x.get(i); @@ -416,8 +415,7 @@ parameter is an ArrayList containing the independent values of the data set): return jacobian; } - public MultivariateMatrixFunction jacobian() - { + public MultivariateMatrixFunction jacobian() { return new MultivariateMatrixFunction() { private static final long serialVersionUID = -8673650298627399464L; public double[][] value(double[] point) { @@ -458,7 +456,8 @@ Below is the the class containing all the implementation details

-private static class QuadraticProblem implements DifferentiableMultivariateVectorialFunction, Serializable { +private static class QuadraticProblem + implements DifferentiableMultivariateVectorFunction, Serializable { private static final long serialVersionUID = 7072187082052755854L; private List<Double> x; @@ -474,11 +473,9 @@ private static class QuadraticProblem implements DifferentiableMultivariateVecto this.y.add(y); } - public double[] calculateTarget() - { + public double[] calculateTarget() { double[] target = new double[y.size()]; - for (int i = 0; i < y.size(); i++) - { + for (int i = 0; i < y.size(); i++) { target[i] = y.get(i).doubleValue(); } return target; @@ -522,34 +519,30 @@ optimal set of quadratic curve fitting parameters: QuadraticProblem problem = new QuadraticProblem(); - problem.addPoint (1, 34.234064369); - problem.addPoint (2, 68.2681162306); - problem.addPoint (3, 118.6158990846); - problem.addPoint (4, 184.1381972386); - problem.addPoint (5, 266.5998779163); - problem.addPoint (6, 364.1477352516); - problem.addPoint (7, 478.0192260919); - problem.addPoint (8, 608.1409492707); - problem.addPoint (9, 754.5988686671); - problem.addPoint (10, 916.1288180859); + problem.addPoint(1, 34.234064369); + problem.addPoint(2, 68.2681162306); + problem.addPoint(3, 118.6158990846); + problem.addPoint(4, 184.1381972386); + problem.addPoint(5, 266.5998779163); + problem.addPoint(6, 364.1477352516); + problem.addPoint(7, 478.0192260919); + problem.addPoint(8, 608.1409492707); + problem.addPoint(9, 754.5988686671); + problem.addPoint(10, 916.1288180859); - LevenbergMarquardtOptimizer optimizer - = new LevenbergMarquardtOptimizer(); + LevenbergMarquardtOptimizer optimizer = new LevenbergMarquardtOptimizer(); - double[] weights = - { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; + final double[] weights = { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }; - double[] initialSolution = {1, 1, 1}; + final double[] initialSolution = {1, 1, 1}; - VectorialPointValuePair optimum = - optimizer.optimize( - 100, - problem, - problem.calculateTarget(), - weights, - initialSolution); + PointVectorValuePair optimum = optimizer.optimize(100, + problem, + problem.calculateTarget(), + weights, + initialSolution); - double[] optimalValues = optimum.getPoint(); + final double[] optimalValues = optimum.getPoint(); System.out.println("A: " + optimalValues[0]); System.out.println("B: " + optimalValues[1]); @@ -574,14 +567,14 @@ C: 16.324008168386605 href="../apidocs/org/apache/commons/math3/optimization/general/NonLinearConjugateGradientOptimizer.html"> NonLinearConjugateGradientOptimizer class provides a non-linear conjugate gradient algorithm to optimize - DifferentiableMultivariateRealFunction. Both the Fletcher-Reeves and the Polak-Ribière + href="../apidocs/org/apache/commons/math3/analysis/DifferentiableMultivariateFunction.html"> + DifferentiableMultivariateFunction. Both the Fletcher-Reeves and the Polak-Ribière search direction update methods are supported. It is also possible to set up a preconditioner or to change the line-search algorithm of the inner loop if desired (the default one is a Brent solver).

- The + The PowellOptimizer provides an optimization method for non-differentiable functions.

@@ -612,8 +605,8 @@ C: 16.324008168386605 CurveFitter class provides curve fitting for general curves. Users must provide their own implementation of the curve template as a class implementing the - ParametricRealFunction interface and they must provide the initial guess of the + href="../apidocs/org/apache/commons/math3/analysis/ParametricUnivariateFunction.html"> + ParametricUnivariateFunction interface and they must provide the initial guess of the parameters. The more specialized PolynomialFitter and PolynomialFitter fitter = new PolynomialFitter(degree, new LevenbergMarquardtOptimizer()); -fitter.addObservedPoint(-1.00, 2.021170021833143); -fitter.addObservedPoint(-0.99 2.221135431136975); -fitter.addObservedPoint(-0.98 2.09985277659314); -fitter.addObservedPoint(-0.97 2.0211192647627025); -// lots of lines ommitted +fitter.addObservedPoint(-1.00, 2.021170021833143); +fitter.addObservedPoint(-0.99, 2.221135431136975); +fitter.addObservedPoint(-0.98, 2.09985277659314); +fitter.addObservedPoint(-0.97, 2.0211192647627025); +// ... Lots of lines omitted ... fitter.addObservedPoint( 0.99, -2.4345814727089854); PolynomialFunction fitted = fitter.fit();
xd(ax^2+bx+c)/dad(ax^2+bx+c)/dbd(ax^2+bx+c)/dcd(ax2 + bx + c)/dad(ax2 + bx + c)/dbd(ax2 + bx + c)/dc
1