-
Notifications
You must be signed in to change notification settings - Fork 0
/
greedy.py
47 lines (38 loc) · 1.59 KB
/
greedy.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
from data_structure import *
from publicFunctions import *
def greedy(self):
self.num_visitedNodes = -2
h=heu(self, self.start)
startNode = Node(state=self.start, parent=None, action=None, heuristic=h, cost=0)
availableNodes = []
self.visitedNodes = set()
availableNodes.append(startNode)
while True:
if len(availableNodes) == 0:
print("No solution")
break
currentNode = availableNodes.pop(getMinH(availableNodes))
self.num_visitedNodes += 1
# reached the goal ?
if currentNode.state == self.goal:
actions = []
cells = []
while currentNode.parent is not None:
actions.append(currentNode.action)
cells.append(currentNode.state)
currentNode = currentNode.parent
actions.reverse()
cells.reverse()
self.solution = (actions, cells)
print ("The Final Cost: ", FinalCost(actions))
print()
print("Actions: "+', '.join(actions))
print()
print("Number of visited nodes: ", self.num_visitedNodes)
return
self.visitedNodes.add(currentNode.state)
# Add available nodes
for action, state in self.availableActions(currentNode.state):
if state not in self.visitedNodes:
child = Node(state=state, parent=currentNode, action=action, heuristic=heu(self, currentNode.state), cost=0)
availableNodes.append(child)