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_highest = 0.0;
159 for(
long iter_idx=0; iter_idx<max_loops; iter_idx++) {
162 x[idx] = x0_search[idx] + step * search_direction[idx];
169 double d__dot__g_current = 0.0;
171 d__dot__g_current += search_direction[idx] * g[idx];
175 if (f < f_best || fabs(d__dot__g_current) < d__dot__g_abs_best) {
179 d__dot__g_abs_best = fabs(d__dot__g_current);
192 if (f < f_lowest + step * 0.1 * d__dot__g0) {
194 if (d__dot__g_current >= d__dot__g0 * 0.7) {
207 d__dot__g_highest = d__dot__g_current;
218 if (iter_idx == 0 || step_lowest > 0.0) {
226 double d__dot__g_lowest_fit = (f_highest - f_lowest) / (step_highest - step_lowest)*2.0 - d__dot__g_highest;
232 scale = -d__dot__g_lowest_fit*0.5 / (d__dot__g_highest - d__dot__g_lowest_fit);
233 scale = std::max( 0.1, scale);
235 step = step_lowest + scale * (step_highest - step_lowest);
255 if (step != step_best) {
263 x[idx] = x0_search[idx] + step * search_direction[idx];
310 double fprev = fabs(f + f + 1.0);
319 double search_direction__grad_overlap = 0.0;
325 if (search_direction__grad_overlap < 0.0) {
328 d__dot__g = search_direction__grad_overlap;
333 double gradient_norm = 0.0;
335 gradient_norm += g[idx] * g[idx];
340 if (gradient_norm <= acc * acc) {
346 if (d__dot__g >= 0.0) {
373 line_search(x, g, search_direction, x0_search, g0_search, maximal_step, d__dot__g, f);
412 if ( fabs(search_direction[kdx]) < 1e-5 ) {
416 double step_bound_tmp = std::abs(2*
M_PI/search_direction[kdx]);
420 if (maximal_step == 0.0 || step_bound_tmp < maximal_step) {
422 maximal_step = step_bound_tmp;
464 memset(search_direction.
get_data(), 0, search_direction.
size()*
sizeof(double) );
466 for (
long row_idx = 0; row_idx <
variable_num; row_idx++) {
467 search_direction[row_idx] = -g[row_idx];
472 search_direction__grad_overlap = 0.0;
475 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