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
ec7a4af1
Commit
ec7a4af1
authored
Nov 23, 2017
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
A bit of cleanup and documentation
parent
e7e5d238
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
564 additions
and
58 deletions
+564
-58
find_global_maximum.h
dlib/global_optimization/find_global_maximum.h
+49
-41
find_global_maximum_abstract.h
dlib/global_optimization/find_global_maximum_abstract.h
+261
-0
global_function_search.cpp
dlib/global_optimization/global_function_search.cpp
+8
-6
global_function_search.h
dlib/global_optimization/global_function_search.h
+9
-11
global_function_search_abstract.h
dlib/global_optimization/global_function_search_abstract.h
+237
-0
No files found.
dlib/global_optimization/find_global_maximum.h
View file @
ec7a4af1
...
...
@@ -3,10 +3,11 @@
#ifndef DLIB_FiND_GLOBAL_MAXIMUM_hH_
#define DLIB_FiND_GLOBAL_MAXIMUM_hH_
#include "find_global_maximum_abstract.h"
#include "global_function_search.h"
#include "../metaprogramming.h"
#include <utility>
#include <chrono>
namespace
dlib
{
...
...
@@ -32,7 +33,7 @@ namespace dlib
)
->
decltype
(
f
(
a
(
indices
-
1
)...))
{
DLIB_CASSERT
(
a
.
size
()
==
sizeof
...(
indices
),
"You invoked dlib::call_
with_vect
(f,a) but the number of arguments expected by f() doesn't match the size of 'a'. "
"You invoked dlib::call_
function_and_expand_args
(f,a) but the number of arguments expected by f() doesn't match the size of 'a'. "
<<
"Expected "
<<
sizeof
...(
indices
)
<<
" arguments but got "
<<
a
.
size
()
<<
"."
);
return
f
(
a
(
indices
-
1
)...);
...
...
@@ -42,7 +43,7 @@ namespace dlib
// So we write the terrible garbage in the #else for visual studio. When Visual Studio supports C++11 I'll update this #ifdef to use the C++11 code.
#ifndef _MSC_VER
template
<
size_t
max_unpack
>
struct
call_
with_vect
struct
call_
function_and_expand_args
{
template
<
typename
T
>
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
_cwv
(
std
::
forward
<
T
>
(
f
),
a
,
typename
make_compile_time_integer_range
<
max_unpack
>::
type
()))
...
...
@@ -51,14 +52,14 @@ namespace dlib
}
template
<
typename
T
>
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
call_
with_vect
<
max_unpack
-
1
>::
template
go
(
std
::
forward
<
T
>
(
f
),
a
))
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
call_
function_and_expand_args
<
max_unpack
-
1
>::
template
go
(
std
::
forward
<
T
>
(
f
),
a
))
{
return
call_
with_vect
<
max_unpack
-
1
>::
go
(
std
::
forward
<
T
>
(
f
),
a
);
return
call_
function_and_expand_args
<
max_unpack
-
1
>::
go
(
std
::
forward
<
T
>
(
f
),
a
);
}
};
template
<>
struct
call_
with_vect
<
0
>
struct
call_
function_and_expand_args
<
0
>
{
template
<
typename
T
>
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
f
(
disable_decay_to_scalar
(
a
)))
...
...
@@ -68,7 +69,7 @@ namespace dlib
};
#else
template
<
size_t
max_unpack
>
struct
call_
with_vect
struct
call_
function_and_expand_args
{
template
<
typename
T
>
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
std
::
forward
<
T
>
(
f
),
disable_decay_to_scalar
(
a
))
{
return
f
(
disable_decay_to_scalar
(
a
));
}
template
<
typename
T
>
static
auto
go
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
std
::
forward
<
T
>
(
f
),
a
(
0
))
{
DLIB_CASSERT
(
a
.
size
()
==
1
);
return
f
(
a
(
0
));
}
...
...
@@ -86,13 +87,13 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
auto
call_
with_vect
(
auto
call_
function_and_expand_args
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
a
)
->
decltype
(
gopt_impl
::
call_
with_vect
<
40
>::
go
(
f
,
a
))
)
->
decltype
(
gopt_impl
::
call_
function_and_expand_args
<
40
>::
go
(
f
,
a
))
{
// unpack up to 40 parameters when calling f()
return
gopt_impl
::
call_
with_vect
<
40
>::
go
(
std
::
forward
<
T
>
(
f
),
a
);
return
gopt_impl
::
call_
function_and_expand_args
<
40
>::
go
(
std
::
forward
<
T
>
(
f
),
a
);
}
// ----------------------------------------------------------------------------------------
...
...
@@ -105,6 +106,10 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
size_t
max_calls
=
std
::
numeric_limits
<
size_t
>::
max
();
};
// ----------------------------------------------------------------------------------------
const
auto
FOREVER
=
std
::
chrono
::
hours
(
24
*
356
*
290
);
// 290 years
// ----------------------------------------------------------------------------------------
template
<
...
...
@@ -114,7 +119,7 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
std
::
vector
<
funct
>&
functions
,
const
std
::
vector
<
function_spec
>&
specs
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
...
...
@@ -126,7 +131,7 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
for
(
size_t
i
=
0
;
i
<
num
.
max_calls
&&
std
::
chrono
::
steady_clock
::
now
()
<
time_to_stop
;
++
i
)
{
auto
next
=
opt
.
get_next_x
();
double
y
=
call_
with_vect
(
functions
[
next
.
function_idx
()],
next
.
x
());
double
y
=
call_
function_and_expand_args
(
functions
[
next
.
function_idx
()],
next
.
x
());
next
.
set
(
y
);
...
...
@@ -163,48 +168,51 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
vector
<
bool
>&
is_integer_variable
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
std
::
vector
<
funct
>
functions
(
1
,
f
);
std
::
vector
<
function_spec
>
specs
(
1
,
function_spec
(
lower
,
upper
));
auto
forever
=
std
::
chrono
::
hours
(
24
*
356
*
290
);
return
find_global_maximum
(
functions
,
specs
,
num
,
forever
,
solver_epsilon
).
second
;
std
::
vector
<
funct
>
functions
(
1
,
std
::
move
(
f
));
std
::
vector
<
function_spec
>
specs
(
1
,
function_spec
(
bound1
,
bound2
,
is_integer_variable
));
return
find_global_maximum
(
functions
,
specs
,
num
,
max_runtime
,
solver_epsilon
).
second
;
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
double
lower
,
const
double
upper
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
f
,
matrix
<
double
,
0
,
1
>
({
lower
}),
matrix
<
double
,
0
,
1
>
({
upper
}),
num
,
solver_epsilon
);
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
std
::
vector
<
bool
>
(
bound1
.
size
(),
false
),
num
,
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
const
std
::
vector
<
bool
>&
is_integer_variable
,
const
double
bound1
,
const
double
bound2
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
std
::
vector
<
funct
>
functions
(
1
,
std
::
move
(
f
));
std
::
vector
<
function_spec
>
specs
(
1
,
function_spec
(
lower
,
upper
,
is_integer_variable
));
auto
forever
=
std
::
chrono
::
hours
(
24
*
356
*
290
);
return
find_global_maximum
(
functions
,
specs
,
num
,
forever
,
solver_epsilon
).
second
;
return
find_global_maximum
(
std
::
move
(
f
),
matrix
<
double
,
0
,
1
>
({
bound1
}),
matrix
<
double
,
0
,
1
>
({
bound2
}),
num
,
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
...
...
@@ -214,46 +222,46 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
std
::
vector
<
funct
>
functions
(
1
,
f
);
std
::
vector
<
function_spec
>
specs
(
1
,
function_spec
(
lower
,
upper
));
return
find_global_maximum
(
functions
,
specs
,
max_function_calls
(),
max_runtime
,
solver_epsilon
).
second
;
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
max_function_calls
(),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
double
lower
,
const
double
upper
,
const
double
bound1
,
const
double
bound2
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
f
,
matrix
<
double
,
0
,
1
>
({
lower
}),
matrix
<
double
,
0
,
1
>
({
upper
}
),
max_runtime
,
solver_epsilon
);
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
max_function_calls
(
),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
vector
<
bool
>&
is_integer_variable
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
std
::
vector
<
funct
>
functions
(
1
,
std
::
move
(
f
));
std
::
vector
<
function_spec
>
specs
(
1
,
function_spec
(
lower
,
upper
,
is_integer_variable
));
return
find_global_maximum
(
functions
,
specs
,
max_function_calls
(),
max_runtime
,
solver_epsilon
).
second
;
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
is_integer_variable
,
max_function_calls
(),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
...
...
dlib/global_optimization/find_global_maximum_abstract.h
0 → 100644
View file @
ec7a4af1
// Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_FiND_GLOBAL_MAXIMUM_ABSTRACT_hH_
#ifdef DLIB_FiND_GLOBAL_MAXIMUM_ABSTRACT_hH_
#include "global_function_search_abstract.h"
#include "../metaprogramming.h"
#include <utility>
#include <chrono>
namespace
dlib
{
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
auto
call_function_and_expand_args
(
T
&&
f
,
const
matrix
<
double
,
0
,
1
>&
args
)
->
decltype
(
f
(
args
or
args
expanded
out
as
discussed
below
));
/*!
requires
- f is a function object with one of the following signatures:
auto f(matrix<double,0,1>)
auto f(double)
auto f(double,double)
auto f(double,double,double)
...
auto f(double,double,...,double) // up to 40 double arguments
- if (f() explicitly expands its arguments) then
- args.size() == the number of arguments taken by f.
ensures
- This function invokes f() with the given arguments and returns the result.
However, the signature of f() is allowed to vary. In particular, if f()
takes a matrix<double,0,1> as a single argument then this function simply
calls f(args). However, if f() takes double arguments then args is expanded
appropriately, e.g it calls one of the following as appropriate:
f(args(0))
f(args(0),args(1))
...
f(args(0),args(1),...,args(N))
and the result of f() is returned.
!*/
// ----------------------------------------------------------------------------------------
struct
max_function_calls
{
/*!
WHAT THIS OBJECT REPRESENTS
!*/
max_function_calls
()
=
default
;
explicit
max_function_calls
(
size_t
max_calls
)
:
max_calls
(
max_calls
)
{}
size_t
max_calls
=
std
::
numeric_limits
<
size_t
>::
max
();
};
// ----------------------------------------------------------------------------------------
const
auto
FOREVER
=
std
::
chrono
::
hours
(
24
*
356
*
290
);
// 290 years
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
std
::
pair
<
size_t
,
function_evaluation
>
find_global_maximum
(
std
::
vector
<
funct
>&
functions
,
const
std
::
vector
<
function_spec
>&
specs
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
);
/*!
requires
- functions.size() != 0
- functions.size() == specs.size()
- solver_epsilon >= 0
- for all valid i:
- functions[i] is a real valued multi-variate function object. Moreover,
it must be callable via an expression of the form:
call_function_and_expand_args(functions[i], specs.lower). This means
function[i] should have a signature like one of the following:
double f(matrix<double,0,1>)
double f(double)
double f(double,double)
etc.
- The range of inputs defined by specs[i] must be valid inputs to
functions[i].
ensures
- This function performs global optimization on the set of given functions.
The goal is to maximize the following objective function:
functions[i](x)
subject to the constraints on x defined by specs[i].
Once found, the return value of find_global_maximum() is:
make_pair(i, function_evaluation(x,functions[i](x))).
That is, we search for the settings of i and x that return the largest output
and return those settings.
- The search is performed using the global_function_search object. See its
documentation for details of the algorithm.
- We set the global_function_search::get_solver_epsilon() parameter to
solver_epsilon. Therefore, the search will only attempt to find a global
maximizer to at most solver_epsilon accuracy. Once a local minimizer is
found to that accuracy the search will focus entirely on finding another
minimizers elsewhere rather than on further improving the current local
optima found so far. That is, once a local minima is identified to about
solver_epsilon accuracy, the algorithm will spend all its time exploring the
function to find other local minima to investigate.
- find_global_maximum runs until one of the following is true:
- The total number of calls to the provided functions is == num.max_calls
- More than max_runtime time has elapsed since the start of this function.
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
vector
<
bool
>&
is_integer_variable
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
);
/*!
requires
- bound1.size() == bound2.size() == is_integer_variable.size()
- for all valid i: bound1(i) != bound2(i)
- solver_epsilon >= 0
- f() is a real valued multi-variate function object. Moreover, it must be
callable via an expression of the form: call_function_and_expand_args(f,
bound1). This means f() should have a signature like one of the following:
double f(matrix<double,0,1>)
double f(double)
double f(double,double)
etc.
- The range of inputs defined by function_spec(bound1,bound2,is_integer_variable)
must be valid inputs to f().
ensures
- This function performs global optimization on the given f() function.
The goal is to maximize the following objective function:
f(x)
subject to the constraints on x defined by function_spec(bound1,bound2,is_integer_variable).
Once found, the return value of find_global_maximum() is:
function_evaluation(x,f(x))).
That is, we search for the setting of x that return the largest output and
return those settings.
- The search is performed using the global_function_search object. See its
documentation for details of the algorithm.
- We set the global_function_search::get_solver_epsilon() parameter to
solver_epsilon. Therefore, the search will only attempt to find a global
maximizer to at most solver_epsilon accuracy. Once a local minimizer is
found to that accuracy the search will focus entirely on finding another
minimizers elsewhere rather than on further improving the current local
optima found so far. That is, once a local minima is identified to about
solver_epsilon accuracy, the algorithm will spend all its time exploring the
function to find other local minima to investigate.
- find_global_maximum runs until one of the following is true:
- The total number of calls to f() is == num.max_calls
- More than max_runtime time has elapsed since the start of this function.
!*/
// ----------------------------------------------------------------------------------------
// The following functions are just convenient overloads for calling the above defined
// find_global_maximum() routines.
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
std
::
vector
<
bool
>
(
bound1
.
size
(),
false
),
num
,
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
double
bound1
,
const
double
bound2
,
const
max_function_calls
num
,
const
std
::
chrono
::
nanoseconds
max_runtime
=
FOREVER
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
std
::
move
(
f
),
matrix
<
double
,
0
,
1
>
({
bound1
}),
matrix
<
double
,
0
,
1
>
({
bound2
}),
num
,
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
max_function_calls
(),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
double
bound1
,
const
double
bound2
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
max_function_calls
(),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
template
<
typename
funct
>
function_evaluation
find_global_maximum
(
funct
f
,
const
matrix
<
double
,
0
,
1
>&
bound1
,
const
matrix
<
double
,
0
,
1
>&
bound2
,
const
std
::
vector
<
bool
>&
is_integer_variable
,
const
std
::
chrono
::
nanoseconds
max_runtime
,
double
solver_epsilon
=
1e-11
)
{
return
find_global_maximum
(
std
::
move
(
f
),
bound1
,
bound2
,
is_integer_variable
,
max_function_calls
(),
max_runtime
,
solver_epsilon
);
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_FiND_GLOBAL_MAXIMUM_ABSTRACT_hH_
dlib/global_optimization/global_function_search.cpp
View file @
ec7a4af1
...
...
@@ -368,10 +368,10 @@ namespace dlib
// ----------------------------------------------------------------------------------------
function_spec
::
function_spec
(
const
matrix
<
double
,
0
,
1
>&
lower_
,
const
matrix
<
double
,
0
,
1
>&
upper_
matrix
<
double
,
0
,
1
>
bound1
,
matrix
<
double
,
0
,
1
>
bound2
)
:
lower
(
lower_
),
upper
(
upper_
)
lower
(
std
::
move
(
bound1
)),
upper
(
std
::
move
(
bound2
)
)
{
DLIB_CASSERT
(
lower
.
size
()
==
upper
.
size
());
for
(
size_t
i
=
0
;
i
<
lower
.
size
();
++
i
)
...
...
@@ -386,11 +386,11 @@ namespace dlib
// ----------------------------------------------------------------------------------------
function_spec
::
function_spec
(
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
matrix
<
double
,
0
,
1
>
bound1
,
matrix
<
double
,
0
,
1
>
bound2
,
std
::
vector
<
bool
>
is_integer
)
:
function_spec
(
std
::
move
(
lower
),
std
::
move
(
upper
))
function_spec
(
std
::
move
(
bound1
),
std
::
move
(
bound2
))
{
is_integer_variable
=
std
::
move
(
is_integer
);
DLIB_CASSERT
(
lower
.
size
()
==
(
long
)
is_integer_variable
.
size
());
...
...
@@ -597,6 +597,7 @@ namespace dlib
const
std
::
vector
<
function_spec
>&
functions_
)
{
DLIB_CASSERT
(
functions_
.
size
()
>
0
);
m
=
std
::
make_shared
<
std
::
mutex
>
();
functions
.
reserve
(
functions_
.
size
());
for
(
size_t
i
=
0
;
i
<
functions_
.
size
();
++
i
)
...
...
@@ -613,6 +614,7 @@ namespace dlib
)
:
global_function_search
(
functions_
)
{
DLIB_CASSERT
(
functions_
.
size
()
>
0
);
DLIB_CASSERT
(
functions_
.
size
()
==
initial_function_evals
.
size
());
DLIB_CASSERT
(
relative_noise_magnitude
>=
0
);
relative_noise_magnitude
=
relative_noise_magnitude_
;
...
...
dlib/global_optimization/global_function_search.h
View file @
ec7a4af1
...
...
@@ -3,6 +3,7 @@
#ifndef DLIB_GLOBAL_FuNCTION_SEARCH_Hh_
#define DLIB_GLOBAL_FuNCTION_SEARCH_Hh_
#include "global_function_search_abstract.h"
#include <vector>
#include "../matrix.h"
#include <mutex>
...
...
@@ -17,13 +18,13 @@ namespace dlib
struct
function_spec
{
function_spec
(
const
matrix
<
double
,
0
,
1
>&
lower_
,
const
matrix
<
double
,
0
,
1
>&
upper_
matrix
<
double
,
0
,
1
>
bound1
,
matrix
<
double
,
0
,
1
>
bound2
);
function_spec
(
const
matrix
<
double
,
0
,
1
>&
lower
,
const
matrix
<
double
,
0
,
1
>&
upper
,
matrix
<
double
,
0
,
1
>
bound1
,
matrix
<
double
,
0
,
1
>
bound2
,
std
::
vector
<
bool
>
is_integer
);
...
...
@@ -116,12 +117,6 @@ namespace dlib
void
set
(
double
y
);
/*!
requires
- has_been_evaluated() == false
ensures
- #has_been_evaluated() == true
!*/
private
:
...
...
@@ -143,7 +138,7 @@ namespace dlib
{
public
:
global_function_search
()
=
de
lete
;
global_function_search
()
=
de
fault
;
explicit
global_function_search
(
const
function_spec
&
function
...
...
@@ -162,6 +157,9 @@ namespace dlib
global_function_search
(
const
global_function_search
&
)
=
delete
;
global_function_search
&
operator
=
(
const
global_function_search
&
item
)
=
delete
;
global_function_search
(
global_function_search
&&
item
)
=
default
;
global_function_search
&
operator
=
(
global_function_search
&&
item
)
=
default
;
size_t
num_functions
(
)
const
;
...
...
dlib/global_optimization/global_function_search_abstract.h
0 → 100644
View file @
ec7a4af1
// Copyright (C) 2017 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_GLOBAL_FuNCTION_SEARCH_ABSTRACT_Hh_
#ifdef DLIB_GLOBAL_FuNCTION_SEARCH_ABSTRACT_Hh_
#include <vector>
#include "../matrix.h"
#include "upper_bound_function_abstract.h"
namespace
dlib
{
// ----------------------------------------------------------------------------------------
struct
function_spec
{
/*!
WHAT THIS OBJECT REPRESENTS
This object is a simple struct that lets you define the valid inputs to a
multivariate function. It lets you define bounds constraints for each
variable as well as say if a variable is integer valued or not. Therefore,
an instance of this struct says that a function takes upper.size() input
variables, where the ith variable must be in the range [lower(i) upper(i)]
and be an integer if is_integer_variable[i]==true.
!*/
function_spec
(
matrix
<
double
,
0
,
1
>
bound1
,
matrix
<
double
,
0
,
1
>
bound2
);
/*!
requires
- bound1.size() == bound2.size()
- for all valid i: bound1(i) != bound2(i)
ensures
- #is_integer_variable.size() == bound1.size()
- #lower.size() == bound1.size()
- #upper.size() == bound1.size()
- for all valid i:
- #is_integer_variable[i] == false
- #lower(i) == min(bound1(i), bound2(i))
- #upper(i) == max(bound1(i), bound2(i))
!*/
function_spec
(
matrix
<
double
,
0
,
1
>
lower
,
matrix
<
double
,
0
,
1
>
upper
,
std
::
vector
<
bool
>
is_integer
);
/*!
requires
- bound1.size() == bound2.size() == is_integer.size()
- for all valid i: bound1(i) != bound2(i)
ensures
- #is_integer_variable.size() == bound1.size()
- #lower.size() == bound1.size()
- #upper.size() == bound1.size()
- for all valid i:
- #is_integer_variable[i] == is_integer[i]
- #lower(i) == min(bound1(i), bound2(i))
- #upper(i) == max(bound1(i), bound2(i))
!*/
matrix
<
double
,
0
,
1
>
lower
;
matrix
<
double
,
0
,
1
>
upper
;
std
::
vector
<
bool
>
is_integer_variable
;
};
// ----------------------------------------------------------------------------------------
class
function_evaluation_request
{
/*!
WHAT THIS OBJECT REPRESENTS
!*/
public
:
// You can't make or copy this object, the only way to get one is from the
// global_function_search class via get_next_x().
function_evaluation_request
()
=
delete
;
function_evaluation_request
(
const
function_evaluation_request
&
)
=
delete
;
function_evaluation_request
&
operator
=
(
const
function_evaluation_request
&
)
=
delete
;
// You can however move and swap this object.
function_evaluation_request
(
function_evaluation_request
&&
item
);
function_evaluation_request
&
operator
=
(
function_evaluation_request
&&
item
);
/*!
moving from item causes item.has_been_evaluated() == true, TODO, clarify
!*/
void
swap
(
function_evaluation_request
&
item
);
/*!
ensures
- swaps the state of *this and item
!*/
~
function_evaluation_request
(
);
/*!
ensures
- frees all resources associated with this object.
!*/
size_t
function_idx
(
)
const
;
const
matrix
<
double
,
0
,
1
>&
x
(
)
const
;
bool
has_been_evaluated
(
)
const
;
void
set
(
double
y
);
/*!
requires
- has_been_evaluated() == false
ensures
- #has_been_evaluated() == true
!*/
};
// ----------------------------------------------------------------------------------------
class
global_function_search
{
/*!
WHAT THIS OBJECT REPRESENTS
!*/
public
:
global_function_search
(
);
/*!
ensures
- #num_functions() == 0
!*/
// This object can't be copied.
global_function_search
(
const
global_function_search
&
)
=
delete
;
global_function_search
&
operator
=
(
const
global_function_search
&
item
)
=
delete
;
global_function_search
(
global_function_search
&&
item
)
=
default
;
global_function_search
&
operator
=
(
global_function_search
&&
item
)
=
default
;
/*!
ensures
- moves the state of item into *this
- #item.num_functions() == 0
!*/
explicit
global_function_search
(
const
function_spec
&
function
);
explicit
global_function_search
(
const
std
::
vector
<
function_spec
>&
functions_
);
global_function_search
(
const
std
::
vector
<
function_spec
>&
functions_
,
const
std
::
vector
<
std
::
vector
<
function_evaluation
>>&
initial_function_evals
,
const
double
relative_noise_magnitude
=
0
.
001
);
size_t
num_functions
(
)
const
;
void
set_seed
(
time_t
seed
);
void
get_function_evaluations
(
std
::
vector
<
function_spec
>&
specs
,
std
::
vector
<
std
::
vector
<
function_evaluation
>>&
function_evals
)
const
;
void
get_best_function_eval
(
matrix
<
double
,
0
,
1
>&
x
,
double
&
y
,
size_t
&
function_idx
)
const
;
/*!
requires
- num_functions() != 0
!*/
function_evaluation_request
get_next_x
(
);
/*!
requires
- num_functions() != 0
!*/
double
get_pure_random_search_probability
(
)
const
;
void
set_pure_random_search_probability
(
double
prob
);
double
get_solver_epsilon
(
)
const
;
void
set_solver_epsilon
(
double
eps
);
double
get_relative_noise_magnitude
(
)
const
;
void
set_relative_noise_magnitude
(
double
value
);
size_t
get_monte_carlo_upper_bound_sample_num
(
)
const
;
void
set_monte_carlo_upper_bound_sample_num
(
size_t
num
);
};
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_GLOBAL_FuNCTION_SEARCH_ABSTRACT_Hh_
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