Skip to content

Commit

Permalink
updating obs and reward to fix backboard bounce logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaiyotech committed May 11, 2023
1 parent e3b483b commit b2aa5f4
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Constants_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from pretrained_agents.KBB.kbb import KBB

FRAME_SKIP = 4
TIME_HORIZON = 2 # horizon in seconds
TIME_HORIZON = 3 # horizon in seconds
T_STEP = FRAME_SKIP / 120 # real time per rollout step
ZERO_SUM = False
STEP_SIZE = 500_000
Expand Down
18 changes: 12 additions & 6 deletions CoyoteObs.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from gym import Space
from gym.spaces import Tuple, Box

from rlgym.utils.common_values import BOOST_LOCATIONS, BALL_RADIUS
from rlgym.utils.common_values import BOOST_LOCATIONS, BALL_RADIUS, BACK_WALL_Y

from numba import njit

Expand Down Expand Up @@ -266,12 +266,18 @@ def pre_step(self, state: GameState):

# for double tap
if self.doubletap_indicator:
if state.ball.position[2] < BALL_RADIUS * 2 and 0.55 * self.prev_ball_vel[2] \
< state.ball.linear_velocity[2] > 0.65 * self.prev_ball_vel[2]:
touched = False
for player in state.players:
if player.ball_touched:
touched = True
ball_bounced_ground = self.prev_ball_vel[2] * state.ball.linear_velocity[2] < 0
ball_near_ground = state.ball.position[2] < BALL_RADIUS * 2
if not touched and ball_near_ground and ball_bounced_ground:
self.floor_bounce = True
elif 0.55 * self.prev_ball_vel[1] < state.ball.linear_velocity[1] > 0.65 * \
self.prev_ball_vel[1] and \
abs(state.ball.position[1]) > 4900 and state.ball.position[2] > 500:

ball_bounced_backboard = self.prev_ball_vel[1] * state.ball.linear_velocity[1] < 0
ball_near_wall = abs(state.ball.position[1]) > (BACK_WALL_Y - BALL_RADIUS * 2)
if not touched and ball_near_wall and ball_bounced_backboard:
self.backboard_bounce = True
self.prev_ball_vel = np.array(state.ball.linear_velocity)

Expand Down
8 changes: 4 additions & 4 deletions learner_demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,10 @@
ent_coef=0.01,
)

run_id = "demo_run1.00"
run_id = "demo_run1.02"
wandb.login(key=os.environ["WANDB_KEY"])
logger = wandb.init(dir="./wandb_store",
name="Demo_Run1.00",
name="Demo_Run1.02",
project="Opti",
entity="kaiyotech",
id=run_id,
Expand Down Expand Up @@ -136,11 +136,11 @@
disable_gradient_logging=True,
)

# alg.load("Demo_saves/Opti_1683473991.4737124/Opti_5080/checkpoint.pt")
alg.load("Demo_saves/Opti_1683728019.129012/Opti_2140/checkpoint.pt")

alg.agent.optimizer.param_groups[0]["lr"] = logger.config.actor_lr
alg.agent.optimizer.param_groups[1]["lr"] = logger.config.critic_lr

alg.freeze_policy(50)
# alg.freeze_policy(50)

alg.run(iterations_per_save=logger.config.save_every, save_dir="Demo_saves")
24 changes: 19 additions & 5 deletions rewards.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,13 +286,27 @@ def pre_step(self, state: GameState):
self.orange_touch_timer += 1
self.kickoff_timer += 1
# for double tap
if state.ball.position[2] < BALL_RADIUS * 2 and 0.55 * self.last_state.ball.linear_velocity[2] \
< state.ball.linear_velocity[2] > 0.65 * self.last_state.ball.linear_velocity[2]:
# if state.ball.position[2] < BALL_RADIUS * 2 and 0.55 * self.last_state.ball.linear_velocity[2] \
# < state.ball.linear_velocity[2] > 0.65 * self.last_state.ball.linear_velocity[2]:
# self.floor_bounce = True
# elif 0.55 * self.last_state.ball.linear_velocity[1] < state.ball.linear_velocity[1] > 0.65 * \
# self.last_state.ball.linear_velocity[1] and \
# abs(state.ball.position[1]) > 4900 and state.ball.position[2] > 500:
# self.backboard_bounce = True
touched = False
for player in state.players:
if player.ball_touched:
touched = True
ball_bounced_ground = self.last_state.ball.linear_velocity[2] * state.ball.linear_velocity[2] < 0
ball_near_ground = state.ball.position[2] < BALL_RADIUS * 2
if not touched and ball_near_ground and ball_bounced_ground:
self.floor_bounce = True
elif 0.55 * self.last_state.ball.linear_velocity[1] < state.ball.linear_velocity[1] > 0.65 * \
self.last_state.ball.linear_velocity[1] and \
abs(state.ball.position[1]) > 4900 and state.ball.position[2] > 500:

ball_bounced_backboard = self.last_state.ball.linear_velocity[1] * state.ball.linear_velocity[1] < 0
ball_near_wall = abs(state.ball.position[1]) > (BACK_WALL_Y - BALL_RADIUS * 2)
if not touched and ball_near_wall and ball_bounced_backboard:
self.backboard_bounce = True

# for aerial
# player who last touched is now on the ground, don't allow jumping back up to continue "dribble"
if self.blue_touch_timer < self.orange_touch_timer and state.players[self.blue_toucher].on_ground:
Expand Down

0 comments on commit b2aa5f4

Please sign in to comment.