Skip to content
Snippets Groups Projects
Commit b794f878 authored by asilador's avatar asilador
Browse files

Increased convergence speed by modifying armijo's rule to include beta^m

parent a4e1f547
No related branches found
No related tags found
No related merge requests found
...@@ -8,9 +8,12 @@ b = np.transpose(b) #make b a column vector ...@@ -8,9 +8,12 @@ b = np.transpose(b) #make b a column vector
D = np.asmatrix(np.ones(np.size(b))) D = np.asmatrix(np.ones(np.size(b)))
m = 0 m = 0
# Make a guess for x vector # Make a guess for x vector
x = np.asmatrix(np.zeros(np.size(b))) #x = np.asmatrix(np.zeros(np.size(b)))
x = np.transpose(x) #make column vector #x = np.transpose(x) #make column vector
alpha0 = 10 #x=np.matrix(np.random.rand(5,1))
x = np.transpose(np.asmatrix(np.ones(np.size(b)))*0.1)
alpha0 = 1000.0
count = 0 count = 0
# Define f(x) # Define f(x)
def f(Q,b,c,x): def f(Q,b,c,x):
...@@ -24,11 +27,11 @@ def gradf(Q,b,x): ...@@ -24,11 +27,11 @@ def gradf(Q,b,x):
def armijo(alpha0,Q,b,c,D,m): def armijo(alpha0,Q,b,c,D,m):
alpha = alpha0 alpha = alpha0
print('alpha is ', alpha) #print('alpha is ', alpha)
s = 1 s = 1.0
sigma = 10e-1 sigma = 1.0
beta = 1.0/2 beta = 1.0/2
while f(Q,b,c,x+np.transpose(alpha*D)) > f(Q,b,c,x) + sigma*alpha*(np.transpose(gradf(Q,b,x)))*np.transpose(D): while f(Q,b,c,x+np.transpose(beta**m*alpha*D)) > f(Q,b,c,x) + sigma*beta**m*alpha*(np.transpose(gradf(Q,b,x)))*np.transpose(D):
m+=1 m+=1
#print('m is ', m) #print('m is ', m)
alpha = beta**m*s alpha = beta**m*s
...@@ -45,15 +48,18 @@ def countval(): ...@@ -45,15 +48,18 @@ def countval():
return count return count
# Begin Gradient Descent Algorithm # Begin Gradient Descent Algorithm
def grad_opt(epsilon,x,count): def grad_opt(epsilon,x,count,alpha):
alpha = armijo(alpha0,Q,b,c,D,m)
xnew = x
#print('alpha is ', alpha) #print('alpha is ', alpha)
while np.linalg.norm(gradf(Q,b,x))>= epsilon: while np.linalg.norm(gradf(Q,b,x))>= epsilon:
alpha = armijo(alpha,Q,b,c,D,m)
#print('alpha0 is ', alpha)
count += 1 count += 1
#print('f(x) is ', f(Q,b,c,x)) if count%1000==0:
print('norm of gradf(x) is ', np.linalg.norm(gradf(Q,b,x))) print('f(x) is ', f(Q,b,c,x))
xnew -= alpha*gradf(Q,b,x)
#print('norm of gradf(x) is ', np.linalg.norm(gradf(Q,b,x)))
x -= alpha*gradf(Q,b,x)
alpha = alpha0
print('Done') print('Done')
print('x* is ', x) print('x* is ', x)
...@@ -65,10 +71,10 @@ def grad_opt(epsilon,x,count): ...@@ -65,10 +71,10 @@ def grad_opt(epsilon,x,count):
def run(epsilon): def run(epsilon):
xstart = xval() xstart = xval()
countstart = countval() countstart = countval()
grad_opt(epsilon,xstart,countstart) grad_opt(epsilon,xstart,countstart,alpha0)
return 0 return 0
run(0.8895) run(0.889)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment