Advertisement
bar2104

112233

Apr 18th, 2019
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. from heapq import heappush, heappop
  2. from sys import maxsize
  3. import curses, random
  4. import re
  5. from serial import Serial
  6.  
  7. ser = Serial('/dev/ttyACM0',9600)
  8.  
  9.  
  10. DUNGEON = """
  11. #####################
  12. # # ### # #
  13. # # # # # # #
  14. # # # # ## # #
  15. # ## ## ## ## #
  16. ## ### # ### ## #
  17. # # # #
  18. #### ### ##### ###
  19. #### ## ### ###
  20. ## ##### #
  21. #####################
  22. """
  23.  
  24. F, H, NUM, G, POS, OPEN, VALID, PARENT = range(8)
  25.  
  26. HEIGHT, WIDTH = 11, 21
  27. MAX_LIMIT = HEIGHT * WIDTH
  28. LIMIT = MAX_LIMIT // 2
  29. DEBUG = False
  30. COLOR = True
  31.  
  32. def astar(start_pos, neighbors, goal, start_g, cost, heuristic, limit=maxsize):
  33. nums = iter(range(maxsize))
  34. start_h = heuristic(start_pos)
  35. start = [start_g + start_h, start_h, next(nums), start_g, start_pos, True,
  36. True, None]
  37. nodes = {start_pos: start}
  38. heap = [start]
  39. best = start
  40. while heap:
  41. current = heappop(heap)
  42. current[OPEN] = False
  43. if goal(current[POS]):
  44. best = current
  45. break
  46. for neighbor_pos in neighbors(current[POS]):
  47. neighbor_g = current[G] + cost(current[POS], neighbor_pos)
  48. neighbor = nodes.get(neighbor_pos)
  49. if neighbor is None:
  50. if len(nodes) >= limit:
  51. continue
  52. neighbor_h = heuristic(neighbor_pos)
  53. neighbor = [neighbor_g + neighbor_h, neighbor_h, next(nums),
  54. neighbor_g, neighbor_pos, True, True, current[POS]]
  55. nodes[neighbor_pos] = neighbor
  56. heappush(heap, neighbor)
  57. if neighbor_h < best[H]:
  58. best = neighbor
  59. elif neighbor_g < neighbor[G]:
  60. if neighbor[OPEN]:
  61. neighbor[VALID] = False
  62. nodes[neighbor_pos] = neighbor = neighbor[:]
  63. neighbor[F] = neighbor_g + neighbor[H]
  64. neighbor[NUM] = next(nums)
  65. neighbor[G] = neighbor_g
  66. neighbor[VALID] = True
  67. neighbor[PARENT] = current[POS]
  68. heappush(heap, neighbor)
  69. else:
  70. neighbor[F] = neighbor_g + neighbor[H]
  71. neighbor[G] = neighbor_g
  72. neighbor[PARENT] = current[POS]
  73. neighbor[OPEN] = True
  74. heappush(heap, neighbor)
  75. while heap and not heap[0][VALID]:
  76. heappop(heap)
  77.  
  78. path = []
  79. current = best
  80. while current[PARENT] is not None:
  81. path.append(current[POS])
  82. current = nodes[current[PARENT]]
  83. path.reverse()
  84. return path
  85.  
  86.  
  87. class Cell(object):
  88. def __init__(self, char):
  89. self.char = char
  90. self.tag = 0
  91. self.index = 0
  92. self.neighbors = None
  93.  
  94.  
  95. class Grid(object):
  96.  
  97. def __init__(self, cells):
  98. self.height, self.width = len(cells), len(cells[0])
  99. self.cells = cells
  100.  
  101. def __contains__(self, pos):
  102. y, x = pos
  103. return 0 <= y < self.height and 0 <= x < self.width
  104.  
  105. def __getitem__(self, pos):
  106. y, x = pos
  107. return self.cells[y][x]
  108.  
  109. def neighbors(self, y, x):
  110. for dy, dx in ((-1, -1), (-1, 0), (-1, 1), (0, -1), (0, 1), (1, -1),
  111. (1, 0), (1, 1)):
  112. if (y + dy, x + dx) in self:
  113. yield y + dy, x + dx
  114.  
  115.  
  116. def parse_grid(grid_str, width, height):
  117.  
  118. # Split the grid string into lines.
  119. lines = [line.rstrip() for line in grid_str.splitlines()[1:]]
  120.  
  121. # Pad the top and bottom.
  122. top = (height - len(lines)) // 2
  123. bottom = (height - len(lines) + 1) // 2
  124. lines = ([''] * top + lines + [''] * bottom)[:height]
  125.  
  126. # Pad the left and right sides.
  127. max_len = max(len(line) for line in lines)
  128. left = (width - max_len) // 2
  129. lines = [' ' * left + line.ljust(width - left)[:width - left]
  130. for line in lines]
  131.  
  132. # Create the grid.
  133. cells = [[Cell(char) for char in line] for line in lines]
  134. return Grid(cells)
  135.  
  136.  
  137. class Engine(object):
  138.  
  139. def __init__(self, grid):
  140. self.grid = grid
  141. self.y = 1
  142. self.x = 1
  143. self.goal = (10,20) #Target
  144. self.limit = LIMIT
  145. self.tag = 1
  146. self.nodes = {}
  147. self.path = []
  148. self.dirty = True
  149. self.debug = DEBUG
  150.  
  151.  
  152.  
  153. def update_path(self):
  154. if not self.dirty:
  155. return
  156. self.dirty = False
  157. self.tag += 1
  158. def neighbors(pos):
  159. cell = self.grid[pos]
  160. if cell.neighbors is None:
  161. y, x = pos
  162. cell.neighbors = []
  163. for neighbor_y, neighbor_x in self.grid.neighbors(y, x):
  164. if self.grid[neighbor_y, neighbor_x].char != '#':
  165. cell.neighbors.append((neighbor_y, neighbor_x))
  166. return cell.neighbors
  167. def goal(pos):
  168. return pos == self.goal
  169. def cost(from_pos, to_pos):
  170. from_y, from_x = from_pos
  171. to_y, to_x = to_pos
  172. return 14 if to_y - from_y and to_x - from_x else 10
  173. def estimate(pos):
  174. y, x = pos
  175. goal_y, goal_x = self.goal
  176. dy, dx = abs(goal_y - y), abs(goal_x - x)
  177. return min(dy, dx) * 14 + abs(dy - dx) * 10
  178. def debug(nodes):
  179. self.nodes = nodes
  180. self.path = astar((self.y, self.x), neighbors, goal, 0, cost,
  181. estimate, self.limit)
  182.  
  183.  
  184.  
  185.  
  186.  
  187.  
  188.  
  189.  
  190.  
  191. grid = parse_grid(DUNGEON, WIDTH, HEIGHT)
  192. engine = Engine(grid)
  193. engine.update_path()
  194. print(engine.path)
  195.  
  196.  
  197.  
  198. from http.server import BaseHTTPRequestHandler, HTTPServer
  199.  
  200. class StoreHandler(BaseHTTPRequestHandler):
  201.  
  202. def do_GET(self):
  203. print(self.path)
  204. aud = self.path
  205. print("Path : ", self.path)
  206.  
  207. result = re.search(r'\/[0-9][0-9][0-9]', aud)
  208. if result.group(0) != None:
  209. print('ok')
  210. result = re.search(r'[0-9][0-9][0-9]', aud)
  211. aud = int(result.group(0))
  212. if aud == 100:
  213. x=10
  214. y=20
  215. elif aud == 101:
  216. x=3
  217. y=20
  218.  
  219. engine.goal = (x,y)
  220. engine.dirty = True
  221. print(engine.goal)
  222. engine.update_path()
  223. print(engine.path)
  224.  
  225. command = ""
  226.  
  227. for i in range(1,len(engine.path)-1):
  228. if engine.path[i-1][0] > engine.path[i][0]:
  229. command += "T"
  230. elif engine.path[i-1][0] < engine.path[i][0]:
  231. command += "D"
  232.  
  233. if engine.path[i-1][1] < engine.path[i][1]:
  234. command += "R"
  235. elif engine.path[i-1][1] > engine.path[i][1]:
  236. command += "L"
  237. print(command)
  238. ser.write(command.encode('ascii'))
  239.  
  240. def do_POST(self):
  241. self.send_response(200)
  242.  
  243.  
  244. server = HTTPServer(('', 8080), StoreHandler)
  245. server.serve_forever()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement