def dfs(path):
     if path[-1] == goal_state:
         return path
     for child_state in auxiliary.get_children(path[-1]):
         if child_state not in path:
             new_path = dfs(path + [child_state])
             if new_path:
                 return new_path
     return None
 def dfs(path, depth):
     if depth == 0:
         return
     if path[-1] == goal_state:
         return path
     for state in auxiliary.get_children(path[-1]):
         if state not in path:
             new_path = dfs(path + [state], depth - 1)
             if new_path:
                 return new_path
Example #3
0
 def search(path, g, bound):
     node = path[-1]
     f = g + heuristic_cost_estimate(node, goal_state)
     if f > bound:
         return path, f
     if node == goal_state:
         return path, True
     min_bound = float('inf')
     for child_state in auxiliary.get_children(node):
         if child_state not in path:
             path, t = search(path + [child_state],
                              g + dist_between(node, child_state), bound)
             if t == True:
                 return path, t
             if t < min_bound:
                 min_bound = t
             path = path[:-1]
     return path, min_bound
def a_star_search(initial_state, goal_state):
    closed_set = set()
    open_set = {initial_state}
    come_from = dict()
    g_score = dict()
    f_score = dict()

    come_from[initial_state] = None
    g_score[initial_state] = 0
    f_score[initial_state] = heuristic_cost_estimate(initial_state, goal_state)

    while len(open_set) > 0:
        current_state = min([(v, k) for k, v in f_score.items()
                             if k in open_set])[1]

        if current_state == goal_state:
            return auxiliary.construct_path(come_from, current_state)

        open_set.remove(current_state)
        closed_set.add(current_state)

        for child_state in auxiliary.get_children(current_state):
            if child_state in closed_set:
                continue

            if child_state not in open_set:
                open_set.add(child_state)

            tentative_g_score = g_score[current_state] + dist_between(
                current_state, child_state)
            if child_state in g_score:
                if tentative_g_score >= g_score[child_state]:
                    continue

            come_from[child_state] = current_state
            g_score[child_state] = tentative_g_score
            f_score[
                child_state] = g_score[child_state] + heuristic_cost_estimate(
                    child_state, goal_state)

    return []
def breadth_first_search(initial_state, goal_state):
    open_set = collections.deque()
    closed_set = set()
    come_from = dict()

    open_set.append(initial_state)
    come_from[initial_state] = None

    while len(open_set) > 0:
        state = open_set.popleft()

        if state == goal_state:
            return auxiliary.construct_path(come_from, state)

        for child_state in auxiliary.get_children(state):
            if child_state in closed_set:
                continue
            if child_state not in open_set:
                come_from[child_state] = state
                open_set.append(child_state)

        closed_set.add(state)
def depth_first_search(initial_state, goal_state):
    stack = collections.deque()
    closed_set = set()
    come_from = dict()

    stack.append(initial_state)
    come_from[initial_state] = None

    while len(stack) > 0:
        state = stack.pop()

        if state in closed_set:
            continue

        if state == goal_state:
            return auxiliary.construct_path(come_from, state)

        closed_set.add(state)
        for child_state in auxiliary.get_children(state):
            if child_state not in come_from.keys():
                come_from[child_state] = state
            stack.append(child_state)
def best_first_search(initial_state, goal_state):
    open_set = []
    closed_set = set()
    come_from = dict()

    h = heuristic_cost_estimate(initial_state, goal_state)
    heapq.heappush(open_set, (h, initial_state))
    come_from[initial_state] = None

    while len(open_set) > 0:
        h, state = heapq.heappop(open_set)

        if state == goal_state:
            return auxiliary.construct_path(come_from, state)

        for child_state in auxiliary.get_children(state):
            if child_state in closed_set:
                continue
            if child_state not in open_set:
                come_from[child_state] = state
                h = heuristic_cost_estimate(child_state, goal_state)
                heapq.heappush(open_set, (h, child_state))

        closed_set.add(state)