import random import numpy as np import matplotlib.pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm # the objective function def func(x): return np.square(1-x[0])+100*np.square(x[1]-np.square(x[0])) # first order derivatives of the function def dfunc(x): df1 = -2*(1-x[0])-200*x[0]*(x[1]-np.square(x[0])) df2 = 200*(x[1]-np.square(x[0])) return np.array([df1, df2]) # the armijo algorithm def armijo(valf, grad, niters): #beta = random.random() #sigma = random.uniform(0, .5) beta = 0.5 sigma = 0.5 (miter, iter_conv) = (0, 0) conval = [0,0] val = [] objectf = [] val.append(valf) objectf.append(func(valf)) while miter <= niters: leftf = func(valf+np.power(beta, miter)*grad) rightf = func(valf) + sigma*np.power(beta, miter)*dfunc(valf).dot(grad) if leftf-rightf <= 0: iter_conv = miter conval = valf+np.power(beta, iter_conv)*grad break miter += 1 val.append(conval) objectf.append(func(conval)) return conval, func(conval), iter_conv, val, objectf # initialization start = np.array([-2, 2]) direction = np.array([1, 1]) maximum_iterations = 60 converge_value, minimal, no_iter, val, objf = armijo(start, direction, maximum_iterations) print("The value, minimal and number of iterations are " + str(converge_value) + \ ", " + str(minimal) + ", " + str(no_iter))
We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy. Accept Learn more