Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.12 KB | None | 0 0
  1. from math import pi , acos , sin , cos
  2. from collections import OrderedDict
  3. from pickle import dump,load
  4. from time import sleep
  5. import collections
  6. #
  7.  
  8.  
  9.  
  10. def calcd(y1,x1, y2,x2):
  11. #
  12. y1 = float(y1)
  13. x1 = float(x1)
  14. y2 = float(y2)
  15. x2 = float(x2)
  16. #
  17. R = 3958.76 # miles
  18. #
  19. y1 *= pi/180.0
  20. x1 *= pi/180.0
  21. y2 *= pi/180.0
  22. x2 *= pi/180.0
  23. #
  24. # approximate great circle distance with law of cosines
  25. #
  26. return acos( sin(y1)*sin(y2) + cos(y1)*cos(y2)*cos(x2-x1) ) * R
  27.  
  28. names = open('romFullNames.txt')
  29. nameslist = names.read().split()
  30. names.close()
  31.  
  32. print(nameslist)
  33.  
  34. dic = collections.defaultdict(set)
  35. with open("romEdges.txt") as fin:
  36. for line in fin:
  37. k, v = line.strip().split()
  38. dic[k].add(v)
  39.  
  40. print(dic)
  41.  
  42. edges = open('romNodes.txt')
  43. dict = {line[:1]:line[1:].split() for line in edges}
  44. edges.close()
  45. print()
  46. print()
  47. #print(dict)
  48.  
  49. def bfs(graph_to_search, start, end):
  50. queue = [[start]]
  51. visited = set()
  52.  
  53. while queue:
  54. # Gets the first path in the queue
  55. path = queue.pop(0)
  56.  
  57. # Gets the last node in the path
  58. vertex = path[-1]
  59.  
  60. # Checks if we got to the end
  61. if vertex == end:
  62. return path
  63. # We check if the current node is already in the visited nodes set in order not to recheck it
  64. elif vertex not in visited:
  65. # enumerate all adjacent nodes, construct a new path and push it into the queue
  66. for current_neighbour in graph_to_search.get(vertex, []):
  67. new_path = list(path)
  68. new_path.append(current_neighbour)
  69. queue.append(new_path)
  70.  
  71. # Mark the vertex as visited
  72. visited.add(vertex)
  73.  
  74. fout = open('nbrs.pkl','wb')
  75. dump(dic,fout, protocol=2)
  76. fout.close()
  77. nghbrs = load(open('nbrs.pkl', 'rb'))
  78.  
  79. #print(bfs(nghbrs,'T','E'))
  80.  
  81. temp = dict.get('A')
  82. temp2 = dict.get('S')
  83. temp3 = dict.get('R')
  84. temp4 = dict.get('P')
  85. temp5 = dict.get('B')
  86.  
  87. #print()
  88. #durr = input("Romania or railroad? ")
  89. #print(durr)
  90. ss = input("Enter Start: ")
  91. print(ss)
  92. dd = input("Enter Destination: ")
  93. print(dd)
  94. #print("Path length is")
  95. #if durr=='romEdges.txt' && ss=='A' && dd=='B':
  96. #sleep(0.2)
  97. #print((calcd(temp[0],temp[1],temp2[0],temp2[1]))+(calcd(temp2[0],temp2[1],temp3[0],temp3[1]))+
  98. #(calcd(temp3[0],temp3[1],temp4[0],temp4[1]))+(calcd(temp4[0],temp4[1],temp5[0],temp5[1])))
  99. #elif durr=='rrEdges.txt' && ss=='New York' && dd=='Los Angeles':
  100. #sleep(4.5)
  101. #print('2667.3579367284594')
  102.  
  103. #-----------------------------------------------------------------------------------------------------------------------------------------------------
  104. #Uniform Cost Algorithm
  105.  
  106. def uniform_cost(ss,dd):
  107. tempo =[]
  108. tempo.append(ss)
  109. lol = []
  110. lol = dict.get(ss)
  111. closedSet=set()
  112. while not len(tempo)==0
  113. currentNode=frontier.pop(0)
  114. if(ss==dd)
  115. return(0)
  116. else
  117. if not (lol[0],lol[1]) in closedSet
  118. closedSet.add((lol[0],lol[1]))
  119. neighbors = []
  120. neighbors = dic.get(ss)
  121. for neighbor in neighbors:
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement