Skip to content

Depth-first search raises KeyError for unreachable goal or start equal to goal #1503

Open

Description

@blessedefe4real

DepthFirstSearchPlanner.planning in PathPlanning/DepthFirstSearch/depth_first_search.py raises KeyError: -1 instead of returning a result in two cases.

1. The goal cannot be reached. When the open set is exhausted the loop only breaks, and calc_final_path then evaluates closedset[ngoal.parent_index] with the goal's initial parent index -1.

2. The start is the goal. The goal is found on the first iteration with parent_index == -1, so the same lookup fails.

This is the same bug as in breadth-first search (#1502); the two planners share this code.

Reproduce

from PathPlanning.DepthFirstSearch import depth_first_search as m
m.show_animation = False

# square boundary 0..10 with an inner wall at x = 5
ox, oy = [], []
for i in range(11):
    ox += [0.0, 10.0, float(i), float(i), 5.0]
    oy += [float(i), float(i), 0.0, 10.0, float(i)]
planner = m.DepthFirstSearchPlanner(ox, oy, 1.0, 0.5)

planner.planning(2.0, 5.0, 8.0, 5.0)  # KeyError: -1 (goal behind the wall)
planner.planning(2.0, 5.0, 2.0, 5.0)  # KeyError: -1 (start == goal)

Expected

Tested on current master (61a8b0b), Python 3.13.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions