-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauction.py
230 lines (197 loc) · 7.78 KB
/
auction.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
class Auction:
def __init__(self):
self.epsilon = -1
self.epsilonDiviserMultiplier = 0
self.delta_lim = 0.01
self.bidderAssignments = [-1]
self.goodAssignments = []
self.goodPrices = []
self.costMatrix = [[]]
self.rowSize = 0
self.colSize = 0
self.balancedAssignment = False
def setBalanced(self, balanced):
self.balancedAssignment = balanced
if self.balancedAssignment:
self.goodPrices = [0] * self.colSize
else:
self.goodPrices = [0] * ((self.colSize - 1) + (self.rowSize - 1))
def setInput(self, cMatrix):
self.rowSize = len(cMatrix)
self.colSize = len(cMatrix[0])
self.costMatrix = cMatrix
def initEpsilon(self):
if self.epsilon == -1:
maxValue = -999999999
for i in range(len(self.costMatrix)):
maxValue = max(maxValue, max(self.costMatrix[i]))
self.epsilon = maxValue / 4
if self.epsilon == 0:
self.epsilon = 1
"""epsilon
/= ((epsilonDiviserMultiplier == 0) ? 1 : epsilonDiviserMultiplier * 5);"""
def epsilonScaling(self):
self.epsilon /= 5
def initBiddersAndGoods(self):
self.bidderAssignments = []
self.goodAssignments = []
if self.balancedAssignment:
self.bidderAssignments = [-1] * self.rowSize
self.goodAssignments = [-1] * self.colSize
else:
self.bidderAssignments = [-1] * ((self.rowSize - 1) + (self.colSize - 1))
self.goodAssignments = [-1] * ((self.colSize - 1) + (self.rowSize - 1))
def initFirstRound(self):
self.bidderAssignments[0] = -1
def makeBalancedMatrix(self, matrix):
nRows = len(matrix)
nCols = len(matrix[0])
matrix[nRows - 1][nCols - 1] = 0
# Add rows
for i in range(nCols - 2):
newLine = matrix[nRows - 1]
matrix.append(newLine)
# Add columns
for i in range((nRows - 1) + (nCols - 1)):
for j in range(nRows - 2):
matrix[i].append(matrix[i][nCols - 1])
return matrix
# ----------------------------------------
# Main Functions
# ----------------------------------------
def runAuctionRound(self, cMatrix):
unassignedBidders = []
for i in range(len(self.bidderAssignments)):
unassignedBidders.append(i)
while len(unassignedBidders) != 0:
bidderId = unassignedBidders[0]
unassignedBidders.pop(0)
# Get good with highest value
bestValue = -9999999999
bestSecondValue = -9999999999
bestGoodId = -1
for goodId in range(len(self.goodPrices)):
"""if (
not self.balancedAssignment
and bidderId >= self.rowSize - 1
and goodId >= self.colSize - 1
):
continue"""
if cMatrix[bidderId][goodId] == -1:
continue
goodPrice = self.goodPrices[goodId]
value = -cMatrix[bidderId][goodId] - goodPrice
if value > bestValue:
bestSecondValue = bestValue
bestValue = value
bestGoodId = goodId
elif value > bestSecondValue:
bestSecondValue = value
# Update assignments
self.bidderAssignments[bidderId] = bestGoodId
if self.goodAssignments[bestGoodId] != -1:
unassignedBidders.append(self.goodAssignments[bestGoodId])
self.goodAssignments[bestGoodId] = bidderId
# Update price
delta = abs(bestValue - bestSecondValue) + self.epsilon
self.goodPrices[bestGoodId] = self.goodPrices[bestGoodId] + delta
def run(self):
matchings = []
self.initEpsilon()
# Try to avoid price war
"""tempPrice = max(self.goodPrices)
savedPrices = []
for i in range(len(self.goodPrices)):
old = self.goodPrices[i]
self.goodPrices[i] = self.goodPrices[i] * self.epsilon / (1 if tempPrice == 0 else tempPrice)
t = old - self.goodPrices[i]
savedPrices.append(t)"""
# Make balanced cost matrix
if not self.balancedAssignment:
self.costMatrix = self.makeBalancedMatrix(self.costMatrix)
# Run auction
self.initFirstRound()
while not self.stoppingCriterion(self.costMatrix):
self.initBiddersAndGoods()
self.runAuctionRound(self.costMatrix)
self.epsilonScaling()
"""iter++;
if(numberOfRounds != -1 and iter >= numberOfRounds)
break;"""
# Create output matching
for bidderId in range(len(self.bidderAssignments)):
"""int i = std::min(bidderId, this->rowSize-1);
int j = std::min(bidderAssignments[bidderId], this->colSize-1);"""
i = bidderId
j = self.bidderAssignments[bidderId]
if self.balancedAssignment or (
not self.balancedAssignment
and not (i >= self.rowSize - 1 and j >= self.colSize - 1)
):
matchings.append([i, j, self.costMatrix[i][j]])
# Set prices as before
"""for i in range(len(self.goodPrices)):
self.goodPrices[i] += savedPrices[i]"""
return matchings
# ----------------------------------------
# From data
# ----------------------------------------
def distance(self, p1, p2):
return (p1[0] - p2[0]) ** 2 + (p1[1] - p2[1]) ** 2
def distanceDiag(self, p):
diag = (p[0] + p[1]) / 2
return self.distance(p, [diag, diag])
def makeCostMatrix(self, mu_1, mu_2):
costMatrix = []
for i in range(len(mu_1)):
costMatrix.append([])
for j in range(len(mu_2)):
costMatrix[i].append(self.distance(mu_1[i], mu_2[j]))
costMatrix[i].append(self.distanceDiag(mu_1[i]))
costMatrix.append([])
for j in range(len(mu_2)):
costMatrix[len(mu_1)].append(self.distanceDiag(mu_2[j]))
costMatrix[len(mu_1)].append(0)
return costMatrix
def runFromData(self, mu_1, mu_2):
costMatrix = self.makeCostMatrix(mu_1, mu_2)
self.setInput(costMatrix)
self.setBalanced(False)
matching = self.run()
return matching
def getDistanceFromMatching(self, matching):
return sum(list(map(lambda x: x[2], matching))) ** (1 / 2)
# ----------------------------------------
# Stopping Criterion Functions
# ----------------------------------------
def stoppingCriterion(self, cMatrix):
if self.bidderAssignments[0] == -1: # Auction not started
return False
delta = 5
delta = self.getRelativePrecision(cMatrix)
return not (delta > self.delta_lim)
def getRelativePrecision(self, cMatrix):
d = self.getMatchingDistance(cMatrix)
# if d < 1e-12:
if d < 1e-6:
return 0
denominator = d - len(self.bidderAssignments) * self.epsilon
if denominator <= 0:
return 1
else:
return d / denominator - 1
def getMatchingDistance(self, cMatrix):
d = 0
for bidderId in range(len(self.bidderAssignments)):
i = bidderId
j = self.bidderAssignments[bidderId]
d += cMatrix[i][j]
return d
# ----------------------------------------
# Utils
# ----------------------------------------
def printMatrix(self, matrix):
for i in range(len(matrix)):
for j in range(len(matrix)):
print(matrix[i][j], " ", end="")
print()