Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
7eadede7
Commit
7eadede7
authored
Oct 05, 2013
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Clarified some things in the example and also added code showing how to use the
new bounds constrained solver.
parent
bb49be31
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
37 additions
and
12 deletions
+37
-12
optimization_ex.cpp
examples/optimization_ex.cpp
+37
-12
No files found.
examples/optimization_ex.cpp
View file @
7eadede7
...
...
@@ -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
<<
"
\n
Find 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,
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment