-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
103 lines (83 loc) · 5.28 KB
/
main.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
import argparse
import pandas as pd
import numpy as np
import sys
import os
from utils.parser import read_options
from models.LinUCB import LinUCB
from models.LinTS import LinTS
from models.C2SB import C2SB
from utils.oracle import set_oracle
opts = read_options(sys.argv[1:])
base_dir = os.getcwd()
if opts.dataset == 'numerical':
save_path = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_T_{opts.time_horizon}_error_var_{str(opts.error_var).replace('.','')}_d_{opts.dim}_n_{opts.arms}_nu_{opts.nu}_reward_{opts.reward_function}_k_{opts.super_set_size}_seed_{opts.seed}"
else: # real data
save_path = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_d_{opts.dim}_reward_{opts.reward_function}_k_{opts.super_set_size}_seed_{opts.seed}"
if save_path is not None:
save_path = base_dir + '/results/' + save_path
if not os.path.exists(save_path):
os.makedirs(save_path, exist_ok=True)
with open(os.path.join(save_path + '/args.txt'), 'w') as f:
f.write('\n'.join([str(k) + ',' + str(v) for k, v in sorted(vars(opts).items(), key=lambda x: x[0])]))
f.write('\n')
# load data_loader
if opts.dataset == 'numerical':
from data_loader.numerical_loader import *
data_loader = NumericalDataLoader(opts)
elif opts.dataset == 'movielens':
from data_loader.movielens_loader import *
data_loader = MovieLensDataLoader(opts)
data_loader.print_info()
print("This simulation is running with the following options:")
print(*opts.__dict__.items(), sep="\n")
# exploitation_rate_tuning_list = [1, 0.5, 0.25, 0.125, 0.0625, 0.0125, 0.00625]
exploitation_rate_tuning_list = [0.0125, 0.00625]
tuning_num = len(exploitation_rate_tuning_list)
if opts.is_tuning == True:
if opts.dataset == 'numerical':
save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_error_var_{str(opts.error_var).replace('.','')}_d_{opts.dim}_n_{opts.arms}_nu_{opts.nu}_reward_{opts.reward_function}_k_{opts.super_set_size}"
else:
save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_d_{opts.dim}_reward_{opts.reward_function}_k_{opts.super_set_size}"
# opt_cum_expected_reward, opt_cum_real_reward = optimal()
for model, oracle in eval(opts.models):
oracle_func = set_oracle(oracle)
print(f"Running {model}_{oracle} with tuning")
tuning_table = pd.DataFrame(columns=['exploitation_rate'])
tuning_table['exploitation_rate'] = exploitation_rate_tuning_list
tuning_table['cumulative_real_reward'] = np.nan
for exploitation_rate in exploitation_rate_tuning_list:
real_reward_list = []
for iter in range(opts.iterations):
data_loader.data_init_for_iter(iter)
real_rewards, _ = eval(model)(data_loader, exploitation_rate, oracle_func)
real_reward_list.append(np.cumsum(real_rewards)[-1])
tuning_table.loc[tuning_table['exploitation_rate'] == exploitation_rate, 'cumulative_real_reward'] = np.mean(real_reward_list)
tuning_table.to_csv(f"{save_path}/tuning_real_reward_{model}_{oracle}_{save_name}.csv", index=False)
# when tuning is not needed
else:
if opts.dataset == 'numerical':
tuning_save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_error_var_{str(opts.error_var).replace('.','')}_d_{opts.dim}_n_{opts.arms}_nu_{opts.nu}_reward_{opts.reward_function}_k_{opts.super_set_size}"
save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_T_{opts.time_horizon}_error_var_{str(opts.error_var).replace('.','')}_d_{opts.dim}_n_{opts.arms}_nu_{opts.nu}_reward_{opts.reward_function}_k_{opts.super_set_size}"
else:
tuning_save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_tuningT_{opts.tuning_time_horizon}_d_{opts.dim}_reward_{opts.reward_function}_k_{opts.super_set_size}"
save_name = f"dataset_{opts.dataset}_iteration_{opts.iterations}_d_{opts.dim}_reward_{opts.reward_function}_k_{opts.super_set_size}"
for model, oracle in eval(opts.models):
oracle_func = set_oracle(oracle)
print(f"Running {model}_{oracle} main simulation")
# select optimal exploitation rate
tuning_table = pd.read_csv(f"{save_path}/tuning_real_reward_{model}_{oracle}_{tuning_save_name}.csv")
exploitation_rate = tuning_table.loc[tuning_table['cumulative_real_reward'].idxmax()]['exploitation_rate']
print(f"Optimal exploitation rate for {model}_{oracle} is {exploitation_rate}")
# run the main simulation
cum_regret_list = []
cum_reward_list = []
for iter in range(opts.iterations):
data_loader.data_init_for_iter(iter)
real_rewards, regrets = eval(model)(data_loader, exploitation_rate, oracle_func)
cum_reward_list.append(np.cumsum(real_rewards))
cum_regret_list.append(np.cumsum(regrets))
with open(f"{save_path}/cum_reward_{model}_{oracle}_{save_name}.npy", "wb") as f:
np.save(f, cum_reward_list)
with open(f"{save_path}/cum_regret_{model}_{oracle}_{save_name}.npy", "wb") as f:
np.save(f, cum_regret_list)