It looks like you're encountering an issue where the arena pointer is being used before it has been properly initialized. This can lead to undefined behavior, crashes, or incorrect reward calculations in your reinforcement learning environment. Let's walk through the problem and provide a solution to ensure that arena is correctly initialized before it's used in your reward functions.
Problem Overview
In your EnvCreateFunc(), you have the following code:
Arena* arena;
// ...
auto rewards = new LogCombinedReward(
std::vector<std::pair<RewardFunction*, float>>{
{ new sBP1(arena), 1.0f },
// Add other reward functions as needed
});
Here, arena is declared as a pointer but never initialized before being passed to the sBP1 reward function. This means arena contains a garbage value or nullptr, leading to undefined behavior when sBP1 tries to access it.
Solution
To fix this, you need to ensure that arena is properly instantiated before it's used in any reward functions. Typically, the Arena object should be created prior to creating the Match and Gym objects, and then passed to both the Match and the reward functions.
Here’s how you can modify your EnvCreateFunc() to properly initialize arena:
Step-by-Step Fix
-
Instantiate Arena Before Using It:
Initialize the Arena object before creating the reward functions. Ensure you pass any necessary parameters required by the Arena constructor.
-
Pass the Initialized Arena to Both Match and Reward Functions:
Once arena is initialized, pass it to both the Match constructor and your reward functions.
-
Update Reward Functions to Use the Initialized Arena:
With arena properly initialized, your reward functions like sBP1 can safely utilize it.
Updated EnvCreateFunc()
Here's the revised EnvCreateFunc() with proper arena initialization:
EnvCreateResult EnvCreateFunc() {
constexpr int TICK_SKIP = 12;
constexpr float NO_TOUCH_TIMEOUT_SECS = 8.f;
int team_size = 1;
// static int counter = 0;
// int team_size = (rand() % 100 < 50) ? 1 : ((rand() % 100 < 90) ? 2 : 3);
std::cout << "Team Size: " << team_size << std::endl;
// 1. Initialize Arena properly
// Replace the following line with the actual constructor and parameters for Arena
Arena* arena = new Arena(/* constructor parameters if any */);
// 2. Define PZSUM as a local variable
const auto PZSUM = [](RewardFunction* rewardFunc, float teamSpirit = 1.0f, float opponentScale = 1.0f) {
return new ZeroSumReward(rewardFunc, teamSpirit, opponentScale);
};
// 3. Now that the Arena is created, you can safely pass it to your reward functions
auto rewards = new LogCombinedReward(
std::vector<std::pair<RewardFunction*, float>>{
{ new sBP1(arena), 1.0f },
// Add other reward functions as needed
}
);
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [&] {
for (int i = 0; i < rewards->rewardFuncs.size(); i++)
g_RewardNames.push_back(rewards->GetRewardFuncName(i));
});
}
std::vector<TerminalCondition*> terminalConditions = {
new NoTouchCondition(NO_TOUCH_TIMEOUT_SECS * 120 / TICK_SKIP),
new GoalScoreCondition()
// new BallTouchGroundCondition()
};
auto obs = new DefaultOBS();
auto actionParser = new DiscreteAction();
auto stateSetter = new WeightedSampleSetter(
{
{new RandomState(true, true, false), 0.9f},
{new KickoffState(), 0.1f},
{new AirDribbleSetup(), 0.0f},
{new ReplaySetter(team_size), 0.0f},
}
);
// 4. Create the Match, passing the initialized Arena
Match* match = new Match(
rewards,
terminalConditions,
obs,
actionParser,
stateSetter,
team_size, // Team size
true // Spawn opponents
);
// If Match needs to set the arena, ensure it's done here
// For example:
// match->SetArena(arena); // Uncomment if such a method exists
// 5. Create Gym with the Match (which now has the Arena)
Gym* gym = new Gym(match, TICK_SKIP);
return { match, gym };
}
Important Considerations
-
Arena Constructor Parameters:
Ensure you provide the necessary parameters required by the Arena constructor. If Arena requires specific configurations (like physical dimensions, properties, etc.), supply them accordingly.
-
Memory Management:
Consider using smart pointers (e.g., std::unique_ptr or std::shared_ptr) instead of raw pointers to manage the lifetime of dynamically allocated objects like Arena, Match, and reward functions. This helps prevent memory leaks.
-
Thread Safety:
If your environment is multi-threaded, ensure that the initialization of shared resources like Arena is thread-safe. The use of std::once_flag and std::call_once in your original code is a good practice for thread-safe initialization.
-
Destructor Cleanup:
Make sure to properly delete or manage the destruction of dynamically allocated objects to avoid memory leaks when your program terminates or when objects are no longer needed.
Example with Smart Pointers
Here's an example using std::shared_ptr for better memory management:
#include <memory>
// ...
EnvCreateResult EnvCreateFunc() {
constexpr int TICK_SKIP = 12;
constexpr float NO_TOUCH_TIMEOUT_SECS = 8.f;
int team_size = 1;
std::cout << "Team Size: " << team_size << std::endl;
// Use smart pointer for Arena
std::shared_ptr<Arena> arena = std::make_shared<Arena>(/* constructor parameters */);
const auto PZSUM = [](std::shared_ptr<RewardFunction> rewardFunc, float teamSpirit = 1.0f, float opponentScale = 1.0f) {
return std::make_shared<ZeroSumReward>(rewardFunc, teamSpirit, opponentScale);
};
// Use smart pointers for rewards
auto rewards = std::make_shared<LogCombinedReward>(
std::vector<std::pair<std::shared_ptr<RewardFunction>, float>>{
{ std::make_shared<sBP1>(arena), 1.0f },
// Add other reward functions as needed
}
);
{
static std::once_flag onceFlag;
std::call_once(onceFlag, [&] {
for (size_t i = 0; i < rewards->rewardFuncs.size(); i++)
g_RewardNames.push_back(rewards->GetRewardFuncName(i));
});
}
std::vector<std::shared_ptr<TerminalCondition>> terminalConditions = {
std::make_shared<NoTouchCondition>(NO_TOUCH_TIMEOUT_SECS * 120 / TICK_SKIP),
std::make_shared<GoalScoreCondition>()
// std::make_shared<BallTouchGroundCondition>()
};
auto obs = std::make_shared<DefaultOBS>();
auto actionParser = std::make_shared<DiscreteAction>();
auto stateSetter = std::make_shared<WeightedSampleSetter>(
std::vector<std::pair<std::shared_ptr<StateSetter>, float>>{
{ std::make_shared<RandomState>(true, true, false), 0.9f },
{ std::make_shared<KickoffState>(), 0.1f },
{ std::make_shared<AirDribbleSetup>(), 0.0f },
{ std::make_shared<ReplaySetter>(team_size), 0.0f },
}
);
// Create Match with smart pointers
std::shared_ptr<Match> match = std::make_shared<Match>(
rewards,
terminalConditions,
obs,
actionParser,
stateSetter,
team_size, // Team size
true // Spawn opponents
);
// If Match needs to set the arena, ensure it's done here
// match->SetArena(arena); // Uncomment if such a method exists
std::shared_ptr<Gym> gym = std::make_shared<Gym>(match, TICK_SKIP);
return { match.get(), gym.get() }; // Adjust return type if necessary
}
Note: Adjust the EnvCreateResult structure and other parts of your code to work seamlessly with smart pointers if you choose to adopt this approach.
Final Thoughts
Proper initialization and management of objects are crucial, especially in complex systems involving reinforcement learning environments. By ensuring that arena is correctly instantiated before it's used in any reward functions, you can prevent undefined behavior and ensure that your RL agent receives accurate feedback during training.
If you encounter further issues or need more specific guidance on initializing Arena based on its constructor, please provide additional details about the Arena class, and I'd be happy to assist further!