// $Id$ // // Copyright (C) 2005-2008 Greg Landrum and Rational Discovery LLC // // @@ All Rights Reserved @@ // This file is part of the RDKit. // The contents are covered by the terms of the BSD license // which is included in the file license.txt, found at the root // of the RDKit source tree. // #include #include #include "BFGSOpt.h" namespace BFGSOpt { // ------------------------------------------------------------ // an adaptation of the function lnsearch from Chapter 9 of // Numerical Recipes in C // possible return values: // -1: bad direction // 0: converged // 1: step got too small, probably converged // ------------------------------------------------------------ void linearSearch(unsigned int dim,double *oldPt,double oldVal, double *grad,double *dir,double *newPt, double &newVal, double (*func)(double *), double maxStep,int &resCode){ PRECONDITION(oldPt,"bad input array"); PRECONDITION(grad,"bad input array"); PRECONDITION(dir,"bad input array"); PRECONDITION(newPt,"bad input array"); PRECONDITION(func,"bad function"); double sum=0.0,slope=0.0,test=0.0,lambda=0.0; double lambda2=0.0,lambdaMin=0.0,tmpLambda=0.0,val2=0.0; resCode=0; // get the length of the direction vector: sum=0.0; for(unsigned int i=0;imaxStep){ for(unsigned int i=0;i=0.0){ resCode=-1; return; } test=0.0; for(unsigned int i=0;itest) test=temp; } lambdaMin = MOVETOL/test; lambda = 1.0; while(1){ //std::cout << "\t" << lambda << " " << lambdaMin << std::endl; if(lambda 0.5*lambda ){ tmpLambda = 0.5*lambda; } } } lambda2 = lambda; val2 = newVal; lambda = std::max(tmpLambda,0.1*lambda); } } }