-
Notifications
You must be signed in to change notification settings - Fork 0
/
agt.py
297 lines (253 loc) · 11.9 KB
/
agt.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
from cmath import inf
from dataclasses import dataclass
import random
import time
import itertools
import os
import json
numOfTraits = 16
numOfAdvertisers = 16
max_val_of_traits = 10
valuations_vector = {}
valuations_vector_unknown = {}
#Randomize desirable traits for advertisers
def createDesirableTraits():
return [1 if random.random() > 0.5 else 0 for i in range(0, numOfTraits)]
#Valuation Function for Advertisers
def valuation(j, desirableTraits, stateOfNature):
val = 0
for i in range(0, numOfTraits):
if stateOfNature[i] == '?':
val += valuations_vector_unknown[j][i]
elif stateOfNature[i] == desirableTraits[i]:
val += valuations_vector[j][i]
return val
def createAdvertisers():
advertisers = {}
for i in range(0,numOfAdvertisers):
advertisers[i] = createDesirableTraits()
return advertisers
def greedyAlgorithm(currStateOfNature, currSignaling, unRevealTraits, advertisers, remainingAdvertisers):
newUnReveal = []
l = len(unRevealTraits)
if l == 1:
return currStateOfNature, newUnReveal
for i in range(0, int(l/2)):
sxy = -inf
sx = -inf
sy = -inf
chosenX = -1
chosenY = -1
for a in range (0, len(unRevealTraits)):
for b in range(a+1, len(unRevealTraits)):
x = unRevealTraits[a]
y = unRevealTraits[b]
sumOfx = 0
sumOfy = 0
for adv in remainingAdvertisers:
xState = [currSignaling[i] if i!=x else currStateOfNature[i] for i in range(0,numOfTraits)]
yState = [currSignaling[i] if i!=y else currStateOfNature[i] for i in range(0,numOfTraits)]
xyState = [currSignaling[i] if i!=y and i!=x else currStateOfNature[i] for i in range(0,numOfTraits)]
sumOfx += valuation(adv, advertisers[adv],xState) - valuation(adv, advertisers[adv],xyState)
sumOfy += valuation(adv, advertisers[adv],yState) - valuation(adv, advertisers[adv],xyState)
if max(sumOfx,sumOfy) > sxy:
sxy = max(sumOfx,sumOfy)
sx = sumOfx
sy = sumOfy
chosenX = x
chosenY = y
unRevealTraits.remove(chosenX)
unRevealTraits.remove(chosenY)
if sx > sy:
currSignaling[chosenX] = currStateOfNature[chosenX]
newUnReveal.append(chosenY)
else:
currSignaling[chosenY] = currStateOfNature[chosenY]
newUnReveal.append(chosenX)
return currSignaling, newUnReveal
def greedyAlgorithmPrivate(currStateOfNature, currSignalingDict, unRevealTraitsDict, advertisers, remainingAdvertisers):
newUnRevealDict = {i:[] for i in range(0, numOfAdvertisers)}
l = len(unRevealTraitsDict[remainingAdvertisers[0]])
if l == 1:
return {i:currStateOfNature for i in range(0,numOfAdvertisers)}, None
for adv in remainingAdvertisers:
unRevealTraits = unRevealTraitsDict[adv]
currSignaling = currSignalingDict[adv]
for i in range(0, int(l/2)):
sxy = -inf
sx = -inf
sy = -inf
chosenX = -1
chosenY = -1
for a in range (0, len(unRevealTraits)):
for b in range(a+1, len(unRevealTraits)):
x = unRevealTraits[a]
y = unRevealTraits[b]
xState = [currSignaling[i] if i!=x else currStateOfNature[i] for i in range(0,numOfTraits)]
yState = [currSignaling[i] if i!=y else currStateOfNature[i] for i in range(0,numOfTraits)]
xyState = [currSignaling[i] if i!=y and i!=x else currStateOfNature[i] for i in range(0,numOfTraits)]
sumOfx = valuation(adv, advertisers[adv],xState) - valuation(adv, advertisers[adv],xyState)
sumOfy = valuation(adv, advertisers[adv],yState) - valuation(adv, advertisers[adv],xyState)
if max(sumOfx,sumOfy) > sxy:
sxy = max(sumOfx,sumOfy)
sx = sumOfx
sy = sumOfy
chosenX = x
chosenY = y
unRevealTraits.remove(chosenX)
unRevealTraits.remove(chosenY)
if sx > sy:
currSignaling[chosenX] = currStateOfNature[chosenX]
newUnRevealDict[adv].append(chosenY)
else:
currSignaling[chosenY] = currStateOfNature[chosenY]
newUnRevealDict[adv].append(chosenX)
currSignalingDict[adv] = currSignaling
return currSignalingDict, newUnRevealDict
#Return top half bidders + winning bid and second bid
def getTopHalf(remaining, bids):
half = int(len(remaining)/2)
toSort = [(bids[i], i) for i in remaining]
toSort.sort(key=lambda x: x[0], reverse=True)
newRemaining = toSort[:half]
return toSort[0][0], toSort[1][0], [x[1] for x in newRemaining]
def optimal(currStateOfNature, currSignaling, unRevealTraits, advertisers, remainingAdvertisers, bids):
newUnReveal = []
l = len(unRevealTraits)
maxPaying = -inf
maxBidding = -inf
last_standing = -1
maxSignaling = currSignaling
for traits in itertools.combinations(unRevealTraits, int(l/2)):
signaling = [currSignaling[i] if i not in traits else currStateOfNature[i] for i in range(0,numOfTraits)]
newBids = {}
for adv in remainingAdvertisers:
newBids[adv] = max(bids[adv], valuation(adv, advertisers[adv], signaling))
bidding, paying, newReamainingAdvertisers = getTopHalf(remainingAdvertisers, newBids)
if len(newReamainingAdvertisers) != 1:
_,_, bidding, paying, last = optimal(currStateOfNature, signaling, [i for i in unRevealTraits if i not in traits], advertisers, newReamainingAdvertisers, newBids)
else:
last = newReamainingAdvertisers[0]
if paying > maxPaying:
maxPaying = paying
maxBidding = bidding
maxSignaling = signaling
last_standing = last
newUnReveal = [i for i in unRevealTraits if i not in traits]
return maxSignaling, newUnReveal, maxBidding, maxPaying, last
def auction():
expirment = {}
e_greedy = {}
e_greedy_private = {}
e_optimal = {}
#Create Expirement Data
for i in range(0,numOfAdvertisers):
valuations_vector[i] = [int(random.random()*(max_val_of_traits+1)) for j in range(0, numOfTraits)]
valuations_vector_unknown[i] = [int(random.random()*(2*(valuations_vector[i][j]+1)/3)) for j in range(0, numOfTraits)]
currStateOfNature = createDesirableTraits()
advertisers = createAdvertisers()
currSignaling = ['?' for i in range(0, numOfTraits)]
unRevealTraits = [i for i in range(0,numOfTraits)]
advertisersBids = {i:0 for i in range(0,numOfAdvertisers)}
remainingAdvertisers = [i for i in range(0,numOfAdvertisers)]
#Greedy Public Test
startTime = time.time()
stage = 0
while len(remainingAdvertisers) > 1:
currSignaling, unRevealTraits = greedyAlgorithm(currStateOfNature, currSignaling, unRevealTraits, advertisers, remainingAdvertisers)
stage+=1
for adv in remainingAdvertisers:
advertisersBids[adv] = max(advertisersBids[adv], valuation(adv, advertisers[adv], currSignaling))
bidding, paying, remainingAdvertisers = getTopHalf(remainingAdvertisers, advertisersBids)
time_took = time.time() - startTime
e_greedy["time"] = time_took
e_greedy["revenue"] = paying
adv = remainingAdvertisers[0]
e_greedy["welfare"] = valuation(adv, advertisers[adv], currStateOfNature) - paying
e_greedy["winning_bid"] = bidding
#Refresh Results
currSignalingDict = {j:['?' for i in range(0, numOfTraits)] for j in range(0,numOfAdvertisers)}
unRevealTraitsDict = {j:[i for i in range(0,numOfTraits)] for j in range(0, numOfAdvertisers)}
advertisersBids = {i:0 for i in range(0,numOfAdvertisers)}
remainingAdvertisers = [i for i in range(0,numOfAdvertisers)]
#Greedy Private Test
startTime = time.time()
stage = 0
while len(remainingAdvertisers) > 1:
currSignalingDict, unRevealTraitsDict = greedyAlgorithmPrivate(currStateOfNature, currSignalingDict, unRevealTraitsDict, advertisers, remainingAdvertisers)
stage+=1
for adv in remainingAdvertisers:
advertisersBids[adv] = max(advertisersBids[adv], valuation(adv, advertisers[adv], currSignalingDict[adv]))
bidding, paying, remainingAdvertisers = getTopHalf(remainingAdvertisers, advertisersBids)
time_took = time.time() - startTime
e_greedy_private["time"] = time_took
e_greedy_private["revenue"] = paying
adv = remainingAdvertisers[0]
e_greedy_private["welfare"] = valuation(adv, advertisers[adv], currStateOfNature) - paying
e_greedy_private["winning_bid"] = bidding
#Refresh Results
currSignaling = ['?' for i in range(0, numOfTraits)]
unRevealTraits = [i for i in range(0,numOfTraits)]
advertisersBids = {i:0 for i in range(0,numOfAdvertisers)}
remainingAdvertisers = [i for i in range(0,numOfAdvertisers)]
#Optimal Public Test
startTime = time.time()
_,_, bidding, paying, last = optimal(currStateOfNature, currSignaling, unRevealTraits, advertisers, remainingAdvertisers, advertisersBids)
time_took = time.time() - startTime
e_optimal["time"] = time_took
e_optimal["revenue"] = paying
e_optimal["welfare"] = valuation(last, advertisers[last], currStateOfNature) - paying
e_optimal["winning_bid"] = bidding
#Second Price Test
e_reg_second_price = {}
for adv in remainingAdvertisers:
advertisersBids[adv] = valuation(adv, advertisers[adv], currStateOfNature)
toSort = [(advertisersBids[i], i) for i in range(0, numOfAdvertisers)]
toSort.sort(key=lambda x: x[0], reverse=True)
paying = toSort[1][0]
bidding = toSort[0][0]
e_reg_second_price["revenue"] = paying
e_reg_second_price["welfare"] = bidding - paying
e_reg_second_price["winning_bid"] = bidding
expirment["greedy"] = e_greedy
expirment["greedy_private"] = e_greedy_private
expirment["optimal"] = e_optimal
expirment["second_price_auction"] = e_reg_second_price
return expirment
def append_csv_line(write, obj):
lst = []
lst.append(str(obj["greedy"]["time"]))
lst.append(str(obj["greedy"]["revenue"]))
lst.append(str(obj["greedy"]["welfare"]))
lst.append(str(obj["greedy"]["winning_bid"]))
lst.append(str(obj["greedy_private"]["time"]))
lst.append(str(obj["greedy_private"]["revenue"]))
lst.append(str(obj["greedy_private"]["welfare"]))
lst.append(str(obj["greedy_private"]["winning_bid"]))
lst.append(str(obj["optimal"]["time"]))
lst.append(str(obj["optimal"]["revenue"]))
lst.append(str(obj["optimal"]["welfare"]))
lst.append(str(obj["optimal"]["winning_bid"]))
lst.append(str(obj["second_price_auction"]["revenue"]))
lst.append(str(obj["second_price_auction"]["welfare"]))
lst.append(str(obj["second_price_auction"]["winning_bid"]))
return write + ','.join(lst) + '\n'
#Convert json file to csv
def result_to_csv(start, end):
write = "greedy_time,greedy_revenue,greedy_welfare,greedy_bid,private_time,private_revenue,private_welfare,private_bid,optimal_time,optimal_revenue,optimal_welfare,optimal_bid,second_revenue,second_welfare,second_bid\n"
for i in range(start, end+1):
if os.path.isfile(f"result{i}.json"):
with open(f"result{i}.json") as json_file:
data = json.load(json_file)
for obj in data:
write = append_csv_line(write, obj)
with open("test.csv", 'w') as outfile:
outfile.write(write)
NUM_OF_TEST = 100
if __name__ == '__main__':
data = []
for i in range(0, NUM_OF_TEST):
print(f"Starting Test {i+1}")
data.append(auction())
with open("result.json", 'w') as outfile:
json.dump(data, outfile, indent=4)