-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbest_first_search.py
81 lines (67 loc) · 2.43 KB
/
best_first_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
from decimal import Decimal
import networkx as nx
def calculate_g_values(graph):
g_values = {}
for node in graph.node():
try:
g_values.update({node: nx.shortest_path_length(graph, 0, node, 'weight')})
except nx.NetworkXNoPath:
g_values.update({node: Decimal('Infinity')})
return g_values
def calculate_h_values(graph, destination):
h_values = {}
for node in graph.node():
try:
h_values.update({node: nx.shortest_path_length(graph, node, destination, 'weight')})
except nx.NetworkXNoPath:
h_values.update({node: Decimal('Infinity')})
return h_values
def SelectNode(open, h_values):
f_list = []
for val in open:
f_list.append(h_values.get(val))
min_f_val = min(f_list)
index = f_list.index(min_f_val)
if min_f_val != Decimal('Infinity'):
return open[index]
return -1
def best_first_algo(graph, goal):
parent = {}
open = []
closed = []
g_values = calculate_g_values(graph)
h_values = calculate_h_values(graph, goal)
print("G Vals: ", g_values)
print("h Vals: ", h_values)
parent.update({0: 'none'})
open.append(0)
while open.__len__() != 0:
n = SelectNode(open, h_values)
if n == -1:
return []
print("Node selected ", n)
open.remove(n)
if n == goal:
closed.append(goal)
if len(closed) == 0:
print("No solution exist (Best-First)")
else:
print("Expanded Path nodes (Best-First): ", closed)
return closed
for child in graph.successors(n):
if child in open:
if g_values.get(n) + graph[n][child]['weight'] < g_values.get(child):
g_values.update({child: g_values.get(n) + graph[n][child]['weight']})
parent.update({child: n})
elif child in closed:
if g_values.get(n) + graph[n][child]['weight'] < g_values.get(child):
g_values.update({child: g_values.get(n) + graph[n][child]['weight']})
parent.update({child: n})
closed.remove(child)
open.append(child)
else:
g_values.update({child: g_values.get(n) + graph[n][child]['weight']})
parent.update({child: n})
open.append(child)
closed.append(n)
return []