Commit 7eadede7 authored by Davis King's avatar Davis King

Clarified some things in the example and also added code showing how to use the

new bounds constrained solver.
parent bb49be31
......@@ -4,11 +4,10 @@
This is an example illustrating the use the general purpose non-linear
optimization routines from the dlib C++ Library.
The library provides implementations of the conjugate gradient,
BFGS, L-BFGS, and BOBYQA optimization algorithms. These algorithms allow
you to find the minimum of a function of many input variables.
This example walks though a few of the ways you might put these
routines to use.
The library provides implementations of the conjugate gradient, BFGS,
L-BFGS, and BOBYQA optimization algorithms. These algorithms allow you to
find the minimum of a function of many input variables. This example walks
though a few of the ways you might put these routines to use.
*/
......@@ -22,7 +21,10 @@ using namespace dlib;
// ----------------------------------------------------------------------------------------
// Here we just make a typedef for a variable length column vector of doubles.
// In dlib, the general purpose solvers optimize functions that take a column
// vector as input and return a double. So here we make a typedef for a
// variable length column vector of doubles. This is the type we will use to
// represent the input to our objective functions which we will be minimizing.
typedef matrix<double,0,1> column_vector;
// ----------------------------------------------------------------------------------------
......@@ -30,7 +32,7 @@ typedef matrix<double,0,1> column_vector;
// we can use the optimization algorithms to find the minimums of these functions.
// ----------------------------------------------------------------------------------------
double rosen ( const column_vector& m)
double rosen (const column_vector& m)
/*
This function computes what is known as Rosenbrock's function. It is
a function of two input variables and has a global minimum at (1,1).
......@@ -46,7 +48,7 @@ double rosen ( const column_vector& m)
}
// This is a helper function used while optimizing the rosen() function.
const column_vector rosen_derivative ( const column_vector& m)
const column_vector rosen_derivative (const column_vector& m)
/*!
ensures
- returns the gradient vector for the rosen function
......@@ -158,8 +160,7 @@ int main()
try
{
// make a column vector of length 2
column_vector starting_point;
starting_point.set_size(2);
column_vector starting_point(2);
// Set the starting point to (4,8). This is the point the optimization algorithm
......@@ -234,6 +235,31 @@ int main()
// dlib also supports solving functions subject to bounds constraints on
// the variables. So for example, if you wanted to find the minimizer
// of the rosen function where both input variables were in the range
// 0.1 to 0.8 you would do it like this:
starting_point = 0.1, 0.1; // Start with a valid point inside the constraint box.
find_min_box_constrained(lbfgs_search_strategy(10),
objective_delta_stop_strategy(1e-9),
rosen, rosen_derivative, starting_point, 0.1, 0.8);
// Here we put the same [0.1 0.8] range constraint on each variable, however, you
// can put different bounds on each variable by passing in column vectors of
// constraints for the last two arguments rather than scalars.
cout << endl << "constrained rosen solution: \n" << starting_point << endl;
// You can also use an approximate derivative like so:
starting_point = 0.1, 0.1;
find_min_box_constrained(bfgs_search_strategy(),
objective_delta_stop_strategy(1e-9),
rosen, derivative(rosen), starting_point, 0.1, 0.8);
cout << endl << "constrained rosen solution: \n" << starting_point << endl;
// In many cases, it is useful if we also provide second derivative information
// to the optimizers. Two examples of how we can do that are shown below.
starting_point = 0.8, 1.3;
......@@ -263,8 +289,7 @@ int main()
// functions.
cout << "\nFind the minimum of the test_function" << endl;
column_vector target;
target.set_size(4);
column_vector target(4);
starting_point.set_size(4);
// This variable will be used as the target of the test_function. So,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment