diff --git a/CFiles/csminwel.c b/CFiles/csminwel.c old mode 100644 new mode 100755 index 6bae1803108d08100d1a814bdf11bc845cada849..1b3597d9936b2dacb27f10828c627f30ccbc9fc0 --- a/CFiles/csminwel.c +++ b/CFiles/csminwel.c @@ -106,7 +106,7 @@ static char filename_sp2vecs[STRLEN]; //Two vectors. 1st row: numerical gradie -#define MAX_NUM_BADCASES 3 +#define MAX_NUM_POSSIBLE_BADCASES 250 //After this number, reset inverse hessian (especially dealing with the case for "Correct for low angle"). #define EPS (1.0e-10) //Small number to rectify special case of converging to exactly zero function value. #define TERMINATEVALUE (1.0e+300) //If the value of the objective function at the intial value is greater than this, terminates the program. void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), @@ -205,7 +205,7 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), /* if stuck, give it another try by perturbing Hessian */ memcpy(Hcliff,H,nn*sizeof(double)); for (i=0; i<nn; i+=n+1) - Hcliff[i] *= 1+rand()*randmax; //DDDDebugging. Hcliff[i] *= 1+0.5; + Hcliff[i] *= 1+rand()*randmax; //Random search. DDDDebugging. Hcliff[i] *= 1+0.5; #ifdef VERBOSE_WARNINGS printf("======= Random search takes place now. =======\n"); @@ -372,13 +372,16 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), if ((int)*itct > nit) { #ifdef VERBOSE_WARNINGS - printf("\nWarning: termination as the maximum number of iterations is reached.\n"); + printf("\nWarning: Termination as the maximum number of iterations %d is reached with return code %d.\n", nit, *retcodeh); + fprintf(FPTR_DEBUG, "\nWarning: Termination as the maximum number of iterations %d is reached with return code %d.\n", nit, *retcodeh); #endif done = 1; } - else if (stuck) { + else if (stuck) //stuck = 1 means fabs(*fh-f[0]) < crit, which means converged. why do we use the word "stuck" is beyond me. + { #ifdef VERBOSE_DETOUTPUT printf("\nConvergence (improvement < crit %.4e) with return code %d.\n", crit, *retcodeh); + fprintf(FPTR_DEBUG, "\nConvergence (improvement < crit %.4e) with return code %d.\n", crit, *retcodeh); #endif done = 1; @@ -388,7 +391,7 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), #ifdef VERBOSE_WARNINGS switch (*retcodeh) { case 1: - printf("\nCoverged: Zero gradient.\n"); break; + printf("\nConverged: Zero gradient.\n"); break; case 2: printf("\nWarning: Back adjustment of stepsize didn't finish.\n"); break; case 3: @@ -404,16 +407,19 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), } #endif - //=== Restarts from the initial (inverse of) Hessian when stuck for a while in bad cases. Added by TZ. - if (*retcodeh && *retcodeh != 1) - if (++cnt_n_badcases >= MAX_NUM_BADCASES) { + //=== Restarts from the initial (inverse of) Hessian when stuck for a while in bad cases. This can happen even *retcodeh=0 while stuck=0. + //if (*retcodeh && *retcodeh != 1) + if (!done) + if (cnt_n_badcases++ >= MAX_NUM_POSSIBLE_BADCASES) + { H_dm->M = H; H_dm->nrows = H_dm->ncols = n; InitializeDiagonalMatrix_lf(H_dm, GLB_sclForHess); //H_dm->flag = M_GE | M_SU | M_SL; //Hessian is symmetric. cnt_n_badcases = 0; //Reset after we restart wtih the initial Hessian. #ifdef VERBOSE_WARNINGS - printf("Hessian is reset to the initial value because the maximum number of bad cases, %d, is reached!\n", MAX_NUM_BADCASES); + printf("Hessian is reset to the initial value because the maximum number of possibly bad cases, %d, is reached with return code %d!\n", MAX_NUM_POSSIBLE_BADCASES, *retcodeh); + fprintf(FPTR_DEBUG, "Hessian is reset to the initial value because the maximum number of possibly bad cases, %d, is reached with return code %d!\n", MAX_NUM_POSSIBLE_BADCASES, *retcodeh); #endif } @@ -448,7 +454,7 @@ void csminwel(double (*fcn)(double *x, int n, double **args, int *dims), } tzDestroy(H_dm); } -#undef MAX_NUM_BADCASES +#undef MAX_NUM_POSSIBLE_BADCASES #undef EPS #undef TERMINATEVALUE diff --git a/CFiles/mathlib.c b/CFiles/mathlib.c old mode 100644 new mode 100755