-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunction_Rosenbrock.py
206 lines (147 loc) · 5.76 KB
/
Function_Rosenbrock.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
#**************************************************************************************
#****************************ROSENBROCK's FUNCTION*************************************
#**************************************************************************************
print("Rosenbrock")
import psopy as pso
import scipy.optimize as opt
import numpy as np
import matplotlib.cm as cm
from matplotlib import pyplot as plt
import Functions_util as fs
import random as rd
import data
import time
rd.seed(2610)
D = [50, 500]
L = [-100, 100]
lD = len(D)
Dmax = max(D)
n_try = 100
# generate random shifts and shifted functions
x_shifts = [fs.define_x_rand(count = n_try, dim = d) for d in D]
s_fam = [fs.define_family_rosen(x_shifts[k]) for k in range(len(x_shifts))]
# with real data
x_shifts_r = [ np.array( data.rosenbrock[0:d] ) for d in D]
s_fam_r = [ fs.define_rosen( dim = D[k], x_shift = x_shifts_r[k] ) for k in range(lD) ]
# generate random initial values for x
x_0s = [fs.define_x_rand(count = n_try, dim = d) for d in D]
# **************************************in dimension 50*********************************
print("\n Simulated annealing followed by gradient descent in dimension 50")
n_try = 5
d = 0
dim = D[d]
#*************with random data
print("\n With random data")
# simulated annealing
print("\n - simulated annealing : 5 successive trials ")
maxIter = 5*10**5
divers = 0.3
intens = 10 ** -3
verbal = 2
start = time.time()
myResults = [ fs.simulateAnnealing2(s_fam[d][i], x_0s[d][i], maxIter=maxIter,
divers=divers, intens=intens, verbal=verbal)
for i in range(n_try) ]
end = time.time()
avgTime = (end - start) / n_try
print('average time 1 trial =', avgTime)
# *******************with real data
print("\n With real data")
# simulated annealing
print("\n - simulated annealing : 5 successive trials ")
maxIter = 5*10**5
divers = 0.3
intens = 10 ** -3
verbal = 2
start = time.time()
myResults_r = [fs.simulateAnnealing2(s_fam_r[d], x_0s[d][i], maxIter=maxIter,
divers=divers, intens=intens, verbal=verbal)
for i in range(n_try) ]
end = time.time()
avgTime = (end - start) / n_try
print('average time 1 trial =', avgTime)
# local search with gradient
print("\n - gradient descent on best point ")
myVal = [myResults_r[i]["minimum_energy"] for i in range(n_try)]
iBest = np.argmin(myVal)
x0 = myResults_r[iBest]["minimum_state"]
start = time.time()
xl, fl, nl = fs.gradient_method(dim, x0, s_fam_r[d], eps=10**-3, ro=10**-3,
termination_criterion=fs.termination_by_runs,
max_run = 6000, min_step = 1, bounds = [-100, 100],
ro_adjust = False, ro_optim = True, eps_adjust = False)
end = time.time()
print("gradient time : ", end-start)
print("fitness = ", fl," after ", nl, " runs")
print("Error = ", np.linalg.norm(xl-x_shifts_r[d]))
# ************************in dimension 500*************************************
print("\n Simulated annealing followed by gradient descent in dimension 500")
n_try = 5
d = 1
dim = D[d]
#**************with random data
print("\n With random data")
# simulated annealing
print("\n - simulated annealing : 5 successive trials (be patient...)")
maxIter = 3*10**6
divers = 0.3
intens = 10 ** -4
verbal = 2
accept = 500
start = time.time()
myResults = [ fs.simulateAnnealing2(s_fam[d][i], x_0s[d][i], maxIter=maxIter,
divers=divers, intens=intens, accept=accept,
verbal=verbal)
for i in range(n_try) ]
end = time.time()
avgTime = (end - start) / n_try
print('average time =', avgTime)
myVal = [myResults[i]["minimum_energy"] for i in range(n_try)]
iBest = np.argmin(myVal)
x0 = myResults[iBest]["minimum_state"]
print("The difference between the target shift and the shift we found is : ")
print(x0-x_shifts[1][iBest])
# local search with gradient
print("\n - gradient descent on best point ")
start = time.time()
xl, fl, nl = fs.gradient_method(dim, x0, s_fam[d][iBest], eps=10**-5, ro=10**-4,
termination_criterion=fs.termination_by_runs,
max_run = 2000, min_step = 1, bounds = [-100, 100],
ro_adjust = False, ro_optim = True, eps_adjust = False)
end = time.time()
print("gradient time : ", end-start)
print("fitness = ", fl," after ", nl, " runs")
print("Error = ", np.linalg.norm(xl-x_shifts[1][iBest]))
# *****************with real data
print("\n With real data")
# simulated annealing
print("\n - simulated annealing : 5 successive trials ")
maxIter = 3*10**6
divers = 0.3
intens = 10 ** -4
verbal = 2
accept = 500
n_try = 5
start = time.time()
myResults_r = [fs.simulateAnnealing2(s_fam_r[d], x_0s[d][i], maxIter=maxIter,
divers=divers, intens=intens, accept=accept,
verbal=verbal)
for i in range(n_try) ]
end = time.time()
avgTime = (end - start) / n_try
print('average time 1 trial =', avgTime)
# local search with gradient
print("\n - gradient descent on best point ")
myVal = [myResults_r[i]["minimum_energy"] for i in range(n_try)]
iBest = np.argmin(myVal)
x0 = myResults_r[iBest]["minimum_state"]
start = time.time()
xl, fl, nl = fs.gradient_method(dim, x0, s_fam_r[d], eps=10**-3, ro=10**-3,
termination_criterion=fs.termination_by_runs,
max_run = 5000, min_step = 1, bounds = [-100, 100],
ro_adjust = True, ro_optim = True, eps_adjust = True)
end = time.time()
print("gradient time : ", end-start)
print("fitness = ", fl," after ", nl, " runs")
print("Error = ", np.linalg.norm(xl-x_shifts_r[d]))
input("Press enter to exit")