-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.py
103 lines (79 loc) · 2.96 KB
/
search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
from structures import Queue
from structures import PriorityQueue
# --------------------------------------------------------
# Buscas Não-Informadas
# --------------------------------------------------------
# Algoritmo de busca em largura
def breadth_first(graph, start, goal):
frontier = Queue()
frontier.push(start)
came_from = {}
came_from[start] = None
cost_so_far = {}
cost_so_far[start] = 0
count = 0
while not frontier.empty():
current = frontier.pop()
if current == goal:
break
for neighbour in graph.neighbors(current):
if neighbour not in came_from:
new_cost = cost_so_far[current] + graph.cost(current, neighbour)
cost_so_far[neighbour] = new_cost
frontier.push(neighbour)
came_from[neighbour] = current
count = count + 1
return came_from, cost_so_far, count
def dfs(graph, visited, current, goal, came_from, cost):
if current == goal:
return
if current not in visited:
visited.append(current)
for neighbour in graph.neighbors(current):
if (current not in came_from):
new_cost = cost[current] + graph.cost(current, neighbour)
cost[neighbour] = new_cost
came_from[neighbour] = current
dfs(graph, visited, neighbour, goal, came_from, cost)
# Algoritmo de busca em profundidade
def depth_first(graph, start, goal):
visited = []
came_from = {}
came_from[start] = None
cost_so_far = {}
cost_so_far[start] = 0
dfs(graph, visited, start, goal, came_from, cost_so_far)
return came_from, cost_so_far, len(visited)
# --------------------------------------------------------
# Buscas Informadas - Heurísticas
# --------------------------------------------------------
# A heurística é definida como a distância entre o ponto onde o agente se encontra e o ponto de destino / objetivo
def heuristic(a, b):
(x1, y1) = a
(x2, y2) = b
return abs(x1 - x2) + abs(y1 - y2)
# Algoritmo de busca A*
def a_star(graph, start, goal):
frontier = PriorityQueue()
frontier.push(start, 0)
count = 0
came_from = {}
cost_so_far = {}
eval_v = {}
came_from[start] = None
cost_so_far[start] = 0
eval_v[start] = 0
while not frontier.empty():
current = frontier.pop()
if current == goal:
break
for neighbour in graph.neighbors(current):
new_cost = cost_so_far[current] + graph.cost(current, neighbour)
if neighbour not in cost_so_far or new_cost < cost_so_far[neighbour]:
cost_so_far[neighbour] = new_cost
priority = new_cost + heuristic(goal, neighbour)
eval_v[neighbour] = priority
frontier.push(neighbour, priority)
count = count + 1
came_from[neighbour] = current
return came_from, eval_v, count