int OptCGLike::checkConvg() // check convergence { NLP1* nlp = nlprob(); ColumnVector xc(nlp->getXc()); // Test 1. step tolerance double step_tol = tol.getStepTol(); double snorm = stepTolNorm(); double xnorm = Norm2(xc); double stol = step_tol*max(1.0,xnorm); if (snorm <= stol) { strcpy(mesg,"Algorithm converged - Norm of last step is less than step tolerance"); *optout << "checkConvg: snorm = " << e(snorm,12,4) << " stol = " << e(stol,12,4) << "\n"; return 1; } // Test 2. function tolerance double ftol = tol.getFTol(); double fvalue = nlp->getF(); double rftol = ftol*max(1.0,fabs(fvalue)); Real deltaf = fprev - fvalue; if (deltaf <= rftol) { strcpy(mesg,"Algorithm converged - Difference in successive fcn values less than tolerance"); *optout << "checkConvg: deltaf = " << e(deltaf,12,4) << " ftol = " << e(ftol,12,4) << "\n"; return 2; } // Test 3. gradient tolerance ColumnVector grad(nlp->getGrad()); double gtol = tol.getGTol(); double rgtol = gtol*max(1.0,fabs(fvalue)); double gnorm = Norm2(grad); if (gnorm <= rgtol) { strcpy(mesg,"Algorithm converged - Norm of gradient is less than gradient tolerance"); *optout << "checkConvg: gnorm = " << e(gnorm,12,4) << " gtol = " << e(rgtol, 12,4) << "\n"; return 3; } // Test 4. absolute gradient tolerance if (gnorm <= gtol) { strcpy(mesg,"Algorithm converged - Norm of gradient is less than gradient tolerance"); *optout << "checkConvg: gnorm = " << e(gnorm,12,4) << " gtol = " << e(gtol, 12,4) << "\n"; return 4; } // Nothing to report return 0; }