Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
Programming_Assignments
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
asilador
Programming_Assignments
Commits
442752b7
Commit
442752b7
authored
8 years ago
by
asilador
Browse files
Options
Downloads
Patches
Plain Diff
Added part b and cleaned up code
parent
3694663a
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
Assignment 1/assignment1.py
+16
-7
16 additions, 7 deletions
Assignment 1/assignment1.py
with
16 additions
and
7 deletions
Assignment 1/assignment1.py
+
16
−
7
View file @
442752b7
...
...
@@ -7,10 +7,6 @@ c = np.asmatrix(np.loadtxt('c.txt'))
b
=
np
.
transpose
(
b
)
#make b a column vector
m
=
1
# Initialize iteration counter
global
x
global
count
# Define f(x)
def
f
(
Q
,
b
,
c
,
x
):
return
np
.
transpose
(
x
)
*
Q
*
x
+
np
.
transpose
(
b
)
*
x
+
c
...
...
@@ -33,9 +29,7 @@ def armijo(alpha0,Q,b,c,D,m,x):
alpha
=
beta
**
m
*
s
#print('alpha is ', alpha)
return
alpha
# Begin Gradient Descent Algorithm
def
grad_opt
(
epsilon
,
x
,
count
,
alpha
):
#print('alpha is ', alpha)
...
...
@@ -58,6 +52,18 @@ def grad_opt(epsilon,x,count,alpha):
print
'
epsilon is
'
,
epsilon
return
0
#part b
#gradient of f * x = zero vector. solve for x by getting inverse of grad_f
# grad f = 2*Q*x + b
#setting to zero gives us 2*Q*x = -b
def
partb
(
Q
,
b
,
x
):
Qinv
=
np
.
linalg
.
inv
(
2
*
Q
)
ans
=
Qinv
*
(
-
b
)
print
'
\n
Solving for x* using the inverse of 2*Q*x=-b is
'
,
ans
print
f
(
Q
,
b
,
c
,
x
)
print
'
f(x*) is
'
,
f
(
Q
,
b
,
c
,
ans
)
return
0
def
run
(
epsilon
):
# Make a guess for x vector
#x = np.asmatrix(np.zeros(np.size(b)))
...
...
@@ -66,7 +72,10 @@ def run(epsilon):
x
=
np
.
transpose
(
np
.
asmatrix
(
np
.
ones
(
np
.
size
(
b
)))
*
1
)
count
=
0
alpha0
=
1.0
print
'
******** Part A ********
'
grad_opt
(
epsilon
,
x
,
count
,
alpha0
)
print
'
******** Part B ********
'
partb
(
Q
,
b
,
x
)
return
0
run
(
0.00001
)
\ No newline at end of file
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment