-
Notifications
You must be signed in to change notification settings - Fork 0
/
compAuctions.py
129 lines (113 loc) · 3.44 KB
/
compAuctions.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
from numpy import random
import numpy as np
import sys
# second price
def sp(n):
BidF = []
for rnd in random.uniform(0,1, size = [1,n])[0]:
f = lambda currentBid, bidHist, rnd2=rnd: rnd2
BidF.append( f )
return BidF
# first price
def fp(n):
BidF = []
for rnd in random.uniform(0, 1, size = [1,n])[0]:
f = lambda currentBid, bidHist, rnd2=rnd: (n-1) * rnd2 / float(n)
BidF.append( f )
return BidF
def bh2(currentBid, bidHistory, rnd2):
if len( bidHistory ) > 1:
## Handle the first round which would have no bidHistory
if currentBid < rnd2:
return 1
else:
return 0
"""
if 1.0*sum(bidHistory[-1][’Bids’]) / len(bidHistory[-1][’Bids’]) < .5 and currentBid < 5:
return 1
else:
return 0
"""
else:
### If there is no bid history then don’t bid.
return 0
def oo(n):
BidF = []
for rnd in random.uniform(0, 1, size = [1,n])[0]:
f = lambda currentBid, bidHistory, rnd2=rnd: 1 if currentBid<rnd2 else 0
BidF.append( f )
return BidF
def incrementBidHistory(bidHistory, bidderInfo, value):
rnd = len(bidHistory)+1
bids = []
for row in bidderInfo:
bids.append(row["BidFunction"](value, bidHistory))
bh = bidHistory
bh.append({"Round": rnd,
"Value": value,
"Bids": bids})
#print rnd, bids, bh
return bh
def auctionEvaluator(priceFormat, auctionFormat, bidHistory, bidderInfo, value=None, increment=None):
if auctionFormat == "SB":
bids = []
bidders = []
for row in bidderInfo:
bids.append(row["BidFunction"](None, bidHistory))
bidders.append(row["BidderName"])
zipped = sorted(zip(bids, bidders))
bids, bidders = zip(*zipped)
#print bids
#print bidders
return (bids[-priceFormat], bidders[-1])
elif auctionFormat == "OO":
bh = incrementBidHistory(bidHistory, bidderInfo, value)
if np.sum(bh[-1]["Bids"]) == 1:
#print "winner found"
winner_index = bh[-1]["Bids"].index(1)
winnerName = bidderInfo[winner_index]["BidderName"]
winnerBid = value - (priceFormat - 1)*increment
return (winnerName, winnerBid)
elif np.sum(bh[-1]["Bids"]) == 0:
try:
winner_indices = [i for i, j in enumerate(bh[-2]["Bids"]) if j == 1]
winner_index = random.choice(winner_indices)
except:
return ("No winner", -1)
winnerName = bidderInfo[winner_index]["BidderName"]
winnerBid = value - (priceFormat - 1)*increment
return (winnerName, winnerBid)
else:
#print "no winner yet"
v = value + increment
return auctionEvaluator(priceFormat, auctionFormat, bh, bidderInfo, v, increment)
else:
return ("Not a valid auction format. Should be 'SB' or 'OO'.")
# testing
### a)
##### SB, second price, 10 bidders, valuations 0, .1, .2, ..., .9
bidFunctions = sp(10)
bidderNames = ["Bidder"+str(x) for x in range(1,11)]
BI = []
for i in range(10):
BI.append({"BidderName": bidderNames[i],
"BidFunction": bidFunctions[i]})
auctionEvaluator(2, "SB", [], BI)
### b)
##### SB, first price, 10 bidders, valuations 0, .1, .2, ..., .9
bidFunctions = fp(10)
bidderNames = ["Bidder"+str(x) for x in range(1,11)]
BI = []
for i in range(10):
BI.append({"BidderName": bidderNames[i],
"BidFunction": bidFunctions[i]})
auctionEvaluator(1, "SB", [], BI)
### c)
##### OO, second price, 10 bidders, valuations 0, .1, .2, ..., .9, value=0, increment=0.05
bidFunctions = oo(10)
bidderNames = ["Bidder"+str(x) for x in range(1,11)]
BI = []
for i in range(10):
BI.append({"BidderName": bidderNames[i],
"BidFunction": bidFunctions[i]})
auctionEvaluator(2, "OO", [], BI, 0, 0.05)