forked from Cattharine/product_owner_rl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eval_model.py
266 lines (211 loc) · 9.63 KB
/
eval_model.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
import numpy as np
import matplotlib.pyplot as plt
from environment import CreditPayerEnv, TutorialSolverEnv, ProductOwnerEnv
from environment.backlog_env import BacklogEnv
from environment.userstory_env import UserstoryEnv
from pipeline.study_agent import load_dqn_agent
from pipeline.base_study import MAX_INNER_SPRINT_ACTION_COUNT
from pipeline import TUTORIAL, CREDIT_FULL, CREDIT_START, CREDIT_END, END
from pipeline.aggregator_study import update_reward_system_config
from environment.reward_sytem import (EmpiricalCreditStageRewardSystem,
EmpiricalRewardSystem,
FullPotentialCreditRewardSystem,
PotentialEndStageRewardSystem)
SMALL_SIZE = 16
MEDIUM_SIZE = 20
BIGGER_SIZE = 22
plt.rc('font', size=MEDIUM_SIZE) # controls default text sizes
plt.rc('axes', titlesize=SMALL_SIZE) # font size of the axes title
plt.rc('axes', labelsize=MEDIUM_SIZE) # font size of the x and y labels
plt.rc('xtick', labelsize=SMALL_SIZE) # font size of the tick labels
plt.rc('ytick', labelsize=SMALL_SIZE) # font size of the tick labels
plt.rc('legend', fontsize=SMALL_SIZE) # legend font size
plt.rc('figure', titlesize=BIGGER_SIZE) # font size of the figure title
def eval_model(environments, agents, order, repeat_count: int, is_silent: bool):
rewards, sprints, loyalties, customers, money, wins = [], [], [], [], [], []
for _ in range(repeat_count):
reward = play_eval_trajectory(environments, agents, order, is_silent)
rewards.append(reward)
update_logs(environments[order[-1]], sprints, loyalties, customers, money, wins)
customers = np.array(customers)
loyalties = np.array(loyalties)
return (np.median(rewards),
{"money": money,
"sprints": sprints,
"loyalty": loyalties,
"customers": customers,
"potential money": customers * loyalties * 300,
"wins": wins})
def play_eval_trajectory(environments, agents, order, is_silent):
for stage in order:
assert stage in environments
assert stage in agents or stage == END
env = environments[order[-1]]
full_reward = 0
env.reset()
for stage in order:
if stage == END and not (stage in agents):
full_reward += play_end(env, is_silent)
else:
full_reward += play_some_stage(env,
environments[stage],
agents[stage],
f"{stage} reward",
is_silent)
if not is_silent:
print(f"full reward: {full_reward},"
f"current sprint: {env.game.context.current_sprint}")
return full_reward
def update_logs(env, sprints, loyalties, customers, money, wins):
context = env.game.context
loyalties.append(context.get_loyalty())
customers.append(context.customers)
wins.append(int(context.is_victory))
sprints.append(context.current_sprint)
money.append(context.get_money())
def play_end(main_env, is_silent=True):
done = main_env.game.context.get_money() < 0 or main_env.game.context.customers <= 0
total_reward = 0
while not done:
state, reward, done, info = main_env.step(0)
total_reward += reward
return total_reward
def play_some_stage(main_env: ProductOwnerEnv, translator_env: ProductOwnerEnv, agent,
state_line, is_silent=True):
translator_env.game = main_env.game
lost_customers = not main_env.game.context.is_new_game and main_env.game.context.customers <= 0
done = main_env.game.context.get_money() < 0 or lost_customers
state = translator_env.recalculate_state()
info = translator_env.get_info()
done = done or len(info["actions"]) == 0
inner_sprint_action_count = 0
total_reward = 0
while not done:
action, inner_sprint_action_count = choose_action(agent, state, info,
inner_sprint_action_count)
state, reward, done, info = translator_env.step(action)
total_reward += reward
if not is_silent:
print(f"{state_line}: {total_reward}")
return total_reward
def choose_action(agent, state, info, inner_sprint_action_count, is_silent=True):
action = agent.get_action(state, info)
if action == 0:
inner_sprint_action_count = 0
else:
inner_sprint_action_count += 1
if inner_sprint_action_count == MAX_INNER_SPRINT_ACTION_COUNT:
action = 0
inner_sprint_action_count = 0
if not is_silent:
print("enforced next sprint")
return action, inner_sprint_action_count
def load_agents(paths):
agents = {}
for stage, path in paths.items():
agent = load_dqn_agent(path)
agent.epsilon = 0
agents[stage] = agent
return agents
def get_backlog_environments():
return {
TUTORIAL: BacklogEnv(4, 0, 0, 0, 0, 0),
CREDIT_FULL: BacklogEnv(12, 4, 2, 0, 0, 0),
CREDIT_END: BacklogEnv(12, 4, 2, 0, 0, 0),
CREDIT_START: BacklogEnv(12, 4, 2, 0, 0, 0),
END: BacklogEnv(12, 4, 2, 0, 0, 0)
}
def get_userstory_environments():
return {
TUTORIAL: None,
CREDIT_FULL: UserstoryEnv(6, 2, 2),
CREDIT_END: UserstoryEnv(6, 2, 2),
CREDIT_START: UserstoryEnv(6, 2, 2),
END: UserstoryEnv(6, 2, 2)
}
def get_reward_systems():
return {
TUTORIAL: EmpiricalRewardSystem(config={}),
CREDIT_FULL: FullPotentialCreditRewardSystem(config={}),
CREDIT_START: EmpiricalCreditStageRewardSystem(with_late_purchase_punishment=False,
config={}),
CREDIT_END: EmpiricalCreditStageRewardSystem(with_late_purchase_punishment=True, config={}),
END: PotentialEndStageRewardSystem(config={}, potential_weight=1)
}
def get_environments(userstory_environments, backlog_environments, reward_systems, with_info):
res = {
TUTORIAL: TutorialSolverEnv(userstory_env=userstory_environments[TUTORIAL],
backlog_env=backlog_environments[TUTORIAL],
with_info=with_info,
reward_system=reward_systems[TUTORIAL]),
CREDIT_FULL: CreditPayerEnv(userstory_env=userstory_environments[CREDIT_FULL],
backlog_env=backlog_environments[CREDIT_FULL],
with_end=True,
with_info=with_info,
reward_system=reward_systems[CREDIT_FULL]),
CREDIT_START: CreditPayerEnv(userstory_env=userstory_environments[CREDIT_START],
backlog_env=backlog_environments[CREDIT_START],
with_end=False,
with_info=with_info,
reward_system=reward_systems[CREDIT_START]),
CREDIT_END: CreditPayerEnv(userstory_env=userstory_environments[CREDIT_END],
backlog_env=backlog_environments[CREDIT_END],
with_end=True,
with_info=with_info,
reward_system=reward_systems[CREDIT_END]),
END: ProductOwnerEnv(userstory_env=userstory_environments[END],
backlog_env=backlog_environments[END],
with_info=with_info,
reward_system=reward_systems[END]),
}
for stage, env in res.items():
update_reward_system_config(env, env.reward_system)
return res
def show_usual_plots(results):
for name, result in results.items():
plt.plot(result, '.')
plt.xlabel("Trajectory")
plt.ylabel(name)
plt.show()
def eval_wins_and_losses(results):
wins = np.array(results["wins"])
wins_check = (wins == 1)
print(f"wins: {len(wins[wins_check])}")
money = np.array(results["money"])
print(f"losses: {len(money[money < 0])}")
return wins_check
def show_plots_with_wins(results, show_plots=True):
wins_check = eval_wins_and_losses(results)
if not show_plots:
return
trajectories = np.arange(len(wins_check), dtype=np.int32)
for name in ["money", "sprints"]:
plt.plot(trajectories[wins_check], np.array(results[name])[wins_check], '.',
label="win", color="red")
plt.plot(trajectories[~wins_check], np.array(results[name])[~wins_check], '.',
label="other", color="blue")
plt.xlabel("Trajectory")
plt.ylabel(name)
plt.legend()
plt.show()
def load_and_eval_model(paths, repeats=10, with_plots=True, is_silent=False, with_info=True):
agents = load_agents(paths)
environments = get_environments(get_backlog_environments(), get_reward_systems(), with_info)
full_order = [TUTORIAL, CREDIT_FULL, END]
results = eval_model(environments, agents, full_order, repeats, is_silent=is_silent)
print(results[0])
results = results[1]
if with_plots:
show_usual_plots(results)
show_plots_with_wins(results, with_plots)
def main():
tutorial_path = "./DoubleDQN/model_0_TutorialSolverEnv.pt"
for i in range(1, 50):
paths = {
TUTORIAL: tutorial_path,
CREDIT_FULL: f"./DoubleDQN/model_{i}_CreditPayerEnv.pt"
}
print(f"current model: {i}")
load_and_eval_model(paths, repeats=1000, with_plots=False, is_silent=True, with_info=True)
if __name__ == "__main__":
main()