-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_reward.py
363 lines (289 loc) · 11.3 KB
/
custom_reward.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
351
352
353
354
355
356
357
358
359
360
361
362
363
import math
import numpy as np
import gym
def angle_vector(vector_1, vector_2):
unit_vector_1 = vector_1 / np.linalg.norm(vector_1)
unit_vector_2 = vector_2 / np.linalg.norm(vector_2)
dot_product = np.dot(unit_vector_1, unit_vector_2)
angle = np.arccos(dot_product)
return angle
class WhoWonWrapper(gym.core.Wrapper):
"""
A wrapper to add the info of which team won
Team Blue: 0
Team Orange: 0
"""
def __init__(self, env):
super(WhoWonWrapper, self).__init__(env)
self.env = env
def step(self, action):
obs, rewards, done, info = self.env.step(action)
return (
obs,
rewards,
done,
self._preprocess_info(info, rewards, done),
)
def _preprocess_info(self, info, rewards, done):
for agent_id in info:
info[agent_id]['result'] = 'undefined'
if max(done.values()):
if rewards[0] > 0:
info[agent_id]['result'] = 0
elif rewards[0] < 0:
info[agent_id]['result'] = 1
else:
info[agent_id]['result'] = 'draw'
return info
class CustomRewardWrapper(gym.core.Wrapper):
"""
A wrapper for custom rewards
"""
def __init__(self, env):
super(CustomRewardWrapper, self).__init__(env)
self.env = env
self.goals = [[16.5, 0], [-16.5, 0]]
self.step_counter = 0
def step(self, action):
obs, rewards, done, info = self.env.step(action)
self.step_counter += 1
return (
obs,
self._preprocess_reward(rewards, info, done),
done,
info,
)
def _preprocess_reward(self, rewards, info, done):
agent_ids = list(rewards.keys())
new_rewards = {agent_id: reward for agent_id, reward in rewards.items()}
if max(done.values()):
for agent_id, reward in rewards.items():
new_rewards[agent_id] = reward*100#*(1-self.step_counter/5000)
return new_rewards
ball = info[agent_ids[0]]['ball_info']
for team_id in [0, 1]:
reward = 0
opponent_goal = self.goals[team_id]
vector_ball_to_goal = [opponent_goal[0] - ball['position'][0], opponent_goal[1] - ball['position'][1]]
ball_velocity_norm = np.linalg.norm(ball['velocity'])
if ball_velocity_norm > 1e-3 and not np.isnan(ball_velocity_norm):
angle = angle_vector(vector_ball_to_goal, ball['velocity'])
if not np.isnan(angle):
normalized_angle = (math.pi/2 - angle)/(math.pi/2)
reward += normalized_angle*ball_velocity_norm/40
for agent_id in [team_id*2, (team_id*2)+1]:
new_rewards[agent_id] += reward
print(new_rewards)
return new_rewards
class ColisionRewardWrapper(gym.core.Wrapper):
"""
A wrapper for custom rewards
"""
def __init__(self, env):
super(ColisionRewardWrapper, self).__init__(env)
self.env = env
self.goals = [[16.5, 0], [-16.5, 0]]
self.step_counter = 0
self.last_info = None
def step(self, action):
obs, rewards, done, info = self.env.step(action)
ep_data = (
obs,
self._preprocess_reward(rewards, info, done),
done,
info
)
self._gatter_info(info)
return ep_data
def reset(self):
self.step_counter = 0
self.last_info = None
return self.env.reset()
def _gatter_info(self, info):
self.step_counter += 1
self.last_info = info
def _dist_to_the_ball(self, info, ball_position):
pos = np.zeros((4,))
for agent_id in info:
agent_position = info[agent_id]['player_info']['position']
dist = np.linalg.norm(agent_position-ball_position)
pos[agent_id] = dist
return pos
def _preprocess_reward(self, rewards, info, done):
if max(done.values()):
self.step_counter = 0
self.last_info = None
return rewards
if self.last_info is None or not self.last_info:
return rewards
agent_ids = list(rewards.keys())
new_rewards = {agent_id: reward for agent_id, reward in rewards.items()}
last_ball_info = self.last_info[agent_ids[0]]['ball_info']
ball_info = info[agent_ids[0]]['ball_info']
last_ball_speed = np.linalg.norm(last_ball_info['velocity'])
ball_speed = np.linalg.norm(ball_info['velocity'])
last_ball_position = last_ball_info['position']
ball_position = ball_info['position']
dist_traveld_ball_position = np.linalg.norm(ball_position-last_ball_position)
# If ball speed increased
if last_ball_speed < ball_speed and dist_traveld_ball_position > 0.5:
# wich angent now is closest to the ball where it as before?
dist_by_angent = self._dist_to_the_ball(info, last_ball_position)
agent_id = dist_by_angent.argmin()
if agent_id == 0 or agent_id == 1:
opponent_goal = self.goals[0]
else:
opponent_goal = self.goals[1]
vector_ball_to_goal = [opponent_goal[0] - ball_info['position'][0], opponent_goal[1] - ball_info['position'][1]]
if ball_speed > 1e-3 and not np.isnan(ball_speed):
angle = angle_vector(vector_ball_to_goal, ball_info['velocity'])
if not np.isnan(angle):
normalized_angle = (math.pi/2 - angle)/(math.pi/2)
new_rewards[agent_id] += 0.05*normalized_angle
#print(f"Agent {agent_id} kicked the ball for {0.05*normalized_angle}")
return new_rewards
class BallBehindPenaltyWrapper(gym.core.Wrapper):
def __init__(self, env):
super(BallBehindPenaltyWrapper, self).__init__(env)
self.env = env
def step(self, action):
obs, rewards, done, info = self.env.step(action)
return (
obs,
self._preprocess_reward(rewards, info, done),
done,
info,
)
def _preprocess_reward(self, rewards, info, done):
#If game over, do nothing
if max(done.values()):
return rewards
agent_ids = list(rewards.keys())
new_rewards = {agent_id: reward for agent_id, reward in rewards.items()}
ball_position = info[0]['ball_info']['position']
for agent_id in new_rewards:
agent_position = info[agent_id]['player_info']['position']
if agent_id == 0 or agent_id == 1:
if agent_position[0] > ball_position[0]:
new_rewards[agent_id] += -0.005
#print(f"Agent {agent_id} is behind the ball {-0.005}")
else:
if agent_position[0] < ball_position[0]:
new_rewards[agent_id] += -0.005
#print(f"Agent {agent_id} is behind the ball {-0.005}")
return new_rewards
class BallInTeamFieldSidePenaltyWrapper(gym.core.Wrapper):
def __init__(self, env):
super(BallInTeamFieldSidePenaltyWrapper, self).__init__(env)
self.env = env
def step(self, action):
obs, rewards, done, info = self.env.step(action)
return (
obs,
self._preprocess_reward(rewards, info, done),
done,
info,
)
def _preprocess_reward(self, rewards, info, done):
#If game over, do nothing
if max(done.values()):
return rewards
agent_ids = list(rewards.keys())
new_rewards = {agent_id: reward for agent_id, reward in rewards.items()}
ball_position = info[0]['ball_info']['position']
for agent_id in new_rewards:
ball_position = info[agent_id]['ball_info']['position']
if agent_id == 0 or agent_id == 1:
if ball_position[0] < 0:
new_rewards[agent_id] += -0.001
if ball_position[0] < 12:
new_rewards[agent_id] += -0.004
else:
if ball_position[0] > 0:
new_rewards[agent_id] += -0.001
if ball_position[0] > 12:
new_rewards[agent_id] += -0.004
return new_rewards
class FinalRewardMultiplier(gym.core.Wrapper):
def __init__(self, env):
super(FinalRewardMultiplier, self).__init__(env)
self.env = env
self.multiplier = 2
#self.historic =
def step(self, action):
obs, rewards, done, info = self.env.step(action)
return (
obs,
self._preprocess_reward(rewards, done),
done,
info,
)
def _preprocess_reward(self, rewards, done):
#If game over, do nothing
if max(done.values()):
for agent_id, reward in rewards.items():
rewards[agent_id] = reward*self.multiplier
return rewards
class StrikerGoalieWrapper(gym.core.Wrapper):
"""
A wrapper for rewards for the striker and goalie game
agents
0: Blue Striker
1: Blue Goalie
2: Orange Goalie
3: Orange Striker
"""
def __init__(self, env):
super(StrikerGoalieWrapper, self).__init__(env)
self.env = env
def step(self, action):
obs, rewards, done, info = self.env.step(action)
return (
obs,
self._preprocess_reward(info, done),
done,
info,
)
def _preprocess_reward(self, info, done):
#End of Game
if max(done.values()):
result = info[0]['result']
#Win (Team 0 Blue)
if result == 0:
winner = 0
#Lost (Team 0 Blue)
elif result == 1:
winner = 1
#Draw
else:
return {
0: -2,
1: 0,
2: 0,
3: -2,
}
return {
0: 2 if winner == 0 else 0,
1: 0 if winner == 0 else -2,
2: 0 if winner == 1 else -2,
3: 2 if winner == 1 else 0,
}
#Existial Penaly for Strikers
#Existial Bonus for Goalies
default_reward = {
0: -0.001,
1: 0.001,
2: 0.001,
3: -0.001,
}
#Penality if goalies pass the middle camp
default_reward[1] += -0.005 if info[1]['player_info']['position'][0] > 0 else 0
default_reward[2] += -0.005 if info[2]['player_info']['position'][0] < 0 else 0
#Bonus if goalies is close to the goal
default_reward[1] += 0.001 if info[1]['player_info']['position'][0] < -10 else 0
default_reward[2] += 0.001 if info[2]['player_info']['position'][0] > 10 else 0
#Penality if ball is in the goalies camp
#Bonus if ball is in the opposite goalie camp
default_reward[1] += -0.003 if info[1]['ball_info']['position'][0] < 0 else 0.003
default_reward[2] += -0.003 if info[2]['ball_info']['position'][0] > 0 else 0.003
return default_reward