Skip to content

Commit

Permalink
Fixed loss of state (now tested, also with grids), ready to train RL
Browse files Browse the repository at this point in the history
  • Loading branch information
flimdejong committed Dec 16, 2024
1 parent 4c6929d commit b7e3a41
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 55 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,4 @@ roboteam_ai/src/RL/src/tmp/eval_monitor.monitor.csv
roboteam_ai/src/RL/src/tmp/monitor.monitor.csv
roboteam_ai/src/RL/src/ppo_tensorboard_main
roboteam_ai/src/RL/src/best_model_PPO.zip
docker/runner/ssl-game-controller-config/state-store.json.stream
10 changes: 1 addition & 9 deletions roboteam_ai/include/roboteam_ai/STPManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,22 +58,14 @@ class STPManager {
*/
void decidePlay(world::World* _world, bool ignoreWorldAge = false);

/**
* @brief Updates the dynamic plays (AttackingPass, Attack, Defend) with the current world state
* @details Checks if currentPlay points to a dynamic play and sets it to nullptr if it does.
* Then removes old dynamic plays and creates new ones with updated world information.
* @param _world Pointer to the current world state
*/
void updateDynamicPlays(world::World* _world);

public:
/**
* @brief Starts the AI with a synchronized boolean to ensure that AI exits correctly
* @param exitApplication Indicates whether the AI should exit
*/
void start(std::atomic_flag& exitApplication);

static PlaysVec plays; /**< The vector that contains all plays */
static const PlaysVec plays; /**< The vector that contains all plays */

/**
* @brief Delete copy constructor of the STPManager class
Expand Down
49 changes: 5 additions & 44 deletions roboteam_ai/src/STPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,13 @@ namespace rtt {
STPManager* STPManager::instance = nullptr;

/// Initialize all plays here (since play vector is static, it's better to do it here to make sure it's initialized before use)
STPManager::PlaysVec STPManager::plays = ([] {
const STPManager::PlaysVec STPManager::plays = ([] {
auto plays = std::vector<std::unique_ptr<ai::stp::Play>>();

plays.emplace_back(std::make_unique<plays::AttackingPass>());
plays.emplace_back(std::make_unique<plays::Attack>());
plays.emplace_back(std::make_unique<plays::Halt>());
plays.emplace_back(std::make_unique<plays::Defend>());
plays.emplace_back(std::make_unique<plays::KeeperKickBall>());
plays.emplace_back(std::make_unique<plays::PrepareForcedStart>());
plays.emplace_back(std::make_unique<plays::StopFormation>());
Expand All @@ -73,12 +76,6 @@ STPManager::PlaysVec STPManager::plays = ([] {
plays.emplace_back(std::make_unique<plays::FreeKickUsPass>());
plays.emplace_back(std::make_unique<plays::KickOffUs>());
plays.emplace_back(std::make_unique<plays::KickOffThem>());

// Dynamic plays (these will be recreated each tick)
plays.emplace_back(std::make_unique<plays::AttackingPass>());
plays.emplace_back(std::make_unique<plays::Attack>());
plays.emplace_back(std::make_unique<plays::Defend>());

return plays;
})();

Expand Down Expand Up @@ -194,10 +191,6 @@ void STPManager::decidePlay(world::World *_world, bool ignoreWorldAge) {
return;
}
}

// Update the dynamic plays (AttackingPass, Attack, Defend)
STPManager::updateDynamicPlays(_world);

if (!currentPlay || !currentPlay->isValidPlayToKeep() || ai::stp::PlayDecider::didLockPlay()) {
// Decide the best play (ignoring the interface play value)
currentPlay = ai::stp::PlayDecider::decideBestPlay(_world, plays);
Expand All @@ -209,38 +202,6 @@ void STPManager::decidePlay(world::World *_world, bool ignoreWorldAge) {
currentPlay->update();
}

void STPManager::updateDynamicPlays(world::World* _world) {

// Create the new plays (attackingPass, attack, defend) and set their world immediately
auto attackingPass = std::make_unique<plays::AttackingPass>();
auto attack = std::make_unique<plays::Attack>();
auto defend = std::make_unique<plays::Defend>();

attackingPass->setWorld(_world);
attack->setWorld(_world);
defend->setWorld(_world);

// Check if currentPlay is one of the dynamic plays that we're about to delete
if (currentPlay) {
for (int i = plays.size() - 3; i < plays.size(); ++i) {
if (currentPlay == plays[i].get()) {
currentPlay = nullptr; // If it is, set to nullptr. Otherwise currentPlay will point to deleted memory
break;
}
}
}

// Remove old plays
for (int i = 0; i < 3; i++) {
plays.pop_back();
}

// Add new plays
plays.emplace_back(std::move(attackingPass));
plays.emplace_back(std::move(attack));
plays.emplace_back(std::move(defend));
}

STPManager::STPManager(std::shared_ptr<ai::gui::net::InterfaceGateway> interfaceGateway) :
interfaceGateway(std::move(interfaceGateway)),
rlInterface() {
Expand All @@ -249,7 +210,7 @@ STPManager::STPManager(std::shared_ptr<ai::gui::net::InterfaceGateway> interface
// Set initial grid positions
std::array<bool, 9> defaultGrid = {
false, false, true, // top row
false, true, true, // middle row
false, false, false, // middle row
false, false, true // bottom row
};
rlInterface.setBinaryOccupancyGrid(defaultGrid);
Expand Down
2 changes: 1 addition & 1 deletion roboteam_ai/src/rl/zmq_PUB.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def create_publisher():

def publish_messages(socket, topic="updates"):
# Array of integers to send
numbers = [3, 7, 9, 8]
numbers = [2, 3,6]

while True:
# Convert numbers to strings
Expand Down
2 changes: 1 addition & 1 deletion roboteam_ai/src/stp/plays/offensive/AttackingPass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ uint8_t AttackingPass::score(const rtt::Field& field) noexcept {
Dealer::FlagMap AttackingPass::decideRoleFlags() const noexcept {
int numDefenders = 2;
int numWallers = 4;
int numAttackers = 2;
int numAttackers = STPManager::getRLInterface().getNumAttackers();

Dealer::FlagMap flagMap;

Expand Down

0 comments on commit b7e3a41

Please sign in to comment.