-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHeur1.py
52 lines (48 loc) · 1.38 KB
/
Heur1.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
import sys
import time
from heapq import *
from parser import Parser
from AST import printPath
def search(start, target):
heap = []
score = abs(start.depth()-target.depth())
heappush(heap, (score, [start, None]))
found = False
visited = {}
last = None
if start == target:
found = True
last = [target, None]
while (not found) and len(heap)>0:
node = heappop(heap)
node = node[1]
neigh = node[0].getNeighbors()
for n in neigh:
if n==target:
found = True
last = [target, node]
break
elif str(n) not in visited:
visited[str(n)] = True
score = abs(n.depth()-target.depth())
item = (score, [n, node])
heappush(heap, item)
if last==None:
print('The expressions are not logically equivalent.')
return False
else:
print('Path found! The expressions are logically equivalent!')
printPath(last)
return True
def main():
if len(sys.argv) != 3:
raise Exception('Invalid number of arguments')
p = Parser()
start = p.parse(sys.argv[1])
target = p.parse(sys.argv[2])
s = time.time()
res = search(start, target)
e = time.time()
print("Tree Depth Heuristic Elapsed Time: %s" % (e-s))
if __name__ == '__main__':
main()