Added a minor optimization.

This commit is contained in:
Davis King 2017-12-18 15:59:23 -05:00
parent a966aeb94f
commit 087c1f2e61
2 changed files with 26 additions and 10 deletions

View File

@ -702,7 +702,23 @@ namespace dlib
for (auto& info : functions)
{
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;
new_req.request_id = next_request_id++;

View File

@ -160,29 +160,29 @@ namespace
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 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};
dlog << LINFO << "rosen: " << trans(result.x);
DLIB_TEST_MSG(max(abs(true_x-result.x)) < 1e-5, max(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(max(abs(true_x-result.x)) < 1e-5, max(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(max(abs(true_x-result.x)) < 1e-5, max(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(max(abs(true_x-result.x)) < 1e-5, max(abs(true_x-result.x)));
print_spinner();
result = find_max_global(rosen, {0, 0}, {0.9, 0.9}, {false,false}, max_function_calls(100));
result = find_max_global(rosen, {0.1, 0.1}, {0.9, 0.9}, {false,false}, max_function_calls(140));
true_x = {0.9, 0.81};
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)));
@ -221,24 +221,24 @@ namespace
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 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};
dlog << LINFO << "rosen: " << trans(result.x);
DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x)));
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);
DLIB_TEST_MSG(min(abs(true_x-result.x)) < 1e-5, min(abs(true_x-result.x)));
print_spinner();