8 #error "Cannot compile with HAVE_CONFIG_H symbol! run configure first!"
15 #include "../Exceptions/exceptions.h"
17 #include "../MemOps/MemOps.h"
30 IssmDouble si,gold,intervalgold,oldintervalgold;
39 int nsteps = optpars.
nsteps;
40 int nsize = optpars.
nsize;
53 _printf0_(
" x | Cost function f(x) | List of contributions\n");
56 for(
int n=0;n<nsteps;n++){
59 _printf0_(
"====================== step "<< n+1 <<
"/" << nsteps <<
" ===============================\n");
66 cout<<setprecision(5);
70 fxmin = (*g)(&G,X0,usr);
if(xIsNan<IssmDouble>(fxmin))
_error_(
"Function evaluation returned NaN");
74 for(
int i=0;i<nsize;i++) X[i]=X0[i]+xmax*G[i];
75 fxmax = (*f)(X,usr);
if (xIsNan<IssmDouble>(fxmax))
_error_(
"Function evaluation returned NaN");
79 if(!xIsNan<IssmDouble>(cm_jump[n]) && (xmin==0) && (fxmax/fxmin)<cm_jump[n]){
80 for(
int i=0;i<nsize;i++) X0[i]=X0[i]+xmax*G[i];
81 xDelete<IssmDouble>(G);
87 seps=sqrt(DBL_EPSILON);
89 gold=0.5*(3.0-sqrt(5.0));
93 x1=xmin+gold*(xmax-xmin);
100 for(
int i=0;i<nsize;i++) X[i]=X0[i]+x*G[i];
101 fxbest = (*f)(X,usr);
if(xIsNan<IssmDouble>(fxbest))
_error_(
"Function evaluation returned NaN");
110 tol1=seps*sqrt(pow(xbest,2))+tolerance/3.0;
116 if (!xIsNan<IssmDouble>(cm_jump[n]) && (xmin==0) && ((fxbest/fxmin)<cm_jump[n])){
123 bool goldenflag=
true;
126 if (sqrt(pow(intervalgold,2))>tol1){
130 parab_num=(xbest-x1)*(xbest-x1)*(fxbest-fx2)-(xbest-x2)*(xbest-x2)*(fxbest-fx1);;
131 parab_den=2.0*(xbest-x1)*(fxbest-fx2)-2.0*(xbest-x2)*(fxbest-fx1);
135 parab_num=-parab_num;
137 parab_den=sqrt(pow(parab_den,2));
138 oldintervalgold=intervalgold;
139 intervalgold=distance;
143 if (( sqrt(pow(parab_num,2)) < sqrt(pow(0.5*parab_den*oldintervalgold,2))) &&
144 (parab_num>parab_den*(xmin-xbest)+seps) &&
145 (parab_num<parab_den*(xmax-xbest)-seps)){
148 distance=parab_num/parab_den;
152 if (((x-xmin)<tol2) || ((xmax-x)<tol2)){
153 if ((xm-xbest)<0.0) si=-1;
169 intervalgold=xmin-xbest;
172 intervalgold=xmax-xbest;
174 distance=gold*intervalgold;
178 if(distance<0) si=-1;
180 if(sqrt(pow(distance,2))>tol1) x=xbest+si*sqrt(pow(distance,2));
181 else x=xbest+si*tol1;
185 for(
int i=0;i<nsize;i++) X[i]=X0[i]+x*G[i];
186 fx = (*f)(X,usr);
if(xIsNan<IssmDouble>(
fx))
_error_(
"Function evaluation returned NaN");
191 if (x>=xbest) xmin=xbest;
194 x2=xbest; fx2=fxbest;
200 if ((
fx<=fx2) || (x2==xbest)){
204 else if ( (
fx <= fx1) || (x1 == xbest) || (x1 == x2) ){
208 xm = 0.5*(xmin+xmax);
209 tol1=seps*pow(pow(xbest,2),0.5)+tolerance/3.0;
216 if (sqrt(pow(xbest-xm,2)) < (tol2-0.5*(xmax-xmin))){
220 else if (iter>=maxiter[n]){
224 else if (!xIsNan<IssmDouble>(cm_jump[n]) && (xmin==0) && ((fxbest/fxmin)<cm_jump[n])){
236 xbest=optpars.
xmin; fxbest=fxmin;
239 xbest=optpars.
xmax; fxbest=fxmax;
243 for(
int i=0;i<nsize;i++) X0[i]=X0[i]+xbest*G[i];
244 xDelete<IssmDouble>(G);
249 xDelete<IssmDouble>(X);