-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPolak_Ribiere.py
More file actions
90 lines (67 loc) · 2.78 KB
/
Copy pathPolak_Ribiere.py
File metadata and controls
90 lines (67 loc) · 2.78 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
#!/usr/bin/python
import numpy as np
from numpy.linalg import norm
from time import process_time
from Wolfe_Skel import Wolfe
#############################################################################
# #
# RESOLUTION D'UN PROBLEME D'OPTIMISATION SANS CONTRAINTES #
# #
# Algorithme de Polak-Ribiere #
# #
#############################################################################
from Visualg import Visualg
from Oracle import *
def Polak_Ribiere(Oracle, x0):
##### Initialisation des variables
iter_max = 10000
# gradient_step_ini = 1. # Problème primal.
gradient_step_ini = 1000 # Problème dual.
threshold = 0.000001
error_count = 0 # Compteur de non-convergence de l'algorithme de Fletcher-Lemarechal.
gradient_norm_list = []
gradient_step_list = []
critere_list = []
time_start = process_time()
x = x0
##### Boucle sur les iterations
for k in range(iter_max):
# Nouvelles valeurs du critere et du gradient
critere, gradient = Oracle(x, 4)
# Test de convergence
gradient_norm = norm(gradient)
if gradient_norm <= threshold:
break
# Direction de descente
direction = -gradient
if k > 0:
beta = np.vdot(gradient, gradient - gradient_p) / gradient_norm_list[-1]**2
direction += beta * direction_p
# Pas de descente
gradient_step, error_code = Wolfe(gradient_step_ini, x, direction, Oracle)
if error_code != 1:
error_count += 1
# Mise a jour des variables
gradient_p = gradient # Valeur précédente du gradient
direction_p = direction # Valeur précédente de la direction
x = x + (gradient_step * direction)
# Evolution du gradient, du pas, et du critere
gradient_norm_list.append(gradient_norm)
gradient_step_list.append(gradient_step)
critere_list.append(critere)
if error_count > 0:
print()
print("Non-convergence de l'algorithme de Fletcher-Lemarechal : {}".format(error_count))
##### Resultats de l'optimisation
critere_opt = critere
gradient_opt = gradient
x_opt = x
time_cpu = process_time() - time_start
print()
print('Iteration :', k)
print('Temps CPU :', time_cpu)
print('Critere optimal :', critere_opt)
print('Norme du gradient :', norm(gradient_opt))
# Visualisation de la convergence
Visualg(gradient_norm_list, gradient_step_list, critere_list)
return critere_opt, gradient_opt, x_opt,