Some sparse-reward puzzles have a hidden algebraic structure. In Lights Out and its relatives, pressing a button flips, or advances modulo $q$, a fixed pattern of lights. Pressing buttons commutes, so the order does not matter. The fewest presses that solve a board therefore equals the weight of the lightest solution to a linear system over a finite ring. In coding-theory language, this is the weight of a coset leader: the lightest vector that produces a given syndrome, meaning the board state read as the leftover of the linear system. Finding coset leaders is exactly what error-correcting decoders do. The proposal is to use a decoder's output, negated, as the potential in potential-based reward shaping.
Potential-based reward shaping adds $\gamma\Phi(s')-\Phi(s)$ to the reward and provably leaves the optimal policy unchanged. When the potential is the negated true cost-to-go, as with a decoder here, the shaped reward makes every optimal move locally free and every mistake locally costly. Puzzles of this kind are where current methods fail: the hardest Lights-Out-style Puzzle levels of OGBench give 0% success for every tested method (OGBench). Distance-preserving Gray maps enter at one specific point. When lights take values in $\mathbb{Z}_4$ and a button can be pressed forward or backward, the cost of a press pattern is its Lee weight, the circular distance on $\mathbb{Z}_4$. The Gray map turns that Lee-weight problem into a binary Hamming-weight problem, so the large toolbox of binary decoders becomes available as shaping potentials.
Core Claim
Let the puzzle state $s\in\mathbb{Z}_q^n$ evolve by $s\mapsto s\pm a_j$, where $a_j$ is column $j$ of a press matrix $A$. The minimal number of moves from $s$ to the goal $g$ is
$$d(s,g)=\min\{\, w_{\text{Lee}}(x) : Ax = g-s,\ x\in\mathbb{Z}_q^m \,\}.$$
Here $x$ counts net presses per button, and $w_{\text{Lee}}(x)=\sum_j \min(x_j,q-x_j)$ is its Lee weight. For $q=2$ this reduces to Hamming weight. The solutions form a coset of the code $\ker A$. We set $\Phi(s,g)=-\hat d(s,g)$, where $\hat d$ comes from a decoder for that coset.
The decoder's quality then controls how fast learning goes, but it can never change what the agent learns: any decoder error, however bad, cannot change the optimal policy, because the potential form guarantees invariance. This gives three regimes. When $\ker A$ is small, which is true of the classic square Lights Out board, $\hat d$ is exact by enumeration. That case is simply "use the true value function" and is not the interesting one. The interesting regime is boards and button sets with large kernel dimension, and $\mathbb{Z}_4$ Lee costs, where exact coset-leader search is expensive. There, the question is how much sample efficiency survives when we use cheap approximate decoders, and whether the Gray-mapped binary route or direct $\mathbb{Z}_4$ decoding gives the better cost/accuracy trade-off.
Relation to Prior Work
Two strands of prior work motivate the proposal by showing that distance-shaped potentials help. Potential-based Reward Shaping in Sokoban turns an A* search heuristic into a potential and lifts 2- and 3-box success from about 0.2 to near 1.0. It pays for this with per-state search, and its Manhattan-distance core ignores the puzzle's reachability structure. Occupancy Reward Shaping (ORS) instead learns distance from a flow-matching occupancy model and a Wasserstein-2 metric. It reports a 2.2× average gain over the offline goal-conditioned method GCIQL on OGBench, but it is described as struggling on lock-like combinatorial tasks. We replace both search and learned geometry with a closed-form algebraic computation. That only works for commutative puzzles, and we say so rather than claim generality.
Automata-based shaping supplies the method template of shaping over a discrete progress structure. SPECTRL, ACC-MARL, and DRL² shape over task-automaton states and embed those states with neural networks or Bayesian priors. Our potential is computed over the environment's own state group and needs no learned embedding.
Two further papers constrain the design. ADOPS shows that potentials built from learned critics can collapse on long horizons, with near-zero scores on Montezuma's Revenge for the PBIM and GRM baselines. That is the case against learning $\Phi$ when an exact one is available. Geometric Coherence Learning forces learned values into a consistent partial order through neural projections. A decoder-based potential is a true shortest-path distance, so it has that consistency by construction. We borrow its concern about cyclic value errors but none of its machinery.
The coding-theory papers supply the technique. The Gray map is an isometry from Lee to Hamming distance, over $\mathbb{Z}_4$ in Quaternary Codes and Their Binary Images, over $\mathbb{Z}_{15}$ in Linear Codes over $\mathbb{Z}_{15}$, and over $\mathbb{F}_p+u\mathbb{F}_p$ in Few-weight codes associated with down sets. The same work also exposes the catch we must handle: the binary image of a $\mathbb{Z}_4$-linear code is generally nonlinear. Binary linear syndrome decoders therefore apply only when the linearity criterion of Quaternary Codes and Their Binary Images holds for $\ker A$. Otherwise we must decode on the $\mathbb{Z}_4$ side.
Research Program
We will first build the potential compiler. It reads $A$ and $q$ from the environment and computes $\ker A$ by Gaussian elimination over $\mathbb{Z}_q$, or by Smith normal form for $\mathbb{Z}_4$. It then offers a ladder of decoders:
- exact coset enumeration, for small kernels;
- information-set decoding, which repeatedly guesses a subset of button presses to fix and solves for the rest, with a tunable budget;
- for $\mathbb{Z}_4$, both Lee-metric decoding on the ring and Hamming decoding on the Gray image. The latter uses a linear decoder when the image is linear and a nearest-codeword search over the nonlinear image otherwise.
Every decoder output is tagged with its measured distance error against breadth-first-search ground truth on boards small enough to enumerate.
Second, we will analyze how decoder error becomes learning cost. For tabular Q-learning started from zero, the shaping theory says the agent effectively starts at $\Phi$. Regret should therefore scale with the potential's error $\max_s|\hat d(s)-d(s)|$ rather than with the horizon, and we will test that dependence directly.
Third, we will study the representation question separately from the shaping question. Feeding the network a Gray-coded binary observation, whose input Hamming distance equals board Lee distance, versus a one-hot per light is an ablation. It is not the main claim.
Baselines
The naive reference points are unshaped learning, meaning PPO online and GCIQL offline on OGBench, and a "count the wrong lights" potential. The latter is the Hamming or Lee weight of $g-s$ itself, the direct analogue of the Manhattan potential. It costs nothing to compute and is the thing a practitioner would try first. Beating it is the minimum bar. We expect it to mislead exactly when one press fixes several lights at once.
The upper reference is the exact breadth-first-search value, used as the potential on small boards. No shaping method in this family can do better, so it tells us how much of the achievable gain each decoder captures.
The strongest existing search-based competitor is A shaping as in the Sokoban study. That paper reports success near 1.0 within 50,000 steps on 2- and 3-box Sokoban versus about 0.2 unshaped. That is a different environment, so it is context and not a target. We will re-run A with a search-node budget matched to the decoder's wall-clock cost per state.
The strongest learned competitor offline is ORS (Occupancy Reward Shaping), reported at a 2.2× average success gain over GCIQL across OGBench. No reported result was found on Lacuna for ORS on the Puzzle family specifically. The only Puzzle number in the file is 0% success for all tested methods on the hardest levels (OGBench). We will also include a learned-critic potential in the spirit of ADOPS.
All methods share the same datasets, network sizes, environment-step budget, five or more seeds, and an equal hyperparameter-search budget. The shaping coefficient and $\gamma$ are tuned per method. Beating a baseline means higher final success or fewer steps to 90% success, with non-overlapping seed intervals, at equal or lower per-step compute.
The key ablation isolates decoding quality. We deliberately truncate the information-set decoder's budget and plot learning speed against measured potential error. If speed does not track error, the algebra is not what is helping. A second ablation holds decoding fixed and swaps Gray versus one-hot observations.
Why not just…?
Why not just use the number of wrong lights as the potential? It is the real low-cost competitor, and on sparse button patterns it may be nearly as good. Its error grows with how much buttons overlap, because one press can repair many lights. We will generate boards with controlled overlap and report where the two potentials diverge. If they never diverge meaningfully, the proposal reduces to "count lights" and we will say so.
Why not just solve the linear system exactly by Gaussian elimination? On the classic board this is correct: the kernel is tiny, enumeration is exact, and no coding theory is needed. That is why those boards serve only as a sanity check. Minimum-weight coset search grows exponentially in kernel dimension. So the method's value lives on large-kernel boards and $\mathbb{Z}_4$ Lee costs, and the experiments sweep kernel dimension to locate the crossover where exact enumeration becomes too slow per state.
Why not just run A* as the Sokoban paper did? A with an admissible heuristic is exact given enough time, and on small boards it may match the decoder. The comparison at matched per-state compute across board sizes settles this. We expect A to degrade first because its search space is the full state space, whereas the decoder searches only the coset.
Why not just learn the distance, with a critic or ORS? The Montezuma's Revenge failures of learned-critic potentials reported by ADOPS, and the 0% on hard Puzzle levels (OGBench), suggest that learned geometry is weak on these discrete tasks. The head-to-head on OGBench Puzzle will test this directly rather than assume it.
Proposed Experiments
Online, we will use a generated suite of Lights Out variants over $\mathbb{Z}_2$ and $\mathbb{Z}_4$ with forward and backward presses. Board size, button overlap, and kernel dimension will vary independently, plus rotary combination locks on $\mathbb{Z}_q^m$ as a zero-kernel control. The metrics are success rate, steps to 90% success, per-state potential compute time, and measured potential error.
Offline, we will use the OGBench Puzzle datasets across all difficulty levels, adding the decoder potential on the button-state component to GCIQL. We compare against GCIQL, ORS, and the count-wrong-lights potential. OGBench Puzzle is a manipulation task in which a robot arm presses the buttons. Distance to goal there also includes arm travel, so the potential covers only the puzzle part, and that caveat will be reported.
The decisive checks are three:
- whether any nonzero success appears on the hardest Puzzle levels, where every reported method sits at 0%;
- whether learning speed falls monotonically with decoder error in the truncation ablation;
- whether, for $\mathbb{Z}_4$ boards with nonlinear Gray images, the ring-side decoder beats the binary route. Such a result would be a concrete case where the linearity problem from coding theory matters for reinforcement learning.
As a negative control, Sokoban, whose moves do not commute, should show that the compiler rejects the environment rather than producing a wrong potential.
References
- OGBench: Benchmarking Offline Goal-Conditioned RL
- Potential-based Reward Shaping in Sokoban
- Occupancy Reward Shaping: Improving Credit Assignment for Offline Goal-Conditioned Reinforcement Learning
- A Composable Specification Language for Reinforcement Learning Tasks
- Automata-Conditioned Cooperative Multi-Agent Reinforcement Learning
- Directed Exploration in Reinforcement Learning from Linear Temporal Logic
- Action-Dependent Optimality-Preserving Reward Shaping
- Geometric Coherence Learning for Structuring Value Functions in Plain MDPs
- Quaternary Codes and Their Binary Images
- Linear Codes over the Finite Ring <i>Z</i><sub>15</sub>
- Few-weight codes over Fp+uFp associated with down sets and their distance optimal Gray image