| 151 | typename T |
| 152 | > |
| 153 | double find_min ( |
| 154 | search_strategy_type search_strategy, |
| 155 | stop_strategy_type stop_strategy, |
| 156 | const funct& f, |
| 157 | const funct_der& der, |
| 158 | T& x, |
| 159 | double min_f |
| 160 | ) |
| 161 | { |
| 162 | COMPILE_TIME_ASSERT(is_matrix<T>::value); |
| 163 | // The starting point (i.e. x) must be a column vector. |
| 164 | COMPILE_TIME_ASSERT(T::NC <= 1); |
| 165 | |
| 166 | DLIB_CASSERT ( |
| 167 | is_col_vector(x), |
| 168 | "\tdouble find_min()" |
| 169 | << "\n\tYou have to supply column vectors to this function" |
| 170 | << "\n\tx.nc(): " << x.nc() |
| 171 | ); |
| 172 | |
| 173 | |
| 174 | T g, s; |
| 175 | |
| 176 | double f_value = f(x); |
| 177 | g = der(x); |
| 178 | |
| 179 | if (!is_finite(f_value)) |
| 180 | throw error("The objective function generated non-finite outputs"); |
| 181 | if (!is_finite(g)) |
| 182 | throw error("The objective function generated non-finite outputs"); |
| 183 | |
| 184 | while(stop_strategy.should_continue_search(x, f_value, g) && f_value > min_f) |
| 185 | { |
| 186 | s = search_strategy.get_next_direction(x, f_value, g); |
| 187 | |
| 188 | double alpha = line_search( |
| 189 | make_line_search_function(f,x,s, f_value), |
| 190 | f_value, |
| 191 | make_line_search_function(der,x,s, g), |
| 192 | dot(g,s), // compute initial gradient for the line search |
| 193 | search_strategy.get_wolfe_rho(), search_strategy.get_wolfe_sigma(), min_f, |
| 194 | search_strategy.get_max_line_search_iterations()); |
| 195 | |
| 196 | // Take the search step indicated by the above line search |
| 197 | x += alpha*s; |
| 198 | |
| 199 | if (!is_finite(f_value)) |
| 200 | throw error("The objective function generated non-finite outputs"); |
| 201 | if (!is_finite(g)) |
| 202 | throw error("The objective function generated non-finite outputs"); |
| 203 | } |
| 204 | |
| 205 | return f_value; |
| 206 | } |
| 207 | |
| 208 | // ---------------------------------------------------------------------------------------- |
| 209 | |