Certainly! To incorporate touch height logic into your TouchVelChange reward class, you'll want to factor in the height (z position) at which the player touches the ball. This can be achieved by calculating an additional reward component based on the touch height and then combining it with the existing velocity change reward.
Below is the updated TouchVelChange class with the touch height logic integrated:
#include <algorithm>
#include <cmath>
#include <iostream>
// Assuming necessary includes and namespaces are already present
class TouchVelChange : public RewardFunction {
public:
Vec last_vel;
Vec curr_vel;
static constexpr float MAX_EXTRAP_Z = CommonValues::GOAL_HEIGHT + 230.f;
static constexpr float MIN_SCORE_TIME = 0.9f;
static constexpr float MIN_TOUCH_VEL_CHANGE = 600.0f;
// Constants for touch height reward
static constexpr float TOUCH_HEIGHT_EXP = 2.0f; // Example exponent
static constexpr float TOUCH_HEIGHT_DIV = 10000.0f; // Example divisor
TouchVelChange()
: last_vel(Vec(0, 0, 0)), curr_vel(Vec(0, 0, 0)) {}
virtual void Reset(const GameState& initialState) override {
last_vel = initialState.ball.vel;
curr_vel = initialState.ball.vel;
}
virtual void PreStep(const GameState& state) override {
last_vel = curr_vel;
curr_vel = state.ball.vel;
}
virtual float GetReward(const PlayerData& player, const GameState& state, const Action& prevAction) override {
float reward = 0.0f;
// Velocity Change Reward
if (player.ballTouchedStep) {
float vel_difference = (last_vel - state.ball.vel).Length();
if (vel_difference >= MIN_TOUCH_VEL_CHANGE) {
reward = vel_difference / 4600.0f;
} else {
reward = 0.0f;
}
RS_LOG(vel_difference);
std::cout << "Velocity Difference: " << vel_difference << std::endl;
}
// Extrapolated Position Check
float goalY = RLConst::SOCCAR_GOAL_SCORE_BASE_THRESHOLD_Y * (player.team == Team::BLUE ? 1 : -1);
float distToGoalY = std::abs(state.ball.pos.y - goalY);
float extrapScoreTime = distToGoalY / std::max(std::abs(state.ball.vel.y), 0.01f);
Vec extrapScorePos = state.ball.pos + state.ball.vel * extrapScoreTime;
extrapScorePos.z += CommonValues::GRAVITY_Z * (extrapScoreTime * extrapScoreTime) / 2.0f;
if (extrapScoreTime <= MIN_SCORE_TIME || extrapScorePos.z >= MAX_EXTRAP_Z) {
reward = 0.0f; // Disable reward if the ball is about to score or go too high
}
// Touch Height Reward
if (player.ballTouchedStep) {
float touchHeight = state.ball.pos.z;
// Calculate the adjusted height, ensuring it's within valid bounds
float adjustedHeight = std::min(touchHeight, CommonValues::CEILING_Z / 2.0f) - CommonValues::BALL_RADIUS;
// Ensure adjustedHeight is non-negative to avoid invalid power operations
adjustedHeight = std::max(adjustedHeight, 0.0f);
// Calculate height-based reward
float height_reward = std::pow(adjustedHeight, TOUCH_HEIGHT_EXP) / TOUCH_HEIGHT_DIV;
// Optionally, you can scale the height_reward to match the magnitude of velocity reward
// For example:
// height_reward *= 100.0f;
reward += height_reward;
std::cout << "Touch Height Reward: " << height_reward << std::endl;
}
return reward;
}
};
Explanation of the Added Touch Height Logic
-
Constants for Touch Height Reward:
TOUCH_HEIGHT_EXP: Determines the exponent applied to the adjusted height. Adjusting this can make the reward increase more rapidly with height.
TOUCH_HEIGHT_DIV: A divisor to scale down the height reward so it aligns with the magnitude of the existing velocity-based reward. You can tweak this value based on experimental results to balance the two reward components effectively.
-
Calculating Adjusted Height:
- The height at which the ball was touched is obtained from
state.ball.pos.z.
- To ensure the height stays within reasonable bounds, it's capped at half of the ceiling (
CommonValues::CEILING_Z / 2.0f) and reduced by the ball's radius to avoid rewards when the ball is too low.
- The
std::max function ensures that the adjusted height doesn't go negative, which would result in invalid computations when raising to a power.
-
Calculating the Height-Based Reward:
- The adjusted height is raised to the power defined by
TOUCH_HEIGHT_EXP.
- The result is then divided by
TOUCH_HEIGHT_DIV to scale the reward appropriately.
- This height-based reward is then added to the existing velocity-based reward.
-
Optional Scaling:
- Depending on how significant you want the height reward to be relative to the velocity change reward, you might need to scale it further. Uncomment and adjust the scaling line (
height_reward *= 100.0f;) as necessary based on your observations during training or simulations.
-
Logging:
- Added a
std::cout statement to output the computed height reward for debugging purposes. You can remove or modify this based on your logging preferences.
Tips for Tuning
-
Balancing Rewards: It's essential to ensure that the velocity change and height-based rewards are balanced so that neither dominates the other unless intentionally desired.
-
Hyperparameter Tuning: Experiment with different values for TOUCH_HEIGHT_EXP and TOUCH_HEIGHT_DIV to find the combination that best encourages the desired behavior in your agent.
-
Normalization: Depending on the range of state.ball.pos.z, you might need to normalize or adjust the height reward calculation to ensure stable training.
This enhanced reward function now takes into account both the change in the ball's velocity upon touch and the height at which the touch occurs, providing a more nuanced reward signal for training your agent.