-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathCE_method_with_noise.py
155 lines (101 loc) · 4.53 KB
/
CE_method_with_noise.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
import random
import math
import numpy as np
from scipy import stats
from scipy.optimize import minimize
import Tetris
def simulation_CE_const_noise(alpha, N_iteration,rho,noise): #alpha : taux d'actualistion
#N_iteration : nombre d'iterations
#rho : the fraction of verctors that are selected
#noise : value of the constant noise to add
# Initialisation
mu0 = [0]*21
sigma0 = np.diag([100]*21)
V0 = (mu0, sigma0)
parameters = [V0]
t=1
L_plot=[]
L_norm=[]
for j in range (N_iteration):
# Create the distribution
distribution = stats.multivariate_normal(parameters[t-1][0], parameters[t-1][1])
# Evaluate each parameter pool
N = 100
sample_list = []
sample_score= []
for i in range(N):
sample = distribution.rvs() #vecteur de paramètre W
sample_score.append(Tetris.simulation(sample))
sample_list.append(sample)
# Keeping the rho*N bests vectors
k=math.floor(N*rho)
indices=sorted(range(len(sample_score)), key=lambda i: sample_score[i], reverse=True)[:k]
sample_high = [sample_list[i] for i in indices]
best_sample=sample_list[indices[0]]
# New parameter estimation using MLE
mean = np.mean(sample_high, axis = 0)
cov = np.cov(sample_high, rowvar = False)
res = (mean, cov)
#add noise
matrix_noise = np.diag([noise]*21)
parameters.append((alpha * np.array(res[0]) + (1 - alpha) * np.array(parameters[-1][0]),
alpha ** 2 * np.array(res[1]) + (1 - alpha) ** 2 * np.array(parameters[-1][1])+matrix_noise))
#calcul de la moyenne du meilleur vecteur sur 30 parties
L_mean=[sample_score[indices[0]]] #liste des scores des 30 simulations
for k in range (29):
L_mean.append(Tetris.simulation(best_sample))
print(np.mean(L_mean))
L_plot.append(L_mean)
t+=1
print(L_plot,L_norm,mean)
return(L_plot, mean)
def simulation_CE_deacr_noise(alpha, N_iteration,rho,a,b): #alpha : taux d'actualistion
#N_mean: nombre de simulation par vecteur
#N_iteration : nombre d'iterations
#rho : the fraction of verctors that are selected
#retourne L_plot : le score maximal par itération
#noise : value of the constant noise to add
#a,b : params of the decreasing noise, a=5 , b=100 in the paper
# Initialisation
mu0 = [0]*21
sigma0 = np.diag([100]*21)
V0 = (mu0, sigma0)
parameters = [V0]
t=1
L_plot=[]
L_norm=[]
for j in range (N_iteration):
# Create the distribution
distribution = stats.multivariate_normal(parameters[t-1][0], parameters[t-1][1])
# Evaluate each parameter pool
N = 100
sample_list = []
sample_score= []
for i in range(N):
sample = distribution.rvs() #vecteur de paramètre W
sample_score.append(Tetris.simulation(sample))
sample_list.append(sample)
# Keeping the rho*N bests vectors
k=math.floor(N*rho)
indices=sorted(range(len(sample_score)), key=lambda i: sample_score[i], reverse=True)[:k]
sample_high = [sample_list[i] for i in indices]
best_sample=sample_list[indices[0]]
# New parameter estimation using MLE
mean = np.mean(sample_high, axis = 0)
cov = np.cov(sample_high, rowvar = False)
res = (mean, cov)
L_norm.append(np.linalg.norm(cov))
#add noise
noise = max(0, a-N/b)
matrix_noise = np.diag([noise]*21)
parameters.append((alpha * np.array(res[0]) + (1 - alpha) * np.array(parameters[-1][0]),
alpha ** 2 * np.array(res[1]) + (1 - alpha) ** 2 * np.array(parameters[-1][1])+matrix_noise))
#calcul de la moyenne du meilleur vecteur sur 30 parties
L_mean=[sample_score[indices[0]]] #liste des scores des 30 simulations
for k in range (29):
L_mean.append(Tetris.simulation(best_sample))
print(np.mean(L_mean))
L_plot.append(L_mean)
t+=1
print(L_plot,L_norm,mean)
return(L_plot, L_norm,mean)