Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Iamhankai Forest of Thought Forest Solve

From Leeroopedia
Knowledge Sources
Domains Search_Algorithms, Game_Solving
Last Updated 2026-02-14 03:00 GMT

Overview

Concrete tool for solving Game of 24 puzzles via multi-tree BFS search provided by the Forest-of-Thought repository.

Description

The forest_solve function implements BFS-based Game24 solving with multiple trees. For each tree, it generates shuffled input orderings via init_numbers(), then executes a 3-stage search: Stage 1 uses get_proposals_task1() and get_values() for 4-to-3 number reduction, Stage 2 uses get_proposals_task2() and get_values() for 3-to-2 reduction, and Stage 3 uses get_proposals_task3() for deterministic 2-to-1 computation. Valid solutions are verified via check_expression().

Usage

Called by the Game24 experiment runner (scripts/game24/run.py) for each puzzle. Returns the first valid expression that equals 24, or an empty string if no solution is found.

Code Reference

Source Location

Signature

def forest_solve(args, task, idx, model='None', to_print=True):
    """
    Solve Game24 puzzle using multi-tree BFS search.

    Args:
        args: Configuration with tree_num, n_evaluate_sample,
              n_select_sample, method_select, correction.
        task (Game24Task): Task object with puzzle data and prompts.
        idx (int): Puzzle index in the dataset.
        model: LLM model (default: 'None', uses global).
        to_print (bool): Enable debug output.

    Returns:
        tuple: (expression, info_dict, inference_count)
            - expression (str): Valid expression or empty string.
            - info_dict (dict): Per-step search trace.
            - inference_count (int): Total LLM calls.
    """

Import

from methods.bfs import forest_solve

I/O Contract

Inputs

Name Type Required Description
args Namespace Yes Config with tree_num, n_evaluate_sample, n_select_sample, method_select
task Game24Task Yes Task object with puzzle data and prompt methods
idx int Yes Puzzle index in dataset

Outputs

Name Type Description
expression str Valid arithmetic expression (e.g., "(1+2+3)*4=24") or empty string
info_dict dict Search trace with step-by-step candidates and values
inference_count int Total number of LLM API calls

Usage Examples

from methods.bfs import forest_solve
from tasks import get_task

task = get_task("game24")
expression, info, count = forest_solve(
    args=args, task=task, idx=0, to_print=True
)

if expression:
    print(f"Solution: {expression}")
else:
    print("No solution found")
print(f"LLM calls: {count}")

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment