Commit 31875cfd authored by Davis King's avatar Davis King

More cleanup and added more tests

parent a6fd6929
......@@ -133,24 +133,6 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
auto next = opt.get_next_x();
double y = call_function_and_expand_args(functions[next.function_idx()], next.x());
next.set(y);
// TODO, remove this funky test code
matrix<double,0,1> x;
size_t function_idx;
opt.get_best_function_eval(x,y,function_idx);
using namespace std;
cout << "\ni: "<< i << endl;
cout << "best eval x: "<< trans(x);
cout << "best eval y: "<< y << endl;
cout << "best eval function index: "<< function_idx << endl;
if (std::abs(y - 21.9210397) < 0.0001)
{
cout << "DONE!" << endl;
//cin.get();
break;
}
}
......@@ -181,6 +163,23 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
return find_global_maximum(functions, specs, num, max_runtime, solver_epsilon).second;
}
// ----------------------------------------------------------------------------------------
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,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), bound1, bound2, std::vector<bool>(bound1.size(),false), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -198,6 +197,22 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
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>& bound1,
const matrix<double,0,1>& bound2,
const max_function_calls num,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), bound1, bound2, std::vector<bool>(bound1.size(),false), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -215,6 +230,22 @@ template <typename T> static auto go(T&& f, const matrix<double, 0, 1>& a) -> de
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 double bound1,
const double bound2,
const max_function_calls num,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), matrix<double,0,1>({bound1}), matrix<double,0,1>({bound2}), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......
......@@ -175,6 +175,23 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// 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 std::vector<bool>& is_integer_variable,
const max_function_calls num,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), bound1, bound2, std::vector<bool>(bound1.size(),false), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -192,6 +209,22 @@ namespace dlib
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>& bound1,
const matrix<double,0,1>& bound2,
const max_function_calls num,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), bound1, bound2, std::vector<bool>(bound1.size(),false), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -209,6 +242,22 @@ namespace dlib
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 double bound1,
const double bound2,
const max_function_calls num,
double solver_epsilon
)
{
return find_global_maximum(std::move(f), matrix<double,0,1>({bound1}), matrix<double,0,1>({bound2}), num, FOREVER, solver_epsilon);
}
// ----------------------------------------------------------------------------------------
template <
......@@ -260,7 +309,6 @@ namespace dlib
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_FiND_GLOBAL_MAXIMUM_ABSTRACT_hH_
......
......@@ -125,8 +125,7 @@ namespace dlib
//matrix<double,0,1> z = pinv(W)*r;
lu_decomposition<decltype(W)> lu(W);
matrix<double,0,1> z = lu.solve(r);
if (lu.is_singular())
std::cout << "WARNING, THE W MATRIX IS SINGULAR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
//if (lu.is_singular()) std::cout << "WARNING, THE W MATRIX IS SINGULAR!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!" << std::endl;
matrix<double,0,1> lambda = rowm(z, range(0,M-1));
......@@ -561,8 +560,8 @@ namespace dlib
// was.
double measured_improvement = y-req.anchor_objective_value;
double rho = measured_improvement/std::abs(req.predicted_improvement);
std::cout << "rho: "<< rho << std::endl;
std::cout << "radius: "<< info->radius << std::endl;
//std::cout << "rho: "<< rho << std::endl;
//std::cout << "radius: "<< info->radius << std::endl;
if (rho < 0.25)
info->radius *= 0.5;
else if (rho > 0.75)
......@@ -573,7 +572,7 @@ namespace dlib
{
if (!req.was_trust_region_generated_request && length(req.x - info->best_x) > info->radius*1.001)
{
std::cout << "reset radius because of big move, " << length(req.x - info->best_x) << " radius was " << info->radius << std::endl;
//std::cout << "reset radius because of big move, " << length(req.x - info->best_x) << " radius was " << info->radius << std::endl;
// reset trust region radius since we made a big move. Doing this will
// cause the radius to be reset to the size of the local region.
info->radius = 0;
......@@ -719,7 +718,7 @@ namespace dlib
{
auto tmp = pick_next_sample_quad_interp(info->ub.get_points(),
info->radius, info->spec.lower, info->spec.upper, info->spec.is_integer_variable);
std::cout << "QP predicted improvement: "<< tmp.predicted_improvement << std::endl;
//std::cout << "QP predicted improvement: "<< tmp.predicted_improvement << std::endl;
if (tmp.predicted_improvement > min_trust_region_epsilon)
{
do_trust_region_step = false;
......
......@@ -152,6 +152,68 @@ namespace
DLIB_TEST(found_optimal_point);
}
// ----------------------------------------------------------------------------------------
void test_find_global_maximum(
)
{
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_global_maximum(rosen, {0, 0}, {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_global_maximum(rosen, {0, 0}, {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_global_maximum(rosen, {0, 0}, {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_global_maximum(rosen, {0, 0}, {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_global_maximum(rosen, {0, 0}, {0.9, 0.9}, {false,false}, max_function_calls(100));
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)));
print_spinner();
result = find_global_maximum([](double x){ return -std::pow(x-2,2.0); }, -10, 10, max_function_calls(10), 0);
dlog << LINFO << "(x-2)^2: " << trans(result.x);
DLIB_TEST(result.x.size()==1);
DLIB_TEST(std::abs(result.x - 2) < 1e-9);
print_spinner();
result = find_global_maximum([](double x){ return -std::pow(x-2,2.0); }, -10, 1, max_function_calls(10));
dlog << LINFO << "(x-2)^2, bound at 1: " << trans(result.x);
DLIB_TEST(result.x.size()==1);
DLIB_TEST(std::abs(result.x - 1) < 1e-9);
print_spinner();
result = find_global_maximum([](double x){ return -std::pow(x-2,2.0); }, -10, 1, std::chrono::seconds(2));
dlog << LINFO << "(x-2)^2, bound at 1: " << trans(result.x);
DLIB_TEST(result.x.size()==1);
DLIB_TEST(std::abs(result.x - 1) < 1e-9);
print_spinner();
result = find_global_maximum([](double a, double b){ return -complex_holder_table(a,b);},
{-10, -10}, {10, 10}, max_function_calls(300), 0);
dlog << LINFO << "complex_holder_table y: "<< result.y;
DLIB_TEST_MSG(std::abs(result.y - 21.9210397) < 0.0001, std::abs(result.y - 21.9210397));
}
// ----------------------------------------------------------------------------------------
class global_optimization_tester : public tester
......@@ -170,6 +232,7 @@ namespace
test_upper_bound_function(0.0, 1e-6);
test_upper_bound_function(0.0, 1e-1);
test_global_function_search();
test_find_global_maximum();
}
} a;
......
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