Ricochet Robots Solver
Tags: ricochetrobots, performance, software, rust
Published: 2023-09-15 12:30:34

---
title: Ricochet Robots Solver
published: true
date: 2023-09-02 18:00:00 UTC
tags: RicochetRobots,Performance,Software,Rust
canonical_url: https://kevincox.ca/2023/09/02/ricochet-robots-solver/
---

<main style="--width:min(36em,100%);display:grid;grid-template-columns:1fr var(--width) 1fr;overflow:auto;padding:0;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://en.wikipedia.org/wiki/Ricochet_Robots" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Ricochet Robots</a> is one of my favourite games. It is less of a traditional board game and more of a speed puzzle. All players attempt to solve the same challenge as fast as possible.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/ricochetrobots-box-Rb5_nT.jpg" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://kevincox.ca/a/ricochetrobots-box-Rb5_nT.jpg" alt="Ricochet Robots Box" style="box-sizing:border-box;display:block;margin:auto;max-width:100%;min-width:60%"></a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The game reminds me of the classic Pokémon ice puzzles. Once you step onto the ice you keep sliding until you hit something. The goal is to bounce around until you get to the exit.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/pokemon-ice-sOMRZk.png" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://kevincox.ca/a/pokemon-ice-sOMRZk.png" alt="Pokémon ice puzzles" style="box-sizing:border-box;display:block;image-rendering:-moz-crisp-edges;image-rendering:pixelated;margin:auto;max-width:100%;min-width:60%"></a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Ricochet Robots is basically the same, except you have 5 robots (4 on older versions) to bounce around. Importantly this means that you can bounce off other robots! For example in the following puzzle you can see that in move 3 the yellow bot uses the red bot to stop its movement.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/ricochet-CbwMJ_.svg" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qpcv2y8oqtbfzta5w0fw.png" alt="Ricochet Robots puzzle with a ricochet." style="box-sizing:border-box;display:block;margin:auto;max-width:100%;min-width:60%"></a></p>
<h2 id="the-solver" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#the-solver" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">The Solver</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">I love the game, but you quickly wonder how good you really are. That puzzle requires 22 moves? Surely there is a better option! I talked about writing a solver with some coworkers over lunch and before you know it three of us were inspired to try our hand. We sketched out an interchange format and we were off to the races.</p>
<h2 id="scale-of-the-problem" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#scale-of-the-problem" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Scale of the Problem</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">From the start it is clear that brute-force fuzzing is only going to work for the simplest of problems. There are 5 robots and each robot can move in up to 4 directions. This makes the worst-case complexity <span style="box-sizing:border-box;margin:0;padding:0"><math xmlns="http://www.w3.org/1998/Math/MathML" style="box-sizing:border-box;margin:0"><semantics style="box-sizing:border-box;margin:0"><mrow style="box-sizing:border-box;margin:0"><mi style="box-sizing:border-box;margin:0">O</mi><mrow style="box-sizing:border-box;margin:0"><mo fence="true" style="box-sizing:border-box;margin:0">(</mo><msup style="box-sizing:border-box;margin:0"><mrow style="box-sizing:border-box;margin:0"><mo fence="true" style="box-sizing:border-box;margin:0">(</mo><mn style="box-sizing:border-box;margin:0">5</mn><mo style="box-sizing:border-box;margin:0">×</mo><mn style="box-sizing:border-box;margin:0">4</mn><mo fence="true" style="box-sizing:border-box;margin:0">)</mo></mrow><mi style="box-sizing:border-box;margin:0">n</mi></msup><mo fence="true" style="box-sizing:border-box;margin:0">)</mo></mrow></mrow><annotation encoding="application/x-tex" style="box-sizing:border-box;margin:0">\O{\left(5 \times 4\right)^n}</annotation></semantics></math></span>. For a pretty modest 10-move solution that is <span style="box-sizing:border-box;margin:0;padding:0"><math xmlns="http://www.w3.org/1998/Math/MathML" style="box-sizing:border-box;margin:0"><semantics style="box-sizing:border-box;margin:0"><mrow style="box-sizing:border-box;margin:0"><mn style="box-sizing:border-box;margin:0">1</mn><msup style="box-sizing:border-box;margin:0"><mi style="box-sizing:border-box;margin:0">e</mi><mn style="box-sizing:border-box;margin:0">13</mn></msup></mrow><annotation encoding="application/x-tex" style="box-sizing:border-box;margin:0">1e^{13}</annotation></semantics></math></span>possible board states. If you spent a nanosecond on each solution that would take almost 3h. Using a conservative estimate of 2 available moves per robot (as they tend to be near walls a lot of the time) that goes down to 10s but 10 moves isn’t particularly long and a nanosecond per state is quite short. Even with 2 moves per bot and 1 ns per move that is <strong style="box-sizing:border-box;margin:0">three thousand years</strong> for a 20 move solution (which is uncommon but possible). The longest puzzle I have seen is 25 moves and would take 300 million years.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">So we are going to need to do something much more clever if we want to solve hard puzzles. We’ll get back to that shortly.</p>
<h2 id="visited-state-caching" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#visited-state-caching" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Visited State Caching</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">One of the most fundamental optimizations is caching visited states. This greatly reduces the search space because for a huge number of cases the order in which you move robots doesn’t matter. For example in the following scenario does it matter if the red robot moved before the blue? No, the result is the same either way.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/order-X5bYAl.svg" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jgj4uxa6dou4biwk2dap.png" alt="Image of two robots moving without affecting one another." style="box-sizing:border-box;display:block;margin:auto;max-width:100%;min-width:60%"></a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Fundamentally the state of the game is broken down to the board (walls, mirrors and the target) as well as the robot positions. The board state doesn’t change in a particular puzzle so all that we need to save is the robot positions. The board is 16 by 16, so we have 256 positions per robot which can conveniently be stored in a single byte. The five robot-position bytes can then be stored in a mapping (at first I used a hash map but eventually switched to a more exotic trie-like structure) without using too much memory.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">This caching saves a <strong style="box-sizing:border-box;margin:0">ton</strong> of work. Most possible moves are independent of other robots, so a sequence of two moves likely results the same board state as making those moves in the opposite order. Removing the order of these independent moves from the search space cuts it down to a small fraction of what the full set of possible moves would be.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">On an early version of the solver this brings the solve time for a 10 move puzzle down from 202 s and almost 100 GiB of memory to 105 ms and 10 MiB of memory. On the latest solver a 12 move puzzle goes from 217 s and 50 GiB of memory to 79 ms and 30 MiB of memory. (Larger puzzles just become infeasible) It is unsurprising that this is so effective. Duplicate states are common and both of these solvers have to store a list of visited states. So if a state is seen even twice the memory usage breaks even, and every state past that is pure profit. Runtime is harder to estimate, but these lookups are fairly inexpensive and given the exponential growth that is avoided it pays off very quickly.</p>
<h2 id="search-algorithm-search" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#search-algorithm-search" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Search Algorithm Search</a></h2>
<h3 id="breadth-first-search" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#breadth-first-search" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Breadth-First Search</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The first step was to pick an algorithm. At first I thought I could just use breadth-first search. Despite the high possible fanout I assumed that most states would be redundant, and it would give the shortest solution first.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">This resulted in a decent solver. It can solve a <a href="https://gitlab.com/kevincox/ricochetrobots/-/blob/master/games/17.txt" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">17 move puzzle</a> in 1min 52s. Puzzles that require 17 moves or more are very rare, so this makes the solver usable for general play.</p>
<h3 id="depth-first-search" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#depth-first-search" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Depth-First Search</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Next I experimented with an iterative depth-first solver. While it was slower than the breadth-first solver it was still useful because it used far less memory. This allowed it to solve more puzzles when compiled to WebAssembly without hitting the browser-imposed memory limit or the fundamental memory limit of 32-bit WASM. So I added it as an alternative option in the web interface. Generally I would try the breadth-first option first, but if that ran out of memory I would give this solver a try, and it did occasionally find a solution for these harder problems after locking up the browser for an extended period.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The Depth-First Search solver could solve the same 17 move puzzle in 5min 24s.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">I also doubled-down on the depth-first solver memory use by removing visited state caching. This meant that it used more or less a constant amount of memory, allowing it to solve any puzzle …eventually… Unfortunately it was unbearably slow for any puzzle in which memory usage was a problem to begin with.</p>
<h3 id="astar" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#astar" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">A*</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">This is where things really took a step up. Breadth-first and depth-first search are quite simple algorithms and search in a mostly random manor. <a href="https://en.wikipedia.org/wiki/A*_search_algorithm" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">A*</a> is a variant of breath-first search that searches the most likely options first. I’m not going to give a full explanation of A* here but the basic concept is that while breath-first search is constantly taking another step on the shortest path A* takes a step on the path that has the shortest possible solution. This is done by taking the current path length and then adding an <em style="box-sizing:border-box;margin:0">estimate</em> of how many more moves are required to reach the target. The most important thing is that this estimate can’t be too high. That is if we estimate 4 moves than it must be <strong style="box-sizing:border-box;margin:0">impossible</strong> to reach the target in less than 4 moves. We can then sort our exploration based on this estimated total path length.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">It may seem hard to estimate. How can we know the length of a path that we haven’t found yet? But it turns out that this estimate is incredibly helpful even if it is quite conservative. For Ricochet Robots one possible estimate is what is the minimum number of turns that are needed if we can stop wherever is most convenient and if we can pass though other robots if required. Basically we assume that all the other robots are right where we need them and take the shortest path.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Here is an example estimate for the red robot. We can see that this path is clearly not possible in 4 moves, as it stops before hitting a wall twice then proceeds directly through another robot. However it will serve as a lower bound.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/optimistic-eKZZA-.svg" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e4881sb9cux6xple7ut7.png" alt="Image of an optimistic path." style="box-sizing:border-box;display:block;margin:auto;max-width:100%;min-width:60%"></a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">One nice feature of this estimate is that it only depends on static puzzle sate and the position of the target robot. So we can simply precompute the estimate for every cell, then scoring board positions is just a table lookup based on the location of the target robot.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/a/estimate--E77aP.svg" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray"><img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kveqds449hm0lkagftkg.png" alt="A grid of estimate scores." style="box-sizing:border-box;display:block;margin:auto;max-width:100%;min-width:60%"></a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">With that table computed we can quickly see what the minimum score for a possible solution is, even though we haven’t completed the search. If we have already taken 3 moves and the red robot is on a 4 cell we know that a complete solution will take at least 7 moves.</p>
<h4 id="colours-and-estimates" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#colours-and-estimates" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Colours and Estimates</a></h4>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">In general you only need to check the robot that is the same colour as the target against the estimate as it is the only one that <em style="box-sizing:border-box;margin:0">needs</em> to move to finish the puzzle. However the wild target is different, any robot can be moved there to complete the puzzle. For wild targets you need to check every robot and see the minimum of the minimum move scores. However if gets even more complicated when there are mirrors on the board. Mirrors mean that different colour robots have different movement options. A mirror could block a robot and raise its minimum or it could give the robot a “free” turn to lower it. So for puzzles with mirrors and a wild target you need to compute a different estimate map for every robot.</p>
<aside style="background:#333;box-shadow:4px 4px 8px #000;box-sizing:border-box;color:#fff;grid-column:2;margin:.2em 1em .6em"><p style="box-sizing:border-box;margin:.4em">Right now I actually always calculate one estimate map per robot. They are very cheap to compute as they only need to be done once per board. For coloured targets only one map is ever accessed so the overhead is likely negligible. It wastes 1 kiB of stack space but in order to save that we would need to dynamically allocate so that is likely a good tradeoff. For colourless targets on boards without mirrors there is likely a small performance loss because all of these mappings will be used even though they are the same. Cache hit rate could be improved by using one map for all robots on boards without mirrors.</p></aside><h2 id="board-storage" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#board-storage" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Board Storage</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The next factor was how to represent the puzzle in memory. The solver will continuously access the game board so it was important to be fast to access. Size was also important as the smaller this data is the more likely it is to be in the processor’s cache.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The original schema was pretty straight-forward.</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">struct</span> <span style="box-sizing:border-box;margin:0;padding:0">Game</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;margin:0;padding:0">horizontal_walls</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;margin:0;padding:0">u16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
	<span style="box-sizing:border-box;margin:0;padding:0">vertical_walls</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;margin:0;padding:0">u16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
	<span style="box-sizing:border-box;margin:0;padding:0">target</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">Target</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
		<span style="box-sizing:border-box;margin:0;padding:0">color</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">Option</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&lt;</span><span style="box-sizing:border-box;margin:0;padding:0">Robot</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;</span> <span style="box-sizing:border-box;color:#ccc;font-style:italic;margin:0;padding:0;white-space:pre-wrap">/* 1 byte */</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
		<span style="box-sizing:border-box;margin:0;padding:0">position</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">Position</span> <span style="box-sizing:border-box;color:#ccc;font-style:italic;margin:0;padding:0;white-space:pre-wrap">/* 1 byte */</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">},</span>
	<span style="box-sizing:border-box;margin:0;padding:0">mirrors</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[[</span><span style="box-sizing:border-box;margin:0;padding:0">Mirror</span> <span style="box-sizing:border-box;color:#ccc;font-style:italic;margin:0;padding:0;white-space:pre-wrap">/* 1 byte */</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">];</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
	<span style="box-sizing:border-box;margin:0;padding:0">robots</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;margin:0;padding:0">Position</span> <span style="box-sizing:border-box;color:#ccc;font-style:italic;margin:0;padding:0;white-space:pre-wrap">/* 1 byte */</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">5</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">This is pretty close to optimal space usage for arbitrary boards. There are only a few inefficacies:</p>
<ol style="box-sizing:border-box;grid-column:2;margin:.4em">
<li style="box-sizing:border-box;margin:0">Mirrors use 1 byte each. There are only <span style="box-sizing:border-box;margin:0;padding:0"><math xmlns="http://www.w3.org/1998/Math/MathML" style="box-sizing:border-box;margin:0"><semantics style="box-sizing:border-box;margin:0"><mrow style="box-sizing:border-box;margin:0"><mtext style="box-sizing:border-box;margin:0"> </mtext><mrow style="box-sizing:border-box;margin:0"><mi mathvariant="normal" style="box-sizing:border-box;margin:0">a</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">n</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">g</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">l</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">e</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">s</mi></mrow><mo style="box-sizing:border-box;margin:0">×</mo><mtext style="box-sizing:border-box;margin:0"> </mtext><mrow style="box-sizing:border-box;margin:0"><mi mathvariant="normal" style="box-sizing:border-box;margin:0">c</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">o</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">l</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">o</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">r</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">s</mi></mrow><mo style="box-sizing:border-box;margin:0">+</mo><mtext style="box-sizing:border-box;margin:0"> </mtext><mrow style="box-sizing:border-box;margin:0"><mi mathvariant="normal" style="box-sizing:border-box;margin:0">a</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">b</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">s</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">e</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">n</mi><mi mathvariant="normal" style="box-sizing:border-box;margin:0">t</mi></mrow><mo style="box-sizing:border-box;margin:0">=</mo><mn style="box-sizing:border-box;margin:0">2</mn><mo style="box-sizing:border-box;margin:0">×</mo><mn style="box-sizing:border-box;margin:0">5</mn><mo style="box-sizing:border-box;margin:0">+</mo><mn style="box-sizing:border-box;margin:0">1</mn><mo style="box-sizing:border-box;margin:0">=</mo><mn style="box-sizing:border-box;margin:0">11</mn></mrow><annotation encoding="application/x-tex" style="box-sizing:border-box;margin:0">\u{angles} \times \u{colors} + \u{absent} = 2 \times 5 + 1 = 11</annotation></semantics></math></span>possibilities which rounds up to 4 bits. So 4 wasted bits per mirror, 128 bytes in total.</li>
<li style="box-sizing:border-box;margin:0">The target color only needs 6 options which rounds up to 4 bits. 4 wasted bits.</li>
<li style="box-sizing:border-box;margin:0">There are only really 15 possible walls per row and column, the first and last wall are always present. 4 wasted bytes.</li>
</ol>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">However performance isn’t great. Checking if a robot can move forward a square requires checking:</p>
<ol style="box-sizing:border-box;grid-column:2;margin:.4em">
<li style="box-sizing:border-box;margin:0">Is there a wall in the way. (1 bit lookup by index)</li>
<li style="box-sizing:border-box;margin:0">Is there a mirror in the target location? (1 byte lookup by index)</li>
<li style="box-sizing:border-box;margin:0">Is there a robot in the target location? (search 5 bytes)</li>
</ol>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">It sounds small but moving the robot does these checks for each cell moved and moving is the most frequent operation in the game.</p>
<h3 id="obstacle-bitmap" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#obstacle-bitmap" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Obstacle Bitmap</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The first optimization was to add a fast lookup for obstacles.</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre">	<span style="box-sizing:border-box;margin:0;padding:0">obstacles</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;margin:0;padding:0">u16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">One bit per cell is a small price to pay. Every robot and mirror was set in this table. Now in most cases we could get a negative result for both robot and mirror collisions quickly. This made a nice improvement to performance, but would eventually be superseded.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">One oversight was that disambiguation between a robot and a mirror was done by checking the mirror map. If there was no mirror the collision must have been a robot. This resulted in relatively frequent access to the mirror map even if there were no mirror there. This was done to avoid searching the robot list but was likely too high a price to pay. Especially for puzzles with no mirrors.</p>
<h3 id="per-direction-obstacle-map" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#per-direction-obstacle-map" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Per-Direction Obstacle Map</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">A failed optimization attempt was to merge walls into the obstacle map. The first issue is that walls are “between” cells. So they didn’t fit naturally into the bitmap. If a cell was blocked depended on what direction the robot was moving from.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">This resulted in an attempt to build an obstacle bitmap for each direction. This way a 0 in the bit for a cell was a guaranteed free cell. It also means that we could use bit counting counting instructions to tell not just if a cell was free quickly, but <strong style="box-sizing:border-box;margin:0">how many</strong> cells are free in that direction. So instead of moving and checking one cell at a time we could directly identify the target cell (or the mirror that we would bounce on). Surely this change from iteration to direct jumping would improve performance.</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">fn</span> <span style="box-sizing:border-box;margin:0;padding:0">move_distance</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">lane</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">u16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">pos</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">i</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">-&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">u8</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">lane</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">pos</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span><span style="box-sizing:border-box;margin:0;padding:0">.trailing_zeros</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">()</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">as</span> <span style="box-sizing:border-box;margin:0;padding:0">u8</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Unfortunately this didn’t end up improving performance. I may want to revisit in the future as it seems like it should work, but benchmarks showed ~10% slowdown. It turns out that computers can run simple loops very fast! While I don’t have a detailed analysis on the slowdown I have a few ideas:</p>
<ol style="box-sizing:border-box;grid-column:2;margin:.4em">
<li style="box-sizing:border-box;margin:0">Normalizing the <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:0 .2em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">pos</span></code> to be relative to the direction required some branching. (Could likely be optimized.)</li>
<li style="box-sizing:border-box;margin:0">Still looked into the <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:0 .2em;white-space:pre-wrap"><span style="box-sizing:border-box;margin:0;padding:0">mirrors</span></code> map on a hit.</li>
<li style="box-sizing:border-box;margin:0">Moving a robot required updating 4 obstacle maps. Updating these maps also required checking a reference wall map to determine if removing a robot cleared an obstacle (as moving a robot behind a wall should not clear the obstacle bit). So the regular wall maps were still being used during solving.</li>
</ol>
<h3 id="cell-oriented-map" style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#cell-oriented-map" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Cell-Oriented Map</a></h3>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">At this point I tried to grow my obstacle map to encompass everything required for moving through a cell. This means I would pack the following into one byte:</p>
<ol style="box-sizing:border-box;grid-column:2;margin:.4em">
<li style="box-sizing:border-box;margin:0">All 4 walls.</li>
<li style="box-sizing:border-box;margin:0">If a robot is present.</li>
<li style="box-sizing:border-box;margin:0">Full information about a mirror.</li>
</ol>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Packing this into 8 bits isn’t obvious, but it is possible without any hackery.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">4 walls are simply one bit each. Without assuming anything about the board we have to assume that every combination is possible. This does duplicate every wall (as the east wall of this cell is the west wall of the next cell) but we wanted to have all the info we needed in one place.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The next 4 bits pack in robot presence and mirror info. Importantly a robot can’t be on a mirror cell. So we use this exclusivity to pack the data into one nibble (values 0-15). We reserve one value (15) for an empty cell, and one value (14) for a cell with a robot in it. The remaining 14 values are used for mirror information. A mirror is defined by the lean (top-left to bottom-right or top-right to bottom-left) and robot colour. The lean is one bit, so we are left with 7 options for robot colour. Luckily we only have 5 robots (the original game has no black mirrors, but we support it) so it fits with a few values to spare.</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">enum</span> <span style="box-sizing:border-box;margin:0;padding:0">Contents</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span> <span style="box-sizing:border-box;margin:0;padding:0">Empty</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">Robot</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">Mirror</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">Mirror</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>

<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">fn</span> <span style="box-sizing:border-box;margin:0;padding:0">parse_cell</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">cell</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">u8</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">-&gt;</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">Walls</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">Contents</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;margin:0;padding:0">walls</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">Walls</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">from_u4</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">cell</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">0x0F</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">);</span>
	<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;margin:0;padding:0">contents</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">match</span> <span style="box-sizing:border-box;margin:0;padding:0">cell</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;&gt;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">4</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
		<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">15</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">=&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">Contents</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">Empty</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
		<span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">14</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">=&gt;</span> <span style="box-sizing:border-box;margin:0;padding:0">Contents</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">Robot</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
		<span style="box-sizing:border-box;margin:0;padding:0">mirror</span> <span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">=&gt;</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
			<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;margin:0;padding:0">left_leaning</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">mirror</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&amp;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">0x01</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">!=</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">0</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span>
			<span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">let</span> <span style="box-sizing:border-box;margin:0;padding:0">color</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">=</span> <span style="box-sizing:border-box;margin:0;padding:0">Robot</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">try_from</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">mirror</span> <span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">&gt;&gt;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">1</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span><span style="box-sizing:border-box;margin:0;padding:0">.uwnrap</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">();</span>
			<span style="box-sizing:border-box;margin:0;padding:0">Contents</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">Mirror</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">Mirror</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">::</span><span style="box-sizing:border-box;margin:0;padding:0">new</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">left_leaning</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">color</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">))</span>
		<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">};</span>
	<span style="box-sizing:border-box;color:#999;margin:0;padding:0">(</span><span style="box-sizing:border-box;margin:0;padding:0">walls</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span> <span style="box-sizing:border-box;margin:0;padding:0">contents</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">)</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://gitlab.com/kevincox/ricochetrobots/-/commit/b058d21b481f5879dee093f4dc64e15f2a7bee27" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">This was surprisingly effective</a>, resulting in a 15% performance improvement. With this we could completely remove the previous wall representation and mirror map from the solver. This reduced our core data to only 263 bytes:</p>
<div style="background:#222;border-color:#444;border-style:solid;border-width:1px 0;box-sizing:border-box;display:grid;grid-column:2;grid-column:1/4;grid-template-columns:1fr var(--width) 1fr;margin:0;overflow-x:auto;padding:0"><pre style="box-sizing:border-box;grid-column:2;margin:.4em"><code style="background:none!important;box-sizing:border-box;color:#fff;margin:0;white-space:pre-wrap;white-space:pre"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">struct</span> <span style="box-sizing:border-box;margin:0;padding:0">Game</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">{</span>
	<span style="box-sizing:border-box;margin:0;padding:0">board</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[[</span><span style="box-sizing:border-box;margin:0;padding:0">u8</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">];</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">16</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span> <span style="box-sizing:border-box;color:#ccc;font-style:italic;margin:0;padding:0;white-space:pre-wrap">// Contains walls, mirrors and obstacle map.</span>
	<span style="box-sizing:border-box;margin:0;padding:0">target</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;margin:0;padding:0">Target</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">,</span>
	<span style="box-sizing:border-box;margin:0;padding:0">robots</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">:</span> <span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;margin:0;padding:0">Position</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">;</span> <span style="box-sizing:border-box;color:#ffb433;margin:0;padding:0">5</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">],</span>
<span style="box-sizing:border-box;color:#999;margin:0;padding:0">}</span>
</code></pre></div>
<h2 id="improved-a-estimate" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#improved-a-estimate" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Improved A* Estimate</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The previous estimate was very cheap to calculate, but not particularly accurate. To make it more accurate a penalty was added if the minimum path isn’t possible. If the minimum path requires mid-air turns or passing through a robot the minimum solution is clearly unobtainable. At least one extra move will be required to move a blocker into or out of your path. I couldn’t find an easy way to make the penalty more than one, it seems like doing so would be essentially solving the optimal path again.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">So this path-check is much more expensive and can only improve the estimate by a single point, is it actually an improvement? <strong style="box-sizing:border-box;margin:0">Absolutely</strong>! <a href="https://gitlab.com/kevincox/ricochetrobots/-/commit/e375598a4e4c3e273ef8937ef4b4b09d23dbe46c" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">This change</a> made some complex puzzles solve in half the time! This serves as a great reminder than even minor algorithmic improvements can trounce tons of micro-optimizations.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Making this estimate even better is a clear area for future optimizations. I think the current estimate is pretty good but even minor improvements will provide great value.</p>
<h2 id="robot-sorting" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#robot-sorting" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Robot Sorting</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">In <a href="https://speakerdeck.com/fogleman/ricochet-robots-solver-algorithms?slide=10" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Michael Fogleman’s talk about his solver</a> uses a technique of sorting all of the robots. This makes the <a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#visited-state-caching" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Visited State Caching</a> more effective by making equivalent states identical. There is one important complication though, Michael is solving an earlier edition of the game without mirrors. I actually had a simplified sorting system for the depth-first search solver to prefer moving the target robot, but it was removed when mirror support was added. Without mirrors robots are identical (other than the robot that matches a coloured target). With mirrors each robot is unique as the possible moves it can make depend on its colour. I decided to try sorting anyway since many puzzles don’t have mirrors (or only have a couple).</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">The implementation is quite simple. I “pin” any robot that is the same colour as the target or any mirror. All of the unpinned robots are then sorted before being put into the visited state cache. Some care then needs to be taken to “unsort” the robots when generating the solution.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">To make this a little more efficient I sort the pinned robots to the start of the robot list before starting solving. The target and mirror colours are then updated to match the changes. This is done once per game, then while solving I just <code style="background:#000;border:1px solid #555;border-radius:.3em;box-sizing:border-box;color:#fff;margin:0;padding:0 .2em;white-space:pre-wrap"><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">self</span><span style="box-sizing:border-box;margin:0;padding:0">.robots</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">[</span><span style="box-sizing:border-box;color:#8585e0;font-weight:700;margin:0;padding:0">self</span><span style="box-sizing:border-box;margin:0;padding:0">.pinned_robot_count</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">()</span><span style="box-sizing:border-box;color:#f5a3a3;font-weight:700;margin:0;padding:0">..</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">]</span><span style="box-sizing:border-box;margin:0;padding:0">.sort</span><span style="box-sizing:border-box;color:#999;margin:0;padding:0">()</span></code> to sort the unpinned robots.</p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em"><a href="https://gitlab.com/kevincox/ricochetrobots/-/commit/4929e20bbcd6eb0d47d5092183bcf41e1edbfd41" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">Robot sorting</a> had some big improvements but it depended a lot on how many robots were pinned.</p>
<ul style='box-sizing:border-box;grid-column:2;list-style:disc;list-style:"- ";margin:.4em'>
<li style="box-sizing:border-box;margin:0">Games with a wild target and no mirrors saw 50% improvement. (No robots pinned)</li>
<li style="box-sizing:border-box;margin:0">Games without mirrors saw 30% improvement. (One robot pinned)</li>
<li style="box-sizing:border-box;margin:0">Games with all robots pinned saw no significant slowdown.</li>
</ul>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">Since no games were slowed down it made sense to just enable this for all games.</p>
<h2 id="summary" style="box-sizing:border-box;grid-column:2;margin:1.5em .4em .4em"><a href="https://kevincox.ca/2023/09/02/ricochet-robots-solver/#summary" style="box-sizing:border-box;color:inherit!important;margin:0;text-decoration:inherit;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray;text-shadow:inherit!important">Summary</a></h2>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">After these optimizations I have created the fastest Ricochet Robots solver that I am aware of. You can <a href="https://ricochetrobots.kevincox.ca/" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">try it online</a> or <a href="https://gitlab.com/kevincox/ricochetrobots" style="box-sizing:border-box;margin:0;text-decoration:underline;-webkit-text-decoration-color:gray;text-decoration-color:gray">browse the source code</a></p>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">To summarize my approach:</p>
<ul style='box-sizing:border-box;grid-column:2;list-style:disc;list-style:"- ";margin:.4em'>
<li style="box-sizing:border-box;margin:0">Cache visited states, there is a huge amount of redundancy.</li>
<li style="box-sizing:border-box;margin:0">A* search with an estimate function based on being able to turn at any point.<ul style='box-sizing:border-box;list-style:disc;list-style:"- ";margin:0'><li style="box-sizing:border-box;margin:0">One penalty move if the route isn’t possible.</li></ul>
</li>
<li style="box-sizing:border-box;margin:0">Sort robots to erase any insignificant colours.</li>
<li style="box-sizing:border-box;margin:0">Minimize working set size and try to keep cache locality.</li>
</ul>
<p style="box-sizing:border-box;grid-column:2;margin:.4em">That’s all for now. If you haven’t played yet you should, it is my favourite game. (Although I think it is out of print so it may be hard to find.) If you do, try my solver on some of the harder puzzles, it can help you learn new techniques.</p></main>