-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBacktrackingLineSearch+NewtonStep.py
144 lines (106 loc) · 3.09 KB
/
BacktrackingLineSearch+NewtonStep.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import numpy as np
import math
# In[2]:
def f_seq(X):
return X.T@C@X
def f_hole(X):
return 1-np.exp(-(X.T@C@X))
def df_seq(X):
return 2*X.T@C
def df_hole(X):
return np.exp(-(X.T@C@X))*2*[email protected]@C
def C_fn(entry, size):
C_tmp=[math.pow(entry,(i-1)/(n-1)) for i in range(1,size+1)]
return np.diag(C_tmp)
def GD_with_backtracking(f,df,X,alpha):
count=0
file= open("path.dat","w+")
x_str=""
for j in range(X.shape[0]):
x_str+=str(X[j])+" "
x_str+=str(f(X))+"\n"
file.write(x_str)
while(count<10):
beta=-(df(X)/np.linalg.norm(df(X)))
# beta=-df(X) # Modification to see that the relation is not based on the mag. of gradient
print("beta",beta)
print("condition",(f(X+alpha*beta)> (f(X)+wolfe_para*df(X).T@(alpha*beta))).any())
while (f(X+alpha*beta)> (f(X)+wolfe_para*df(X).T@(alpha*beta))).any():
alpha=alpha_dec*alpha
print("alpha",alpha)
X=X+alpha*beta
# Write in the file
x_str=""
for j in range(X.shape[0]):
x_str+=str(X[j])+" "
x_str+=str(f(X))+"\n"
file.write(x_str)
print("X",X)
alpha=min(alpha_inc*alpha,delta_max)
if (abs(alpha*beta) < tolerance_theta).all():
print(count)
count+=1
file.close()
return X,alpha
def GD_with_backtracking_newton(f,df,X,alpha):
count=0
file= open("path_n.dat","w+")
x_str=""
for j in range(X.shape[0]):
x_str+=str(X[j])+" "
x_str+=str(f(X))+"\n"
file.write(x_str)
while(count<10):
beta=-(np.linalg.inv(C))@(df(X)/np.linalg.norm(df(X)))
print("beta",beta)
print("condition",(f(X+alpha*beta)> (f(X)+wolfe_para*df(alpha*beta).T)).any())
while (f(X+alpha*beta)> (f(X)+wolfe_para*df(alpha*beta).T)).any():
alpha=alpha_dec*alpha
print("alpha",alpha)
X=X+alpha*beta
# Write in the file
x_str=""
for j in range(X.shape[0]):
x_str+=str(X[j])+" "
x_str+=str(f(X))+"\n"
file.write(x_str)
print("X",X)
alpha=min(alpha_inc*alpha,delta_max)
if (abs(alpha*beta) < tolerance_theta).all():
print(count)
count+=1
file.close()
return X,alpha
# In[3]:
#Initialize magic values
n=2
x0=[1. for i in range(n)]
c=10
tolerance_theta=0.1
alpha_inc=1.2
alpha_dec=0.5
delta_max=float("inf")
wolfe_para=0.01
alpha=1
# Get the n value according to the size of x0
X=np.array(x0)
print(X.shape)
# Initialize C
C=C_fn(entry=c, size=n)
# In[4]:
# Test the f_seq(X)
print(df_hole(X))
# df_hole(X)/np.linalg.norm(df_hole(X))
# In[5]:
# Gradient Descent with Backtracking for the f_seq function
GD_with_backtracking(f=f_seq,df=df_seq,X=X,alpha=alpha)
# In[6]:
# Gradient Descent with Backtracking for the f_hole function
GD_with_backtracking(f=f_hole,df=df_hole,X=X,alpha=alpha)
# In[7]:
GD_with_backtracking_newton(f=f_seq,df=df_seq,X=X,alpha=alpha)
# GD_with_backtracking_newton(f=f_hole,df=df_hole,X=X,alpha=alpha)
# %%