一、Overview
The problem of multidimensional minimization requres finding a point x such that the scalar function,
takes a value which is lower than at any neighboring point. For smooth functions the gradient , vanishes at the minimum. In general there are no bracketing methods available for the minimization of n-dimensional functions. The algorithms proceed from an initial guess using a search algorithm which attempts to move in a downhill direction.
使用步骤
Steps
1、initialize minimizer state, s, for algoritm T
2、update s using the iteration T
3、test s for convergence, and repeat iteration if necessary
下面是gsl第39章的例子,这里贴出来。
#include<stdio.h>
#include<iostream>
#include<gsl/gsl_multimin.h>
#include<gsl/gsl_vector.h>
#include<iomanip>
using namespace std;
/* Paraboloid centered on (p[0], p[1]), with
scale factors (p[2], p[3]) and minimum p[4] */
double my_f(const gsl_vector *v, void *params)
{
double x, y;
double *p = (double *)params;
x = gsl_vector_get(v, 0);
y = gsl_vector_get(v, 1);
return p[2] * (x - p[0]) * (x - p[0]) +
p[3] * (y - p[1]) * (y - p[1]) + p[4];
}
/* The gradient of f, df = (df/dx, df/dy). */
void my_df(const gsl_vector *v, void *params, gsl_vector *df)
{
double x, y;
double *p = (double *)params;
x = gsl_vector_get(v, 0);
y = gsl_vector_get(v, 1);
gsl_vector_set(df, 0, 2.0 * p[2] * (x - p[0])); //将2.0 * p[2] * (x - p[0])计算的值放在向量df第0个位置
gsl_vector_set(df, 1, 2.0 * p[3] * (y - p[1])); //将2.0 * p[3] * (y - p[1])计算的值放在向量df第1个位置
}
/* Compute both f and df together. */
void my_fdf(const gsl_vector *x, void *params, double *f, gsl_vector *df)
{
*f = my_f(x, params);
my_df(x, params, df);
}
int main()
{
size_t iter = 0;
int status;
const gsl_multimin_fdfminimizer_type *T; //this type specifies a minimization algorithm using gradients
gsl_multimin_fdfminimizer *s; //this is a workspace for minimizing functions using derivatives
gsl_vector *x;
gsl_multimin_function_fdf my_func; //this data type defines a general function of n variables with parameters
// and the correspongding gradient vector of derivatives
/* Paraboloid center at (1, 2), scale factors (10, 20),
minimum value 30 */
double par[5] = { 1.0, 2.0, 10.0, 20.0, 30.0 };
my_func.n = 2; /* number of function components */
my_func.f = &my_f;
my_func.df = &my_df;
my_func.fdf = &my_fdf;
my_func.params = par;
/* Starting point, x = (5, 7)*/
x = gsl_vector_alloc(2);
gsl_vector_set(x, 0, 5.0);
gsl_vector_set(x, 1, 7.0);
T = gsl_multimin_fdfminimizer_conjugate_fr;
s = gsl_multimin_fdfminimizer_alloc(T, 2); //this function returns a pointer to a newly allocated instance of a minimizer of type
// T for an n-dimension function
gsl_multimin_fdfminimizer_set(s, &my_func, x, 0.01, 1e-4); //initialize the minimizer s to minimize the function fdf starting from the initial
//point x. the size of the first trial step is given bu step_size. The accuracy of the line
//minimization is specified bu tol.
do
{
iter++;
status = gsl_multimin_fdfminimizer_iterate(s);
if (status)
break;
status = gsl_multimin_test_gradient(s->gradient, 1e-3);
if (status == GSL_SUCCESS)
cout << "Minimum found at: \n";
cout<< setw(5) <<fixed<< setprecision(5) << iter <<
setw(10)<< setprecision(5) << gsl_vector_get(s->x, 0) <<
setw(10)<< setprecision(5) << gsl_vector_get(s->x, 1) <<
setw(12)<< setprecision(5) << s->f << endl;
} while (status == GSL_CONTINUE && iter < 100);
gsl_multimin_fdfminimizer_free(s);
gsl_vector_free(x);
/*
gls库vector使用
int i;
gsl_vector *v = gsl_vector_alloc(3);
for (int i = 0; i < 3; i++)
{
gsl_vector_set(v, i, 1.23 + i);
}
for (int i = 0; i < 100; i++)
{
cout << gsl_vector_get(v, i) << endl;
}
gsl_vector_free(v);
*/
/*
double params[5] = { 1, 2, 3, 4, 5 };
double *p = params;
cout << p[1] << endl;
*/
system("pause");
}