I am creating a basic Newton-method algorithm for an unconstrained optimization problem, and my results from the algorithm are not what I expected. It is a simple objective function so it is clear that the algorithm should converge on (1,1). This is confirmed by a gradient descent algorithm I created previously, here:
def grad_descent(x, t, count, magnitude):
xvalues.append(x)
gradvalues.append(np.array([dfx1(x), dfx2(x)]))
fvalues.append(f(x))
temp=x-t*dfx(x)
x = temp
magnitude = mag(dfx(x))
count+=1
return xvalues, gradvalues, fvalues, count
My attempt at creating an algorithm for Newtons-Method is here:
def newton(x, t, count, magnitude):
xvalues=[]
gradvalues=[]
fvalues=[]
temp=x-f(x)/dfx(x)
while count < 10:
xvalues.append(x)
gradvalues.append(dfx(x))
fvalues.append(f(x))
temp=x-t*f(x)/dfx(x)
x = temp
magnitude = mag(dfx(x))
count+=1
if count > 100:
break
return xvalues, gradvalues, fvalues, count
Here is the objective function and gradient function:
f = lambda x: 100*np.square(x[1]-np.square(x[0])) + np.square((1-x[0]))
dfx = lambda x: np.array([-400*x[0]*x[1]+400*np.power(x[0],3)+2*x[0]-2, 200*(x[1]-np.square(x[0]))])
Here are the initial conditions. Note that alpha and beta are not used in the newton method.
x0, t0, alpha, beta, count = np.array([-1.1, 1.1]), 1, .15, .7, 1
magnitude = mag(np.array([dfx1(x0), dfx2(x0)]))
To call the function:
xvalues, gradvalues, fvalues, iterations = newton(x0, t0, count, magnitude)
This produce very strange results. Here are the first 10 iterations of the xvalues, gradient values, and function solution for its respective x input:
[array([-1.1, 1.1]), array([-0.99315589, 1.35545455]), array([-1.11651296, 1.11709035]), array([-1.01732476, 1.35478987]), array([-1.13070578, 1.13125051]), array([-1.03603697, 1.35903467]), array([-1.14368874, 1.14364506]), array([-1.05188162, 1.36561528]), array([-1.15600558, 1.15480705]), array([-1.06599492, 1.37360245])]
[array([-52.6, -22. ]), array([142.64160215, 73.81918332]), array([-62.07323963, -25.90216846]), array([126.11789251, 63.96803995]), array([-70.85773749, -29.44900758]), array([114.31050737, 57.13241151]), array([-79.48668009, -32.87577304]), array([104.93863096, 51.83206539]), array([-88.25737032, -36.308371 ]), array([97.03403558, 47.45145765])]
[5.620000000000003, 17.59584998020613, 6.156932949106968, 14.29937453260906, 6.7080172227439725, 12.305727666787176, 7.297442528545537, 10.926625703722639, 7.944104584786208, 9.89743708419569]
Here is the final output:
final_value = print('Final set of x values: ', xvalues[-1])
final_grad = print('Final gradient values: ', gradvalues[-1])
final_f = print('Final value of the object function with optimized inputs: ', fvalues[-1])
final_grad_mag = print('Final magnitude of the gradient with optimized inputs: ', mag(np.array([dfx1(xvalues[-1]), dfx2(xvalues[-1])])))
total_iterations = print('Total iterations: ', iterations)
a 3d plot is shown here code:
x = np.array([i[0] for i in xvalues])
y = np.array([i[1] for i in xvalues])
z = np.array(fvalues)
fig = plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(x, y, z, label='Newton Method')
ax.legend()
Is the reasoning for this because the initial guess is so close to the optimal point, or is there some error in my algorithm that I am not catching? Any advice would be greatly appreciated. It looks like the solution may even be oscillating, but it is difficult to tell
So I finally figured out what was going on with this. It was all about what data structures Python was storing my variables as. As such, I set all my values to 'float32' and initialized the variables being iterated. Working code is here:
Note: a lambda function is an anonymous function useful for single-line expressions
Output:
I think I've found part of the problem. I was using the incorrect Newton's algorithm. While before I was using:
x{k+1} = x{k}-f(x)⁄∇f(x)
The correct update is:
x{k+1} = x{k} - [f''(x{k})]-1f'(x{k})
When I changed this, the result still diverges, but it is slightly better. The new function is here:
The nearest the solution gets to converging is after the first step, where it goes to (-1.05, 1.1). However, it still diverges. I'm never worked with the Newton-method so I am unsure if this is as accurate as the algorithm is meant to get or not.
I am now certain that there is something wrong with the python code. I decided to implement the algorithm in Matlab and it seems to work fine. Here is that code:
Output: