-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathworker_gp.py
315 lines (287 loc) · 13.6 KB
/
worker_gp.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
import sys
from redis import Redis
from redis.retry import Retry # noqa
from redis.backoff import ExponentialBackoff # noqa
from redis.exceptions import ConnectionError, TimeoutError
from CoyoteObs import CoyoteObsBuilder
from rocket_learn.rollout_generator.redis.redis_rollout_worker import RedisRolloutWorker
from rocket_learn.matchmaker.matchmaker import Matchmaker
from rocket_learn.agent.types import PretrainedAgent
from rocket_learn.utils.truncated_condition import TerminalToTruncatedWrapper
from mybots_terminals import RandomTruncationBallGround
from CoyoteParser import CoyoteAction
from rewards import ZeroSumReward
from torch import set_num_threads
from setter import CoyoteSetter
from mybots_statesets import EndKickoff
import Constants_gp
import os
from random_eval_matchmaker import RandomEvalMatchmaker
from pretrained_agents.necto.necto_v1 import NectoV1
from pretrained_agents.nexto.nexto_v2 import NextoV2
from pretrained_agents.KBB.kbb import KBB
from rlgym.utils.terminal_conditions.common_conditions import GoalScoredCondition, TimeoutCondition, \
NoTouchTimeoutCondition
set_num_threads(1)
if __name__ == "__main__":
#actual GP rewards
rew = ZeroSumReward(zero_sum=Constants_gp.ZERO_SUM,
goal_w=10,
concede_w=-10,
# double_tap_w=5,
velocity_bg_w=0.075 / 2, # fix for the tick skip change
velocity_pb_w=0,
boost_gain_w=0,
punish_boost=True,
use_boost_punish_formula=False,
boost_spend_w=-0.45,
boost_gain_small_w=0,
punish_low_boost_w=-0.02,
demo_w=8,
acel_ball_w=2,
team_spirit=1,
# cons_air_touches_w=2,
jump_touch_w=4,
wall_touch_w=15,
touch_grass_w=0,
punish_bad_spacing_w=0,
handbrake_ctrl_w=0, #-0.006,
tick_skip=Constants_gp.FRAME_SKIP,
flatten_wall_height=True,
slow_w=-0.1,
turtle_w=-4,
punish_dist_goal_score_w=-10,
# touch_wall_w = 0.05,
energy_reward_w=0.015,
)
#demo rewards
# rew = ZeroSumReward(zero_sum=Constants_gp.ZERO_SUM,
# goal_w=10,
# concede_w=-2,
# # double_tap_w=5,
# velocity_bg_w=0.075 / 2, # fix for the tick skip change
# velocity_pb_w=0,
# boost_gain_w=0,
# punish_boost=True,
# use_boost_punish_formula=False,
# boost_spend_w=-0.3,
# boost_gain_small_w=0,
# punish_low_boost_w=0,
# demo_w=20,
# acel_ball_w=2,
# team_spirit=1,
# # cons_air_touches_w=2,
# jump_touch_w=4,
# wall_touch_w=15,
# touch_grass_w=0,
# punish_bad_spacing_w=0,
# handbrake_ctrl_w=0, #-0.006,
# tick_skip=Constants_gp.FRAME_SKIP,
# flatten_wall_height=True,
# slow_w=0,
# turtle_w=0,
# punish_dist_goal_score_w=0,
# # touch_wall_w = 0.05,
# energy_reward_w=0.03,
# use_concede_even_zero_sum=True,
# )
# wall reward
# rew = ZeroSumReward(zero_sum=Constants_gp.ZERO_SUM,
# goal_w=10,
# concede_w=-10,
# # double_tap_w=5,
# velocity_bg_w=0.15, # fix for the tick skip change
# velocity_pb_w=0,
# boost_gain_w=0,
# punish_boost=True,
# use_boost_punish_formula=False,
# boost_spend_w=-0.5,
# boost_gain_small_w=0,
# punish_low_boost_w=0,
# demo_w=5,
# acel_ball_w=2,
# team_spirit=1,
# # cons_air_touches_w=2,
# jump_touch_w=1,
# wall_touch_w=15,
# touch_grass_w=0,
# punish_bad_spacing_w=0,
# handbrake_ctrl_w=-0.004, #-0.006,
# tick_skip=Constants_gp.FRAME_SKIP,
# flatten_wall_height=True,
# pun_rew_ball_height_w=0.02,
# dribble_w=-1,
# aerial_goal_w=10,
# slow_w=0,
# turtle_w=0,
# punish_dist_goal_score_w=0,
# # touch_wall_w = 0.05,
# energy_reward_w=0.03,
# use_concede_even_zero_sum=False,
# )
frame_skip = Constants_gp.FRAME_SKIP
fps = 120 // frame_skip
name = "Default"
send_gamestate = False
streamer_mode = False
local = True
auto_minimize = True
game_speed = 100
evaluation_prob = 0
past_version_prob = 0 # 0.5 # 0.1
non_latest_version_prob = [0.85, 0.1, 0.05, 0.0] # this includes past_version and pretrained
deterministic_streamer = True
force_old_deterministic = True
gamemode_weights = {'1v1': 0.4, '2v2': 0.6, '3v3': 0}
visualize = False
simulator = False
batch_mode = True
team_size = 3
dynamic_game = True
infinite_boost_odds = 0
setter = CoyoteSetter(mode="normal", simulator=False)
host = "127.0.0.1"
epic_rl_exe_path = None # "D:/Program Files/Epic Games/rocketleague_old/Binaries/Win64/RocketLeague.exe"
model_name = "necto-model-30Y.pt"
nectov1 = NectoV1(model_string=model_name, n_players=6)
model_name = "nexto-model.pt"
nexto = NextoV2(model_string=model_name, n_players=6)
model_name = "kbb.pt"
kbb = KBB(model_string=model_name)
pretrained_agents = Constants_gp.pretrained_agents
# matchmaker = Matchmaker(sigma_target=0.5, pretrained_agents=pretrained_agents, past_version_prob=past_version_prob,
# full_team_trainings=0.8, full_team_evaluations=1, force_non_latest_orange=False,
# non_latest_version_prob=non_latest_version_prob)
matchmaker = RandomEvalMatchmaker(sigma_target=0.5, past_version_prob=0, full_team_trainings=1, full_team_evaluations=1,
force_non_latest_orange=False, non_latest_version_prob=non_latest_version_prob,
min_to_test=400)
# terminals = [GoalScoredCondition(),
# TerminalToTruncatedWrapper(
# RandomTruncationBallGround(avg_frames_per_mode=[fps * 10, fps * 20, fps * 30],
# avg_frames=None,
# min_frames=fps * 10)),
# ]
terminals = [GoalScoredCondition(), TerminalToTruncatedWrapper(TimeoutCondition(fps * 300)),
TerminalToTruncatedWrapper(NoTouchTimeoutCondition(fps * 30))]
if len(sys.argv) > 1:
host = sys.argv[1]
if host != "127.0.0.1" and host != "localhost":
local = False
batch_mode = True
epic_rl_exe_path = None
if len(sys.argv) > 2:
name = sys.argv[2]
# if len(sys.argv) > 3 and not dynamic_game:
# team_size = int(sys.argv[3])
if len(sys.argv) > 3:
if sys.argv[3] == 'GAMESTATE':
send_gamestate = True
elif sys.argv[3] == 'STREAMER':
streamer_mode = True
evaluation_prob = 0
game_speed = 1
auto_minimize = False
infinite_boost_odds = 0
simulator = False
past_version_prob = 0
pretrained_agents = {
nexto: {'prob': 1, 'eval': True, 'p_deterministic_training': 1., 'key': "Nexto"},
kbb: {'prob': 0, 'eval': True, 'p_deterministic_training': 1., 'key': "KBB"}
}
non_latest_version_prob = [1, 0, 0, 0]
matchmaker = Matchmaker(sigma_target=1, pretrained_agents=pretrained_agents,
past_version_prob=past_version_prob,
full_team_trainings=1, full_team_evaluations=1,
force_non_latest_orange=streamer_mode,
non_latest_version_prob=non_latest_version_prob,
showmatch=True,
orange_agent_text_file='orange_stream_file.txt'
)
gamemode_weights = {'1v1': 0.3, '2v2': 0.4, '3v3': 0.3}
terminals = [GoalScoredCondition(), NoTouchTimeoutCondition(fps * 30), TimeoutCondition(fps * 300)]
# setter = EndKickoff()
elif sys.argv[3] == 'VISUALIZE':
visualize = True
terminals = [GoalScoredCondition(), NoTouchTimeoutCondition(fps * 30), TimeoutCondition(fps * 10)]
if simulator:
from rlgym_sim.envs import Match as Sim_Match
else:
from rlgym.envs import Match
match = Match(
game_speed=game_speed,
spawn_opponents=True,
team_size=team_size,
state_setter=setter,
obs_builder=CoyoteObsBuilder(expanding=True, tick_skip=Constants_gp.FRAME_SKIP, team_size=team_size,
extra_boost_info=True, embed_players=True,
infinite_boost_odds=infinite_boost_odds),
action_parser=CoyoteAction(),
terminal_conditions=terminals,
reward_function=rew,
tick_skip=frame_skip,
) if not simulator else Sim_Match(
spawn_opponents=True,
team_size=team_size,
state_setter=setter,
obs_builder=CoyoteObsBuilder(expanding=True, tick_skip=Constants_gp.FRAME_SKIP, team_size=team_size,
extra_boost_info=True, embed_players=True,
infinite_boost_odds=infinite_boost_odds),
action_parser=CoyoteAction(),
terminal_conditions=terminals,
reward_function=rew,
)
# local Redis
if local:
r = Redis(host=host,
username="user1",
password=os.environ["redis_user1_key"],
db=Constants_gp.DB_NUM,
)
# remote Redis
else:
# noinspection PyArgumentList
r = Redis(host=host,
username="user1",
password=os.environ["redis_user1_key"],
retry_on_error=[ConnectionError, TimeoutError],
retry=Retry(ExponentialBackoff(cap=10, base=1), 25),
db=Constants_gp.DB_NUM,
)
# pretrained_agents = {nectov1: 0, nexto: 0.05, kbb: 0.05}
# pretrained_agents = {nexto: PretrainedAgent(prob=0.5, eval=True, p_deterministic_training=1., key="Nexto"),
# kbb: PretrainedAgent(prob=0.5, eval=True, p_deterministic_training=1., key="KBB")}
# pretrained_agents = None
worker = RedisRolloutWorker(r, name, match,
matchmaker=matchmaker,
sigma_target=1,
evaluation_prob=evaluation_prob,
force_paging=False,
dynamic_gm=dynamic_game,
send_obs=True,
auto_minimize=auto_minimize,
send_gamestates=send_gamestate,
gamemode_weights=gamemode_weights, # default 1/3
streamer_mode=streamer_mode,
deterministic_streamer=deterministic_streamer,
force_old_deterministic=force_old_deterministic,
# testing
batch_mode=batch_mode,
step_size=Constants_gp.STEP_SIZE,
pretrained_agents=pretrained_agents,
eval_setter=EndKickoff(),
# full_team_evaluations=True,
epic_rl_exe_path=epic_rl_exe_path,
simulator=simulator,
visualize=visualize,
live_progress=False,
tick_skip=Constants_gp.FRAME_SKIP,
random_boost_states_on_reset=True,
)
worker.env._match._obs_builder.env = worker.env # noqa
if simulator and visualize:
from rocketsimvisualizer import VisualizerThread
arena = worker.env._game.arena # noqa
v = VisualizerThread(arena, fps=60, tick_rate=120, tick_skip=frame_skip, step_arena=False, # noqa
overwrite_controls=False) # noqa
v.start()
worker.run()