forked from angiuf/OLA-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ts_split_test.py
161 lines (123 loc) · 5.62 KB
/
ts_split_test.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
from Source.TSLearner2 import *
from Source.SplittingLearner import *
from Source.Auxiliary import *
from tqdm import trange
def main():
fully_i_regret, fully_i_reward, opt_rew = run()
not_fully_i_regret, not_fully_i_reward, not_fully_opt_rew = run(False)
plt.figure(1, (16,9))
plt.suptitle("TS test, split case")
show_results(fully_i_regret, "Fully connected: regret", 221)
show_results(not_fully_i_regret, "Not fully connected: regret", 222)
show_reward(fully_i_reward, opt_rew, "Fully connected: reward", 223)
show_reward(not_fully_i_reward, not_fully_opt_rew, "Not fully connected: reward", 224)
plt.show()
def run(f_c=True):
env1, model = generate_environment(f_c)
real_conv_rates = model["real_conversion_rates"]
prices = model["prices"]
T = 180
n_exp = 20
daily_user = 200
all_features = [[0, 0], [0, 1], [1, 0], [1, 1]]
optimal_reward = 0
optimal_reward_c = np.zeros(3)
for c in range(3):
env_c, model_c = generate_environment_class(c, f_c)
real_conv_rates_c = model_c["real_conversion_rates"]
prices_c = model_c["prices"]
optimal_arm_c = optimization_algorithm(model_c, False) # pull the optimal arm
print("Optimal_arm of class ", c, " : ", optimal_arm_c)
optimal_act_rate_c = mc_simulation(model_c, real_conv_rates_c[range(5), optimal_arm_c], 5, 10000)
optimal_reward_c[c] = return_reward(model_c, prices_c[range(5), optimal_arm_c],
real_conv_rates_c[range(5), optimal_arm_c],
optimal_act_rate_c, model_c['real_alpha_ratio'], model_c['real_quantity'])
print("Optimal reward of class ", c, " : ", optimal_reward_c[c])
optimal_reward += optimal_reward_c[c] * model["class_probability"][c]
print("\nOptimal reward: ", optimal_reward)
learner = TSLearner2(model.copy())
instant_regret_obs = [[] for _ in range(n_exp)]
instant_reward_obs = [[] for _ in range(n_exp)]
for i in range(n_exp):
print("Experiment number", i + 1)
alldata = []
for t in trange(14):
pulled_arm = learner.act()
alpha_ratio = env1.alpha_ratio_otd()
data = env1.round_single_day_split(daily_user, alpha_ratio, [pulled_arm for _ in range(4)],
all_features)
alldata.append(data)
cr_data = conv_data(data)
ar_data = alpha_data(data)
q_data = quantity_data(data)
learner.update(pulled_arm, cr_data, ar_data, q_data)
obs_reward = 0
if len(data):
for i_ in range(len(data)):
obs_reward += np.sum(data[i_][0])
obs_reward /= len(data)
instant_regret_obs[i].append(optimal_reward - obs_reward)
instant_reward_obs[i].append(obs_reward)
learner.feat = all_features
learners = first_split(model.copy(), alldata.copy(), False, learner.model.copy())
# for ler in learners:
# print(ler.feat)
if learners == []:
learners = [learner]
for t in trange(T - 14):
pulled_arms = []
for ii in range(len(learners)):
pulled_arms.append(learners[ii].act())
pulled_arm = []
for features in all_features:
for jj in range(len(learners)):
if features in learners[jj].feat:
pulled_arm.append(pulled_arms[jj])
alpha_ratio = env1.alpha_ratio_otd()
data = env1.round_single_day_split(daily_user, alpha_ratio, pulled_arm,
all_features)
alldata.append(data)
# run base learner
# day_0_0 = customers with feature [0,0] ...
day_0_0 = []
day_0_1 = []
day_1_0 = []
day_1_1 = []
for cust in data:
if cust[3] == [0, 0]:
day_0_0.append(cust)
elif cust[3] == [0, 1]:
day_0_1.append(cust)
elif cust[3] == [1, 0]:
day_1_0.append(cust)
else:
day_1_1.append(cust)
day_tot = {0: day_0_0,
1: day_0_1,
2: day_1_0,
3: day_1_1,
}
for feat in all_features:
cr_data = conv_data(day_tot[all_features.index(feat)])
ar_data = alpha_data(day_tot[all_features.index(feat)])
q_data = quantity_data(day_tot[all_features.index(feat)])
learner.update(day_tot[all_features.index(feat)][0][7], cr_data, ar_data, q_data)
for ler in learners:
if feat in ler.feat:
ler.update(day_tot[all_features.index(feat)][0][7], cr_data, ar_data, q_data)
obs_reward = 0
if len(data):
for i_ in range(len(data)):
obs_reward += np.sum(data[i_][0])
obs_reward /= len(data)
instant_regret_obs[i].append(optimal_reward - obs_reward)
instant_reward_obs[i].append(obs_reward)
if t % 14 == 0 and t > 0:
learners = first_split(model.copy(), alldata.copy(), False, learner.model.copy())
# for ler in learners:
# print(ler.feat)
if learners == []:
learners = [learner]
learner.reset()
return instant_regret_obs, instant_reward_obs, optimal_reward
main()