gotnull

Part 11 of 13 in Ghostveil

Finding rooms that need both players

Random search found 7 co-op rooms in four five-minute runs. Repairing the near misses, and aiming each repair at the solver's own shortcut, found 39 in the same time.

Ghostveil's co-op rooms put two heroes on one small board, each moved by a different player, sharing one undo and one move count. A room is only worth playing together if it actually needs both of them. A room can look co-operative and still let one hero throw the lever every time it needs throwing, and the solver can tell in a few milliseconds.

The rooms in question are small lever rooms: walls, one lever, two kinds of rune pool, the two heroes, the stairs and sometimes a stone to push. Walking into the lever throws it. A pool of one kind is full until the lever is thrown, and a pool of the other kind is full only while it is thrown. A drained pool is a pit nobody may stand in, so every throw opens one way and closes another.

What "needs both players" means

The solver works over both heroes' positions, the pieces and the lever together, and finds the fewest moves to bring both down the stairs. Run again with a rule added, it answers the questions that make a room co-op. Forbid hero 2 from touching the lever and the room must become impossible, and the same the other way round, so each hero has to throw it at least once.

Two more checks count. Every solution must change the lever at least twice and hand over, change who is moving, at least three times.

The fourth check came from playing rather than from the rules. A room can need both players and still, in its fairest solution at par, leave one of them watching for eighteen moves while the other walks, so no player may be left waiting more than four.

The first search made rooms at random and threw away every one that failed. It tries about 450 rooms a second on one core. In an earlier batch, four five-minute runs tried 548,000 rooms; about 3,400 a run were solvable with a par of at least 14, and ten passed everything. One room in 55,000.

Three rooms from these random searches are in the game now.

The search had a shape I didn't like, though. A room that solved and failed only the hand-over check was thrown away exactly like a room with no solution at all, so thousands of rooms a run solved, missed one check or two, and were discarded without a second look.

Repairing near misses

So the second search keeps them. A pool of twelve rooms that solve but fail some check is changed one small step at a time: a wall cell opened or closed, or the lever, a pool cell, a hero, the stairs or a stone moved somewhere else. A change is kept when the room is no further from passing than before.

"Further from passing" needs a number. Each check contributes how far short it falls:

d=3 [1 alone]+3 [2 alone]+1.5max⁡(0, 2−L)+max⁡(0, 3−H)+0.5max⁡(0, W−4)+max⁡ ⁣(0, P−455)\begin{gathered} d = 3\,[\text{1 alone}] + 3\,[\text{2 alone}] + 1.5\max(0,\,2 - L) \\ {} + \max(0,\,3 - H) + 0.5\max(0,\,W - 4) + \max\!\left(0,\,\tfrac{P - 45}{5}\right) \end{gathered}

Here dd is the distance from passing, each bracketed term is 1 when that hero can finish the room throwing the lever alone and 0 otherwise, LL is the fewest lever changes, HH the fewest hand-overs, WW the fairest solution's longest wait and PP the par. Rooms with no solution, or a par under 14, are not scored at all; for the rest, d=0d = 0 means every check passes. The heaviest weight goes on the lone thrower, since a room one hero can finish alone is not a co-op room.

A slot that goes 400 changes without getting nearer starts again from a fresh random room, and so does one that passes, once it has been handed out, so one family of rooms does not fill the output. A room that differs from one already found in three cells or fewer does not count as new.

Aiming at the shortcut

The solver does more than say a room failed. When hero 1 can throw the lever alone, the solver has the route that does it. That route is a counterexample: a specific way round the rule. A change made at random mostly lands somewhere that route never goes.

So the scoring hands back these routes along with the distance, replayed into the list of cells each one crosses. About a third of the changes are aimed: one of those cells is picked and something is put there, a wall or one of the pools moved onto it, so the way opens only with the lever one way round.

final targets = [for (final w in room.witnesses) ...w];
final aim = targets.isNotEmpty && rng.nextDouble() < aimed; // aimed = 0.35
final child = aim
    ? block(room.map, targets[rng.nextInt(targets.length)], rng)
    : mutate(room.map, rng);

The route with the fewest hand-overs goes into the same list when a room hands over too seldom, since that route shows how the two heroes avoid needing each other.

Here is one room's repair, every kept change in order. For nineteen changes the search shuffles pools, walls and the lever about, and gets from a distance of 11 to 9.5. One aimed change finishes it. A pool moves onto the bottom corridor that both heroes' lone routes run along, and the room passes every check at once, with three lever changes, five hand-overs and a par of 31.

The filled pool is full until the lever is thrown; the outlined one fills only while it is thrown. The coloured dots are where each hero walks to throw the lever alone.

The race

Three searches, the same four seeds each, run side by side on an Apple M2 for five minutes: random search, the near-miss search with no aimed changes, and the near-miss search with them.

Bar chart of rooms passing every check in four five-minute runs: random search 7, near-miss search 21, near-miss search with aimed changes 39.

Random search found 7 rooms, the near-miss search 21, and with aimed changes 39. I ran all 39 back through the separate room checker and every one passes, with pars from 17 to 45.

The runs vary a lot. The aimed search found 12, 7, 2 and 18, and on the third seed it found fewer than the search without aiming. Over four runs it is well ahead in total, but four runs can't say by how much.

One number I can't fully explain yet. Aimed changes were 35% of the changes tried and 40% of the changes that improved a room, which makes an aimed change only about 1.24 times as likely to help as a random one. That alone does not account for nearly doubling the rooms found. My guess is that aimed changes are worth more when they land, removing a whole shortcut where a random change shaves off a hand-over, but I haven't measured it.

Stopping early

Scoring a room costs about 72 milliseconds. Measured over 223 rooms, the plain solve took 14 ms, each lone-thrower solve about 12 ms, the hand-over search 16 ms and fairness 17 ms; the lever changes take 1 ms.

Since a change is kept only if it is no further from passing than its parent, and every term of dd only adds, scoring can stop the moment the running total passes the parent's. The cheap terms go first and fairness last. This never changes which rooms are kept, only how soon the search gets there: 35,007 rooms searched in the same time against 30,528, and 32 kept against 27.

More cores

The search runs well on a ten-core Intel i9-10900K I have, so I gave it a standalone build there. The first version ran ten searches as Dart isolates inside one program. Isolates started that way share one heap, and garbage collection can pause the whole group; the solver makes a lot of short-lived objects, so they come often. I haven't measured the collector directly, but the next numbers fit.

Bar chart of rooms solved in two minutes on an i9-10900K: 10 isolates in one process 19,142, 10 separate processes 37,496, 18 separate processes 29,082.

Ten separate programs solved 37,496 rooms in two minutes where ten isolates solved 19,142. Eighteen programs, to use the second thread on each core, solved fewer: 29,082. Limiting each program to one collector thread changed nothing for ten programs and still left eighteen behind ten, so hyper-threading doesn't help here. Ten programs, one to a core, is the setting.

What it doesn't prove

Aiming doesn't carry over to every kind of room. I tried the same search on single-player fire rooms, where the shortcut is a way round the fire, with three thirty-minute runs per method. Climbing from scratch found 1 room and the near-miss search 2, too few to call, and there a wall on the shortcut mostly broke the room: aimed changes were 35% of tries and 24% of the improvements. A spike floor that the fire switches, aimed the same way, did worse still, under 1% of the improvements, so the fire search keeps its walls and gets what it gains there from keeping the near misses.

The checks prove a room needs both players, and nothing about whether it is fun: a room with three lever changes and five hand-overs can still be two people walking a corridor back and forth for half a minute, and only playing it shows which kind of room the search has handed me.