Created
June 12, 2019 05:02
-
-
Save terjehaukaas/f081b911867ea14ec10e40d0bcaeb610 to your computer and use it in GitHub Desktop.
newtonLineSearch()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ------------------------------------------------------------------------ | |
# 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 newtonLineSearch(F, startPoint, maxIterations, tolerance, plot): | |
# Initiate plot if requested (plot = delay if positive) | |
if plot > 0: | |
plt.clf() | |
plt.ion() | |
plt.title('Newton Search') | |
plt.grid(True) | |
plt.autoscale(True) | |
plt.ylabel('Derivative of Objective Function') | |
plt.xlabel('Design Variable') | |
xData = [] | |
fData = [] | |
# Start the loop | |
counter = 0 | |
convergence = False | |
x = startPoint | |
while not convergence: | |
# Increment counter | |
counter += 1 | |
# Exit if we are at max number of iterations | |
if counter > maxIterations: | |
print("Newton search reached the maximum number of iterations without convergence") | |
optimum = 0 | |
break | |
# Initialize the search | |
xPrevious = x | |
# Evaluate the ingredients of the Newton fraction | |
fValue = f(x, F) | |
df = h(x, F) | |
# Add point to the plot | |
if plot > 0: | |
xData.append(x) | |
fData.append(fValue) | |
plt.plot(xData, fData, 'ko', linewidth=1.0) | |
plt.show() | |
plt.pause(plot) | |
# Evaluate the Newton formula | |
x = xPrevious - fValue / df | |
# Output | |
# print("At step", counter, "the design variable value is", x) | |
# Check convergence | |
if np.abs(x - xPrevious) < tolerance: | |
convergence = True | |
optimum = x | |
# Output | |
print('\n'"Newton search done after", counter, "steps with solution", optimum) | |
# Hold the plot for a few seconds before proceeding | |
if plot > 0: | |
print('\n'"Pausing a few seconds before closing the plot...") | |
plt.pause(0.5) | |
return optimum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment