Beam Search over Partial Plans
Problem Statement
Expand a tree of candidate plan steps with beam search: at each depth keep only the top-beam_width partial plans by cumulative score, then return the best complete plan's total score.
Background
Start from the root (empty plan, score 0). At each level, every kept partial plan branches into children given by successors(node), each child adding its step score. After generating all children of a level, keep the beam_width highest-scoring ones (ties broken by the smaller node id for determinism). A node with no successors is terminal. After exploring depth levels (or when no expansions remain), return the maximum total score among all partial plans ever kept (including terminals). Nodes are integer ids; successors maps an id to a list of (child_id, step_score).
Your Task
def beam_search(successors, root, beam_width, depth):
Return the best cumulative score (float) found, rounded to 4 decimals.
Input Format
- successors (dict: id -> list of (child_id, score)), root (int), beam_width (int), depth (int).
Output Format
- A float rounded to 4 decimals.
Sample
succ = {0: [(1, 2.0), (2, 5.0)], 1: [(3, 10.0)], 2: [(4, 1.0)]}
print(beam_search(succ, 0, 1, 2))
Output:
6.0
Example
succ = {0: [(1, 2.0), (2, 5.0)], 1: [(3, 10.0)], 2: [(4, 1.0)]}
print(beam_search(succ, 0, 1, 2))6.0
-
Initialization: Start with the root node having a cumulative score of . The beam width is , meaning only the single best partial plan is kept at each step, and we explore up to a depth of .
-
Depth 1 Expansion: Expand the current beam using its successors. Node branches to node (score ) and node (score ). Since the beam width is , we keep only the highest-scoring child, which is node with a score of . The current best score is updated to .
-
Depth 2 Expansion: Expand the current beam using its successors. Node branches to node with a step score of . The new cumulative score is . This is the only child, so it becomes the new beam. The best score is updated to .
-
Termination: The maximum allowed depth of has been reached. The algorithm stops and returns the highest cumulative score observed in any kept beam, which is .
-
The final output is 6.0
Constraints
- Beam keeps top
beam_widthby cumulative score each level; ties -> smaller node id. - A node absent from
successors(or with empty list) is terminal. - Track the best cumulative score across all kept nodes; round to 4 decimals.