forked from KAYounes/AIProject
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfringe.py
120 lines (90 loc) · 3.64 KB
/
fringe.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
class PriorityQueue(object):
def __init__(self):
# List of items, flattened binary heap. The first element is not used.
# Each node is a tuple of (value, priority, insert_counter)
self.nodes = [None] # first element is not used
# Current state of the insert counter
self.insert_counter = 0 # tie breaker, keeps the insertion order
# Comparison function between two nodes
# Higher priority wins
# On equal priority: Lower insert counter wins
def _is_higher_than(self, a, b):
return b[1] > a[1] or (a[1] == b[1] and a[2] < b[2])
# Move a node up until the parent is bigger
def _heapify(self, new_node_index):
while 1 < new_node_index:
new_node = self.nodes[new_node_index]
parent_index = new_node_index // 2
parent_node = self.nodes[parent_index]
# Parent too big?
if self._is_higher_than(parent_node, new_node):
break
# Swap with parent
tmp_node = parent_node
self.nodes[parent_index] = new_node
self.nodes[new_node_index] = tmp_node
# Continue further up
new_node_index = parent_index
# Add a new node with a given priority
def add(self, value, priority):
new_node_index = len(self.nodes)
self.insert_counter += 1
self.nodes.append((value, priority, self.insert_counter))
# Move the new node up in the hierarchy
self._heapify(new_node_index)
# Return the top element
def peek(self):
if len(self.nodes) == 1:
return None
else:
return self.nodes[1][0]
# Remove the top element and return it
def pop(self):
if len(self.nodes) == 1:
raise LookupError("Heap is empty")
result = self.nodes[1][0]
# Move empty space down
empty_space_index = 1
while empty_space_index * 2 < len(self.nodes):
left_child_index = empty_space_index * 2
right_child_index = empty_space_index * 2 + 1
# Left child wins
if (
len(self.nodes) <= right_child_index
or self._is_higher_than(self.nodes[left_child_index], self.nodes[right_child_index])
):
self.nodes[empty_space_index] = self.nodes[left_child_index]
empty_space_index = left_child_index
# Right child wins
else:
self.nodes[empty_space_index] = self.nodes[right_child_index]
empty_space_index = right_child_index
# Swap empty space with the last element and heapify
last_node_index = len(self.nodes) - 1
self.nodes[empty_space_index] = self.nodes[last_node_index]
self._heapify(empty_space_index)
# Throw out the last element
self.nodes.pop()
return result
def inside(self, item):
for indx in range(1, len(self.nodes)):
if self.nodes[indx][0] == item:
return indx
return 0
def replace(self, item):
index = self.inside(item[0])
if (index):
item_cpy = (item[0], item[1], self.insert_counter + 1)
self.nodes[index] = item_cpy
def empty(self):
return not (len(self.nodes) - 1)
def display_queue(self, show_cost = True):
temp = []
for node in self.nodes:
if (node is None):
continue
if(show_cost):
temp.append([node[0].state, node[1]])
else:
temp.append(node[0].state)
print(temp)