SOLVEUR
RICOCHET ROBOTSRICOCHET ROBOTS
SOLVER
Dans Ricochet Robots, les robots glissent en ligne droite jusqu'au premier obstacle. Il faut amener le robot bleu sur la cible en un minimum de coups, quitte à déplacer d'autres robots pour qu'il s'appuie dessus. Nous avons résolu le jeu par recherche arborescente, puis avec A* et deux heuristiques.
In Ricochet Robots, robots slide in a straight line until they hit something. The goal is to bring the blue robot onto the target in as few moves as possible, even if that means moving other robots so it can stop against them. We solved the game with tree search, then with A* and two heuristics.
- ContexteContext
- Master AI2D, cours RP (résolution de problèmes), Sorbonne Université, 2024AI2D master's, RP course (problem solving), Sorbonne Université, 2024
- ÉquipeTeam
- En binôme avec Saad MousstaidPair project with Saad Mousstaid
- Stack
- Python, NumPy, Matplotlib, Pygame
- Code
- Dépôt privéPrivate repository
LE PROBLÈMETHE PROBLEM
Le plateau est une grille carrée avec des murs, un îlot central, plusieurs robots et une cible. Un coup consiste à pousser un robot vers le haut, le bas, la gauche ou la droite : il avance jusqu'à rencontrer un mur, un autre robot ou le bord. Chaque coup compte pour un, quelle que soit la distance. Le jeu est NP-difficile : on cherche une solution optimale, ou la preuve qu'il n'en existe pas sous un nombre de coups donné.
The board is a square grid with walls, a central island, several robots and a target. A move pushes one robot up, down, left or right: it keeps going until it hits a wall, another robot or the edge. Every move costs one, whatever the distance. The game is NP-hard: we look for an optimal solution, or a proof that none exists under a given number of moves.
LA DÉMOTHE DEMO
Des plateaux générés et résolus par notre code Python, puis rejoués ici comme dans notre notebook. D'abord le robot bleu seul, puis la solution optimale qui déplace aussi les autres robots.
Boards generated and solved by our Python code, then replayed here as in our notebook. First the blue robot alone, then the optimal solution that also moves the other robots.
r1 est le robot bleu, à amener sur la cible t. r2 à r5 sont les autres robots. Les traits noirs épais sont des murs, les traces claires montrent les coups joués.
r1 is the blue robot, to be brought onto the target t. r2 to r5 are the other robots. Thick black lines are walls, light traces show the moves played.
QUATRE MÉTHODESFOUR METHODS
- Largeur, robot bleu seul. Une recherche en largeur qui ne déplace que le robot bleu. Elle trouve vite une solution, qui sert de borne supérieure pour la suite.
- Breadth-first, blue robot alone. A breadth-first search that only moves the blue robot. It quickly finds a solution, used as an upper bound afterwards.
- Largeur, tous les robots. Les autres robots bougent aussi, pour offrir des appuis. La borne précédente limite la profondeur de l'arbre, qui grossit très vite avec le nombre de robots.
- Breadth-first, all robots. The other robots move too, to offer stopping points. The previous bound limits the depth of the tree, which grows very fast with the number of robots.
- A* avec h1. L'estimation vaut 0 sur la cible, 1 sur la même ligne ou colonne, 2 sinon. Elle est monotone, donc minorante : A* trouve bien une solution optimale.
- A* with h1. The estimate is 0 on the target, 1 on the same row or column, 2 otherwise. It is monotone, hence admissible: A* does find an optimal solution.
- A* avec h2. On relâche une règle : le robot peut s'arrêter n'importe où sur sa trajectoire. Le nombre de coups de ce jeu simplifié, précalculé pour toutes les cases, sert d'estimation.
- A* with h2. One rule is relaxed: the robot may stop anywhere along its path. The number of moves in this simpler game, precomputed for every cell, is used as the estimate.
RÉSULTATSRESULTS
Les figures sont celles de notre notebook, telles quelles, d'où leurs légendes en français.
The figures are taken unchanged from our notebook, hence their French labels.
Déplacer tous les robots coûte cher : le temps moyen passe de quelques secondes sur 8 × 8 à près de 6 minutes sur 16 × 16, alors qu'avec le robot bleu seul il reste négligeable. En échange, chaque robot supplémentaire est un appui possible, et la solution raccourcit : 6,2 coups en moyenne avec un robot, 5 avec cinq.
Moving every robot is expensive: the average time goes from a few seconds on 8 × 8 to nearly 6 minutes on 16 × 16, while it stays negligible with the blue robot alone. In return, every extra robot is a possible stopping point, and solutions get shorter: 6.2 moves on average with one robot, 5 with five.
A* avec h1 reste aussi rapide que la recherche en largeur. Avec h2, l'estimation est plus fine, mais son précalcul sur tout le plateau prend le dessus sur les grands plateaux : 0,35 s en 18 × 18, contre quelques millisecondes pour les deux autres.
A* with h1 is as fast as breadth-first search. With h2 the estimate is sharper, but precomputing it over the whole board dominates on large boards: 0.35 s on 18 × 18, against a few milliseconds for the other two.
La démo rejoue des solutions calculées par notre code Python (génération des plateaux et recherches en largeur). Seul l'affichage est réécrit en JavaScript, dans le style de nos figures Matplotlib.
The demo replays solutions computed by our Python code (board generation and breadth-first searches). Only the display is rewritten in JavaScript, in the style of our Matplotlib figures.