Skip to content

Commit

Permalink
Integrated dynamic attacker allocation in code. Tested with static va…
Browse files Browse the repository at this point in the history
…lues but dynamic values are successfully received
  • Loading branch information
flimdejong committed Dec 13, 2024
1 parent cf6ae7c commit 4c6929d
Show file tree
Hide file tree
Showing 11 changed files with 142 additions and 82 deletions.
1 change: 1 addition & 0 deletions roboteam_ai/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ target_link_libraries(roboteam_ai_computation
PRIVATE roboteam_networking
PRIVATE roboteam_utils
PRIVATE roboteam_ai_evaluation
PRIVATE ixwebsocket
)
target_compile_options(roboteam_ai_computation PRIVATE "${COMPILER_FLAGS}")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,17 @@ class PositionComputations {
* @return A position that is outside the given shape
*/
static Vector2 calculatePositionOutsideOfShape(Vector2 targetPosition, const Field &field, const std::unique_ptr<Shape> &avoidShape, const AvoidObjects &avoidObj);

/**
* @brief Gets a specific grid (the type) from the field based on a grid number we input (1-9)
* @param field The field containing the grid positions
* @param gridNumber Number identifying which grid to return (1-9):
* 1: top left 2: top middle 3: top right
* 4: middle left 5: middle middle 6: middle right
* 7: bottom left 8: bottom middle 9: bottom right
* @return The requested grid if the number is valid (1-9), std::nullopt otherwise
*/
static std::optional<Grid> getGridFromNumber(const Field& field, int gridNumber);
};
} // namespace rtt::ai::stp
#endif // RTT_POSITIONCOMPUTATIONS_H
11 changes: 11 additions & 0 deletions roboteam_ai/src/STPManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,17 @@ STPManager::STPManager(std::shared_ptr<ai::gui::net::InterfaceGateway> interface
interfaceGateway(std::move(interfaceGateway)),
rlInterface() {
instance = this;

// Set initial grid positions
std::array<bool, 9> defaultGrid = {
false, false, true, // top row
false, true, true, // middle row
false, false, true // bottom row
};
rlInterface.setBinaryOccupancyGrid(defaultGrid);

// Set initial attacker number
rlInterface.setNumAttackers(2);
}

} // namespace rtt
84 changes: 42 additions & 42 deletions roboteam_ai/src/rl/test.cpp
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
#include <string>
#include <chrono>
#include <thread>
#include <iostream>
#include <zmq.hpp>
// #include <string>
// #include <chrono>
// #include <thread>
// #include <iostream>
// #include <zmqpp/zmqpp.hpp>

int main()
{
using namespace std::chrono_literals;
// int main()
// {
// using namespace std::chrono_literals;

std::cout << "Starting server..." << std::endl;
// std::cout << "Starting server..." << std::endl;

// initialize the zmq context with a single IO thread
zmq::context_t context{1};
// // initialize the zmq context with a single IO thread
// zmqpp::context context;

// construct a REP (reply) socket and bind to interface
zmq::socket_t socket{context, zmq::socket_type::rep};
socket.bind("tcp://*:5555");
// // construct a REP (reply) socket and bind to interface
// zmqpp::socket_t socket{context, zmqpp::socket_type::rep};
// socket.bind("tcp://*:5555");

std::cout << "Server is running on port 5555..." << std::endl;
// std::cout << "Server is running on port 5555..." << std::endl;

// prepare some static data for responses
const std::string data{"World"};
// // prepare some static data for responses
// const std::string data{"World"};

for (;;)
{
zmq::message_t request;
// for (;;)
// {
// zmqpp::message_t request;

std::cout << "Waiting for messages..." << std::endl;
// std::cout << "Waiting for messages..." << std::endl;

// After (warning fixed):
if (!socket.recv(request, zmq::recv_flags::none)) {
std::cerr << "Failed to receive message" << std::endl;
continue;
}
std::cout << "Received " << request.to_string() << std::endl;

// simulate work
std::this_thread::sleep_for(1s);

// send the reply to the client
auto res = socket.send(zmq::buffer(data), zmq::send_flags::none);
if (!res) {
std::cerr << "Failed to send response" << std::endl;
continue;
}
std::cout << "Sent response: " << data << std::endl;
}

return 0;
}
// // After (warning fixed):
// if (!socket.recv(request, zmqpp::recv_flags::none)) {
// std::cerr << "Failed to receive message" << std::endl;
// continue;
// }
// std::cout << "Received " << request.to_string() << std::endl;

// // simulate work
// std::this_thread::sleep_for(1s);

// // send the reply to the client
// auto res = socket.send(zmqpp::buffer(data), zmqpp::send_flags::none);
// if (!res) {
// std::cerr << "Failed to send response" << std::endl;
// continue;
// }
// std::cout << "Sent response: " << data << std::endl;
// }

// return 0;
// }
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 = [2, 7, 9]
numbers = [3, 7, 9, 8]

while True:
# Convert numbers to strings
Expand Down
10 changes: 0 additions & 10 deletions roboteam_ai/src/rl/zmq_client.py

This file was deleted.

2 changes: 1 addition & 1 deletion roboteam_ai/src/stp/Role.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Status Role::update(StpInfo const& info) noexcept {

// Success if the tactic returned success and if all tactics are done
if (status == Status::Success && robotTactics.finished()) {
RTT_INFO("ROLE SUCCESSFUL for ", info.getRobot()->get()->getId())
// RTT_INFO("ROLE SUCCESSFUL for ", info.getRobot()->get()->getId())
return Status::Success;
}

Expand Down
91 changes: 69 additions & 22 deletions roboteam_ai/src/stp/computations/PositionComputations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
#include "stp/computations/PositionScoring.h"
#include "utilities/Constants.h"
#include "world/World.hpp"
#include "rl/RLInterface.hpp"
#include "STPManager.h"

namespace rtt::ai::stp {
int PositionComputations::amountOfWallers = 4;
Expand Down Expand Up @@ -473,34 +475,79 @@ void PositionComputations::calculateInfoForDefendersAndWallers(std::unordered_ma

void PositionComputations::calculateInfoForAttackers(std::unordered_map<std::string, StpInfo> &stpInfos, std::array<std::unique_ptr<Role>, constants::MAX_ROBOT_COUNT> &roles,
const Field &field, world::World *world) noexcept {
// List of all active attackers
// Get list of attackers
auto attackerNames = std::vector<std::string>{};
for (size_t i = 0; i < world->getWorld()->getUs().size(); i++) {
if (roles[i]->getName().find("attacker") != std::string::npos) {
attackerNames.emplace_back(roles[i]->getName());
}
}
if (attackerNames.size() == 0)
; // Do nothing
else if (attackerNames.size() == 1) {
stpInfos["attacker_0"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.middleRightGrid, gen::AttackingPass, field, world));
} else if (attackerNames.size() == 2) {
stpInfos["attacker_0"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.topRightGrid, gen::AttackingPass, field, world));
stpInfos["attacker_1"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.bottomRightGrid, gen::AttackingPass, field, world));
} else if (attackerNames.size() >= 3) {
stpInfos["attacker_0"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.topRightGrid, gen::SafePass, field, world));
stpInfos["attacker_1"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.middleRightGrid, gen::AttackingPass, field, world));
stpInfos["attacker_2"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.bottomRightGrid, gen::SafePass, field, world));
}
if (attackerNames.size() == 4) {
stpInfos["attacker_3"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.middleMidGrid, gen::AttackingPass, field, world));
} else if (attackerNames.size() == 5) {
stpInfos["attacker_3"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.topMidGrid, gen::AttackingPass, field, world));
stpInfos["attacker_4"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.bottomMidGrid, gen::AttackingPass, field, world));
} else if (attackerNames.size() >= 6) {
stpInfos["attacker_3"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.topMidGrid, gen::AttackingPass, field, world));
stpInfos["attacker_4"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.middleMidGrid, gen::SafePass, field, world));
stpInfos["attacker_5"].setPositionToMoveTo(PositionComputations::getPosition(std::nullopt, field.bottomMidGrid, gen::AttackingPass, field, world));

// If no attackers, nothing to do
if (attackerNames.empty()) return;

// Get grid array
std::array<bool,9> gridArray = STPManager::getRLInterface().getBinaryOccupancyGrid();

// Just print first 9 values directly
RTT_INFO("First 9 grid values:");
RTT_INFO("0: " + std::to_string(gridArray[0]));
RTT_INFO("1: " + std::to_string(gridArray[1]));
RTT_INFO("2: " + std::to_string(gridArray[2]));
RTT_INFO("3: " + std::to_string(gridArray[3]));
RTT_INFO("4: " + std::to_string(gridArray[4]));
RTT_INFO("5: " + std::to_string(gridArray[5]));
RTT_INFO("6: " + std::to_string(gridArray[6]));
RTT_INFO("7: " + std::to_string(gridArray[7]));
RTT_INFO("8: " + std::to_string(gridArray[8]));

// Number of attackers
//RTT_INFO("Number of attackers: " + std::to_string(attackerNames.size()));

// Assign attackers to positions where gridArray is true
size_t currentAttacker = 0;
for (int i = 0; i < 9 && currentAttacker < attackerNames.size(); i++) {
if (gridArray[i]) {
if (auto grid = PositionComputations::getGridFromNumber(field, i + 1)) {
stpInfos[attackerNames[currentAttacker]].setPositionToMoveTo(
PositionComputations::getPosition(std::nullopt, *grid, gen::SafePass, field, world));
currentAttacker++;
}
}
}
}

std::optional<Grid> PositionComputations::getGridFromNumber(const Field& field, int gridNumber){
if (gridNumber == 1){
return field.topLeftGrid;
}
if (gridNumber == 2){
return field.topMidGrid;
}
if (gridNumber == 3){
return field.topRightGrid;
}
if (gridNumber == 4){
return field.middleLeftGrid;
}
if (gridNumber == 5){
return field.middleMidGrid;
}
if (gridNumber == 6){
return field.middleRightGrid;
}
if (gridNumber == 7){
return field.bottomLeftGrid;
}
if (gridNumber == 8){
return field.bottomMidGrid;
}
if (gridNumber == 9){
return field.bottomRightGrid;
}
else{
RTT_WARNING("Invalid grid number");
return std::nullopt;
}
}

Expand Down
4 changes: 2 additions & 2 deletions roboteam_ai/src/stp/plays/offensive/Attack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ Attack::Attack() : Play() {
std::make_unique<role::Defender>("waller_1"),
std::make_unique<role::Formation>("attacker_1"),
std::make_unique<role::Defender>("defender_3"),
std::make_unique<role::Formation>("attacker_2"),
std::make_unique<role::Defender>("defender_4"),
};
}

Expand All @@ -67,9 +67,9 @@ Dealer::FlagMap Attack::decideRoleFlags() const noexcept {
flagMap.insert({"defender_1", {DealerFlagPriority::MEDIUM_PRIORITY, {}}});
flagMap.insert({"defender_2", {DealerFlagPriority::MEDIUM_PRIORITY, {}}});
flagMap.insert({"defender_3", {DealerFlagPriority::MEDIUM_PRIORITY, {}}});
flagMap.insert({"defender_4", {DealerFlagPriority::MEDIUM_PRIORITY, {}}});
flagMap.insert({"attacker_0", {DealerFlagPriority::LOW_PRIORITY, {}}});
flagMap.insert({"attacker_1", {DealerFlagPriority::LOW_PRIORITY, {}}});
flagMap.insert({"attacker_2", {DealerFlagPriority::LOW_PRIORITY, {}}});

return flagMap;
}
Expand Down
4 changes: 2 additions & 2 deletions roboteam_ai/src/utilities/Settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ void GameSettings::setLeft(bool _left) {
return;
}

RTT_INFO("This secondary AI can not alter settings")
//RTT_INFO("This secondary AI can not alter settings")
}

net::RobotHubMode GameSettings::getRobotHubMode() { return robotHubMode; }
Expand All @@ -55,7 +55,7 @@ bool GameSettings::setRobotHubMode(net::RobotHubMode mode) {
return true;
}

RTT_INFO("This secondary AI can not alter settings")
//RTT_INFO("This secondary AI can not alter settings")
return false;
}

Expand Down
4 changes: 2 additions & 2 deletions roboteam_ai/src/world/Robot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,8 @@ void Robot::setWorkingBallSensor(bool _workingBallSensor) noexcept { Robot::work
void Robot::setBallSensorSeesBall(bool _seesBall) noexcept { ballSensorSeesBall = _seesBall; }

void Robot::setDribblerSeesBall(bool _seesBall) noexcept {
if (_seesBall) RTT_INFO("Robot " + std::to_string(id) + " has ball")
dribblerSeesBall = _seesBall;
// if (_seesBall) RTT_INFO("Robot " + std::to_string(id) + " has ball")
// dribblerSeesBall = _seesBall;
}

void Robot::setHasBall(bool _hasBall) noexcept { Robot::robotHasBall = _hasBall; }
Expand Down

0 comments on commit 4c6929d

Please sign in to comment.