-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGreedy.py
More file actions
58 lines (52 loc) · 2.36 KB
/
Copy pathGreedy.py
File metadata and controls
58 lines (52 loc) · 2.36 KB
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
from PriorityQueue import PriorityQueue
from Node import Node
class Greedy:
def greedy_cost_search(self, problem):
#lambda function to compare in the priority queue
f = lambda node: problem.value(node.state)
#make node from initial node
node = Node(problem.initial,None, None, 0, 0)
#create pq for frontier
pq = PriorityQueue(min, f)
#push to pq
pq.enqueue(node)
#set of explored states
#use set because contains is quicker in sets than lists
#set of states, not nodes because we dont want duplicate states, nodes dont matter
explored = []
#loop until pq is empty ---- or goal state is reached?
while pq.size() > 0:
node = pq.dequeue()
#print("woot", node.state, " costs ", node.path_cost)
#check if it is goal state
if problem.goal_test(node.state):
#if goal state return node
return node
#otherwise, build out new nodes and push to pq
else:
explored.append(node.state)
problem.visited += 1
#array of child nodes
frontier_nodes = node.expand_frontier(problem)
#frontier_states = [(x.state, x.path_cost) for x in frontier_nodes]
#print("frontier: ", frontier_states)
#push them onto pq
for fnode in frontier_nodes:
#dont want duplicates, so can't exist in explored or pq
if fnode.state not in explored and fnode not in pq:
pq.enqueue(fnode)
problem.time += 1
if(pq.size() > problem.frontier):
problem.frontier = pq.size()
#if by chance there is a duplicate node in frontier,
#delete the "larger"/worst node
elif fnode in pq:
#print(fnode.node_path())
#print(pq.size())
current_node = pq[fnode]
#print(current_node)
if f(fnode) < f(current_node):
del pq[current_node]
pq.enqueue(fnode)
#if a node was not returned at this point, nothing matches the goal state
return None