Пример #1
0
def a_star(successors, start_state, goal_test, heuristic=lambda s: 0, alpha=0):
    if goal_test(start_state):
        return [start_state]
    agenda = PriorityQueue()
    agenda.push(SearchNode(start_state, None, cost=0),
                alpha * heuristic(start_state))
    expanded = set()
    while len(agenda) > 0:
        priority, parent = agenda.pop()
        if parent.state not in expanded:
            expanded.add(parent.state)
            if goal_test(parent.state):
                print(str(len(expanded)) + ",")
                #print("Cost of path: " + str(path_cost(parent.path())))
                return parent.path()
            for child_state, cost in successors(parent.state):
                child = SearchNode(child_state, parent, parent.cost + cost)
                if child_state not in expanded:
                    agenda.push(child, (1 - alpha) * child.cost +
                                alpha * heuristic(child_state))
    return None
Пример #2
0
def uniform_cost_search(successors,
                        start_state,
                        goal_test,
                        heuristic=lambda s: 0):
    if goal_test(start_state):
        return [start_state]
    agenda = PriorityQueue()
    agenda.push(SearchNode(start_state, None, cost=0), heuristic(start_state))
    expanded = set()
    while len(agenda) > 0:
        priority, parent = agenda.pop()
        if parent.state not in expanded:
            expanded.add(parent.state)
            if goal_test(parent.state):
                print("Number of expanded states: " + str(len(expanded)))
                print("Cost of path: " + str(path_cost(parent.path())))
                return parent.path()
            for child_state, cost in successors(parent.state):
                child = SearchNode(child_state, parent, parent.cost + cost)
                if child_state not in expanded:
                    agenda.push(child, child.cost + heuristic(child_state))
    return None
Пример #3
0
def a_star (successors, start_state, goal_test, heuristic = lambda s: 0, alpha = 0):
    if goal_test(start_state):
        return [start_state]
    agenda = PriorityQueue()
    agenda.push(SearchNode(start_state, None, cost=0), alpha*heuristic(start_state))
    expanded = set()
    while len(agenda) > 0:
        priority, parent = agenda.pop()
        if parent.state not in expanded:
            expanded.add(parent.state)
            if goal_test(parent.state):
                print(str(len(expanded)) + ",")
                #print("Cost of path: " + str(path_cost(parent.path())))
                return parent.path()
            for child_state, cost in successors(parent.state):
                child = SearchNode(child_state, parent, parent.cost+cost)
                if child_state not in expanded:
                    agenda.push(child, (1-alpha)*child.cost+alpha*heuristic(child_state))
    return None
Пример #4
0
def uniform_cost_search(successors, start_state, goal_test, heuristic=lambda s: 0):
    if goal_test(start_state):
        return [start_state]
    agenda = PriorityQueue()
    agenda.push(SearchNode(start_state, None, cost=0), heuristic(start_state))
    expanded = set()
    while len(agenda) > 0:
        priority, parent = agenda.pop()
        if parent.state not in expanded:
            expanded.add(parent.state)
            if goal_test(parent.state):
                print("Number of expanded states: " + str(len(expanded)))
                print("Cost of path: " + str(path_cost(parent.path())))
                return parent.path()
            for child_state, cost in successors(parent.state):
                child = SearchNode(child_state, parent, parent.cost+cost)
                if child_state not in expanded:
                    agenda.push(child, child.cost+heuristic(child_state))
    return None