Skip to content

Commit

Permalink
In line serach the initial alpha can be restricted to avoid overflow …
Browse files Browse the repository at this point in the history
…in the forward model if one has this information. It saves un-neccessary alpha back tracking in particular in the initial step in BFGS.
  • Loading branch information
LutzGross committed Nov 6, 2024
1 parent d738216 commit e3a3bfa
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 3 deletions.
18 changes: 17 additions & 1 deletion escript/py_src/costfunctions.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,6 @@ def getStatistics(self):
out="Number of cost function evaluations: %d\n" % self.Value_calls
out+="Number of gradient evaluations: %d\n" % self.Gradient_calls
out+="Number of inverse Hessian evaluations: %d\n" % self.InverseHessianApproximation_calls
out+="Number of gradient evaluations: %d\n" % self.Gradient_calls
out+="Number of inner product evaluations: %d\n" % self.DualProduct_calls
out+="Number of argument evaluations: %d\n" % self.Arguments_calls
out+="Number of norm evaluations: %d" % self.Norm_calls
Expand Down Expand Up @@ -264,6 +263,23 @@ def getGradient(self, m, *args):
"""
raise NotImplementedError

def getSqueezeFactor(self, m, p, *args):
"""
The new solution is calculated as m+a*p with a>0. This function allows to provide an upper bound
for a to make sure that m+a*p is valid typically to avoid overflow when the cost function is evaluated.
the solver will take action to make sure that the value of a is not too small.
:param m: a solution approximation
:type m: m-type
:param p: an increment to the solution
:type m: m-type
:param args: pre-calculated values for ``m`` from `getArgumentsAndCount()`
:rtype: positive ``float`` or None
:note: Overwrite this method to implement a cost function.
"""
return None

def getInverseHessianApproximation(self, r, m, *args, initializeHessian = True):
"""
returns an approximate evaluation *p* of the inverse of the Hessian
Expand Down
11 changes: 9 additions & 2 deletions escript/py_src/minimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -992,8 +992,15 @@ def run(self, m):
else:
alphaMin = self._relAlphaMin
self.getLineSearch().setOptions(alphaMin=alphaMin)
alpha = max(alpha, alphaMin*1.10)
self.logger.debug("Starting line search with alphaMin, alpha = %g, %g" % (alphaMin, alpha))
alpha = max(alpha, alphaMin * 1.10)
alpha_s = self.getCostFunction().getSqueezeFactor(m, p)
if not alpha_s is None:
assert alpha_s >0
self.logger.debug("Safe alpha value given as %g." % (alpha_s,))
alpha_s*=0.5
if alpha_s > alphaMin:
alpha = min(alpha, alpha_s)
self.logger.info("Starting line search with alpha = %g."% (alpha, ))
try:
phi_new = self.getLineSearch().run(phi, alpha)
alpha = phi_new.alpha
Expand Down

0 comments on commit e3a3bfa

Please sign in to comment.