-
Notifications
You must be signed in to change notification settings - Fork 3
/
experiment_facebook_comparison.py
350 lines (304 loc) · 10.6 KB
/
experiment_facebook_comparison.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#! /usr/local/bin/python3
import pandas as pd
import random
import os
import sys
import numpy as np
import logging
import pickle
from tqdm import tqdm
from functools import partial
from matplotlib import pyplot as plt
import multiprocessing
import joblib
from timlinucb import (
generate_node2vec_fetures,
timlinucb,
timlinucb_parallel_oim,
timlinucb_parallel_t,
)
from helpers import tim_t, tqdm_joblib, tim_t_parallel, run_algorithm
from rsb import rsb2
# --------------------------------------------------------------------------------------
# %% ------------------------------ Initial setup --------------------------------------
# --------------------------------------------------------------------------------------
# Fancy plots in matplotlib and pandas
random.seed(42)
plt.style.use("ggplot")
pd.options.mode.chained_assignment = None # default='warn'
NUMEXPR_MAX_THREADS = 8 # For RSB
# Setting up logging
VERBOSE = True
LOGGING_FMT = (
"%(levelname)s | %(asctime)s | line %(lineno)s | %(funcName)s | %(message)s"
)
LOGGING_DATEFMT = "%H:%M:%S"
logging_conf = partial(
logging.basicConfig, format=LOGGING_FMT, datefmt=LOGGING_DATEFMT, stream=sys.stdout
)
if VERBOSE:
logging_conf(level=logging.DEBUG)
else:
logging_conf(level=logging.INFO)
# --------------------------------------------------------------------------------------
# %% ------------------------------ Dataset setup --------------------------------------
# --------------------------------------------------------------------------------------
DATASET_DIR = os.path.join("..", "Datasets")
if not os.path.exists(DATASET_DIR):
print("Can't find the dataset directory!")
DATASET_FACEBOOK = os.path.join(
DATASET_DIR, "fb-wosn-friends", "fb-wosn-friends-clean.edges"
)
DATASET_DIGG = os.path.join(DATASET_DIR, "digg-friends", "digg-friends.edges")
DATASET_SCHOOL = os.path.join(
DATASET_DIR, "ia-primary-school-proximity", "ia-primary-school-proximity.edges"
)
DATASET_PHONES = os.path.join(DATASET_DIR, "mit", "out.mit")
NUMBER_OF_DAYS = 20
# NUMBER_OF_DAYS = "ALL"
NUM_FEATURES_NODE2VEC = 20
NUM_SEEDS_TO_FIND = 5
#
# --------------------------------------------------------------------------------------
# %% ------------------------- Preparing the Facebook friends dataset ------------------
# --------------------------------------------------------------------------------------
#
logging.debug("Processing the Facebook dataset...")
logging.debug("Getting the graph...")
df_facebook = pd.read_csv(
DATASET_FACEBOOK,
sep=" ",
# ??? is probably weights, they are always 1
names=["source", "target", "???", "timestamp"],
skiprows=2,
)
# Processing the dataframe
logging.debug("Splitting the graph into time steps...")
df_facebook["day"] = pd.to_datetime(df_facebook["timestamp"], unit="s").dt.floor("d")
df_facebook = df_facebook.sort_values(by=["timestamp", "source", "target"])
df_facebook = df_facebook[["source", "target", "day"]]
df_facebook_nodes = np.sort(
np.unique(np.hstack((df_facebook["source"], df_facebook["target"])))
)
# Getting the true weights
logging.debug('Generating "true" activation probabilities...')
df_facebook["probab"] = np.random.uniform(0, 0.1, size=df_facebook.shape[0])
# df_facebook['probab'] = np.random.uniform(0, 1, size=df_facebook.shape[0])
if NUMBER_OF_DAYS == "ALL":
df_facebook_times = np.sort(np.unique(df_facebook["day"]))[0:]
else:
df_facebook_times = np.sort(np.unique(df_facebook["day"]))[0:NUMBER_OF_DAYS]
# TIMLinUCB only
logging.debug("Generating node2vec features...")
df_facebook_feats = generate_node2vec_fetures(
df_facebook, num_features=NUM_FEATURES_NODE2VEC
)
DATASET = df_facebook
DATASET_FEATS = df_facebook_feats
DATASET_TIMES = df_facebook_times
DATASET_NODES = df_facebook_nodes
# --------------------------------------------------------------------------------------
# %% ----------------------- Comparing TIMLinUCB w/ RSB and t_TIM ---------------------
# --------------------------- Details: 20 seeds, start->30 days ------------------------
logging.debug(
"Comparing TIMLinUCB with t_TIM and RSB - Facebook dataset (30 days/start)"
)
NUMBER_OF_DAYS = 30
DATASET_TIMES = np.sort(np.unique(DATASET["day"]))[0:NUMBER_OF_DAYS]
NUM_SEEDS_TO_FIND = 20
# RSB parameters
OPTIMAL_C_RSB = 1
OPTIMAL_GAMMA_RSB = 0.4
NUM_REPEATS_EXPECT_RSB = 20
# TIMLinUCB parameters
OPTIMAL_EPS_TLU = 0.5
OPTIMAL_SIGMA_TLU = 1
OPTIMAL_C_TLU = 0.1
OPTIMAL_NUM_REPEATS_OIM_TLU = 20
OPTIMAL_NUM_REPEATS_REW_TLU = 20
# TIM_T parameters
OPTIMAL_EPS_TIM_T = 0.5
OPTIMAL_NUM_REPEATS_REW_TIM_T = 20
setup_array = []
# Setting up TIM_t (offline algorithm)
setup_array.append(
{
"algo_name": "tim_t",
"function": tim_t_parallel,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_reward": OPTIMAL_NUM_REPEATS_REW_TIM_T,
"epsilon": OPTIMAL_EPS_TIM_T,
},
}
)
# Setting up RSB with persistent features
setup_array.append(
{
"algo_name": "rsb_persist",
"function": rsb2,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"C": OPTIMAL_C_RSB,
"gamma": OPTIMAL_GAMMA_RSB,
"num_repeats_expect": NUM_REPEATS_EXPECT_RSB,
"persist_params": True,
"hide_tqdm": True,
},
}
)
# Setting up RSB without persistent features
setup_array.append(
{
"algo_name": "rsb_nopersist",
"function": rsb2,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"C": OPTIMAL_C_RSB,
"gamma": OPTIMAL_GAMMA_RSB,
"num_repeats_expect": NUM_REPEATS_EXPECT_RSB,
"persist_params": False,
"hide_tqdm": True,
},
}
)
# Setting up TIMLinUCB
setup_array.append(
{
"algo_name": "timlinucb_nopersist",
"function": timlinucb_parallel_oim,
"args": [DATASET, DATASET_FEATS, DATASET_TIMES, DATASET_NODES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_oim": OPTIMAL_NUM_REPEATS_OIM_TLU,
"num_repeats_oim_reward": OPTIMAL_NUM_REPEATS_REW_TLU,
"sigma": OPTIMAL_SIGMA_TLU,
"c": OPTIMAL_C_TLU,
"epsilon": OPTIMAL_EPS_TLU,
"hide_tqdm": True,
"process_id": "fb1",
"max_jobs": -5,
},
}
)
# Setting up TIMLinUCB (persistent parameters)
setup_array.append(
{
"algo_name": "timlinucb_persist",
"function": timlinucb_parallel_t,
"args": [DATASET, DATASET_FEATS, DATASET_TIMES, DATASET_NODES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_oim": OPTIMAL_NUM_REPEATS_OIM_TLU,
"num_repeats_oim_reward": OPTIMAL_NUM_REPEATS_REW_TLU,
"sigma": OPTIMAL_SIGMA_TLU,
"c": OPTIMAL_C_TLU,
"epsilon": OPTIMAL_EPS_TLU,
"persist": True,
"process_id": "fb2",
},
}
)
with tqdm_joblib(tqdm(desc="Algorithm comparison (start)", total=len(setup_array))):
results_array = joblib.Parallel(n_jobs=len(setup_array))(
joblib.delayed(run_algorithm)(setup_dict) for setup_dict in setup_array
)
with open("comparison_facebook_20s_30t_start_2.pickle", "wb") as f:
pickle.dump(results_array, f)
# --------------------------------------------------------------------------------------
# %% ----------------------- Comparing TIMLinUCB w/ RSB and t_TIM ---------------------
# --------------------------- Details: 20 seeds, 30 days->end --------------------------
logging.debug("Comparing TIMLinUCB with t_TIM and RSB - Facebook dataset (30 days/end)")
DATASET_TIMES = np.sort(np.unique(DATASET["day"]))[0:NUMBER_OF_DAYS]
setup_array = []
# Setting up TIM_t (offline algorithm)
setup_array.append(
{
"algo_name": "tim_t",
"function": tim_t_parallel,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_reward": OPTIMAL_NUM_REPEATS_REW_TIM_T,
"epsilon": OPTIMAL_EPS_TIM_T,
},
}
)
# Setting up RSB with persistent features
setup_array.append(
{
"algo_name": "rsb_persist",
"function": rsb2,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"C": OPTIMAL_C_RSB,
"gamma": OPTIMAL_GAMMA_RSB,
"num_repeats_expect": NUM_REPEATS_EXPECT_RSB,
"persist_params": True,
"hide_tqdm": True,
},
}
)
# Setting up RSB without persistent features
setup_array.append(
{
"algo_name": "rsb_nopersist",
"function": rsb2,
"args": [DATASET, DATASET_NODES, DATASET_TIMES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"C": OPTIMAL_C_RSB,
"gamma": OPTIMAL_GAMMA_RSB,
"num_repeats_expect": NUM_REPEATS_EXPECT_RSB,
"persist_params": False,
"hide_tqdm": True,
},
}
)
# Setting up TIMLinUCB
setup_array.append(
{
"algo_name": "timlinucb_nopersist",
"function": timlinucb_parallel_oim,
"args": [DATASET, DATASET_FEATS, DATASET_TIMES, DATASET_NODES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_oim": OPTIMAL_NUM_REPEATS_OIM_TLU,
"num_repeats_oim_reward": OPTIMAL_NUM_REPEATS_REW_TLU,
"sigma": OPTIMAL_SIGMA_TLU,
"c": OPTIMAL_C_TLU,
"epsilon": OPTIMAL_EPS_TLU,
"hide_tqdm": True,
"process_id": "fb1",
"max_jobs": -5,
},
}
)
# Setting up TIMLinUCB (persistent parameters)
setup_array.append(
{
"algo_name": "timlinucb_persist",
"function": timlinucb_parallel_t,
"args": [DATASET, DATASET_FEATS, DATASET_TIMES, DATASET_NODES],
"kwargs": {
"num_seeds": NUM_SEEDS_TO_FIND,
"num_repeats_oim": OPTIMAL_NUM_REPEATS_OIM_TLU,
"num_repeats_oim_reward": OPTIMAL_NUM_REPEATS_REW_TLU,
"sigma": OPTIMAL_SIGMA_TLU,
"c": OPTIMAL_C_TLU,
"epsilon": OPTIMAL_EPS_TLU,
"persist": True,
"process_id": "fb2",
},
}
)
with tqdm_joblib(tqdm(desc="Algorithm comparison (start)", total=len(setup_array))):
results_array = joblib.Parallel(n_jobs=len(setup_array))(
joblib.delayed(run_algorithm)(setup_dict) for setup_dict in setup_array
)
with open("comparison_facebook_20s_30t_end_2.pickle", "wb") as f:
pickle.dump(results_array, f)