NeurIPS 2026Coming to NeurIPS?Explore papers, orals & directions

Teaching Transformers to Solve Combinatorial Problems through Efficient Trial & Error

Panagiotis Giannoulis, Yorgos Pantis, Christos Tzamos

Abstract

Large Language Models (LLMs) frequently struggle with structured logic and NP-complete combinatorial problems, largely due to a "one-shot" generation paradigm that lacks error recovery. This paper presents a framework that enables a vanilla GPT-2 model to solve complex puzzles like Sudoku and 1-in-3 SAT with 99% accuracy. By integrating explicit Depth-First Search (DFS) into the training process and employing a novel loss function inspired by the Min-Sum Set Cover problem, the model learns to navigate search spaces, apply logical rules, and perform efficient backtracking.

1. Introduction

While Large Language Models have demonstrated remarkable capabilities in natural language tasks, their performance on structured combinatorial optimization remains poor. Standard autoregressive models often fail at basic arithmetic or puzzles like Sudoku because they are expected to predict a solution in a single forward pass. This "one-shot" approach is fundamentally at odds with the nature of NP-complete problems, which inherently require search and the ability to recover from incorrect decisions.

2. The Search-Based Framework

The proposed method moves away from the "one-shot" fallacy by treating combinatorial puzzles as a sequence of search actions. The researchers utilized a 42M-parameter GPT-2 model, modified to interact with the problem space through several key innovations:

2.1 Action-Level Tokenization

To improve efficiency, the model uses action-level tokenization. In Sudoku, for instance, a move is represented as a single 3-digit token (e.g., 172 representing row 1, column 7, value 2) rather than three separate tokens. This compression reduces sequence length and accelerates training.

2.2 Multi-Target Training and DFS Integration

Instead of training the model to predict a single "correct" next move, the authors employ a multi-target training strategy. The loss function sums the cross-entropies of all valid next moves, providing a richer learning signal. Furthermore, the model is trained on transcripts that include explicit search markers: "rules end" (when logic is exhausted), "guess" tokens, and "dead end" tokens. This allows the Transformer to recognize when it must transition from deterministic logic to tentative guessing and when it must backtrack.

2.3 Beyond Imitation: The $L_1$ Loss

A core contribution of this work is the introduction of a novel loss function ($L_1$) based on the Min-Sum Set Cover problem. This loss optimizes the model to minimize the total number of guesses required to reach a solution. It encourages the model to identify "backdoors"—specific guesses that, once made, allow the rest of the puzzle to be solved through pure logic.

3. Experimental Results

The framework was evaluated on Sudoku (using SudokuPy, Kaggle, and RRN datasets) and 1-in-3 SAT.

3.1 Accuracy and Efficiency

The model achieved a 99% board accuracy on Sudoku, significantly outperforming previous neuro-symbolic approaches like Recurrent Relational Networks (RRN). Notably, the model became more efficient than human-like DFS, achieving a median of just 1.5 guesses per puzzle. This efficiency is driven by the model's ability to prioritize moves that simplify the remaining search space.

3.2 Comparison with Frontier Models

In a stark contrast, "frontier" models such as GPT-4o and Gemini-1.5 Pro—when tested without external tools—achieved 0.00% board accuracy on the same puzzles. These models frequently hallucinated contradictory moves, highlighting the necessity of the search-integrated approach.

3.3 Generality

To demonstrate the framework's versatility, it was applied to 1-in-3 SAT problems, where it achieved 99.1% accuracy. This suggests that the method is a generalizable framework for NP-complete problems rather than a specialized Sudoku solver.

4. Discussion and Limitations

While highly effective, the current implementation has limitations. The $L_1$ loss is currently optimized for "depth-1" guessing (one guess followed by a restart if it fails). While this covers the vast majority of Sudoku puzzles, more complex SAT instances may require multi-level optimized guessing. Additionally, the current policy is non-adaptive, meaning the model does not retain information from failed guesses within a single search attempt.

5. Conclusion

This research demonstrates that Transformers can excel at combinatorial logic when they are taught to embrace search rather than avoid it. By combining sequence prediction with explicit backtracking and optimized guessing strategies, vanilla LLM architectures can achieve state-of-the-art performance on complex, structured puzzles.

Summary updated

BibTeX
@inproceedings{giannoulis2025teaching,
  author = {Panagiotis Giannoulis and Yorgos Pantis and Christos Tzamos},
  title = {{Teaching Transformers to Solve Combinatorial Problems through Efficient Trial \& Error}},
  booktitle = {Advances in Neural Information Processing Systems},
  year = {2025},
  url = {https://openreview.net/forum?id=MLprqOvAAK},
}
Figure