-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
217 lines (176 loc) · 5.18 KB
/
utils.py
File metadata and controls
217 lines (176 loc) · 5.18 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
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
207
208
209
210
211
212
213
214
215
216
217
import numpy as np
import copy
def fast_non_dominated_sorting(population, number_of_functions = 2):
population = -population
S = [[] for i in range(0, population.shape[0])]
front = [[]]
n = [0 for i in range(0, population.shape[0])]
rank = [0 for i in range(0, population.shape[0])]
for p in range(0, population.shape[0]):
S[p] = []
n[p] = 0
for q in range(0, population.shape[0]):
if ((population[p,-number_of_functions:] <= population[q,-number_of_functions:]).all()):
if (q not in S[p]):
S[p].append(q)
elif ((population[q,-number_of_functions:] <= population[p,-number_of_functions:]).all()):
n[p] = n[p] + 1
if (n[p] == 0):
rank[p] = 0
if (p not in front[0]):
front[0].append(p)
i = 0
while (front[i] != []):
Q = []
for p in front[i]:
for q in S[p]:
n[q] = n[q] - 1
if(n[q] == 0):
rank[q] = i+1
if q not in Q:
Q.append(q)
i = i+1
front.append(Q)
del front[len(front)-1]
rank = np.zeros((population.shape[0], 1))
for i in range(0, len(front)):
for j in range(0, len(front[i])):
rank[front[i][j], 0] = i + 1
return np.where(rank == 1)[0]
def par_non_dominated_sorting(pop: np.ndarray):
pop = np.atleast_2d(pop)
Np = len(pop)
non_dominate_ind = []
for i in range(Np):
p = pop[i]
num_d = 0
for q in pop:
if par_dominance(q, p):
num_d += 1
break
if num_d == 0: non_dominate_ind.append(i)
return np.array(non_dominate_ind)
def lex_dominance(u: np.ndarray, v: np.ndarray, epsilon=1e-6) -> bool:
"""
To judge if u dominates v lexicographically
in maximization
Parameters
----------
u : np.ndarray
_description_
v : np.ndarray
_description_
Returns
-------
bool
_description_
"""
for i in range(len(u)):
if u[i]-v[i]>epsilon:
return True
elif np.abs(u[i]-v[i]) <= epsilon:
continue
else:
return False
def par_dominance(u: np.ndarray, v: np.ndarray) -> bool:
"""
To judge if u dominates v in Pareto form
in maximization
Parameters
----------
u : np.ndarray
_description_
v : np.ndarray
_description_
Returns
-------
bool
_description_
"""
for i in range(len(u)):
if v[i] > u[i]: return False
if (u==v).all(): return False
return True
def pc_dominance(u:list, v:list) -> bool:
"""
Optimality whether u is Pareto-lexicographic
under priority chains dominated v
Parameters
----------
u : list
_description_
v : list
_description_
Returns
-------
bool
_description_
"""
c = len(u)
for i in range(c):
if not lex_dominance(u[i], v[i]):
return False
return True
def pc_non_dominated_sorting(pop: list):
c = len(pop)
Np = pop[0].shape[0]
non_dominate_ind = []
for i in range(Np):
p = [pop[j][i] for j in range(c)]
num_d = 0
for k in range(Np):
q = [pop[j][k] for j in range(c)]
if pc_dominance(q, p):
num_d +=1
break
if num_d == 0: non_dominate_ind.append(i)
return np.array(non_dominate_ind)
def prior_free_lexi_filter(ucb: np.ndarray, lcb: np.ndarray) -> np.ndarray:
"""
filter the optimal arms based on transitive closure relation of the linked relation
Parameters
----------
ucb : np.ndarray
upper confidence bound of the arms
lcb : np.ndarray
lower confidence bound of the arms
Returns
-------
np.ndarray
index of the optimal arms
"""
K,mc = ucb.shape
opt_ind = [np.arange(K)]
for i in range(mc):
x_i = opt_ind[i][np.argmax(ucb[opt_ind[i], i])]
opt_ind.append(
chain_filter(x_i,opt_ind[i],ucb[:, i],lcb[:, i])
)
return opt_ind[-1]
def chain_filter(arm, D1, u_t, l_t):
pre_results = []
results = [arm]
lowest = l_t[arm]
while len(results) != len(pre_results):
pre_results = copy.deepcopy(results)
for i in D1:
if u_t[i] >= lowest and i not in results:
results.append(i)
lowest = np.min([lowest, l_t[i]])
return np.array(results)
if __name__ == "__main__":
# x1 = [np.array([1., -1.]), np.array([1., -1.])]
# x2 = [np.array([1., 1.]), np.random.randn(2)]
# print(par_lex_dominance(x1, x2))
# print(x1)
# print(x2)
# K = 10
# pop = [np.random.rand(K,2), np.random.rand(K,2)]
# print(pop)
# print(par_lex_non_dominated_sorting(pop))
# [0.95272219, 0.4606369 ],[0.55505175, 0.65496876],[0.62047633, 0.01251895]
# [0.40258481, 0.38275378],[0.68383117, 0.5550799 ],[0.61244796, 0.50949707]
# x1 = np.array([1., 2.2])
# x2 = np.array([1.1, 2.1])
# print(par_dominance(x1, x2))
print()