| 215 | typename T |
| 216 | > |
| 217 | double find_max ( |
| 218 | search_strategy_type search_strategy, |
| 219 | stop_strategy_type stop_strategy, |
| 220 | const funct& f, |
| 221 | const funct_der& der, |
| 222 | T& x, |
| 223 | double max_f |
| 224 | ) |
| 225 | { |
| 226 | COMPILE_TIME_ASSERT(is_matrix<T>::value); |
| 227 | // The starting point (i.e. x) must be a column vector. |
| 228 | COMPILE_TIME_ASSERT(T::NC <= 1); |
| 229 | |
| 230 | DLIB_CASSERT ( |
| 231 | is_col_vector(x), |
| 232 | "\tdouble find_max()" |
| 233 | << "\n\tYou have to supply column vectors to this function" |
| 234 | << "\n\tx.nc(): " << x.nc() |
| 235 | ); |
| 236 | |
| 237 | T g, s; |
| 238 | |
| 239 | // This function is basically just a copy of find_min() but with - put in the right places |
| 240 | // to flip things around so that it ends up looking for the max rather than the min. |
| 241 | |
| 242 | double f_value = -f(x); |
| 243 | g = -der(x); |
| 244 | |
| 245 | if (!is_finite(f_value)) |
| 246 | throw error("The objective function generated non-finite outputs"); |
| 247 | if (!is_finite(g)) |
| 248 | throw error("The objective function generated non-finite outputs"); |
| 249 | |
| 250 | while(stop_strategy.should_continue_search(x, f_value, g) && f_value > -max_f) |
| 251 | { |
| 252 | s = search_strategy.get_next_direction(x, f_value, g); |
| 253 | |
| 254 | double alpha = line_search( |
| 255 | negate_function(make_line_search_function(f,x,s, f_value)), |
| 256 | f_value, |
| 257 | negate_function(make_line_search_function(der,x,s, g)), |
| 258 | dot(g,s), // compute initial gradient for the line search |
| 259 | search_strategy.get_wolfe_rho(), search_strategy.get_wolfe_sigma(), -max_f, |
| 260 | search_strategy.get_max_line_search_iterations() |
| 261 | ); |
| 262 | |
| 263 | // Take the search step indicated by the above line search |
| 264 | x += alpha*s; |
| 265 | |
| 266 | // Don't forget to negate these outputs from the line search since they are |
| 267 | // from the unnegated versions of f() and der() |
| 268 | g *= -1; |
| 269 | f_value *= -1; |
| 270 | |
| 271 | if (!is_finite(f_value)) |
| 272 | throw error("The objective function generated non-finite outputs"); |
| 273 | if (!is_finite(g)) |
| 274 | throw error("The objective function generated non-finite outputs"); |