r/AskComputerScience • • Mar 14 '21

Is this problem intractable?

So some background, there's a game called Loop Hero. We don't need to get too into the details of it, but there's an optimization problem in the game that basically works as follows:

  • Given an m x n region, place "thickets" and "rivers" to generate the highest possible score.

  • A thicket which has no rivers above, below, left, or right of it, is worth 2 points.

  • A thicket with 1 adjacency is worth 4 points.

  • A thicket with 2 adjacency is worth 8 points.

  • A thicket with 3 adjacency is worth 12 points.

  • A thicket with full (4) adjacency is worth 16 points.

  • A river must begin on the border of a region and it cannot branch, in other words, you can't ever backtrack on a river.

This app (which doesn't enforce the river placement rules) will give you a very intuitive feel for the problem: https://loopherolayout.xyz/#

I wrote an algorithm to attempt to solve this problem via brute force, but I believe the time-complexity of my solution is O(n!) and so solving the problem for even a 5x12 region seems intractable (I've been searching on 8 threads for 6+ hours and no threads have finished a full search from their starting point). The code can be found here: https://github.com/Lutzy/LoopHeroThicketSolver/blob/master/src/com/lutzy/loophero/solver/Solver.java

The best my algorithm has found so far yielded 224 points: https://loopherolayout.xyz/?c=111221211211211211212211211212212112112122121211212121121112 but then someone was able to modify that solution by hand to come up with a 234 point solution, but we've still no idea if that's optimal: https://loopherolayout.xyz/?c=111221211211211211211211211211211212212121121112111211211122

(Notice what a valid river looks like, you can't simply create a checkerboard, it must begin on one edge of the region and be contiguous with no backtracking.)

Does anyone have any ideas on if there are possible improvements to this algorithm? I attempted to memoize it by storing the score of the board in a ConcurrentHashMap, but very quickly had a 30 GB+ Map and even for much smaller areas where the non-memoized implementation sped through them, the memoized version struggled.

The only optimization I could think of was that in the case of the 5x14 problem, we only need to exhaustively search 8 unique starting positions, as everything else should be a mirror of these 8 marked positions: https://loopherolayout.xyz?c=111001000010000100001000010000000000000000000000000000000000

I've sort of got a feeling there is no good known algorithm for something like this, but I found the problem interesting enough from a computer science perspective that I wanted to get other peoples' thoughts. Thanks!

29 Upvotes

9 comments sorted by

5

u/teraflop Mar 14 '21

You can solve this problem with dynamic programming. This approach still isn't tractable for large board sizes boards that are large in both dimensions, but it's much better than brute force.

First, let's simplify the problem and get rid of the branching constraint on rivers, so that we can arbitrarily choose each square to be empty, a river, or a thicket. If we first consider the case of an Mx1 board with one row, we have 3N possibilities that we can brute-force.

Extending this to an Mx2 board, for each of the 3N possibilities of the top row, we can try all 3N possibilities for the bottom row, and calculate the total score. This involves testing 32M boards which is still basically brute force.

But now let's look at an Mx3 board. We could take the same approach -- try all possible combinations of an Mx2 board with all possible choices of the bottom row. But we can do better by memoizing, for each possible choice of the second row, the maximum possible score achievable from the first two rows. If we know what's in the second row, that's enough to determine how many points are added by the third row; what's in the first row doesn't matter any more, except insofar as it adds to the score.

Now we just keep doing the same thing: for every row, we can "collapse" all of the boards that have the same state for that row, and only consider the best score for each state when choosing the next row. By doing this optimization, we reduce the number of boards that we need to check from O(3MN) to O(N 3M). At each step, we need enough memory to store the best store for each of the 3M possible last rows. Fortunately, 35 isn't a very big number.

Now, this gets trickier when you need to ensure that rivers don't branch. In order to determine which states are valid for a given tile, you need to know what rivers it might be connected to. For example, this 5x3 board and this one have the same 3rd row, but they differ in terms of which tiles on the 4th row can legally contain rivers. So you need to track extra information for each row about which river tiles are connected to each other through a path that travels through previous rows, or directly to a river tile on the previous row, or indirectly to a border. This adds somewhat to the number of combinations, but it still only depends on the width of the board, not the total size.

1

u/RLutz Mar 14 '21

This is perfect. Yeah, I was memoizing the wrong thing. We're hand-waving a bit about the branching thing, sure, but this seems like something I could run with. Thanks!

2

u/RicardoBrancas Mar 14 '21

What you are doing sounds like the perfect task for a MaxSAT solver!

2

u/RLutz Mar 14 '21

Yeah, I was willing to bet my hat that this was NP-hard. I did think the best thing might be to try and add heuristics (we know little diagonals of 16 are "good")

I don't think I'm invested enough in this problem to actually do that, but that was the only thing I thought stood a chance of improving performance

2

u/onemanandhishat Mar 14 '21

Seems like something you could write as an integer programming problem. The scoring is quite simple, the challenge would be to write the equations governing the river. Since it would be integer, the problem would be NP-hard, but solvers like cplex would do much better than your brute force method. There are also heuristic algorithms that can give decent performance on integer problems and will give you a good solution, albeit not guaranteed to be optimal.

So maybe it could be done by formulating it as an integer score maximization problem and then applying known algorithms to solve it.

1

u/TransientVoltage409 Mar 14 '21

I may not be understanding fully, but it looks like the grid has 2^(MN) possible states, the vast majority of which are not valid. A few basic algorithms would be able to weed out invalid grids quickly. A union-find will identify a non-contiguous river, spanning-tree should reveal any river loops, and...well, the rules for "begin" and "end" seem a little ill-defined, but you might say that a river block, not on an edge, with only one river neighbor, would constitute an endpoint not on the edge and thus an invalid grid.

Not saying this would be fast, but I think there are ways that are much faster than O(n!).

1

u/trocar Mar 14 '21

Not sure to understand the rules for the rivers. Is this a valid instance https://loopherolayout.xyz?c=211221211111212211211211211211211211211211211211222112112112

1

u/TehDragonGuy Mar 14 '21

I think you can only have a single river (ie all river tiles must be connected).