-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgradient_method.py
More file actions
156 lines (120 loc) · 3.98 KB
/
Copy pathgradient_method.py
File metadata and controls
156 lines (120 loc) · 3.98 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
# -*- coding: cp1252 -*-
"""
Methodes de gradients.
"""
############################
##### IMPORTED MODULES #####
############################
import numpy as np
import matplotlib.pyplot as plt
import itertools
import cPickle
import scipy.linalg as spl
################################
##### FUNCTION DEFINITIONS #####
################################
def function(A,b,c,xx):
shape=xx[0].shape
ZZ=np.zeros_like(xx[0])
for item in itertools.product(*map(xrange,shape)):
vect=list()
for elem in xx:
vect.append(elem[item])
vec=np.array(vect,ndmin=1)
ZZ[item]=0.5*np.dot(vec.T,A.dot(vec))-np.dot(vec.T,b) + c
return ZZ
def gradient(A,b,x):
return A.dot(x)-b
def pas_fixe(x0,fonction,pas=1.0e-2,tol=1.0e-10,itermax=10000):
A=fonction['A']
b=fonction['b']
c=fonction['c']
#***** Initialisation *****
xx=[x0]
dir= - gradient(A,b,xx[-1])
residu=[np.linalg.norm(gradient(A,b,xx[-1]))]
k=0
while residu[-1] > tol and k < itermax :
#----- Calcul de x(k+1) -----
xx.append(xx[-1]+dir*pas)
#----- Calcul de la nouvelle direction de descente d(x+1) -----
dir = - gradient(A,b,xx[-1])
#----- Calcul du residu r(k+1) -----
residu.append(np.linalg.norm(gradient(A,b,xx[-1])))
k += 1
return {'xx':np.asarray(xx),'residu':np.asarray(residu)}
def conjugate(x0,fonction,tol=1.0e-10,itermax=10000):
A=fonction['A']
b=fonction['b']
c=fonction['c']
#***** Initialisation *****
xx=[x0]
dir = - gradient(A,b,xx[-1])
p = dir
residu = [np.linalg.norm(gradient(A,b,xx[-1]))]
k=0
while residu[-1] > tol and k < itermax :
#----- Calcul de rho(k) -----
rho = (np.linalg.norm(dir))**2/(np.transpose(p).dot(A).dot(p))
#----- Calcul de x(k+1) -----
xx.append(xx[-1]+p*rho)
#----- Calcul de la direction d(k+1) -----
dir = dir-rho*A.dot(p)
#----- Calcul de beta(k) -----
beta=np.linalg.norm(dir)**2 / residu[-1]**2
#----- Calcul du projeté p(x+1) -----
p = dir+beta*p
#----- Calcul du résidu r(x+1) -----
residu.append(np.linalg.norm(dir))
k += 1
return {'xx':np.asarray(xx),'residu':np.asarray(residu)}
def plot(xx,res):
plt.figure()
X1, X2 = np.meshgrid(np.linspace(-5.0,5.0,101),np.linspace(-5.0,5.0,101))
Z=function(A,b,c,[X1,X2])
plt.contour(X1,X2,Z)
plt.plot(xx.T[0],xx.T[1],'k-x')
plt.axes().set_aspect('equal')
plt.figure()
plt.plot(res)
plt.yscale('log')
plt.grid()
plt.xlabel('Iterations')
plt.ylabel(r'$||\nabla f(x) ||_2$')
plt.title('Convergence')
plt.show()
#######################
##### SCRIPT PART #####
#######################
###############################
##### SELF-SUSTAINED PART #####
###############################
if __name__=="__main__":
#***** Exercice 01 *****
"""
Pour cet exercice le minimum est [-0.1429,-0.4286]
"""
x0=np.array([3,3])
A=np.array([[4,1],[1,2]])
b=np.array([-1,-1])
c=np.zeros(1)
fonction={'A':A, 'b':b, 'c':c}
#----- Pas fixe -----
cas_01=pas_fixe(x0,fonction,1.0e-1,1.0e-6,10000)
print cas_01['xx'][-1], len(cas_01['xx'])-1
plot(cas_01['xx'],cas_01['residu'])
#----- Gradient conjugue -----
cas_02=conjugate(x0,fonction,1.0e-6,10000)
print cas_02['xx'][-1], len(cas_02['xx'])-1
plot(cas_02['xx'],cas_02['residu'])
#***** Exercice 02 *****
#***** Exercice 03 *****
#----- Import data -----
"""
Les donnees sont stockees dans la liste data sous forme de dictionnaire ayant les cles 'A' et
'b'
"""
data=list()
for ind in xrange(1,6):
data.append(cPickle.load(open('paire_%i.pickle' % ind,'r')))
data[ind-1]['A']=np.asarray(data[ind-1]['A'].todense())