-
Notifications
You must be signed in to change notification settings - Fork 4
/
algorithms.py
342 lines (238 loc) · 13.2 KB
/
algorithms.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
import numpy as np
import scipy.sparse.csgraph
import itertools
########################################################################################################################
def fair_k_center_exact(dmat,sexes,nr_centers_per_sex,given_centers):
'''Exhaustive search to exactly solve the fair k-center problem (2) --- only works for small problem instances.
INPUT:
dmat ... distance matrix of size nxn
sexes ... integer-vector of length n with entries in 0,...,m-1, where m is the number of groups
nr_centers_per_sex ... integer-vector of length m with entries in 0,...,k and sum over entries equaling k
given_centers ... integer-vector with entries in 0,...,n-1
RETURNS: (optimal centers, clustering, optimal fair k-center cost)'''
n = dmat.shape[0]
m = nr_centers_per_sex.size
k = np.sum(nr_centers_per_sex)
cost = np.inf
best_choice = []
for mmm in itertools.combinations(np.arange(n),k):
cluster_centers = np.array(mmm)
curr_nr_clusters_per_sex = np.zeros(m)
for ell in np.arange(m):
curr_nr_clusters_per_sex[ell] = np.sum(sexes[cluster_centers]==ell)
if sum(curr_nr_clusters_per_sex==nr_centers_per_sex)==m:
curr_cost = np.amax(np.amin(dmat[np.ix_(np.hstack((cluster_centers,given_centers)), np.arange(n))],axis=0))
else:
curr_cost = np.inf
if curr_cost<cost:
cost = curr_cost
best_choice = cluster_centers.copy()
clustering = np.array([np.argmin(dmat[ell, np.hstack((best_choice,given_centers))]) for ell in np.arange(n)])
return best_choice, clustering, cost
########################################################################################################################
########################################################################################################################
def k_center_greedy_with_given_centers(dmat,k,given_centers):
'''Implementation of Algorithm 1.
INPUT:
dmat ... distance matrix of size nxn
k ... integer smaller than n
given_centers ... integer-vector with entries in 0,...,n-1
RETURNS: approx. optimal centers'''
n=dmat.shape[0]
if k==0:
cluster_centers = np.array([],dtype=int)
else:
if given_centers.size==0:
cluster_centers = np.random.choice(n,1,replace=False)
kk = 1
else:
cluster_centers = given_centers
kk = 0
distance_to_closest = np.amin(dmat[np.ix_(cluster_centers,np.arange(n))],axis=0)
while kk<k:
temp = np.argmax(distance_to_closest)
cluster_centers = np.append(cluster_centers,temp)
distance_to_closest = np.amin(np.vstack((distance_to_closest,dmat[temp,:])),axis=0)
kk+=1
cluster_centers = cluster_centers[given_centers.size:]
return cluster_centers
########################################################################################################################
########################################################################################################################
def fair_k_center_APPROX(dmat,sexes,nr_centers_per_sex,given_centers):
'''Implementation of Algorithm 4.
INPUT:
dmat ... distance matrix of size nxn
sexes ... integer-vector of length n with entries in 0,...,m-1, where m is the number of groups
nr_centers_per_sex ... integer-vector of length m with entries in 0,...,k and sum over entries equaling k
given_centers ... integer-vector with entries in 0,...,n-1
RETURNS: approx. optimal centers'''
n = dmat.shape[0]
m = nr_centers_per_sex.size
k = np.sum(nr_centers_per_sex)
if m==1:
cluster_centers = k_center_greedy_with_given_centers(dmat, k, given_centers)
else:
cluster_centersTE = k_center_greedy_with_given_centers(dmat, k, given_centers)
CURRENT_nr_clusters_per_sex = np.zeros(m, dtype=int)
for ell in np.arange(k):
CURRENT_nr_clusters_per_sex[sexes[cluster_centersTE[ell]]] += 1
partition = np.array([np.argmin(dmat[ell, np.hstack((cluster_centersTE,given_centers))]) for ell in np.arange(n)])
G,centersTE = swapping_graph(partition[partition<k], np.array([np.where(np.arange(n)[partition < k] ==
cluster_centersTE[ell])[0][0] for ell in np.arange(k)]),sexes[partition<k], nr_centers_per_sex)
cluster_centersTE=np.arange(n)[partition < k][centersTE]
if G.size==0:
cluster_centers=cluster_centersTE
else:
new_data_set=np.array([],dtype=int)
new_given_centersT=np.array([],dtype=int)
for ell in np.arange(k):
if np.isin(sexes[cluster_centersTE[ell]],G):
new_data_set=np.hstack((new_data_set,np.where(partition==ell)[0]))
else:
new_given_centersT=np.hstack((new_given_centersT,cluster_centersTE[ell]))
new_given_centers=np.hstack((new_given_centersT,given_centers))
sexes_new = sexes[new_data_set]
sexes_newT=np.zeros(new_data_set.size,dtype=int)
cc=0
for ell in G:
sexes_newT[sexes_new==ell]=cc
cc+=1
new_data_set=np.hstack((new_data_set,new_given_centers))
sexes_newT=np.hstack((sexes_newT,np.zeros(new_given_centers.size,dtype=int)))
cluster_centers_rek=fair_k_center_APPROX(dmat[np.ix_(new_data_set,new_data_set)], sexes_newT,
nr_centers_per_sex[G],np.arange(new_data_set.size-new_given_centers.size,new_data_set.size))
new_given_centersT_additional= np.array([],dtype=int)
for ell in np.setdiff1d(np.arange(m),G):
if np.sum(sexes[new_given_centersT]==ell)<nr_centers_per_sex[ell]:
toadd=nr_centers_per_sex[ell]-np.sum(sexes[new_given_centersT]==ell)
toadd_pot=np.setdiff1d(np.where(sexes == ell)[0], new_given_centersT)
if toadd_pot.size>toadd:
new_given_centersT_additional=np.hstack((new_given_centersT_additional,toadd_pot[0:toadd]))
else:
new_given_centersT_additional = np.hstack((new_given_centersT_additional, toadd_pot))
cluster_centers=np.hstack((new_given_centersT,new_given_centersT_additional,new_data_set[cluster_centers_rek]))
return cluster_centers
########################################################################################################################
########################################################################################################################
def swapping_graph(partition,centers,sexes,nr_centers_per_sex):
'''Implementation of Algorithm 3.
INPUT:
partition ... integer-vector of length n with entries in 0 ... k-1
centers ... integer-vector of length k with entries in 0 ... n-1
sexes ... integer-vector of length n with entries in 0 ... m-1
nr_centers_per_sex ... integer-vector of length m with entries in 0,...,k and sum over entries equaling k
RETURNS: (G, swapped centers)'''
n = partition.size
m = nr_centers_per_sex.size
k = centers.size
CURRENT_nr_clusters_per_sex = np.zeros(m, dtype=int)
for ell in np.arange(k):
CURRENT_nr_clusters_per_sex[sexes[centers[ell]]] += 1
sex_of_assigned_center = sexes[centers[partition]]
Adja = np.zeros((m, m))
for ell in np.arange(n):
Adja[sex_of_assigned_center[ell],sexes[ell]] = 1
dmat_gr,predec = scipy.sparse.csgraph.shortest_path(Adja, directed=True, return_predecessors=True)
is_there_a_path=0
for ell in np.arange(m):
for zzz in np.arange(m):
if ((CURRENT_nr_clusters_per_sex[ell]>nr_centers_per_sex[ell]) and (CURRENT_nr_clusters_per_sex[zzz]<nr_centers_per_sex[zzz])):
if dmat_gr[ell,zzz]!=np.inf:
path = np.array([zzz])
while path[0]!=ell:
path = np.hstack((predec[ell,path[0]],path))
is_there_a_path = 1
break
if is_there_a_path==1:
break
while (is_there_a_path):
for hhh in np.arange(path.size - 1):
for ell in np.arange(n):
if (sexes[ell]==path[hhh+1]) and (sex_of_assigned_center[ell]==path[hhh]):
centers[partition[ell]] = ell
sex_of_assigned_center[partition==partition[ell]] = sexes[ell]
break
CURRENT_nr_clusters_per_sex[path[0]] -= 1
CURRENT_nr_clusters_per_sex[path[-1]] += 1
Adja = np.zeros((m, m))
for ell in np.arange(n):
Adja[sex_of_assigned_center[ell], sexes[ell]] = 1
dmat_gr, predec = scipy.sparse.csgraph.shortest_path(Adja, directed=True, return_predecessors=True)
is_there_a_path = 0
for ell in np.arange(m):
for zzz in np.arange(m):
if ((CURRENT_nr_clusters_per_sex[ell] > nr_centers_per_sex[ell]) and (CURRENT_nr_clusters_per_sex[zzz] < nr_centers_per_sex[zzz])):
if dmat_gr[ell, zzz] != np.inf:
path = np.array([zzz])
while path[0] != ell:
path = np.hstack((predec[ell, path[0]], path))
is_there_a_path = 1
break
if is_there_a_path == 1:
break
if sum(CURRENT_nr_clusters_per_sex==nr_centers_per_sex)==m:
return np.array([]), centers
else:
G = np.where(CURRENT_nr_clusters_per_sex > nr_centers_per_sex)[0]
for ell in np.arange(m):
for zzz in np.arange(m):
if (((dmat_gr[ell, zzz] != np.inf) and np.isin(ell, G)) and (not np.isin(zzz, G))):
G = np.hstack((G, zzz))
return G,centers
########################################################################################################################
########################################################################################################################
def heuristic_greedy_on_each_group(dmat,sexes,nr_centers_per_sex,given_centers):
'''Implementation of Heuristic A as described in Section 5.3.
INPUT:
dmat ... distance matrix of size nxn
sexes ... integer-vector of length n with entries in 0,...,m-1, where m is the number of groups
nr_centers_per_sex ... integer-vector of length m with entries in 0,...,k and sum over entries equaling k
given_centers ... integer-vector with entries in 0,...,n-1
RETURNS: heuristically chosen centers'''
m = nr_centers_per_sex.size
cluster_centers=np.array([],dtype=int)
for ell in np.arange(m):
subgroup=np.where(sexes==ell)[0]
given_centers_subgroup=np.where(np.isin(subgroup,given_centers))[0]
cent_subgroup=k_center_greedy_with_given_centers(dmat[np.ix_(subgroup, subgroup)],
nr_centers_per_sex[ell], given_centers_subgroup)
cluster_centers=np.hstack((cluster_centers,subgroup[cent_subgroup]))
return cluster_centers
########################################################################################################################
########################################################################################################################
def heuristic_greedy_till_constraint_is_satisfied(dmat,sexes,nr_centers_per_sex,given_centers):
'''Implementation of Heuristic B as described in Section 5.3.
INPUT:
dmat ... distance matrix of size nxn
sexes ... integer-vector of length n with entries in 0,...,m-1, where m is the number of groups
nr_centers_per_sex ... integer-vector of length m with entries in 0,...,k and sum over entries equaling k
given_centers ... integer-vector with entries in 0,...,n-1
RETURNS: heuristically chosen centers
SINCE WE DO NOT REQUIRE THE GENERAL CASE IN OUR EXPERIMENTS AND IT ALLOWS FOR A SLIGHTLY SIMPLER CODE,
HERE WE ASSUME THAT EITHER length(given_centers)>0 OR nr_centers_per_sex only[i]>0 FOR ALL i'''
n = dmat.shape[0]
m = nr_centers_per_sex.size
k=np.sum(nr_centers_per_sex)
current_nr_per_sex=np.zeros(m)
if k==0:
cluster_centers = np.array([], dtype=int)
else:
if given_centers.size==0:
cluster_centers=np.random.choice(n,1,replace=False)
current_nr_per_sex[sexes[cluster_centers]]+=1
kk=1
else:
cluster_centers = given_centers
kk=0
distance_to_closest = np.amin(dmat[np.ix_(cluster_centers, np.arange(n))], axis=0)
while kk<k:
feasible_groups=np.where(current_nr_per_sex<nr_centers_per_sex)[0]
feasible_points=np.where(np.isin(sexes,feasible_groups))[0]
new_point=feasible_points[np.argmax(distance_to_closest[feasible_points])]
current_nr_per_sex[sexes[new_point]] += 1
cluster_centers = np.append(cluster_centers, new_point)
distance_to_closest = np.amin(np.vstack((distance_to_closest, dmat[new_point, :])), axis=0)
kk+=1
cluster_centers=cluster_centers[given_centers.size:]
return cluster_centers
########################################################################################################################