Skip to content

Instantly share code, notes, and snippets.

@terjehaukaas
Created June 12, 2019 05:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save terjehaukaas/96f2d9dbf2a8bb58ea896e66d8befbd0 to your computer and use it in GitHub Desktop.
Save terjehaukaas/96f2d9dbf2a8bb58ea896e66d8befbd0 to your computer and use it in GitHub Desktop.
armijoLineSearch()
# ------------------------------------------------------------------------
# The following function is implemented in Python by Professor Terje Haukaas
# at the University of British Columbia in Vancouver, Canada. It is made
# freely available online at terje.civil.ubc.ca together with notes,
# examples, and additional Python code. Please be cautious when using
# this code; it may contain bugs and comes without any form of warranty.
#
# The following notation applies:
# F(x) = objective function, or merit function, relevant in optimization, not in root-finding
# f(x) = dF/dx = derivative of the objective function, i.e., the function whose root is sought
# h(x) = df/dx = d^2F/dx^2 = second-order derivative, i.e., Hessian of the objective function
# ------------------------------------------------------------------------
def armijoLineSearch(F, initialStepSize, maxIterations):
# Store the incoming step size
stepSize = initialStepSize
# Objective function value at initial step size
Fprevious = F(stepSize)
# Start the loop
counter = 0
convergence = False
while not convergence:
# Increment counter
counter += 1
# Chop the step size in half
stepSize = 0.5 * stepSize
# Objective function one step back
Fback = F(stepSize)
# Check if we need to continue chopping
if Fback > Fprevious or counter == maxIterations:
optimum = 2*stepSize
convergence = True
else:
Fprevious = Fback
# Output
print('\n'"Armijo search done after", counter, "steps with solution", optimum)
return optimum
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment