-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbid_experiment.py
322 lines (295 loc) · 14 KB
/
bid_experiment.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
import os.path
import random
from argparse import ArgumentParser
import json, yaml, copy
import numpy as np
import time
from bidding.DGA import eval_DGA_profit
VALUE_RANGE = (0, 1)
COLOR_LIST = ['tab:blue', 'tab:orange', 'tab:green', 'tab:brown', 'tab:gray']
VAL_TIMES = 20
def load_performance_results(config):
result_path = config['performance_result']
performance = json.load(open(result_path, 'r'))
N = len(performance)
performance_improvement = {'val': np.zeros((VAL_TIMES, N)),
'test': np.zeros(N)}
# performance_improvement = np.zeros(N)
for i in range(N):
performance_improvement['test'][i] = performance[str(i)][
"test_improve"]
local = performance[str(i)]["local"]
for j, v in enumerate(performance[str(i)]['vfl_valid']):
performance_improvement['val'][j, i] = v - local
return performance_improvement
def generate_values(N, random_type='uniform'):
if random_type == 'uniform':
return np.round(
np.random.random(N) * (VALUE_RANGE[1] - VALUE_RANGE[0]), 3)
else:
raise NotImplementedError
def emulate_untruthful(client_values, untruthful_client, granularity=100):
'''
client_value: real client values, float in (0, 1)
untruthful_client: assuming the
:return: list of bids in which only varying untruthful_client bids
'''
untruthful_cases = []
manipulates = []
for i in range(granularity):
case = copy.deepcopy(client_values)
untruthful_bid = np.round(
(VALUE_RANGE[1] - VALUE_RANGE[0]) / granularity * (i + 1), 3)
case[untruthful_client] = untruthful_bid
untruthful_cases.append(case)
manipulates.append(untruthful_bid)
return untruthful_cases, manipulates
def eval_fixed_k_profit(client_values, client_bids_or_ranking, performances,
config):
K = config['K']
profit = np.zeros(len(client_values))
if 'changing_ranking' in config and config[
'changing_ranking'] and 'cur_untruthful' in config:
current_untruthful = config['cur_untruthful']
sorted_bidders = get_sorted(performances, client_values)
sorted_bidders = [i for i in sorted_bidders if i != current_untruthful]
ranking = client_bids_or_ranking
if ranking <= K:
# force this client one of the winner
threshold_bidder = sorted_bidders[-K]
payment = client_values[threshold_bidder] * performances[
threshold_bidder]
winners = np.append(sorted_bidders[-(K - 1):], current_untruthful)
else:
# force this client not to be winner
threshold_bidder = sorted_bidders[-(K + 1)]
payment = client_values[threshold_bidder] * performances[
threshold_bidder]
winners = sorted_bidders[-K:]
elif 'changing_score' in config and config[
'changing_score'] and 'cur_untruthful' in config:
current_untruthful = config['cur_untruthful']
hacked_perfs = copy.deepcopy(performances)
hacked_perfs[current_untruthful] = client_bids_or_ranking[
current_untruthful] * config[
'sensitivity']
hacked_bids = copy.deepcopy(client_bids_or_ranking)
hacked_bids[current_untruthful] = 1.0
sorted_bidders = get_sorted(hacked_perfs, hacked_bids)
threshold_bidder = sorted_bidders[-(K + 1)]
payment = hacked_bids[threshold_bidder] * hacked_perfs[
threshold_bidder]
winners = sorted_bidders[-K:]
print(current_untruthful, hacked_perfs[current_untruthful],
hacked_bids[current_untruthful])
elif 'changing_performance' in config and config[
'changing_performance'] and 'cur_untruthful' in config:
current_untruthful = config['cur_untruthful']
hacked_perfs = copy.deepcopy(performances)
hacked_perfs[current_untruthful] = client_bids_or_ranking[
current_untruthful] * config[
'sensitivity']
hacked_bids = copy.deepcopy(client_bids_or_ranking)
hacked_bids[current_untruthful] = client_values[current_untruthful]
sorted_bidders = get_sorted(hacked_perfs, hacked_bids)
threshold_bidder = sorted_bidders[-(K + 1)]
payment = hacked_bids[threshold_bidder] * hacked_perfs[
threshold_bidder]
winners = sorted_bidders[-K:]
print(current_untruthful, hacked_perfs[current_untruthful],
hacked_bids[current_untruthful])
else:
client_bids = client_bids_or_ranking
sorted_bidders = get_sorted(performances, client_bids)
threshold_bidder = sorted_bidders[-(K + 1)]
payment = client_bids[threshold_bidder] * performances[
threshold_bidder]
winners = sorted_bidders[-K:]
print('== winners', winners)
gains = client_values[winners] * performances[winners]
profit[winners] = gains - payment
seller_profit = payment * len(winners)
return profit, seller_profit
def get_sorted(performances, bids):
performances = np.array(performances)
bids = np.array(bids)
gain = performances * bids
sorted_clients = np.argsort(gain)
return sorted_clients
def eval_with_val(eval_profit, client_values, bids, client_performance,
config):
eval_val_client_results = []
eval_val_seller_results = []
for i in range(VAL_TIMES):
client_profit, seller_profit = eval_profit(client_values, bids,
client_performance['val'][
i], config)
eval_val_client_results.append(client_profit)
eval_val_seller_results.append(seller_profit)
if isinstance(eval_val_client_results[0][0], float):
eval_val_client_avg = np.mean(eval_val_client_results, axis=0)
eval_val_client_std = np.std(eval_val_client_results, axis=0)
eval_val_client_min = np.min(eval_val_client_results, axis=0)
eval_val_client_max = np.max(eval_val_client_results, axis=0)
else:
# for EM
eval_val_client_avg = np.mean(eval_val_client_results, axis=(0, 1))
eval_val_client_std = np.std(eval_val_client_results, axis=(0, 1))
eval_val_client_min = np.quantile(eval_val_client_results, q=0.25,
axis=(0, 1))
eval_val_client_max = np.quantile(eval_val_client_results, q=0.75,
axis=(0, 1))
eval_val_seller_avg = np.mean(eval_val_seller_results)
eval_val_seller_std = np.std(eval_val_seller_results)
return eval_val_client_avg, \
eval_val_client_std, \
eval_val_client_min, \
eval_val_client_max, \
eval_val_seller_avg, \
eval_val_seller_std
def eval_with_test(eval_profit, client_values, bids, client_performance,
config):
if config['auction_type'] == 'DGA' and config["strategy"] != "naive":
client_profit, seller_profit = eval_profit(client_values, bids,
client_performance['test'],
config)
client_profit = np.mean(client_profit, axis=0)
seller_profit = np.mean(seller_profit)
else:
client_profit, seller_profit = eval_profit(client_values, bids,
client_performance['test'],
config)
return client_profit, seller_profit
def save_to_json(results, untruthful_client, true_rank, config):
file_name = "./results/bidding/" + str(config['fig_save_name']).replace(
'.png', '')
file_name += '.json'
if os.path.isfile(file_name):
with open(file_name, 'r') as jsonf:
old_results = json.load(jsonf)
else:
if not os.path.isdir("./results/bidding/"):
os.makedirs("./results/bidding/")
old_results = {}
old_results[untruthful_client] = [results, true_rank]
with open(file_name, 'w') as jsonf:
json.dump(old_results, jsonf, indent=2)
print("save to " + file_name)
def clean_json(config):
file_name = "./results/bidding/" + str(config['fig_save_name']).replace(
'.png', '')
file_name += '.json'
with open(file_name, 'w') as jsonf:
json.dump({}, jsonf, indent=2)
def simulate_profit(args):
# load config
config = yaml.safe_load(open(args.config, 'r'))
if 'seed' in config:
np.random.seed(config['seed'])
random.seed(config['seed'])
client_performance = load_performance_results(config)
print(np.max(client_performance['test']),
np.max(client_performance['val']))
print("max perf:", max(client_performance['test']),
np.max(client_performance['val']))
client_test_performance = client_performance['test']
client_val_performance = client_performance['val']
client_values = generate_values(config['N'])
print(client_values)
print("best:", np.argsort(client_performance['test'] * client_values))
client_bids = copy.deepcopy(client_values)
N = len(client_values)
# different auctions
if config['auction_type'] == 'DGA':
eval_profit = eval_DGA_profit
else:
eval_profit = eval_fixed_k_profit
clean_json(config)
for untruthful_client in config['untruthful_client']:
print(
f"untruthful client performance improved: {client_performance['test'][untruthful_client]}"
f" {client_performance['val'][:, untruthful_client]}")
untruthful_bidding_cases, bids = emulate_untruthful(client_bids,
untruthful_client)
various_profit = {}
truthful_eval_config = copy.deepcopy(config)
if 'strategy' in truthful_eval_config:
truthful_eval_config['strategy'] = 'naive'
truthful_profits, seller_profit = \
eval_with_test(eval_profit, client_values, client_values,
client_performance, truthful_eval_config)
if 'changing_performance' in config:
various_profit['truthful'] = (
client_performance['test'][untruthful_client],
truthful_profits[untruthful_client])
elif 'changing_score' in config:
various_profit['truthful'] = (
client_performance['test'][untruthful_client] * client_values[
untruthful_client],
truthful_profits[untruthful_client])
else:
various_profit['truthful'] = (client_values[untruthful_client],
truthful_profits[untruthful_client])
print(untruthful_client, various_profit, various_profit['truthful'])
# exit()
various_profit['untruthful'] = {}
# for manipulate untruthful client ranking
config['cur_untruthful'] = untruthful_client
if 'changing_ranking' in config and config['changing_ranking']:
untruthful_bidding_cases = np.arange(1, len(client_values) + 1)
bids = list(range(1, len(client_values) + 1))
if 'changing_performance' in config and config['changing_performance']:
various_profit['truthful'] = (
client_performance['test'][untruthful_client],
truthful_profits[untruthful_client])
granularity = 100
bids = np.arange(0, config['sensitivity'] + 1e-5,
config['sensitivity'] * (
VALUE_RANGE[1] - VALUE_RANGE[
0]) / granularity)
if 'changing_score' in config and config['changing_score']:
various_profit['truthful'] = (
client_performance['test'][untruthful_client] * client_values[
untruthful_client],
truthful_profits[untruthful_client])
granularity = 100
bids = np.arange(0, config['sensitivity'] + 1e-5,
config['sensitivity'] * (
VALUE_RANGE[1] - VALUE_RANGE[
0]) / granularity)
for i, ut_bids in enumerate(untruthful_bidding_cases):
# print(ut_bids[untruthful_client])
eval_val_client_avg, eval_val_client_std, eval_val_client_min, eval_val_client_max, eval_val_seller_avg, eval_val_seller_std = \
eval_with_val(eval_profit, client_values, ut_bids,
client_performance, config)
client_ut_test_profit, seller_ut_test_profit = \
eval_with_test(eval_profit, client_values, ut_bids,
client_performance, config)
various_profit['untruthful'][bids[i]] = [
eval_val_client_avg[untruthful_client],
eval_val_client_std[untruthful_client],
client_ut_test_profit[untruthful_client],
eval_val_client_min[untruthful_client],
eval_val_client_max[untruthful_client],
]
del config['cur_untruthful']
true_rank = -1
sorted_true = get_sorted(client_test_performance, client_values)
print("sorted test (no manipulation):", sorted_true)
if 'K' in config:
print(sorted_true[-int(config['K']):],
sorted_true[-int(config['K']) - 5:-int(config['K'])])
for i, c in enumerate(sorted_true):
if c == untruthful_client:
true_rank = i
save_to_json(various_profit, untruthful_client, N - true_rank, config)
if __name__ == "__main__":
parser = ArgumentParser()
# Dataset can be provided via command line
parser.add_argument("-c", "--config", type=str,
help='bidding config file path')
args = parser.parse_args()
start_time = time.time()
simulate_profit(args)
end_time = time.time()
print(f"auction mechanism takes time: {end_time - start_time}")