| 1 |
|
| #include <math.h> |
| 2 |
|
| #include <stdio.h> |
| 3 |
|
| #include <string.h> |
| 4 |
|
| #include <stdarg.h> |
| 5 |
|
| #include "newton.h" |
| 6 |
|
| |
| 7 |
|
| #ifndef min |
| 8 |
63 |
▶ | template <class T> static inline T min(T x,T y) { return (x<y)?x:y; } |
Branch: 1 branch
| Branch | True | False | Total |
| template <class T> static inline T min(T x,T y) { return (x<y)?x:y; } |
63 |
0 |
63 |
|
| 9 |
|
| #endif |
| 10 |
|
| |
| 11 |
|
| #ifndef max |
| 12 |
63 |
▶ | template <class T> static inline T max(T x,T y) { return (x>y)?x:y; } |
Branch: 1 branch
| Branch | True | False | Total |
| template <class T> static inline T max(T x,T y) { return (x>y)?x:y; } |
63 |
0 |
63 |
|
| 13 |
|
| #endif |
| 14 |
|
| |
| 15 |
|
| #ifdef __cplusplus |
| 16 |
|
| extern "C" { |
| 17 |
|
| #endif |
| 18 |
|
| |
| 19 |
|
| extern double dnrm2_(int *, double *, int *); |
| 20 |
|
| extern double ddot_(int *, double *, int *, double *, int *); |
| 21 |
|
| extern int daxpy_(int *, double *, double *, int *, double *, int *); |
| 22 |
|
| extern int dscal_(int *, double *, double *, int *); |
| 23 |
|
| |
| 24 |
|
| #ifdef __cplusplus |
| 25 |
|
| } |
| 26 |
|
| #endif |
| 27 |
|
| |
| 28 |
0 |
| static void default_print(const char *buf) |
| 29 |
|
| { |
| 30 |
0 |
| fputs(buf,stdout); |
| 31 |
0 |
| fflush(stdout); |
| 32 |
0 |
| } |
| 33 |
|
| |
| 34 |
|
| // On entry *f must be the function value of w |
| 35 |
|
| // On exit w is updated and *f is the new function value |
| 36 |
0 |
| double function::linesearch_and_update(double *w, double *s, double *f, double *g, double alpha) |
| 37 |
|
| { |
| 38 |
0 |
| double gTs = 0; |
| 39 |
0 |
| double eta = 0.01; |
| 40 |
0 |
| int n = get_nr_variable(); |
| 41 |
0 |
| int max_num_linesearch = 20; |
| 42 |
0 |
▶ | double *w_new = new double[n]; |
Branch: 1 branch
| Branch | True | False | Total |
| double *w_new = new double[n]; |
0 |
0 |
0 |
|
| 43 |
0 |
| double fold = *f; |
| 44 |
|
| |
| 45 |
0 |
▶ | for (int i=0;i<n;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i=0;i<n;i++) |
0 |
0 |
0 |
|
| 46 |
0 |
| gTs += s[i] * g[i]; |
| 47 |
|
| |
| 48 |
0 |
| int num_linesearch = 0; |
| 49 |
0 |
▶ | for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(num_linesearch=0; num_linesearch < max_num_linesearch; num_linesearch++) |
0 |
0 |
0 |
|
| 50 |
|
| { |
| 51 |
0 |
▶ | for (int i=0;i<n;i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (int i=0;i<n;i++) |
0 |
0 |
0 |
|
| 52 |
0 |
| w_new[i] = w[i] + alpha*s[i]; |
| 53 |
0 |
| *f = fun(w_new); |
| 54 |
0 |
▶ | if (*f - fold <= eta * alpha * gTs) |
Branch: 1 branch
| Branch | True | False | Total |
| if (*f - fold <= eta * alpha * gTs) |
0 |
0 |
0 |
|
| 55 |
0 |
| break; |
| 56 |
|
| else |
| 57 |
0 |
| alpha *= 0.5; |
| 58 |
|
| } |
| 59 |
|
| |
| 60 |
0 |
▶ | if (num_linesearch >= max_num_linesearch) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_linesearch >= max_num_linesearch) |
0 |
0 |
0 |
|
| 61 |
|
| { |
| 62 |
0 |
| *f = fold; |
| 63 |
0 |
| return 0; |
| 64 |
|
| } |
| 65 |
|
| else |
| 66 |
0 |
| memcpy(w, w_new, sizeof(double)*n); |
| 67 |
|
| |
| 68 |
0 |
▶ | delete [] w_new; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] w_new; |
0 |
0 |
0 |
|
| 69 |
0 |
| return alpha; |
| 70 |
|
| } |
| 71 |
|
| |
| 72 |
139 |
| void NEWTON::info(const char *fmt,...) |
| 73 |
|
| { |
| 74 |
|
| char buf[BUFSIZ]; |
| 75 |
|
| va_list ap; |
| 76 |
139 |
| va_start(ap,fmt); |
| 77 |
139 |
| vsprintf(buf,fmt,ap); |
| 78 |
139 |
| va_end(ap); |
| 79 |
139 |
▶ | (*newton_print_string)(buf); |
Branch: 1 branch
| Branch | True | False | Total |
| (*newton_print_string)(buf); |
139 |
0 |
139 |
|
| 80 |
139 |
| } |
| 81 |
|
| |
| 82 |
76 |
| NEWTON::NEWTON(const function *fun_obj, double eps, double eps_cg, int max_iter) |
| 83 |
|
| { |
| 84 |
76 |
| this->fun_obj=const_cast<function *>(fun_obj); |
| 85 |
76 |
| this->eps=eps; |
| 86 |
76 |
| this->eps_cg=eps_cg; |
| 87 |
76 |
| this->max_iter=max_iter; |
| 88 |
76 |
| newton_print_string = default_print; |
| 89 |
76 |
| } |
| 90 |
|
| |
| 91 |
76 |
| NEWTON::~NEWTON() |
| 92 |
|
| { |
| 93 |
76 |
| } |
| 94 |
|
| |
| 95 |
76 |
| void NEWTON::newton(double *w) |
| 96 |
|
| { |
| 97 |
76 |
▶ | int n = fun_obj->get_nr_variable(); |
Branch: 1 branch
| Branch | True | False | Total |
| int n = fun_obj->get_nr_variable(); |
76 |
0 |
76 |
|
| 98 |
|
| int i, cg_iter; |
| 99 |
|
| double step_size; |
| 100 |
|
| double f, fold, actred; |
| 101 |
76 |
| double init_step_size = 1; |
| 102 |
76 |
| int search = 1, iter = 1, inc = 1; |
| 103 |
76 |
▶ | double *s = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *s = new double[n]; |
76 |
0 |
76 |
| double *s = new double[n]; |
76 |
0 |
76 |
|
| 104 |
76 |
▶ | double *r = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *r = new double[n]; |
76 |
0 |
76 |
| double *r = new double[n]; |
76 |
0 |
76 |
|
| 105 |
76 |
▶ | double *g = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *g = new double[n]; |
76 |
0 |
76 |
| double *g = new double[n]; |
76 |
0 |
76 |
|
| 106 |
|
| |
| 107 |
76 |
| const double alpha_pcg = 0.01; |
| 108 |
76 |
▶ | double *M = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *M = new double[n]; |
76 |
0 |
76 |
| double *M = new double[n]; |
76 |
0 |
76 |
|
| 109 |
|
| |
| 110 |
|
| // calculate gradient norm at w=0 for stopping condition. |
| 111 |
76 |
▶ | double *w0 = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *w0 = new double[n]; |
76 |
0 |
76 |
| double *w0 = new double[n]; |
76 |
0 |
76 |
|
| 112 |
1064 |
▶ | for (i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<n; i++) |
988 |
76 |
1064 |
|
| 113 |
988 |
| w0[i] = 0; |
| 114 |
76 |
▶ | fun_obj->fun(w0); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->fun(w0); |
76 |
0 |
76 |
|
| 115 |
76 |
▶ | fun_obj->grad(w0, g); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->grad(w0, g); |
76 |
0 |
76 |
|
| 116 |
76 |
▶ | double gnorm0 = dnrm2_(&n, g, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| double gnorm0 = dnrm2_(&n, g, &inc); |
76 |
0 |
76 |
|
| 117 |
76 |
▶ | delete [] w0; |
Branch: 1 branch
| Branch | True | False | Total |
| delete [] w0; |
76 |
0 |
76 |
|
| 118 |
|
| |
| 119 |
76 |
▶ | f = fun_obj->fun(w); |
Branch: 1 branch
| Branch | True | False | Total |
| f = fun_obj->fun(w); |
76 |
0 |
76 |
|
| 120 |
76 |
▶ | fun_obj->grad(w, g); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->grad(w, g); |
76 |
0 |
76 |
|
| 121 |
76 |
▶ | double gnorm = dnrm2_(&n, g, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| double gnorm = dnrm2_(&n, g, &inc); |
76 |
0 |
76 |
|
| 122 |
76 |
▶ | info("init f %5.3e |g| %5.3e\n", f, gnorm); |
Branch: 1 branch
| Branch | True | False | Total |
| info("init f %5.3e |g| %5.3e\n", f, gnorm); |
76 |
0 |
76 |
|
| 123 |
|
| |
| 124 |
76 |
▶ | if (gnorm <= eps*gnorm0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (gnorm <= eps*gnorm0) |
59 |
17 |
76 |
|
| 125 |
59 |
| search = 0; |
| 126 |
|
| |
| 127 |
122 |
▶ | while (iter <= max_iter && search) |
Branch: 2 branches
| Branch | True | False | Total |
| while (iter <= max_iter && search) |
122 |
0 |
122 |
| while (iter <= max_iter && search) |
63 |
59 |
122 |
|
| 128 |
|
| { |
| 129 |
63 |
▶ | fun_obj->get_diag_preconditioner(M); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->get_diag_preconditioner(M); |
63 |
0 |
63 |
|
| 130 |
882 |
▶ | for(i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for(i=0; i<n; i++) |
819 |
63 |
882 |
|
| 131 |
819 |
| M[i] = (1-alpha_pcg) + alpha_pcg*M[i]; |
| 132 |
63 |
▶ | cg_iter = pcg(g, M, s, r); |
Branch: 1 branch
| Branch | True | False | Total |
| cg_iter = pcg(g, M, s, r); |
63 |
0 |
63 |
|
| 133 |
|
| |
| 134 |
63 |
| fold = f; |
| 135 |
63 |
▶ | step_size = fun_obj->linesearch_and_update(w, s, &f, g, init_step_size); |
Branch: 1 branch
| Branch | True | False | Total |
| step_size = fun_obj->linesearch_and_update(w, s, &f, g, init_step_size); |
63 |
0 |
63 |
|
| 136 |
|
| |
| 137 |
63 |
▶ | if (step_size == 0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (step_size == 0) |
0 |
63 |
63 |
|
| 138 |
|
| { |
| 139 |
0 |
▶ | info("WARNING: line search fails\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: line search fails\n"); |
0 |
0 |
0 |
|
| 140 |
0 |
| break; |
| 141 |
|
| } |
| 142 |
|
| |
| 143 |
63 |
▶ | fun_obj->grad(w, g); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->grad(w, g); |
63 |
0 |
63 |
|
| 144 |
63 |
▶ | gnorm = dnrm2_(&n, g, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| gnorm = dnrm2_(&n, g, &inc); |
63 |
0 |
63 |
|
| 145 |
|
| |
| 146 |
63 |
▶ | info("iter %2d f %5.3e |g| %5.3e CG %3d step_size %4.2e \n", iter, f, gnorm, cg_iter, step_size); |
Branch: 1 branch
| Branch | True | False | Total |
| info("iter %2d f %5.3e |g| %5.3e CG %3d step_size %4.2e \n", iter, f, gnorm, cg_iter, step_size); |
63 |
0 |
63 |
|
| 147 |
|
| |
| 148 |
63 |
▶ | if (gnorm <= eps*gnorm0) |
Branch: 1 branch
| Branch | True | False | Total |
| if (gnorm <= eps*gnorm0) |
17 |
46 |
63 |
|
| 149 |
17 |
| break; |
| 150 |
46 |
▶ | if (f < -1.0e+32) |
Branch: 1 branch
| Branch | True | False | Total |
| if (f < -1.0e+32) |
0 |
46 |
46 |
|
| 151 |
|
| { |
| 152 |
0 |
▶ | info("WARNING: f < -1.0e+32\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: f < -1.0e+32\n"); |
0 |
0 |
0 |
|
| 153 |
0 |
| break; |
| 154 |
|
| } |
| 155 |
46 |
| actred = fold - f; |
| 156 |
46 |
▶ | if (fabs(actred) <= 1.0e-12*fabs(f)) |
Branch: 1 branch
| Branch | True | False | Total |
| if (fabs(actred) <= 1.0e-12*fabs(f)) |
0 |
46 |
46 |
|
| 157 |
|
| { |
| 158 |
0 |
▶ | info("WARNING: actred too small\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: actred too small\n"); |
0 |
0 |
0 |
|
| 159 |
0 |
| break; |
| 160 |
|
| } |
| 161 |
|
| |
| 162 |
46 |
| iter++; |
| 163 |
|
| } |
| 164 |
|
| |
| 165 |
76 |
▶ | if(iter >= max_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if(iter >= max_iter) |
0 |
76 |
76 |
|
| 166 |
0 |
▶ | info("\nWARNING: reaching max number of Newton iterations\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("\nWARNING: reaching max number of Newton iterations\n"); |
0 |
0 |
0 |
|
| 167 |
|
| |
| 168 |
76 |
▶ | delete[] g; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] g; |
76 |
0 |
76 |
|
| 169 |
76 |
▶ | delete[] r; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] r; |
76 |
0 |
76 |
|
| 170 |
76 |
▶ | delete[] s; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] s; |
76 |
0 |
76 |
|
| 171 |
76 |
▶ | delete[] M; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] M; |
76 |
0 |
76 |
|
| 172 |
76 |
| } |
| 173 |
|
| |
| 174 |
63 |
| int NEWTON::pcg(double *g, double *M, double *s, double *r) |
| 175 |
|
| { |
| 176 |
63 |
| int i, inc = 1; |
| 177 |
63 |
▶ | int n = fun_obj->get_nr_variable(); |
Branch: 1 branch
| Branch | True | False | Total |
| int n = fun_obj->get_nr_variable(); |
63 |
0 |
63 |
|
| 178 |
63 |
| double one = 1; |
| 179 |
63 |
▶ | double *d = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *d = new double[n]; |
63 |
0 |
63 |
| double *d = new double[n]; |
63 |
0 |
63 |
|
| 180 |
63 |
▶ | double *Hd = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *Hd = new double[n]; |
63 |
0 |
63 |
| double *Hd = new double[n]; |
63 |
0 |
63 |
|
| 181 |
|
| double zTr, znewTrnew, alpha, beta, cgtol, dHd; |
| 182 |
63 |
▶ | double *z = new double[n]; |
Branch: 2 branches
| Branch | True | False | Total |
| double *z = new double[n]; |
63 |
0 |
63 |
| double *z = new double[n]; |
63 |
0 |
63 |
|
| 183 |
63 |
| double Q = 0, newQ, Qdiff; |
| 184 |
|
| |
| 185 |
882 |
▶ | for (i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<n; i++) |
819 |
63 |
882 |
|
| 186 |
|
| { |
| 187 |
819 |
| s[i] = 0; |
| 188 |
819 |
| r[i] = -g[i]; |
| 189 |
819 |
| z[i] = r[i] / M[i]; |
| 190 |
819 |
| d[i] = z[i]; |
| 191 |
|
| } |
| 192 |
|
| |
| 193 |
63 |
▶ | zTr = ddot_(&n, z, &inc, r, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| zTr = ddot_(&n, z, &inc, r, &inc); |
63 |
0 |
63 |
|
| 194 |
63 |
| double gMinv_norm = sqrt(zTr); |
| 195 |
63 |
| cgtol = min(eps_cg, sqrt(gMinv_norm)); |
| 196 |
63 |
| int cg_iter = 0; |
| 197 |
63 |
| int max_cg_iter = max(n, 5); |
| 198 |
|
| |
| 199 |
190 |
▶ | while (cg_iter < max_cg_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| while (cg_iter < max_cg_iter) |
190 |
0 |
190 |
|
| 200 |
|
| { |
| 201 |
190 |
| cg_iter++; |
| 202 |
|
| |
| 203 |
190 |
▶ | fun_obj->Hv(d, Hd); |
Branch: 1 branch
| Branch | True | False | Total |
| fun_obj->Hv(d, Hd); |
190 |
0 |
190 |
|
| 204 |
190 |
▶ | dHd = ddot_(&n, d, &inc, Hd, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| dHd = ddot_(&n, d, &inc, Hd, &inc); |
190 |
0 |
190 |
|
| 205 |
|
| // avoid 0/0 in getting alpha |
| 206 |
190 |
▶ | if (dHd <= 1.0e-16) |
Branch: 1 branch
| Branch | True | False | Total |
| if (dHd <= 1.0e-16) |
0 |
190 |
190 |
|
| 207 |
0 |
| break; |
| 208 |
|
| |
| 209 |
190 |
| alpha = zTr/dHd; |
| 210 |
190 |
▶ | daxpy_(&n, &alpha, d, &inc, s, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| daxpy_(&n, &alpha, d, &inc, s, &inc); |
190 |
0 |
190 |
|
| 211 |
190 |
| alpha = -alpha; |
| 212 |
190 |
▶ | daxpy_(&n, &alpha, Hd, &inc, r, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| daxpy_(&n, &alpha, Hd, &inc, r, &inc); |
190 |
0 |
190 |
|
| 213 |
|
| |
| 214 |
|
| // Using quadratic approximation as CG stopping criterion |
| 215 |
190 |
▶ | newQ = -0.5*(ddot_(&n, s, &inc, r, &inc) - ddot_(&n, s, &inc, g, &inc)); |
Branch: 2 branches
| Branch | True | False | Total |
| newQ = -0.5*(ddot_(&n, s, &inc, r, &inc) - ddot_(&n, s, &inc, g, &inc)); |
190 |
0 |
190 |
| newQ = -0.5*(ddot_(&n, s, &inc, r, &inc) - ddot_(&n, s, &inc, g, &inc)); |
190 |
0 |
190 |
|
| 216 |
190 |
| Qdiff = newQ - Q; |
| 217 |
190 |
▶ | if (newQ <= 0 && Qdiff <= 0) |
Branch: 2 branches
| Branch | True | False | Total |
| if (newQ <= 0 && Qdiff <= 0) |
190 |
0 |
190 |
| if (newQ <= 0 && Qdiff <= 0) |
190 |
0 |
190 |
|
| 218 |
|
| { |
| 219 |
190 |
▶ | if (cg_iter * Qdiff >= cgtol * newQ) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cg_iter * Qdiff >= cgtol * newQ) |
63 |
127 |
190 |
|
| 220 |
63 |
| break; |
| 221 |
|
| } |
| 222 |
|
| else |
| 223 |
|
| { |
| 224 |
0 |
▶ | info("WARNING: quadratic approximation > 0 or increasing in CG\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: quadratic approximation > 0 or increasing in CG\n"); |
0 |
0 |
0 |
|
| 225 |
0 |
| break; |
| 226 |
|
| } |
| 227 |
127 |
| Q = newQ; |
| 228 |
|
| |
| 229 |
1778 |
▶ | for (i=0; i<n; i++) |
Branch: 1 branch
| Branch | True | False | Total |
| for (i=0; i<n; i++) |
1651 |
127 |
1778 |
|
| 230 |
1651 |
| z[i] = r[i] / M[i]; |
| 231 |
127 |
▶ | znewTrnew = ddot_(&n, z, &inc, r, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| znewTrnew = ddot_(&n, z, &inc, r, &inc); |
127 |
0 |
127 |
|
| 232 |
127 |
| beta = znewTrnew/zTr; |
| 233 |
127 |
▶ | dscal_(&n, &beta, d, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| dscal_(&n, &beta, d, &inc); |
127 |
0 |
127 |
|
| 234 |
127 |
▶ | daxpy_(&n, &one, z, &inc, d, &inc); |
Branch: 1 branch
| Branch | True | False | Total |
| daxpy_(&n, &one, z, &inc, d, &inc); |
127 |
0 |
127 |
|
| 235 |
127 |
| zTr = znewTrnew; |
| 236 |
|
| } |
| 237 |
|
| |
| 238 |
63 |
▶ | if (cg_iter == max_cg_iter) |
Branch: 1 branch
| Branch | True | False | Total |
| if (cg_iter == max_cg_iter) |
0 |
63 |
63 |
|
| 239 |
0 |
▶ | info("WARNING: reaching maximal number of CG steps\n"); |
Branch: 1 branch
| Branch | True | False | Total |
| info("WARNING: reaching maximal number of CG steps\n"); |
0 |
0 |
0 |
|
| 240 |
|
| |
| 241 |
63 |
▶ | delete[] d; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] d; |
63 |
0 |
63 |
|
| 242 |
63 |
▶ | delete[] Hd; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] Hd; |
63 |
0 |
63 |
|
| 243 |
63 |
▶ | delete[] z; |
Branch: 1 branch
| Branch | True | False | Total |
| delete[] z; |
63 |
0 |
63 |
|
| 244 |
|
| |
| 245 |
63 |
| return cg_iter; |
| 246 |
|
| } |
| 247 |
|
| |
| 248 |
76 |
| void NEWTON::set_print_string(void (*print_string) (const char *buf)) |
| 249 |
|
| { |
| 250 |
76 |
| newton_print_string = print_string; |
| 251 |
76 |
| } |