Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
087c1f2e
Commit
087c1f2e
authored
Dec 18, 2017
by
Davis King
Browse files
Added a minor optimization.
parent
a966aeb9
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
26 additions
and
10 deletions
+26
-10
dlib/global_optimization/global_function_search.cpp
dlib/global_optimization/global_function_search.cpp
+17
-1
dlib/test/global_optimization.cpp
dlib/test/global_optimization.cpp
+9
-9
No files found.
dlib/global_optimization/global_function_search.cpp
View file @
087c1f2e
...
@@ -702,7 +702,23 @@ namespace dlib
...
@@ -702,7 +702,23 @@ namespace dlib
for
(
auto
&
info
:
functions
)
for
(
auto
&
info
:
functions
)
{
{
const
long
dims
=
info
->
spec
.
lower
.
size
();
const
long
dims
=
info
->
spec
.
lower
.
size
();
if
(
info
->
ub
.
num_points
()
<
std
::
max
<
long
>
(
3
,
dims
))
if
(
info
->
ub
.
num_points
()
<
1
)
{
outstanding_function_eval_request
new_req
;
new_req
.
request_id
=
next_request_id
++
;
// Pick the point right in the center of the bounds to evaluate first since
// people will commonly center the bound on a location they think is good.
// So might as well try there first.
new_req
.
x
=
(
info
->
spec
.
lower
+
info
->
spec
.
upper
)
/
2.0
;
for
(
long
i
=
0
;
i
<
new_req
.
x
.
size
();
++
i
)
{
if
(
info
->
spec
.
is_integer_variable
[
i
])
new_req
.
x
(
i
)
=
std
::
round
(
new_req
.
x
(
i
));
}
info
->
outstanding_evals
.
emplace_back
(
new_req
);
return
function_evaluation_request
(
new_req
,
info
);
}
else
if
(
info
->
ub
.
num_points
()
<
std
::
max
<
long
>
(
3
,
dims
))
{
{
outstanding_function_eval_request
new_req
;
outstanding_function_eval_request
new_req
;
new_req
.
request_id
=
next_request_id
++
;
new_req
.
request_id
=
next_request_id
++
;
...
...
dlib/test/global_optimization.cpp
View file @
087c1f2e
...
@@ -160,29 +160,29 @@ namespace
...
@@ -160,29 +160,29 @@ namespace
print_spinner
();
print_spinner
();
auto
rosen
=
[](
const
matrix
<
double
,
0
,
1
>&
x
)
{
return
-
1
*
(
100
*
std
::
pow
(
x
(
1
)
-
x
(
0
)
*
x
(
0
),
2.0
)
+
std
::
pow
(
1
-
x
(
0
),
2
));
};
auto
rosen
=
[](
const
matrix
<
double
,
0
,
1
>&
x
)
{
return
-
1
*
(
100
*
std
::
pow
(
x
(
1
)
-
x
(
0
)
*
x
(
0
),
2.0
)
+
std
::
pow
(
1
-
x
(
0
),
2
));
};
auto
result
=
find_max_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
max_function_calls
(
100
),
0
);
auto
result
=
find_max_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
max_function_calls
(
100
),
0
);
matrix
<
double
,
0
,
1
>
true_x
=
{
1
,
1
};
matrix
<
double
,
0
,
1
>
true_x
=
{
1
,
1
};
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_max_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
max_function_calls
(
100
));
result
=
find_max_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
max_function_calls
(
100
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_max_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
std
::
chrono
::
seconds
(
5
));
result
=
find_max_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
std
::
chrono
::
seconds
(
5
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_max_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
{
false
,
false
},
max_function_calls
(
100
));
result
=
find_max_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
{
false
,
false
},
max_function_calls
(
100
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_max_global
(
rosen
,
{
0
,
0
},
{
0.9
,
0.9
},
{
false
,
false
},
max_function_calls
(
1
0
0
));
result
=
find_max_global
(
rosen
,
{
0
.1
,
0
.1
},
{
0.9
,
0.9
},
{
false
,
false
},
max_function_calls
(
1
4
0
));
true_x
=
{
0.9
,
0.81
};
true_x
=
{
0.9
,
0.81
};
dlog
<<
LINFO
<<
"rosen, bounded at 0.9: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen, bounded at 0.9: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
max
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
max
(
abs
(
true_x
-
result
.
x
)));
...
@@ -221,24 +221,24 @@ namespace
...
@@ -221,24 +221,24 @@ namespace
print_spinner
();
print_spinner
();
auto
rosen
=
[](
const
matrix
<
double
,
0
,
1
>&
x
)
{
return
+
1
*
(
100
*
std
::
pow
(
x
(
1
)
-
x
(
0
)
*
x
(
0
),
2.0
)
+
std
::
pow
(
1
-
x
(
0
),
2
));
};
auto
rosen
=
[](
const
matrix
<
double
,
0
,
1
>&
x
)
{
return
+
1
*
(
100
*
std
::
pow
(
x
(
1
)
-
x
(
0
)
*
x
(
0
),
2.0
)
+
std
::
pow
(
1
-
x
(
0
),
2
));
};
auto
result
=
find_min_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
max_function_calls
(
100
),
0
);
auto
result
=
find_min_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
max_function_calls
(
100
),
0
);
matrix
<
double
,
0
,
1
>
true_x
=
{
1
,
1
};
matrix
<
double
,
0
,
1
>
true_x
=
{
1
,
1
};
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_min_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
max_function_calls
(
100
));
result
=
find_min_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
max_function_calls
(
100
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_min_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
std
::
chrono
::
seconds
(
5
));
result
=
find_min_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
std
::
chrono
::
seconds
(
5
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
result
=
find_min_global
(
rosen
,
{
0
,
0
},
{
2
,
2
},
{
false
,
false
},
max_function_calls
(
100
));
result
=
find_min_global
(
rosen
,
{
0
.1
,
0
.1
},
{
2
,
2
},
{
false
,
false
},
max_function_calls
(
100
));
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
dlog
<<
LINFO
<<
"rosen: "
<<
trans
(
result
.
x
);
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
DLIB_TEST_MSG
(
min
(
abs
(
true_x
-
result
.
x
))
<
1e-5
,
min
(
abs
(
true_x
-
result
.
x
)));
print_spinner
();
print_spinner
();
...
...
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