19 #define _USE_MATH_DEFINES 144 double step = std::min(1.0, maximal_step);
147 double f_highest = f;
149 double step_highest = maximal_step;
150 double step_lowest = 0.0;
152 double step_best = 0.0;
154 double d__dot__g_abs_best = fabs(d__dot__g0);
156 double d__dot__g_lowest = d__dot__g0;
157 double d__dot__g_highest = 0.0;
160 for(
long iter_idx=0; iter_idx<max_loops; iter_idx++) {
163 x[idx] = x0_search[idx] + step * search_direction[idx];
170 double d__dot__g_current = 0.0;
172 d__dot__g_current += search_direction[idx] * g[idx];
176 if (f < f_best || fabs(d__dot__g_current) < d__dot__g_abs_best) {
180 d__dot__g_abs_best = fabs(d__dot__g_current);
193 if (f < f_lowest + step * 0.1 * d__dot__g0) {
195 if (d__dot__g_current >= d__dot__g0 * 0.7) {
201 d__dot__g_lowest = d__dot__g_current;
209 d__dot__g_highest = d__dot__g_current;
220 if (iter_idx == 0 || step_lowest > 0.0) {
228 double d__dot__g_lowest_fit = (f_highest - f_lowest) / (step_highest - step_lowest)*2.0 - d__dot__g_highest;
234 scale = -d__dot__g_lowest_fit*0.5 / (d__dot__g_highest - d__dot__g_lowest_fit);
235 scale = std::max( 0.1, scale);
237 step = step_lowest + scale * (step_highest - step_lowest);
257 if (step != step_best) {
265 x[idx] = x0_search[idx] + step * search_direction[idx];
312 double fprev = fabs(f + f + 1.0);
321 double search_direction__grad_overlap = 0.0;
327 if (search_direction__grad_overlap < 0.0) {
330 d__dot__g = search_direction__grad_overlap;
335 double gradient_norm = 0.0;
337 gradient_norm += g[idx] * g[idx];
342 if (gradient_norm <= acc * acc) {
348 if (d__dot__g >= 0.0) {
375 line_search(x, g, search_direction, x0_search, g0_search, maximal_step, d__dot__g, f);
414 if ( fabs(search_direction[kdx]) < 1e-5 ) {
418 double step_bound_tmp = std::abs(2*M_PI/search_direction[kdx]);
422 if (maximal_step == 0.0 || step_bound_tmp < maximal_step) {
424 maximal_step = step_bound_tmp;
466 memset(search_direction.
get_data(), 0.0, search_direction.
size()*
sizeof(double) );
468 for (
long row_idx = 0; row_idx <
variable_num; row_idx++) {
469 search_direction[row_idx] = -g[row_idx];
474 search_direction__grad_overlap = 0.0;
477 search_direction__grad_overlap += search_direction[idx] * g[idx];
void * meta_data
additional data needed to evaluate the cost function
void get_f_ang_gradient(Matrix_real &x, double &f, Matrix_real &g)
Call this method to obtain the cost function and its gradient at a gives position given by x...
Matrix_real copy() const
Call to create a copy of the matrix.
void get_Maximal_Line_Search_Step(Matrix_real &search_direction, double &maximal_step, double &search_direction__grad_overlap)
Call this method to obtain the maximal step size during the line search.
long maximal_iterations
maximal count of iterations during the optimization
void line_search(Matrix_real &x, Matrix_real &g, Matrix_real &search_direction, Matrix_real &x0_search, Matrix_real &g0_search, double &maximal_step, double &d__dot__g, double &f)
Call to perform inexact line search terminated with Wolfe 1st and 2nd conditions. ...
scalar * get_data() const
Call to get the pointer to the stored data.
void(* export_fnc)(double, Matrix_real &, void *)
function pointer to evaluate the cost function and its gradient vector
double Start_Optimization(Matrix_real &x, long maximal_iterations_in=5001)
Call this method to start the optimization.
~Grad_Descend()
Destructor of the class.
virtual void Optimize(Matrix_real &x, double &f)
Call this method to start the optimization process.
enum solver_status status
status of the solver
long function_call_count
number of function calls during the optimization process
int size() const
Call to get the number of the allocated elements.
Grad_Descend(void(*f_pointer)(Matrix_real, void *, double *, Matrix_real &), void *meta_data_in)
Constructor of the class.
int variable_num
number of independent variables in the problem
double num_precision
numerical precision used in the calculations
virtual void get_search_direction(Matrix_real &g, Matrix_real &search_direction, double &search_direction__grad_overlap)
Method to get the search direction in the next line search.
Class to store data of complex arrays and its properties.
void(* costfnc__and__gradient)(Matrix_real x, void *params, double *f, Matrix_real &g)
function pointer to evaluate the cost function and its gradient vector