Advertisement
Guest User

Untitled

a guest
Feb 17th, 2020
286
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.23 KB | None | 0 0
  1. def a_search(kaart, goal):
  2. path = []
  3. frontier = PriorityQueue()
  4. nei = []
  5. frontier.put((heuristic(start, goal), start))
  6. came_from = {}
  7. came_from[start] = None
  8. rows = len(kaart)
  9. columns = len(kaart[0])
  10. old = None
  11. visited = []
  12. flagM = 0
  13. cost_so_far = {}
  14. cost_so_far[start] = 0
  15. flag = True
  16.  
  17.  
  18. while not frontier.empty():
  19.  
  20. if len(nei) > 1:
  21. if flag is True:
  22. flag = False
  23. a = nei[0]
  24. frontier.put(nei[1])
  25. else:
  26. flag = True
  27. a = nei[1]
  28. frontier.put(nei[0])
  29. elif len(nei) == 1:
  30. a = nei[0]
  31. else:
  32. a = frontier.get()
  33. nei.clear()
  34. current = a[1]
  35. prio = a[0]
  36. print("Start = " + str(current))
  37. if current[0] - 1 >= 0 and kaart[current[0] - 1][current[1]] != "*" and (current[0] - 1, current[1]) not in came_from.keys():
  38. next = (current[0] - 1, current[1])
  39. came_from[(current[0] - 1, current[1])] = current
  40. flagM += 1
  41. new_cost = cost_so_far[current] + 1
  42. if next not in cost_so_far or new_cost < cost_so_far[next]:
  43. cost_so_far[next] = new_cost
  44. priority = new_cost + heuristic(next, goal) # g(n) + h(n)
  45. came_from[next] = current
  46. print(str(priority) + " " + str(next))
  47. if prio == priority:
  48. nei.append((priority, next))
  49. else:
  50. frontier.put((priority, next))
  51. if current[0] + 1 < rows and kaart[current[0] + 1][current[1]] != "*" and (current[0] + 1, current[1]) not in came_from.keys():
  52. next = (current[0] + 1, current[1])
  53. came_from[(current[0] + 1, current[1])] = current
  54. flagM += 1
  55. new_cost = cost_so_far[current] + 1
  56. if next not in cost_so_far or new_cost < cost_so_far[next]:
  57. cost_so_far[next] = new_cost
  58. priority = new_cost + heuristic(next, goal) # g(n) + h(n)
  59. came_from[next] = current
  60. if prio == priority:
  61. nei.append((priority, next))
  62. else:
  63. frontier.put((priority, next))
  64.  
  65.  
  66. print(str(priority) + " " + str(next))
  67. if current[1] + 1 < columns and kaart[current[0]][current[1] + 1] != "*" and (current[0], current[1] + 1) not in came_from.keys():
  68. next = (current[0], current[1] + 1)
  69. came_from[(current[0], current[1] + 1)] = current
  70. flagM += 1
  71. new_cost = cost_so_far[current] + 1
  72. if next not in cost_so_far or new_cost < cost_so_far[next]:
  73. cost_so_far[next] = new_cost
  74. priority = new_cost + heuristic(next, goal) # g(n) + h(n)
  75. came_from[next] = current
  76. if prio == priority:
  77. nei.append((priority, next))
  78. else:
  79. frontier.put((priority, next))
  80. print(str(priority) + " " + str(next))
  81. if current[1] - 1 >= 0 and kaart[current[0]][current[1] - 1] != "*" and (current[0], current[1] - 1) not in came_from.keys():
  82. next = (current[0], current[1] - 1)
  83. came_from[(current[0], current[1] - 1)] = current
  84. flagM += 1
  85. new_cost = cost_so_far[current] + 1
  86. if next not in cost_so_far or new_cost < cost_so_far[next]:
  87. cost_so_far[next] = new_cost
  88. priority = new_cost + heuristic(next, goal) # g(n) + h(n)
  89. came_from[next] = current
  90. if prio == priority:
  91. nei.append((priority, next))
  92. else:
  93. frontier.put((priority, next))
  94. print(str(priority) + " " + str(next))
  95. if current not in came_from.keys():
  96. came_from[current] = old
  97. if kaart[current[0]][current[1]] == "D":
  98. break
  99. visited.append(current)
  100. old = current
  101.  
  102.  
  103.  
  104. x = goal
  105. while x != start:
  106. path.append(x)
  107. x = came_from[x]
  108. path.append(start)
  109. return path
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement