forked from Impossibum/kaiyo-bot
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrewards.py
More file actions
106 lines (79 loc) · 3.18 KB
/
rewards.py
File metadata and controls
106 lines (79 loc) · 3.18 KB
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
import math
from rlgym.utils.reward_functions import RewardFunction
from rlgym.utils.gamestates import GameState, PlayerData
import numpy as np
SIDE_WALL_X = 4096 # +/-
BACK_WALL_Y = 5120 # +/-
CEILING_Z = 2044
BACK_NET_Y = 6000 # +/-
GOAL_HEIGHT = 642.775
ORANGE_GOAL_CENTER = (0, BACK_WALL_Y, GOAL_HEIGHT / 2)
BLUE_GOAL_CENTER = (0, -BACK_WALL_Y, GOAL_HEIGHT / 2)
ORANGE_GOAL_BACK = (0, BACK_NET_Y, GOAL_HEIGHT / 2)
BLUE_GOAL_BACK = (0, -BACK_NET_Y, GOAL_HEIGHT / 2)
BALL_RADIUS = 92.75
BALL_MAX_SPEED = 6000
CAR_MAX_SPEED = 2300
SUPERSONIC_THRESHOLD = 2200
CAR_MAX_ANG_VEL = 5.5
BLUE_TEAM = 0
ORANGE_TEAM = 1
NUM_ACTIONS = 8
class MillennialKickoffReward(RewardFunction):
def __init__(self):
super().__init__()
def reset(self, initial_state: GameState):
pass
def closest_to_ball(self, player: PlayerData, state: GameState) -> bool:
player_dist = np.linalg.norm(player.car_data.position - state.ball.position)
for p in state.players:
if p.team_num == player.team_num and p.car_id != p.car_id:
dist = np.linalg.norm(p.car_data.position - state.ball.position)
if dist < player_dist:
return False
return True
def get_reward(self, player: PlayerData, state: GameState, previous_action: np.ndarray) -> float:
if state.ball.position[0] == 0 and state.ball.position[1] == 0 and self.closest_to_ball(player, state):
return -1
return 0
class JumpTouchReward(RewardFunction):
def __init__(self, min_height=92.75):
self.min_height = min_height
self.max_height = 2044-92.75
self.range = self.max_height - self.min_height
def reset(self, initial_state: GameState):
pass
def get_reward(
self, player: PlayerData, state: GameState, previous_action: np.ndarray
) -> float:
if player.ball_touched and not player.on_ground and state.ball.position[2] >= self.min_height:
return (state.ball.position[2] - self.min_height) / self.range
return 0
class TouchVelChange(RewardFunction):
def __init__(self):
self.last_vel = np.zeros(3)
def reset(self, initial_state: GameState):
self.last_vel = np.zeros(3)
def get_reward(
self, player: PlayerData, state: GameState, previous_action: np.ndarray
) -> float:
reward = 0
if player.ball_touched:
vel_difference = abs(np.linalg.norm(self.last_vel - state.ball.linear_velocity))
reward = vel_difference / 4600.0
self.last_vel = state.ball.linear_velocity
return reward
class OmniBoostDiscipline(RewardFunction):
def __init__(self):
super().__init__()
self.values = [0 for _ in range(64)]
def reset(self, initial_state: GameState):
self.values = [0 for _ in range(64)]
def get_reward(self, player: PlayerData, state: GameState, previous_action: np.ndarray) -> float:
old, self.values[player.car_id] = self.values[player.car_id], player.boost_amount
if player.car_data.position[2] < (2 * BALL_RADIUS):
return -int(self.values[player.car_id] < old)
else:
return 0
if __name__ == "__main__":
pass