From b794f878fef03ee0993ad6a48e159305806f47bd Mon Sep 17 00:00:00 2001 From: asilador <asilador@illinois.edu> Date: Wed, 15 Feb 2017 10:45:56 -0600 Subject: [PATCH] Increased convergence speed by modifying armijo's rule to include beta^m --- Assignment 1/assignment1.py | 36 +++++++++++++++++++++--------------- 1 file changed, 21 insertions(+), 15 deletions(-) diff --git a/Assignment 1/assignment1.py b/Assignment 1/assignment1.py index ec014de..7938956 100644 --- a/Assignment 1/assignment1.py +++ b/Assignment 1/assignment1.py @@ -8,9 +8,12 @@ b = np.transpose(b) #make b a column vector D = np.asmatrix(np.ones(np.size(b))) m = 0 # Make a guess for x vector -x = np.asmatrix(np.zeros(np.size(b))) -x = np.transpose(x) #make column vector -alpha0 = 10 +#x = np.asmatrix(np.zeros(np.size(b))) +#x = np.transpose(x) #make column vector +#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 # Define f(x) def f(Q,b,c,x): @@ -24,11 +27,11 @@ def gradf(Q,b,x): def armijo(alpha0,Q,b,c,D,m): alpha = alpha0 - print('alpha is ', alpha) - s = 1 - sigma = 10e-1 + #print('alpha is ', alpha) + s = 1.0 + sigma = 1.0 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 #print('m is ', m) alpha = beta**m*s @@ -45,15 +48,18 @@ def countval(): return count # Begin Gradient Descent Algorithm -def grad_opt(epsilon,x,count): - alpha = armijo(alpha0,Q,b,c,D,m) - xnew = x +def grad_opt(epsilon,x,count,alpha): #print('alpha is ', alpha) while np.linalg.norm(gradf(Q,b,x))>= epsilon: + alpha = armijo(alpha,Q,b,c,D,m) + #print('alpha0 is ', alpha) count += 1 - #print('f(x) is ', f(Q,b,c,x)) - print('norm of gradf(x) is ', np.linalg.norm(gradf(Q,b,x))) - xnew -= alpha*gradf(Q,b,x) + if count%1000==0: + print('f(x) is ', f(Q,b,c,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('x* is ', x) @@ -65,10 +71,10 @@ def grad_opt(epsilon,x,count): def run(epsilon): xstart = xval() countstart = countval() - grad_opt(epsilon,xstart,countstart) + grad_opt(epsilon,xstart,countstart,alpha0) return 0 -run(0.8895) +run(0.889) -- GitLab