summaryrefslogtreecommitdiff
path: root/README
blob: 68c5ed54b113502520ba0955aa4517d2895ca318 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
## 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<drone_id>-<zone>|<connection>`, 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: <number> ```

- Zones are defined one per line using the following type prefixes:

  ``` start_hub: <name> <x> <y> [metadata] end_hub:   <name> <x> <y> [metadata]
hub:       <name> <x> <y> [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=<type>` — Default: `normal`
- `color=<value>` — Default: `none`
- `max_drones=<number>` — 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: <name1>-<name2> [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=<number>` — 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 <https://www.gnu.org/licenses/>.

See the file COPYING.