-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdeletion_copeland.py
More file actions
226 lines (205 loc) · 10.1 KB
/
deletion_copeland.py
File metadata and controls
226 lines (205 loc) · 10.1 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
218
219
220
221
222
223
224
225
226
from numpy.core.fromnumeric import sort
import iterative_copeland as ic
import pickle
import numpy as np
from celluloid import Camera
import imageio
from os import listdir
from os.path import isfile, join
import matplotlib.pyplot as plt
def copelandWrapper(utility_profile_segment, surviving_candidates):
# The preference profile is augmented with a column of candidate ids.
agents = len(utility_profile_segment[0]) - 1
candidates = len(utility_profile_segment)
# calculation of pairwise score and copeland score
# removes the ID column from the utility profile
score_list = ic.pairwiseScoreCalcListFull(
utility_profile_segment[:, 1:], candidates, agents)
copeland_score = ic.copelandScoreFull(score_list, candidates, agents)
# Finding the weaklings
sorted_copeland_score = np.argsort(copeland_score)
no_of_deleted_candidates = len(
utility_profile_segment) - surviving_candidates
# Takes the highest x candidates
to_be_kept = sorted_copeland_score[no_of_deleted_candidates:]
# sanity check
assert len(sorted_copeland_score) == len(copeland_score) == utility_profile_segment.shape[0]
# Deleting weaklings from from preference profile and copeland score.
utility_profile_segment = [utility_profile_segment[x] for x in to_be_kept]
copeland_score = [copeland_score[x] for x in to_be_kept]
# Sanity check
assert len(utility_profile_segment) == len(copeland_score)
return (utility_profile_segment, copeland_score)
def score_comparison(indexes, deletion_copeland_scores, copeland_scores):
deletion_winner_cs = max(deletion_copeland_scores) # Copeland winner of deletion algorithm
id_of_deletion_winner = indexes[deletion_copeland_scores.index(deletion_winner_cs)] # ID of Copeland winner of deletion algorithm
ground_truth_score_of_deletion_winner = copeland_scores[id_of_deletion_winner] # Ground Truth Copeland Score of defined ID
ground_truth_winner_cs = max(copeland_scores) # Ground Truth Copeland Winner
print(" Gap: GT Winner vs Deletion Winner(on GT) :"+ str(abs(ground_truth_winner_cs - ground_truth_score_of_deletion_winner) / ground_truth_winner_cs))
print(" Reliability: Deletion Winner(on GT) vs Deletion Winner :"+ str(abs(deletion_winner_cs - ground_truth_score_of_deletion_winner) / ground_truth_score_of_deletion_winner))
# Print the IDs and scores of the deletion winner and ground truth winner.
# Gap is measure of quality of candidate(how good of a solution we picked), reliability is copeland score on the pool close to the copeland score on the GT.
# Absolute reliability: difference b/w
def deletionCopeland(utility_profile, step, surviving_candidates, budget):
# Processing the data.
candidates = len(utility_profile)
agents = len(utility_profile[0])
i_utility_profile = None
copeland_score = None
for i in range(0, budget, step):
# Growing set of candidiates.
if i == 0:
i_utility_profile = utility_profile[0:step]
else:
i_utility_profile = np.append(
i_utility_profile, utility_profile[i: i+step], axis=0)
i_utility_profile, copeland_score = copelandWrapper(
i_utility_profile, surviving_candidates)
# Sanity check
assert i_utility_profile != None and copeland_score != None
return (i_utility_profile, copeland_score)
def deletionCopelandFamily(utility_profile, step, surviving_candidates, budget):
# Processing the data.
candidates = len(utility_profile)
# We add a column on the 0th index to keep track of candidate IDs thus, we remove that column.
agents = len(utility_profile[0]) - 1
familyipp = []
familycs = []
i_utility_profile = None
for i in range(0, budget, step):
if i == 0:
i_utility_profile = utility_profile[0:step]
else:
i_utility_profile = np.append(
i_utility_profile, utility_profile[i: i+step], axis=0)
i_utility_profile, copeland_score = copelandWrapper(
i_utility_profile, surviving_candidates)
# Storing preference profile and associated copeland score
familyipp.append(i_utility_profile)
familycs.append(copeland_score)
assert len(familyipp) == len(familycs)
for i in range(len(familyipp)):
assert len(familyipp[i]) == len(familycs[i])
return (familyipp, familycs)
def plot(directory, filename, step, surviving_candidates, budget, show_plots_during_execution):
# Reading pickled files and storing the data.
pickled_file = open(directory + "_profiles/" + filename+".vt", "rb")
preference_profile = pickle.load(pickled_file)
utility_profile = pickle.load(pickled_file)
# Processing the data.
candidates = len(utility_profile)
agents = len(utility_profile[0])
# Augmenting preference profile with IDs.
utility_profile = np.insert(
utility_profile, 0, range(candidates), axis=1)
if "normal" == filename[0:6]:
true_copeland_score = pickle.load(pickled_file)
else:
score_list = ic.pairwiseScoreCalcListFull(
utility_profile[:, 1:], candidates, agents)
true_copeland_score = ic.copelandScoreFull(score_list, candidates, agents)
# Relative Copeland Score
true_copeland_score = [i/candidates for i in true_copeland_score]
pickled_file.close()
ipp, cs = deletionCopeland(
utility_profile, step, surviving_candidates,budget)
cs = [i/(step+surviving_candidates) for i in cs]
not_deleted_candidate_ids = np.stack(ipp, axis=0)[:, 0].tolist()
winners_index = np.argsort(true_copeland_score)[-5:].tolist()
winners_value = [true_copeland_score[x] for x in winners_index]
true_copeland_score_deletion_labels = [true_copeland_score[i] for i in not_deleted_candidate_ids]
score_comparison(not_deleted_candidate_ids, cs, true_copeland_score)
fig, (ax1) = plt.subplots(1)
fig.suptitle(directory + ' ' + filename)
fig.set_size_inches(11.69, 8.27)
ax1.plot(true_copeland_score, 'b.')
ax1.plot(not_deleted_candidate_ids, cs, 'ro')
ax1.plot(not_deleted_candidate_ids, true_copeland_score_deletion_labels, 'm*')
ax1.plot(winners_index, winners_value, 'y+')
ax1.legend(['Copeland Score Post Deletion', 'Real Copeland Score'])
ax1.set_xlabel('Candidate IDs')
ax1.set_ylabel('Normalised Copeland Score')
plt.savefig("plots/" + directory + "/png/" + filename)
if show_plots_during_execution:
plt.show()
def plot_gif(directory, filename, step, surviving_candidates, budget):
# Reading pickled files and storing the data.
pickled_file = open(directory + "_profiles/" + filename+".vt", "rb")
true_copeland_score = None
preference_profile = pickle.load(pickled_file)
utility_profile = pickle.load(pickled_file)
candidates = len(utility_profile)
agents = len(utility_profile[0])
# Augmenting preference profile with IDs.
utility_profile = np.insert(
utility_profile, 0, range(candidates), axis=1)
score_list = ic.pairwiseScoreCalcListFull(
utility_profile[:, 1:], candidates, agents)
true_copeland_score = ic.copelandScoreFull(score_list, candidates, agents)
true_copeland_score = [i/candidates for i in true_copeland_score]
pickled_file.close()
familyipp, familycs = deletionCopelandFamily(
utility_profile, step, surviving_candidates,budget)
winners_index = np.argsort(true_copeland_score)[-5:].tolist()
winners_value = [true_copeland_score[x] for x in winners_index]
fig, ax1 = plt.subplots()
camera = Camera(fig)
for i in range(len(familyipp)):
not_deleted_candidate_id = np.stack(
familyipp[i], axis=0)[:, 0].tolist()
true_copeland_score_deletion_labels = [true_copeland_score[i] for i in not_deleted_candidate_id]
# print(not_deleted_candidate_id)
cs = familycs[i]
cs = [j/(step+surviving_candidates) for j in cs]
assert len(cs) == len(not_deleted_candidate_id)
fig.suptitle(directory + ' ' + filename)
fig.set_size_inches(11.69, 8.27)
ax1.set_ylim(-0.1, 1.1)
ax1.plot(true_copeland_score, 'bo')
ax1.plot(winners_index, winners_value, 'y+')
ax1.plot(not_deleted_candidate_id, cs, 'ro')
ax1.plot(not_deleted_candidate_id, true_copeland_score_deletion_labels, 'm*')
ax1.legend(['Real Copeland Score', 'Copeland Score Post Deletion'])
ax1.set_xlabel('Candidate IDs')
ax1.set_ylabel('Normalised Copeland Score')
camera.snap()
# ax1.lines.pop(0)
animation = camera.animate(blit=False)
filepath = "plots/" + directory + "/gif/" + filename + '.gif'
animation.save(filepath, writer='imagemagick')
change_frame_rate(filepath)
def change_frame_rate(file):
gif = imageio.mimread(file)
imageio.mimsave(file, gif, fps=1)
if __name__ == "__main__":
SHOW_PLOTS_DURING_EXECUTION = False
PLOT_GIFS = True
# benchmarks = ["scheduling"] # photo_placement_bipolar, "scheduling"
benchmarks = {
'scheduling':[[4,100,60,120],[5,100,60,120]],
'photo_placement_bipolar':[[7, 100, 60, 150],[8, 100, 60, 150]],
'vehicle_routing':[[3,20,10,20],[4,20,10,20]],
'project_assignment':[[2,50,30,160],[3,50,30,160]]
}
# step = 100
# surviving_candidates = 60
# budget = 400
profile_types = ["normal"] # "search_more", "inverted", "random"
for benchmark in benchmarks:
datafiles = benchmarks[benchmark]
print("🟢 Running " + benchmark)
for datafile in datafiles: # , '1','2','3','4', '5', '6'
i = str(datafile[0])
step = datafile[1]
survivors = datafile[2]
budget = datafile[3]
for profile_type in profile_types:
try:
print(" " + profile_type+str(i))
plot(benchmark, profile_type+str(i), step,
survivors, budget, SHOW_PLOTS_DURING_EXECUTION)
# plot_gif(benchmark, profile_type+str(i),
# step, surviving_candidates, budget)
except Exception as e:
print(e)
None