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

Add ECE490 Programming Assignment 1

parent b0c9b636
No related branches found
No related tags found
No related merge requests found
1.596289104158055361e+00 1.519878384043357400e+00 1.401695563741551576e+00 7.558383570476635560e-01 1.694225410021412026e+00
1.519878384043357400e+00 2.026586923317563294e+00 1.361733716144521544e+00 1.022542464160217435e+00 1.777975206631031035e+00
1.401695563741551576e+00 1.361733716144521544e+00 1.654686339316024934e+00 7.738649790748873825e-01 1.458829996658279615e+00
7.558383570476635560e-01 1.022542464160217435e+00 7.738649790748873825e-01 7.629588167864660431e-01 8.693506443055185606e-01
1.694225410021412026e+00 1.777975206631031035e+00 1.458829996658279615e+00 8.693506443055185606e-01 2.140362235979225591e+00
import numpy as np
# Initialize iteration counter
# Import text files
Q = np.asmatrix(np.loadtxt('Q.txt'))
b = np.asmatrix(np.loadtxt('b.txt'))
c = np.asmatrix(np.loadtxt('c.txt'))
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 = 1
count = 0
# Define f(x)
def f(Q,b,c,x):
return np.transpose(x)*Q*x + np.transpose(b)*x + c
# Define gradient f(x)
def gradf(Q,b,x):
return 2*Q*x + b
# Define algorithm for Armijos rule
def armijo(alpha0,Q,b,c,D,m):
alpha = alpha0
s = 1
sigma = 10e-1
beta = 1/10
if 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):
return alpha
else:
m+=1
print('m is ', m)
alpha = beta**m*s
alpha = armijo(alpha,Q,b,c,D,m)
return alpha
def xval():
return x
def countval():
return count
# Begin Gradient Descent Algorithm
def grad_opt(epsilon):
x = xval()
count = countval()
alpha = armijo(alpha0,Q,b,c,D,m)
print('m is ', m)
f1 = f(Q,b,c,x)
x = x - alpha*np.transpose(gradf(Q,b,x))*(np.identity(np.size(b)))*gradf(Q,b,x)
if np.linalg.norm(gradf(Q,b,x))>= epsilon:
count += 1
print('f(x*) is ', f(Q,b,c,x))
grad_opt(epsilon)
return 0
else:
print('Done')
print('x* is ', x)
print('f(x*) is ', f(Q,b,c,x))
print('epsilon is ', epsilon)
return 0
grad_opt(0.1)
#def simpgrad_opt(e)
File added
3.192138092317694520e-01
1.421810329431174580e-01
5.075663514331025805e-01
5.376033097754365775e-01
3.500027406765493510e-01
9.541640489231357769e-01
\ No newline at end of file
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