Scattered Forest Search: Smarter Code Space Exploration with LLMs

Jonathan Light, Yue Wu, Yiyou Sun, Wenchao Yu, Yanchi Liu, Xujiang Zhao, Ziniu Hu, Haifeng Chen, Wei ChengarXiv 2024

Abstract

We frame code generation as a black-box optimization problem within the code space and demonstrate how optimization-inspired techniques can enhance inference scaling. Based on this perspective, we propose SCATTERED FOREST SEARCH (SFS), a novel approach that improves solution diversity and better exploits feedback during evolutionary search. Our theoretical analysis illustrates how these methods help avoid local optima during optimization, leading to more efficient exploration. Extensive experiments on HumanEval, MBPP, APPS, CodeContests, and Leetcode reveal significant performance gains. For instance, our method achieves a pass@1 rate of 67.1% on HumanEval+ and 87.2% on HumanEval with GPT-3.5, marking improvements of 8.6% and 4.3% over the state-of-the-art, while also halving the iterations needed to find the correct solution. Furthermore, our approach scales more efficiently than existing search techniques, including tree search, line search, and repeated sampling.

Introduction: The Challenge of Inference Scaling

Large Language Models (LLMs) have revolutionized automated code generation, yet they frequently struggle with complex, multi-step programming tasks. A standard remedy is "inference scaling"—allocating more computational resources at test time to improve output quality. Current strategies typically involve either sampling a large number of independent solutions (Best-of-N) or iteratively refining a single candidate solution.

However, these methods face a critical bottleneck: insufficient exploration. When an LLM is tasked with generating multiple variants or fixing a bug, it tends to produce semantically similar code. In the landscape of code optimization, the model becomes trapped in a "local optimum"—a cluster of solutions that appear plausible but fail on the same edge cases. Traditional search strategies like Line Search or standard Tree Search often lack the mechanisms to escape these clusters.

Comparison of existing search strategies in the code landscape.

Scattered Forest Search (SFS)

To overcome these limitations, we introduce Scattered Forest Search (SFS). SFS treats the code space as a non-differentiable landscape to be navigated using three core optimization-inspired techniques:

  1. Scattering (Textual Gradients): Rather than simply asking for a "better" solution, SFS prompts the LLM to identify multiple distinct "textual directions" for improvement (e.g., "optimize for time complexity" vs. "improve error handling"). These directions act as discrete gradients, forcing the search into diverse, often orthogonal regions of the code space.
  2. Foresting (Multi-start Optimization): Instead of relying on a single search tree, SFS initializes a "forest" of multiple trees from different seed solutions. This multi-start approach ensures broader coverage of the search space from the outset.
  3. Scouting (Shared Insights): Drawing inspiration from Ant Colony Optimization, SFS maintains a global memory of insights gathered across all branches. Observations about what logic failed in one tree are shared to guide the exploration of others, preventing redundant errors.

These components are integrated using Monte Carlo Tree Search (MCTS) and the Upper Confidence Bound for Trees (UCT) formula to dynamically balance the exploration of new directions with the exploitation of promising code candidates.

Experimental Evaluation

We evaluated SFS across five rigorous benchmarks: HumanEval/HumanEval+, MBPP/MBPP+, APPS, CodeContests, and a curated set of recent LeetCode problems to mitigate data contamination.

A key finding from our ablation studies is that diversity often outweighs initial accuracy. Even when search trees were initialized with low-quality or "nonsense" seeds, the SFS framework was able to navigate toward correct solutions. This suggests that the primary value of inference scaling lies in its ability to prevent early convergence on incorrect logic.

Results and Scaling Efficiency

SFS demonstrates superior performance and scaling characteristics compared to existing methods. Using GPT-3.5, SFS achieved a 67.1% pass@1 rate on HumanEval+, representing an 8.6% improvement over the previous state-of-the-art. On the standard HumanEval benchmark, it reached 87.2%.

Beyond raw accuracy, SFS is significantly more efficient. It typically finds the correct solution in roughly half the iterations required by standard tree search. Most importantly, while traditional methods like Best-of-N often plateau after a limited number of samples, SFS exhibits a steeper and more sustained scaling curve as more compute is applied.

Scaling curves for different search methods.

Conclusion and Future Work

Scattered Forest Search provides a robust framework for navigating the discrete space of source code by leveraging textual gradients and multi-start optimization. While the method is computationally intensive—requiring additional LLM calls for reflection and direction generation—it offers a powerful way to enhance the capabilities of smaller, more efficient models. Future research will explore applying this "textual optimization" framework to other complex reasoning domains, such as mathematical theorem proving and multi-step logical inference.

Summary updated

BibTeX
@misc{light2024scattered,
  author = {Jonathan Light and Yue Wu and Yiyou Sun and Wenchao Yu and Yanchi Liu and Xujiang Zhao and Ziniu Hu and Haifeng Chen and Wei Cheng},
  title = {{Scattered Forest Search: Smarter Code Space Exploration with LLMs}},
  year = {2024},
  eprint = {2411.05010},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2411.05010},
}
Figure