## Description This a Multi-Agent Path Finding (MAPF) algorithm. An exercise I did at school to learn about MAPF. The aim is to route agents through nodes from 'start' to 'goal'. Nodes can be of type NORMAL, RESTRICTED, PRIORITY or BLOCKED. See 'Algorithm explanation' for more details. ## Algorithm explanation - the program first computes the shortest path using Djikstra algorithm - then, starts sending agents through the shortest-path - if an agent cannot move to its next node, then the program recomputes a shortest-path for that specific agent - if no shortest path is possible, the program skips to next agent ### RESTRICTED node If a node is RESTRICTED: - the agent checks whether connection between two nodes has capacity and whether the RESTRICTED node is `reserved` - if so: - the agent reserves the RESTRICTED node and moves to the connection between two nodes - on the following turn, the agent moves to the RESTRICTED node and unreserve the node ### PRIORITY node The algorithm that computes the shortest path counts each PRIORITY node, and choose the path with the highest number of PRIORITY node at the end. ### BLOCKED node BLOCKED node are skipped when computing the shortest path. ## Instructions Create a virtual environment: $ make env Install dependencies: $ make install Run the program: $ make run MAP=path/to/map also: $ ./main.py path/to/map Clean: $ make clean Debug: $ make debug MAP=path/to/map Lint: $ make lint and: $ make lint-strict ### About passing arguments to 'make run' Right now I use the variable $ARG. Another option is to use $(filter-out $@,$(MAKECMDGOALS)). What is this? The make file uses 'MAKECMDGOALS' to pass arguments to the program. However, 'MAKECMDGOALS' prepends the argument with the target. In our case that is 'run', which we don't want. So we want to filter the target, 'run', out. To do so we use 'filter-out', i.e. $(filter-out pattern, list) In Make the target is represented by the variable '$@'. So to remove the target we can write: $(filter-out $@,$(MAKECMDGOALS)) Source: https://www.gnu.org/software/make/manual/html_node/Goals.html ### About ctags in Vim If you want to use ctags with Vim, you can create a tag table file with: $ make tags ## Visual representation ``` D1-waypoint1 D1-waypoint2 D2-waypoint1 D1-goal D2-waypoint2 D2-goal ``` The syntax is `D-|`, with color when provided. ## Explanation of the network zone files ### Input Format - The first line defines the number of drones (agents) using `nb_drones`: ``` nb_drones: ``` - Zones are defined one per line using the following type prefixes: ``` start_hub: [metadata] end_hub: [metadata] hub: [metadata] ``` - `start_hub` marks the starting zone. - `end_hub` marks the end zone. - `hub` defines a regular zone. - The connection syntax forbids dashes (`-`) in zone names. ### Zone Metadata All metadata is optional and enclosed in square brackets (`[...]`). Metadata has the following default values: - `zone=` — Default: `normal` - `color=` — Default: `none` - `max_drones=` — Default: `1`. The maximum number of drones that can occupy the zone simultaneously. Tags inside brackets can appear in any order. ### Zone Types - `normal` — Standard zone with a movement cost of 1 turn. This is the default. - `blocked` — Inaccessible zone. Drones must not enter or pass through this zone. Any path using it is invalid. - `restricted` — Sensitive or dangerous zone. Movement to this zone costs 2 turns. - `priority` — Preferred zone. Movement to this zone costs 1 turn but should be prioritized during pathfinding. ### Colors - Colors are optional and can be used for visual representation, such as terminal output or graphical displays. - Any valid single-word string can be used as a color value, for example: - `red` - `blue` - `gray` - There is no fixed list of allowed colors. - When colors are specified, the implementation should provide visual feedback through colored terminal output or a graphical representation. ### Connections Connections are defined using the following syntax: ``` connection: - [metadata] ``` - Connections define a bidirectional edge between two zones. - The connection syntax forbids dashes (`-`) in zone names. - Optional metadata can be specified in square brackets (`[...]`): - `max_link_capacity=` — Default: `1`. The maximum number of drones that can traverse the connection simultaneously. ### Comments Comments start with `#` and are ignored: ## Resources - the Wikipedia pages of Dijkstra and multi-agent pathfinding (MAPF) - on silencing prints in Make file: https://www.gnu.org/software/make/manual/make.html#Echoing - on naming methods: https://peps.python.org/pep-0008/#descriptive-naming-styles - on hashable objects: https://docs.python.org/3/glossary.html#term-hashable - on color syntax, escape char etc: https://en.wikipedia.org/wiki/ANSI_escape_code - on 256-color: https://tintin.mudhalla.net/info/256color/ - Xterm 256-color palette with code://upload.wikimedia.org/wikipedia/commons/f/f5/Xterm-256color.svg ## To improve - rewrite models.py without Pydantics (no dependencies) - move `colors` to their own class and file - optimize program for high number of drones ### Misc. Command to get the list of colors from all maps: grep -oiP 'color=\K[a-z]+' maps/**/*.txt | sort | uniq - -o, don't print the line, only the match - -i ignore case - \K look ahead, keep 'color=' but don't print it, -P (perl necessary) - [a-z]+ match or more lower case otherwise stop - sort to prepare for uniq ## License mapf Copyright (C) 2026 yctct This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with this program. If not, see . See the file COPYING.