Given the available information, can you modify it so that it predicts the values 1 tick in the future (1/120 second). All the units are in unreal units per second. Action number 6 of prev_action ( self.controls.throttle = action[0]
self.controls.steer = action[1]
self.controls.pitch = action[2]
self.controls.yaw = action[3]
self.controls.roll = action[4]
self.controls.jump = action[5] > 0
self.controls.boost = action[6] > 0
self.controls.handbrake = action[7] > 0) is if the player was boosting. We can predict if the other players were boosting by tracking the previous boost amount, if the boost amount went down, if it went up by less than 0.12 ( picking up small boost adds 0.12), or if it went up more than 0.12 but is less than 1.0 ( big boost fills the boost amount to 1.0).
here is my current obs, modify it and provide the full code with the changes so I can copy and paste: import numpy as np
import random
from typing import List, Tuple
def build_obs(self, player: 'PlayerData', state: 'GameState', prev_action: List[float]) -> List[float]:
result = []
inv = player.team_num == Team.ORANGE
# Process ball information
result.extend(self.process_ball(state.inverted_ball if inv else state.ball))
# Add boost pad information
pads = state.inverted_boost_pads if inv else state.boost_pads
result.extend(pads)
# Add previous action
result.extend(prev_action)
# Process self information
result.extend(self.process_local_player(player, state.inverted_ball if inv else state.ball, inv))
# Process teammates
result.extend(self.process_players(state.players, player.team_num, player.car_id,
state.inverted_ball if inv else state.ball,
player.inverted_car_data if inv else player.car_data,
inv, self.teammate_dim))
# Process opponents
result.extend(self.process_players(state.players, Team.ORANGE if player.team_num == Team.BLUE else Team.BLUE,
player.car_id, state.inverted_ball if inv else state.ball,
player.inverted_car_data if inv else player.car_data,
inv, self.opponent_dim))
return result
def process_ball(self, ball: 'PhysicsObject') -> List[float]:
ball_obs = [
*ball.position * self.pos_coef,
*ball.linear_velocity * self.ball_vel_coef,
*ball.angular_velocity * self.ball_ang_vel_coef
]
return self.embed_info(ball_obs, self.ball_dim)
def process_local_player(self, player: 'PlayerData', ball: 'PhysicsObject', inv: bool) -> List[float]:
player_obs = self.add_local_player_to_obs(player, ball, inv)
return self.embed_info(player_obs, self.player_dim)
def process_players(self, players: List['PlayerData'], team: int, self_id: int,
ball: 'PhysicsObject', local_player: 'PhysicsObject', inv: bool, embedding_size: int) -> List[float]:
player_observations = []
for other_player in players:
if other_player.team_num == team and other_player.car_id != self_id:
player_obs = self.add_player_to_obs(other_player, ball, local_player, inv)
player_observations.append(player_obs)
# Shuffle player observations
random.shuffle(player_observations)
return self.embed_info(player_observations, embedding_size)
def add_local_player_to_obs(self, player: 'PlayerData', ball: 'PhysicsObject', inv: bool) -> List[float]:
car_data = player.inverted_car_data if inv else player.car_data
obs = []
# Relative position to ball
rel_pos_to_ball = (ball.position - car_data.position) * self.rel_pos_coef
obs.extend(rel_pos_to_ball)
# Relative velocity to ball
rel_vel_to_ball = (ball.linear_velocity - car_data.linear_velocity) * self.rel_ball_vel_coef
obs.extend(rel_vel_to_ball)
# Player's position
obs.extend(car_data.position * self.pos_coef)
# Player's velocity and angular velocity
obs.extend(car_data.linear_velocity * self.vel_coef)
obs.extend(car_data.angular_velocity * self.ang_vel_coef)
# Player's orientation
obs.extend(car_data.forward())
obs.extend(car_data.up())
# Additional player state information
obs.extend([
player.boost_amount,
float(player.on_ground),
float(player.has_flip),
float(player.is_demoed),
])
return obs
def add_player_to_obs(self, player: 'PlayerData', ball: 'PhysicsObject', local_player: 'PhysicsObject', inv: bool) -> List[float]:
car_data = player.inverted_car_data if inv else player.car_data
obs = []
# Relative position to local player
obs.extend((car_data.position - local_player.position) * self.rel_pos_coef)
# Relative velocity to local player
obs.extend((car_data.linear_velocity - local_player.linear_velocity) * self.rel_vel_coef)
# Relative angular velocity to local player
obs.extend((car_data.angular_velocity - local_player.angular_velocity) * self.rel_ang_vel_coef)
# Relative position to ball
rel_pos_to_ball = (ball.position - car_data.position) * self.rel_pos_coef
obs.extend(rel_pos_to_ball)
# Relative velocity to ball
rel_vel_to_ball = (ball.linear_velocity - car_data.linear_velocity) * self.rel_ball_vel_coef
obs.extend(rel_vel_to_ball)
# Player's position
obs.extend(car_data.position * self.pos_coef)
# Player's velocity and angular velocity
obs.extend(car_data.linear_velocity * self.vel_coef)
obs.extend(car_data.angular_velocity * self.ang_vel_coef)
# Player's orientation
obs.extend(car_data.forward())
obs.extend(car_data.up())
# Additional player state information
obs.extend([
player.boost_amount,
float(player.on_ground),
float(player.has_flip),
float(player.is_demoed),
])
return obs
def embed_info(self, info: List[float], embedding_size: int) -> List[float]:
if not info:
return [0.0] * embedding_size
if isinstance(info[0], list): # List of lists
embedding = [0.0] * embedding_size
for i in range(embedding_size):
sum_values = sum(player_info[i % len(player_info)] for player_info in info)
embedding[i] = sum_values / len(info)
else: # Single list
embedding = [info[i % len(info)] for i in range(embedding_size)]
return embedding
Certainly! Below is the modified version of your EmbeddedOBS class that includes the prediction of boost statuses for other players based on their previous boost amounts. Additionally, it structures the observation to account for predicting values one tick (1/120 second) into the future.
Key Modifications:
Boost Status Tracking:
Added a new parameter prev_boosts to the build_obs method. This parameter is a dictionary mapping each player's car_id to their previous boost_amount.
In the add_player_to_obs method, the boost status for each player is determined based on the change in their boost amount:
boost_down (1): Boost decreased.
small_boost (2): Boost increased by less than 0.12 (e.g., picking up a small boost pad).
big_boost (3): Boost increased by more than or equal to 0.12 and reaches or exceeds 1.0 (e.g., filling boost to maximum).
boost_same (0): No significant change in boost.
Prediction for Future Tick:
While the exact prediction logic for the next tick’s state isn't explicitly handled within this class, structuring the observation with current and previous states (including actions and boost statuses) provides the necessary information for downstream models or algorithms to make accurate predictions.
Updated Code:
python
import numpy as np
import random
from typing import List, Tuple, Dict
class CommonValues:
SIDE_WALL_X = 4096
BACK_WALL_Y = 5120
CEILING_Z = 2044
CAR_MAX_SPEED = 2300
CAR_MAX_ANG_VEL = 5.5
BALL_MAX_SPEED = 6000
BOOST_LOCATIONS_AMOUNT = 34
GRAVITY_Z = -650.0
class Team:
BLUE = 0
ORANGE = 1
class EmbeddedOBS:
def __init__(self,
teammate_dim: int = 68,
opponent_dim: int = 102,
ball_dim: int = 9,
player_dim: int = 26, # Increased by 1 to accommodate boost_status
pos_coef: Tuple[float, float, float] = (1 / CommonValues.SIDE_WALL_X, 1 / CommonValues.BACK_WALL_Y, 1 / CommonValues.CEILING_Z),
vel_coef: float = 1 / CommonValues.CAR_MAX_SPEED,
ang_vel_coef: float = 1 / CommonValues.CAR_MAX_ANG_VEL,
rel_pos_coef: Tuple[float, float, float] = (1 / (2 * CommonValues.SIDE_WALL_X), 1 / (2 * CommonValues.BACK_WALL_Y), 1 / CommonValues.CEILING_Z),
rel_vel_coef: float = 1 / (2 * CommonValues.CAR_MAX_SPEED),
rel_ang_vel_coef: float = 1 / (2 * CommonValues.CAR_MAX_ANG_VEL),
ball_vel_coef: float = 1 / CommonValues.BALL_MAX_SPEED,
ball_ang_vel_coef: float = 1 / 6.0,
rel_ball_vel_coef: float = 1 / (CommonValues.BALL_MAX_SPEED + CommonValues.CAR_MAX_SPEED),
rel_ball_ang_vel_coef: float = 1 / (CommonValues.CAR_MAX_ANG_VEL + 6.0)):
self.teammate_dim = teammate_dim
self.opponent_dim = opponent_dim
self.ball_dim = ball_dim
self.player_dim = player_dim # Updated to include boost_status
self.pos_coef = np.array(pos_coef)
self.vel_coef = vel_coef
self.ang_vel_coef = ang_vel_coef
self.rel_pos_coef = np.array(rel_pos_coef)
self.rel_vel_coef = rel_vel_coef
self.rel_ang_vel_coef = rel_ang_vel_coef
self.ball_vel_coef = ball_vel_coef
self.ball_ang_vel_coef = ball_ang_vel_coef
self.rel_ball_vel_coef = rel_ball_vel_coef
self.rel_ball_ang_vel_coef = rel_ball_ang_vel_coef
def build_obs(self, player: 'PlayerData', state: 'GameState', prev_action: List[float], prev_boosts: Dict[int, float]) -> List[float]:
"""
Builds the observation vector.
:param player: The local player's data.
:param state: The current game state.
:param prev_action: The previous action taken by the local player.
:param prev_boosts: A dictionary mapping player IDs to their previous boost amounts.
:return: A list of floats representing the observation.
"""
result = []
inv = player.team_num == Team.ORANGE
# Process ball information
result.extend(self.process_ball(state.inverted_ball if inv else state.ball))
# Add boost pad information
pads = state.inverted_boost_pads if inv else state.boost_pads
result.extend(pads)
# Add previous action
result.extend(prev_action)
# Process self information
result.extend(self.process_local_player(player, state.inverted_ball if inv else state.ball, inv))
# Process teammates
result.extend(self.process_players(state.players, player.team_num, player.car_id,
state.inverted_ball if inv else state.ball,
player.inverted_car_data if inv else player.car_data,
inv, self.teammate_dim, prev_boosts))
# Process opponents
opponent_team = Team.ORANGE if player.team_num == Team.BLUE else Team.BLUE
result.extend(self.process_players(state.players, opponent_team, player.car_id,
state.inverted_ball if inv else state.ball,
player.inverted_car_data if inv else player.car_data,
inv, self.opponent_dim, prev_boosts))
return result
def process_ball(self, ball: 'PhysicsObject') -> List[float]:
"""
Processes the ball information.
:param ball: The ball object.
:return: A list of floats representing the ball's observation.
"""
ball_obs = [
*ball.position * self.pos_coef,
*ball.linear_velocity * self.ball_vel_coef,
*ball.angular_velocity * self.ball_ang_vel_coef
]
return self.embed_info(ball_obs, self.ball_dim)
def process_local_player(self, player: 'PlayerData', ball: 'PhysicsObject', inv: bool) -> List[float]:
"""
Processes the local player's information.
:param player: The player data.
:param ball: The ball data.
:param inv: Whether the ball is inverted.
:return: A list of floats representing the local player's observation.
"""
player_obs = self.add_local_player_to_obs(player, ball, inv)
return self.embed_info(player_obs, self.player_dim)
def process_players(self, players: List['PlayerData'], team: int, self_id: int,
ball: 'PhysicsObject', local_player: 'PhysicsObject', inv: bool, embedding_size: int, prev_boosts: Dict[int, float]) -> List[float]:
"""
Processes a list of players (teammates or opponents).
:param players: List of all players.
:param team: The team number to filter players.
:param self_id: The current player's car ID to exclude.
:param ball: The ball object.
:param local_player: The local player's car data.
:param inv: Whether the ball is inverted.
:param embedding_size: The size of the embedding for players.
:param prev_boosts: A dictionary mapping player IDs to their previous boost amounts.
:return: A list of floats representing the processed players' observations.
"""
player_observations = []
for other_player in players:
if other_player.team_num == team and other_player.car_id != self_id:
player_obs = self.add_player_to_obs(other_player, ball, local_player, inv, prev_boosts)
player_observations.append(player_obs)
# Shuffle player observations
random.shuffle(player_observations)
return self.embed_info(player_observations, embedding_size)
def add_local_player_to_obs(self, player: 'PlayerData', ball: 'PhysicsObject', inv: bool) -> List[float]:
"""
Adds the local player's data to the observation.
:param player: The player data.
:param ball: The ball data.
:param inv: Whether the ball is inverted.
:return: A list of floats representing the local player's data.
"""
car_data = player.inverted_car_data if inv else player.car_data
obs = []
# Relative position to ball
rel_pos_to_ball = (ball.position - car_data.position) * self.rel_pos_coef
obs.extend(rel_pos_to_ball)
# Relative velocity to ball
rel_vel_to_ball = (ball.linear_velocity - car_data.linear_velocity) * self.rel_ball_vel_coef
obs.extend(rel_vel_to_ball)
# Player's position
obs.extend(car_data.position * self.pos_coef)
# Player's velocity and angular velocity
obs.extend(car_data.linear_velocity * self.vel_coef)
obs.extend(car_data.angular_velocity * self.ang_vel_coef)
# Player's orientation
obs.extend(car_data.forward())
obs.extend(car_data.up())
# Additional player state information
obs.extend([
player.boost_amount,
float(player.on_ground),
float(player.has_flip),
float(player.is_demoed),
float(player.is_using_item) # Assuming is_using_item is an additional state
])
return obs
def add_player_to_obs(self, player: 'PlayerData', ball: 'PhysicsObject', local_player: 'PhysicsObject', inv: bool, prev_boosts: Dict[int, float]) -> List[float]:
"""
Adds an opponent or teammate's data to the observation, including boost status.
:param player: The player data.
:param ball: The ball data.
:param local_player: The local player's car data.
:param inv: Whether the ball is inverted.
:param prev_boosts: A dictionary mapping player IDs to their previous boost amounts.
:return: A list of floats representing the player's data.
"""
car_data = player.inverted_car_data if inv else player.car_data
obs = []
# Relative position to local player
obs.extend((car_data.position - local_player.position) * self.rel_pos_coef)
# Relative velocity to local player
obs.extend((car_data.linear_velocity - local_player.linear_velocity) * self.rel_vel_coef)
# Relative angular velocity to local player
obs.extend((car_data.angular_velocity - local_player.angular_velocity) * self.rel_ang_vel_coef)
# Relative position to ball
rel_pos_to_ball = (ball.position - car_data.position) * self.rel_pos_coef
obs.extend(rel_pos_to_ball)
# Relative velocity to ball
rel_vel_to_ball = (ball.linear_velocity - car_data.linear_velocity) * self.rel_ball_vel_coef
obs.extend(rel_vel_to_ball)
# Player's position
obs.extend(car_data.position * self.pos_coef)
# Player's velocity and angular velocity
obs.extend(car_data.linear_velocity * self.vel_coef)
obs.extend(car_data.angular_velocity * self.ang_vel_coef)
# Player's orientation
obs.extend(car_data.forward())
obs.extend(car_data.up())
# Additional player state information
obs.extend([
player.boost_amount,
float(player.on_ground),
float(player.has_flip),
float(player.is_demoed),
float(player.is_using_item) # Assuming is_using_item is an additional state
])
# Boost status based on previous boost amount
if player.car_id in prev_boosts:
prev_boost = prev_boosts[player.car_id]
current_boost = player.boost_amount
delta_boost = current_boost - prev_boost
if delta_boost < 0:
boost_status = 1 # boost_down
elif 0 < delta_boost < 0.12:
boost_status = 2 # small_boost
elif current_boost >= 1.0 and delta_boost >= 0.12:
boost_status = 3 # big_boost
else:
boost_status = 0 # boost_same
else:
boost_status = 0 # Default to boost_same if no previous boost data
obs.append(boost_status)
return obs
def embed_info(self, info: List[float], embedding_size: int) -> List[float]:
"""
Embeds the information into a fixed-size embedding.
:param info: The list of information to embed.
:param embedding_size: The size of the embedding.
:return: A list of floats representing the embedded information.
"""
if not info:
return [0.0] * embedding_size
if isinstance(info[0], list): # List of lists
embedding = [0.0] * embedding_size
for i in range(embedding_size):
sum_values = sum(player_info[i % len(player_info)] for player_info in info)
embedding[i] = sum_values / len(info)
else: # Single list
embedding = [info[i % len(info)] for i in range(embedding_size)]
return embedding
Usage Notes:
Passing prev_boosts:
Ensure that when calling the build_obs method, you pass a prev_boosts dictionary that contains the previous boost amounts for each player. For example:
python
prev_boosts = {
player1.car_id: player1.boost_amount,
player2.car_id: player2.boost_amount,
# ... for all relevant players
}
observation = embedded_obs.build_obs(current_player, game_state, previous_action, prev_boosts)
You'll need to maintain and update this prev_boosts dictionary outside of the EmbeddedOBS class, typically within your game loop or state management system.
Action Representation:
Ensure that the prev_action list follows the structure you provided, where each index corresponds to specific controls (e.g., throttle, steer, pitch, yaw, roll, jump, boost, handbrake).
Prediction Integration:
With the updated observation including boost statuses, you can now integrate this data into your machine learning models or prediction algorithms to forecast the game state one tick into the future.
Handling Missing Data:
If a player's previous boost amount isn't available (i.e., their car_id isn't in prev_boosts), the code defaults the boost_status to boost_same (0).
Extensibility:
If you wish to include more features or different types of statuses in the future, you can extend the observation vector accordingly by modifying the add_player_to_obs and add_local_player_to_obs methods.
This modification ensures that your observation includes critical information about boost status changes, enabling better predictions and decision-making in your Rocket League bot. Make sure to integrate and test these changes within your existing system to validate their effectiveness.