Block the Spreading Fire
- Problem
- A fire spreads across a grid of trees and houses. I can cut one firebreak at a time, each cut takes several turns, and the score is the value that doesn't burn.
- Hard limit
- About 95 ms of search per turn after a longer first turn. The number of possible cut orders is far too large to check them all.
- Approach
- Search for the region of the map worth saving, not for a list of cuts. The cuts and their order come from the region's boundary. Hill climbing finds a first region, then simulated annealing (a search that sometimes accepts a worse option, less often as time runs out, so it doesn't get stuck) improves it.
- Result
- 25th of 855 on the puzzle's leaderboard. When I wrote up the approach on the CodinGame forum in 2022 it was ranked 4th.
- What it shows
- How the problem is represented matters more than the search built on top of it. A random list of cuts almost never scores, so the search would spend its time on nothing.
- In the replay
- The white outline marks the region the cuts end up protecting, so you can see where the plan is heading from the first turn. Turn it off to watch only the fire and the cuts.
Replays
Below are recorded runs of my solution on six of the puzzle’s test cases. The page replays the moves my program printed; it doesn’t run any search. The fire is simulated in the browser from the puzzle rules, and the final score it reaches matches the one CodinGame reported for each run. The program doesn’t print the region it chose, so the outline is worked out from the end of the simulation: the cells that are still untouched once the fire is out. That is the region the printed cuts protect.
- Turn
- Value intact
- Cooldown
- Final score
- Tree
- House
- Burning
- Burnt
- Cut
- Saved region
Keyboard: focus the grid, then use the left and right arrows to step, space to play or pause, and Home or End to jump to the start or finish. The animation starts by itself unless your system asks for reduced motion.
The problem in full
A fire starts on a grid of trees and houses and spreads to the four neighbouring cells each time a burning cell finishes burning. Each turn you can name one cell to cut, which makes it safe at once, but cutting takes several turns and you can’t cut anything else until it’s done. The score is the value of the cells that neither burned nor got cut, so the job is to decide which firebreak to build, and in what order, before the fire gets there.
How my solution works
This is my explanation from the CodinGame forum, posted in July 2022 when the solution was ranked 4th, with light edits.
The most significant part of my algorithm is how I represent a candidate solution. Representing candidate solutions as a sequence of random cuts is very unlikely to yield any points, or at least to make effective use of the cuts. All sequences of cuts that generate some points have something in common: they disconnect sections of the map from the fire.
Therefore I represent my candidate solutions as a set of cells on the map that I want to save (be disconnected from where the fire is). From this I can determine a few things:
- The perimeter around the set of cells, which will become the target cells to cut.
- The order in which the cuts must be made, or whether there is no such order that can prevent the fire from reaching any of the saved cells. In that case, the solution is discarded.
- The total value of the cells that have been saved from the fire.
I initially used a simple hill climbing approach, as follows:
- Choose a random starting point on the map (optionally weighted by value).
- Check if there is a sequence of cuts that can be made to save that cell. If not, start again.
- While there is time left, and the number of iterations since the last improvement is below a maximum:
- Choose a perimeter cell to convert to a saved cell (or, with a very low probability, add a random cell on the map instead. This allows multiple disconnected regions to be saved).
- Check if there is a sequence of cuts that can be made to save the new set of cells. If not, don’t accept the new cell.
This approach was already quite effective. Regarding performance, I could get around 60-80k iterations (of the inner loop) for the 7th test case.
The improvements that got my solution to 4th:
-
I removed redundant iterations that were caused by the process of randomly selecting cells. When choosing a random cell to add, I now randomly iterate through all possible cell indices, rather than randomly generating an index over and over. This means that I only check a cell once at most, and I will check every cell before giving up with the current solution (or accepting a valid cell if found).
-
I spent the last 500ms of the initial turn and about 95ms of every future turn using simulated annealing to try to improve my current best solution. The hill climbing approach will often get stuck in a local maximum, so the goal of using simulated annealing was to get past it.
I randomly remove saved cells, and randomly add saved cells, with adding cells having a higher probability than removing them (because more saved cells generally means better value). I only accept states that are valid. I also restart the annealing schedule every 20 or so iterations so it does not traverse too far away from the current best solution.
My previous score on the 7th test case was almost always 7523, but with simulated annealing it is almost always 8622. On other test cases it usually only offers a small improvement or none.
Back to CodinGame and the other post-mortems.