| 290 | typename T |
| 291 | > |
| 292 | double find_min_using_approximate_derivatives ( |
| 293 | search_strategy_type search_strategy, |
| 294 | stop_strategy_type stop_strategy, |
| 295 | const funct& f, |
| 296 | T& x, |
| 297 | double min_f, |
| 298 | double derivative_eps = 1e-7 |
| 299 | ) |
| 300 | { |
| 301 | COMPILE_TIME_ASSERT(is_matrix<T>::value); |
| 302 | // The starting point (i.e. x) must be a column vector. |
| 303 | COMPILE_TIME_ASSERT(T::NC <= 1); |
| 304 | |
| 305 | DLIB_CASSERT ( |
| 306 | is_col_vector(x) && derivative_eps > 0, |
| 307 | "\tdouble find_min_using_approximate_derivatives()" |
| 308 | << "\n\tYou have to supply column vectors to this function" |
| 309 | << "\n\tx.nc(): " << x.nc() |
| 310 | << "\n\tderivative_eps: " << derivative_eps |
| 311 | ); |
| 312 | |
| 313 | T g, s; |
| 314 | |
| 315 | double f_value = f(x); |
| 316 | g = derivative(f,derivative_eps)(x); |
| 317 | |
| 318 | if (!is_finite(f_value)) |
| 319 | throw error("The objective function generated non-finite outputs"); |
| 320 | if (!is_finite(g)) |
| 321 | throw error("The objective function generated non-finite outputs"); |
| 322 | |
| 323 | while(stop_strategy.should_continue_search(x, f_value, g) && f_value > min_f) |
| 324 | { |
| 325 | s = search_strategy.get_next_direction(x, f_value, g); |
| 326 | |
| 327 | double alpha = line_search( |
| 328 | make_line_search_function(f,x,s,f_value), |
| 329 | f_value, |
| 330 | derivative(make_line_search_function(f,x,s),derivative_eps), |
| 331 | dot(g,s), // Sometimes the following line is a better way of determining the initial gradient. |
| 332 | //derivative(make_line_search_function(f,x,s),derivative_eps)(0), |
| 333 | search_strategy.get_wolfe_rho(), search_strategy.get_wolfe_sigma(), min_f, |
| 334 | search_strategy.get_max_line_search_iterations() |
| 335 | ); |
| 336 | |
| 337 | // Take the search step indicated by the above line search |
| 338 | x += alpha*s; |
| 339 | |
| 340 | g = derivative(f,derivative_eps)(x); |
| 341 | |
| 342 | if (!is_finite(f_value)) |
| 343 | throw error("The objective function generated non-finite outputs"); |
| 344 | if (!is_finite(g)) |
| 345 | throw error("The objective function generated non-finite outputs"); |
| 346 | } |
| 347 | |
| 348 | return f_value; |
| 349 | } |