bfs.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. @author: Huiming Zhou
  5. """
  6. import numpy as np
  7. import matplotlib.pyplot as plt
  8. from matplotlib import colors
  9. from queue import *
  10. from mazemods import *
  11. def breadthFirstSearch(xI, xG, n, m, O):
  12. q_bfs = QueueFIFO()
  13. q_bfs.put(xI)
  14. parent = {xI: xI}
  15. actions = {xI: (0, 0)}
  16. u_set = {(-1, 0), (1, 0), (0, 1), (0, -1)}
  17. while not q_bfs.empty():
  18. x_current = q_bfs.get()
  19. if x_current == xG:
  20. break
  21. for u_next in u_set:
  22. x_next = tuple([x_current[i] + u_next[i] for i in range(len(x_current))])
  23. if 0 <= x_next[0] < n and 0 <= x_next[1] < m \
  24. and x_next not in parent \
  25. and not collisionCheck(x_current, u_next, O):
  26. q_bfs.put(x_next)
  27. parent[x_next] = x_current
  28. actions[x_next] = u_next
  29. [path_bfs, actions_bfs] = extractpath(xI, xG, parent, actions)
  30. [simple_cost, west_cost, east_cost] = cost_calculation(xI, actions_bfs, O)
  31. return path_bfs, actions_bfs, len(parent), simple_cost, west_cost, east_cost