mesa-examples icon indicating copy to clipboard operation
mesa-examples copied to clipboard

feat: Add Swarm Minefield Mapper (Dynamic Agent Role management and co-operation)

Open apfine opened this issue 8 months ago • 13 comments

Description

This PR introduces the Swarm Minefield navigator, a deterministic, production-oriented spatial navigation model. It simulates a heterogeneous drone swarm (1 Leader, 3 Followers) tasked with mapping a strict 1-meter safe corridor through a dense, dynamically generated minefield under partial observability.

I built this model to establish a mathematically rigorous spatial baseline in preparation for a GSoC 2026 proposal.

Key Technical Implementations:

  • Strict A Verification:* The final path extraction requires a cell and all 8 of its Moore neighbors to be explicitly scanned and safe.
  • Dynamic Leadership Handoff: If the Leader drone is trapped, a Follower with better forward (Y-axis) progress automatically assumes the Leader role, triggering the swarm to rally to the new coordinates.
  • Macro-Node DFS Backtracking: The Leader actively drops Checkpoints at fully safe intersections. If it detects a soft-deadlock (via a frustration timer), it retreats and explicitly paints the abandoned branch as a DEAD_END in the global mesh network.
  • Airborne Regrouping: Followers utilize a distinct behavioral state to temporarily bypass ground hazards (ignoring safety buffers) when rallying to a retreating Leader.

(Note: Please see the attached GIFs/Screenshots below demonstrating the trap-escape and dynamic handoff logic.)

mesa-swarm-gif

The swarm can be seen navigating a minefield with 450 mines in 100 X 100 grid .

You can read Readme of the project to know about all other configurations.

Motivation

The goal of this deterministic expert system is to serve as the foundation for an LLM-integration project. For GSoC, I plan to propose using the mesa-llm module to replace these hardcoded state machines (like the frustration timer and U-trap detection) with local LLM-driven spatial reasoning and semantic memory.

Checklist

  • [x] Code follows PEP8 formatting guidelines.
  • [x] Includes README.md with instructions and architecture details.
  • [x] Includes requirements.txt (only requires mesa).
  • [x] Unit tests provided in tests/test_model.py.

apfine avatar Mar 07 '26 12:03 apfine

pre-commit.ci autofix

apfine avatar Mar 07 '26 12:03 apfine

pre-commit.ci autofix

apfine avatar Mar 07 '26 12:03 apfine

Thanks for the PR, looks like a cool model, thanks for the gif.

Could you:

  • Checkout the test failure
  • Request a peer-review (and maybe do one or two yourself)

EwoutH avatar Mar 15 '26 20:03 EwoutH

Thanks for the PR, looks like a cool model, thanks for the gif.

Could you:

  • Checkout the test failure
  • Request a peer-review (and maybe do one or two yourself)

Sure @EwoutH , I am reverting back after fixing the errors

Thanks , so much for the peer review tag !!

apfine avatar Mar 16 '26 11:03 apfine

Thanks for the PR, looks like a cool model, thanks for the gif.

Could you:

  • Checkout the test failure
  • Request a peer-review (and maybe do one or two yourself)

@EwoutH I have updated the contributions to fix the CI erros !!

sir please review those.

apfine avatar Mar 17 '26 06:03 apfine

Thanks for the PR, looks like a cool model, thanks for the gif.

Could you:

  • Checkout the test failure
  • Request a peer-review (and maybe do one or two yourself)

@EwoutH I have requested a peer review and invited if someone wants to get reviewed in the channel . Now I will review the tagged pulls in the repository .

Thankyou for your guidance.

apfine avatar Mar 17 '26 06:03 apfine

Peer Review Swarm Minefield Mapper

Reviewed agents.py, model.py, server.py, README.md, and tests/test_model.py in full.


Overall

Really impressive work. The architecture is production-quality heterogeneous swarm with dynamic leadership, checkpoint-based retreat, and distributed pathfinding. The README is one of the most thorough I've seen in this repo. A few things worth discussing before merge.


Mesa API Usage

Mesa 4.x APIs are used correctly throughout super().__init__(rng=seed), self.agents.shuffle_do("step"), and DataCollector are all right. One structural point: the model implements a custom SimpleMultiGrid class instead of using Mesa's native OrthogonalMooreGrid. The comment says "across Mesa versions" which suggests this is intentional for compatibility. Could you clarify whether migrating to the native grid is planned, or if this is a deliberate design choice? It works correctly as-is, but it does step outside the standard Mesa ecosystem.


Agent Cooperation Logic

The leader-follower state machine is sound. The frustration counter for detecting horizontal loops, the checkpoint creation rule (only when the full 3×3 Moore neighborhood is mine-free), and the dead-end immutability logic are all correct and tested.

One thing worth a closer look: in leadership_handoff(), followers are reassigned formation offsets sorted by (x, unique_id). If a drone's physical position doesn't match its newly assigned offset after handoff, it could rally toward a mismatched target temporarily. Not a crash, but the existing handoff test checks that promotion happens it doesn't verify offset correctness during the subsequent rally. Worth adding a test case for that.


Pathfinding

Both A* implementations are correct local movement uses Manhattan heuristic, and the global final-path search uses Chebyshev distance which is the right choice for Moore-neighborhood movement. The verified-safe cell rule (all Moore neighbors must be explicitly known) is also correct and the test for buffer-adjacent cells confirms it.


Test Coverage

13 tests covering the core scenarios solid. Gaps worth filling if time allows:

  • Formation offset correctness after leadership handoff
  • Checkpoint retirement workflow (when the stack is exhausted)
  • Airborne override (safety_override) behavior during rally

Visualization

Portrayal function handles all agent and cell types clearly with distinct colors and layers. The HTML status panel showing battery levels, mines discovered, and path length is a nice touch for debugging.


Summary

Ready for merge pending a clarification on the SimpleMultiGrid design choice. The formation offset test is optional but would strengthen confidence in the handoff logic. Great work overall the README alone shows a level of care that most examples don't have.

abhinavk0220 avatar Mar 17 '26 07:03 abhinavk0220

Peer Review Swarm Minefield Mapper

Reviewed agents.py, model.py, server.py, README.md, and tests/test_model.py in full.

Overall

Really impressive work. The architecture is production-quality heterogeneous swarm with dynamic leadership, checkpoint-based retreat, and distributed pathfinding. The README is one of the most thorough I've seen in this repo. A few things worth discussing before merge.

Mesa API Usage

Mesa 4.x APIs are used correctly throughout super().__init__(rng=seed), self.agents.shuffle_do("step"), and DataCollector are all right. One structural point: the model implements a custom SimpleMultiGrid class instead of using Mesa's native OrthogonalMooreGrid. The comment says "across Mesa versions" which suggests this is intentional for compatibility. Could you clarify whether migrating to the native grid is planned, or if this is a deliberate design choice? It works correctly as-is, but it does step outside the standard Mesa ecosystem.

Agent Cooperation Logic

The leader-follower state machine is sound. The frustration counter for detecting horizontal loops, the checkpoint creation rule (only when the full 3×3 Moore neighborhood is mine-free), and the dead-end immutability logic are all correct and tested.

One thing worth a closer look: in leadership_handoff(), followers are reassigned formation offsets sorted by (x, unique_id). If a drone's physical position doesn't match its newly assigned offset after handoff, it could rally toward a mismatched target temporarily. Not a crash, but the existing handoff test checks that promotion happens it doesn't verify offset correctness during the subsequent rally. Worth adding a test case for that.

Pathfinding

Both A* implementations are correct local movement uses Manhattan heuristic, and the global final-path search uses Chebyshev distance which is the right choice for Moore-neighborhood movement. The verified-safe cell rule (all Moore neighbors must be explicitly known) is also correct and the test for buffer-adjacent cells confirms it.

Test Coverage

13 tests covering the core scenarios solid. Gaps worth filling if time allows:

  • Formation offset correctness after leadership handoff
  • Checkpoint retirement workflow (when the stack is exhausted)
  • Airborne override (safety_override) behavior during rally

Visualization

Portrayal function handles all agent and cell types clearly with distinct colors and layers. The HTML status panel showing battery levels, mines discovered, and path length is a nice touch for debugging.

Summary

Ready for merge pending a clarification on the SimpleMultiGrid design choice. The formation offset test is optional but would strengthen confidence in the handoff logic. Great work overall the README alone shows a level of care that most examples don't have.

Hi @abhinavk0220, thank you so much for the extremely detailed feedback! I really appreciate the time you took to dive deep into the architecture and the state machines.

Now, regarding the points you raised:

  1. Mesa API Usage: SimpleMultiGrid vs. OrthogonalMooreGrid: You're absolutely right! The grid was a design choice, and I had chosen to use a custom grid in order to decouple the spatial representations in preparation for the mesa-llm integration. I had to squeeze the topological state into a strict JSON prompt (State-to-Text) without invoking the native grid hooks during the deep lookahead. Running the agent decoupled from the grid keeps the simulation blazing fast! However, I am totally open to moving to OrthogonalMooreGrid and utilizing PropertyLayer, as the maintainers of mesa-examples might want the alignment! I'd be happy to make the change in this PR or as a fast-follow issue.*

  2. Agent Cooperation Logic: Leadership Handoff Offsets: Wow, that is a fantastic catch on the formation offsets! You're right, of course—sorting on (x, unique_id) could potentially result in a temporary 'criss-cross' rally in the case that the physical topology doesn't align with the new offset array during the handoff. It won’t break the simulation, but it is a mathematically inefficient pathing choice. I will create a test case for the offset validation post-handoff and modify the reassignment logic to minimize the Euclidean travel distance for each of the followers. I will get those test updates pushed shortly. Thanks again for the sharp eyes on this!"

Thankyou alot for the review !!

apfine avatar Mar 17 '26 08:03 apfine

I'd like to review this. Cool concept. I'll assign myself for the review. Others can also review this alongside.

Thankyou @IlamaranMagesh .

apfine avatar Mar 17 '26 08:03 apfine

@codebreaker32 I have tried to resolve the issue or change leakage .

apfine avatar Mar 17 '26 13:03 apfine

Please take some time and go through the original mesa modules, your code shows lack of module knowledge, for eg. we have mesa.discrete_space module which contains implementation of grids and cells but it seems you are implementing it yourself. If I am missing something please let me know.

codebreaker32 avatar Mar 17 '26 14:03 codebreaker32

Thanks, these are exactly the conversations I would like to see! This helps us a lot, and I hope it helps you also.

Keep it going!

EwoutH avatar Mar 17 '26 14:03 EwoutH

Thanks, these are exactly the conversations I would like to see! This helps us a lot, and I hope it helps you also.

Keep it going!

Thankyou !!

apfine avatar Mar 17 '26 14:03 apfine