Changed the single variable optimization code so that it throws an exception

if it fails to converge within the given max number of iterations.

--HG--
extra : convert_revision : svn%3Afdd8eb12-d10e-0410-9acb-85c331704f74/trunk%403242
This commit is contained in:
Davis King 2009-10-15 23:12:06 +00:00
parent 49854b5b29
commit c55c0994c1
2 changed files with 49 additions and 1 deletions

View File

@ -438,6 +438,12 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
class optimize_single_variable_failure : public error {
public: optimize_single_variable_failure(const std::string& s):error(s){}
};
// ----------------------------------------------------------------------------------------
template <typename funct>
@ -450,6 +456,13 @@ namespace dlib
const long max_iter = 100
)
{
// You get an error on this line when you pass in a global function to this function.
// You have to either use a function object or pass a pointer to your global function
// by taking its address using the & operator. (This check is here because gcc 4.0
// has a bug that causes it to silently corrupt return values from functions that
// invoked through a reference)
COMPILE_TIME_ASSERT(is_function<funct>::value == false);
DLIB_CASSERT( eps > 0 &&
max_iter > 1 &&
begin <= starting_point && starting_point <= end,
@ -503,7 +516,13 @@ namespace dlib
while ( !(f1 > f2 && f2 < f3))
{
// check for hitting max_iter or if the interval is now too small
if (f_evals >= max_iter || p3-p1 < eps)
if (f_evals >= max_iter)
{
throw optimize_single_variable_failure(
"The max number of iterations of single variable optimization have been reached\n"
"without converging.");
}
if (p3-p1 < eps)
{
if (f1 < min(f2,f3))
{
@ -673,6 +692,13 @@ namespace dlib
++f_evals;
}
if (f_evals >= max_iter)
{
throw optimize_single_variable_failure(
"The max number of iterations of single variable optimization have been reached\n"
"without converging.");
}
starting_point = p2;
return f2;
}
@ -689,6 +715,13 @@ namespace dlib
const long max_iter = 100
)
{
// You get an error on this line when you pass in a global function to this function.
// You have to either use a function object or pass a pointer to your global function
// by taking its address using the & operator. (This check is here because gcc 4.0
// has a bug that causes it to silently corrupt return values from functions that
// invoked through a reference)
COMPILE_TIME_ASSERT(is_function<funct>::value == false);
return -find_min_single_variable(negate_function(f), starting_point, begin, end, eps, max_iter);
}

View File

@ -171,6 +171,13 @@ namespace dlib
and also in the more recent book Numerical Optimization by Nocedal and Wright.
*/
// ----------------------------------------------------------------------------------------
class optimize_single_variable_failure : public error;
/*!
This is the exception class used by the functions defined below.
!*/
// ----------------------------------------------------------------------------------------
template <
@ -200,6 +207,10 @@ namespace dlib
The search will begin with the given starting_point.
- #starting_point == P
- returns f(P)
throws
- optimize_single_variable_failure
This exception is thrown if max_iter iterations are performed without
determining the min point to the requsted accuracy of eps.
!*/
// ----------------------------------------------------------------------------------------
@ -231,6 +242,10 @@ namespace dlib
The search will begin with the given starting_point.
- #starting_point == P
- returns f(P)
throws
- optimize_single_variable_failure
This exception is thrown if max_iter iterations are performed without
determining the max point to the requsted accuracy of eps.
!*/
// ----------------------------------------------------------------------------------------