I am currently working on embedding the following code on our website.
'''
Credited to "A name not taken AB"
'''
# This class represent a graph
from PIL import Image
class Graph:
# Initialize the class
def __init__(self, graph_dict=None, directed=True):
self.graph_dict = graph_dict or {}
self.directed = directed
if not directed:
self.make_undirected()
# Create an undirected graph by adding symmetric edges
def make_undirected(self):
for a in list(self.graph_dict.keys()):
for (b, dist) in self.graph_dict[a].items():
self.graph_dict.setdefault(b, {})[a] = dist
def connect(self, A, B, distance=1):
self.graph_dict.setdefault(A, {})[B] = distance
if not self.directed:
self.graph_dict.setdefault(B, {})[A] = distance
# Get neighbors or a neighbor
def get(self, a, b=None):
links = self.graph_dict.setdefault(a, {})
if b is None:
return links
else:
return links.get(b)
# Return a list of nodes in the graph
def nodes(self):
s1 = set([k for k in self.graph_dict.keys()])
s2 = set([k2 for v in self.graph_dict.values() for k2, v2 in v.items()])
nodes = s1.union(s2)
return list(nodes)
# This class represent a node
class Node:
# Initialize the class
def __init__(self, name:str, parent:str):
self.name = name
self.parent = parent
self.g = 0 # Distance to start node
self.h = 0 # Distance to goal node
self.f = 0 # Total cost
# Compare nodes
def __eq__(self, other):
return self.name == other.name
# Sort nodes
def __lt__(self, other):
return self.f < other.f
# Print node
def __repr__(self):
return ('({0},{1})'.format(self.position, self.f))
# A* search
def astar_search(graph, heuristics, start, end):
# Create lists for open nodes and closed nodes
open = []
closed = []
# Create a start node and an goal node
start_node = Node(start, None)
goal_node = Node(end, None)
# Add the start node
open.append(start_node)
# Loop until the open list is empty
while len(open) > 0:
# Sort the open list to get the node with the lowest cost first
open.sort()
# Get the node with the lowest cost
current_node = open.pop(0)
# Add the current node to the closed list
closed.append(current_node)
# Check if we have reached the goal, return the path
if current_node == goal_node:
path = []
while current_node != start_node:
path.append(current_node.name + ': ' + str(current_node.g))
current_node = current_node.parent
path.append(start_node.name + ': ' + str(start_node.g))
# Return reversed path
return path[::-1]
# Get neighbours
neighbors = graph.get(current_node.name)
# Loop neighbors
for key, value in neighbors.items():
# Create a neighbor node
neighbor = Node(key, current_node)
# Check if the neighbor is in the closed list
if(neighbor in closed):
continue
# Calculate full path cost
neighbor.g = current_node.g + graph.get(current_node.name, neighbor.name)
neighbor.h = heuristics.get(neighbor.name)
neighbor.f = neighbor.g + neighbor.h
# Check if neighbor is in open list and if it has a lower f value
if(add_to_open(open, neighbor) == True):
# Everything is green, add neighbor to open list
open.append(neighbor)
# Return None, no path is found
return None
# Check if a neighbor should be added to open list
def add_to_open(open, neighbor):
for node in open:
if (neighbor == node and neighbor.f > node.f):
return False
return True
# The main entry point for this module
def main():
# Create a graph
graph = Graph()
# Create graph connections (Actual distance)
graph.connect('Obs', 'Dorm', 13)
graph.connect('Dorm', 'Obs', 20)
graph.connect('Dorm', 'Eng', 7)
graph.connect('Dorm', 'Nat', 9)
graph.connect('Dorm', 'Soc', 9)
graph.connect('Nat', 'Dorm', 12)
graph.connect('Nat', 'Soc', 3)
graph.connect('Nat', 'Field', 2)
graph.connect('Soc', 'Field', 2)
graph.connect('Soc', 'Arc', 6)
graph.connect('Soc', 'Dorm', 12)
graph.connect('Field', 'Nat', 4)
graph.connect('Field', 'Soc', 4)
graph.connect('Field', 'Base', 6)
graph.connect('Field', 'Gate', 8)
graph.connect('Field', 'Arc', 7)
graph.connect('Arc', 'Soc', 3)
graph.connect('Arc', 'Field', 4)
graph.connect('Arc', 'Eng', 8)
graph.connect('Arc', 'Kor', 8)
graph.connect('Arc', 'Gym', 11)
graph.connect('Eng', 'Kor', 5)
graph.connect('Eng', 'Arc', 6)
graph.connect('Kor', 'Arc', 6)
graph.connect('Kor', 'Gym', 12)
graph.connect('Gym', 'Arc', 9)
graph.connect('Gym', 'Kor', 10)
graph.connect('Gate', 'Golf', 11)
graph.connect('Golf', 'Gate', 9)
# Make graph undirected, create symmetric connections
graph.make_undirected()
# Create heuristics (straight-line distance, air-travel distance)
heuristics = {}
heuristics['Obs'] = 647.98
heuristics['Dorm'] = 192.45
heuristics['Eng'] = 140.86
heuristics['Kor'] = 100.8
heuristics['Gym'] = 276.29
heuristics['Nat'] = 71.7
heuristics['Field'] = 54.83
heuristics['Arc'] = 92.49
heuristics['Gate'] = 379.3
heuristics['Base'] = 272.49
heuristics['Golf'] = 385.37
heuristics['Soc'] = 0
# Run the search algorithm
path = astar_search(graph, heuristics, 'Eng', 'Golf') # Starting point, Destination
print(path)
print()
image = Image.open('Eng_Golf.jpg')
image.show()
# Tell python to run main method
if __name__ == "__main__": main()