searching.py 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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. from environment import *
  12. from bfs import *
  13. from dijkstra import *
  14. from astar import *
  15. class Searching:
  16. def __init__(self, Start_State, Goal_State, n, m, O):
  17. self.xI = Start_State
  18. self.xG = Goal_State
  19. self.u_set = [(-1, 0), (1, 0), (0, 1), (0, -1)]
  20. self.n = n
  21. self.m = m
  22. self.O = O
  23. def depth_fist_search(self):
  24. q_dfs = QueueLIFO()
  25. q_dfs.put(self.xI)
  26. parent = {self.xI: self.xI}
  27. actions = {self.xI: (0, 0)}
  28. while not q_dfs.empty():
  29. x_current = q_dfs.get()
  30. if x_current == xG:
  31. break
  32. for u_next in self.u_set:
  33. x_next = tuple([x_current[i] + u_next[i] for i in range(len(x_current))])
  34. if 0 <= x_next[0] < n and 0 <= x_next[1] < m \
  35. and x_next not in parent \
  36. and not collisionCheck(x_current, u_next, O):
  37. q_dfs.put(x_next)
  38. parent[x_next] = x_current
  39. actions[x_next] = u_next
  40. [path_dfs, actions_dfs] = extractpath(xI, xG, parent, actions)
  41. [simple_cost, west_cost, east_cost] = cost_calculation(xI, actions_dfs, O)
  42. return path_dfs, actions_dfs, len(parent), simple_cost, west_cost, east_cost
  43. if __name__ == '__main__':
  44. xI = (1, 1)
  45. xG = (23, 12)
  46. searching = Searching(xI, xG, n, m, O)
  47. [path_df, actions_df, num_visited_df, simple_cost_df, west_cost_df, east_cost_df] = searching.depth_fist_search()
  48. print('1 - Depth_First_Searching algorithm: ')
  49. print('Legal control actions: \n', actions_df)
  50. print('Number of explored nodes was [%d], basic cost was [%d], stay west cost was [%d], stay east cost was [%d] \n'
  51. % (num_visited_df, simple_cost_df, west_cost_df, east_cost_df))
  52. showPath(xI, xG, path_df, n, m, O, 'Depth First Searching algorithm')