Advertisement
Guest User

Untitled

a guest
Apr 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. import math
  2. from simpleai.search import SearchProblem, greedy
  3.  
  4.  
  5. class SRTS(SearchProblem):
  6. def __init__(self, board):
  7. self.board = board
  8. self.goal = (0, 0)
  9.  
  10. for y in range(len(self.board)):
  11. for x in range(len(self.board[y])):
  12. if self.board[y][x].lower() == "o":
  13. self.initial = (x, y)
  14. elif self.board[y][x].lower() == "x":
  15. self.goal = (x, y)
  16.  
  17. super(SRTS, self).__init__(initial_state=self.initial)
  18.  
  19.  
  20. def actions(self, state):
  21. actions = []
  22. for action in COSTS.keys():
  23. newx, newy = self.result(state, action)
  24. if self.board[newy][newx] != "#" and self.board[newy][newx] != "_":
  25. actions.append(action)
  26.  
  27. return actions
  28.  
  29. def result(self, state, action):
  30. x, y = state
  31. if action.count("up"):
  32. y -= 1
  33. if action.count("down"):
  34. y += 1
  35. if action.count("left"):
  36. x -= 1
  37. if action.count("right"):
  38. x += 1
  39. new_state = (x, y)
  40. return new_state
  41.  
  42. def is_goal(self, state):
  43. return state == self.goal
  44.  
  45. def is_safe(self, state):
  46. x, y = state
  47. return self.board[x][y] == '+' or self.board[x][y] == 'x'
  48.  
  49. def cost(self, state, action, state2):
  50. return COSTS[action]
  51.  
  52. def heuristic(self, state):
  53. x, y = state
  54. gx, gy = self.goal
  55.  
  56. safe_dist = float('inf')
  57. for by in range(len(self.board)):
  58. for bx in range(len(self.board[by])):
  59. if self.board[by][bx] == "+":
  60. safe_dist = \
  61. min(math.sqrt((x - bx) ** 2 + (y - by) ** 2),safe_dist)
  62.  
  63. if safe_dist == 0: safe_dist = 2
  64. goal_dist = math.sqrt((x - gx) ** 2 + (y - gy) ** 2)
  65.  
  66. return min(safe_dist,goal_dist)
  67.  
  68. from simpleai.search import greedy as best_first
  69. if __name__ == "__main__":
  70. TRACK = """
  71. ###################################
  72. ###################################
  73. ###### + ##
  74. #### ##
  75. ### x
  76. ## + ##
  77. ## + ##
  78. ## ######################
  79. ## #######################
  80. ## + ########################
  81. ## ########################
  82. ## ########################
  83. ## + ########################
  84. ## ########################
  85. ## ########################
  86. ## o ########################
  87. ___________________________________
  88. """
  89.  
  90. print(TRACK)
  91. TRACK = [list(x) for x in TRACK.split("\n") if x]
  92. result = None
  93. cost_regular = 1.0
  94. cost_diagonal = math.sqrt(2)
  95.  
  96. COSTS = {
  97. "up": cost_regular,
  98. "down": cost_regular,
  99. "left": cost_regular,
  100. "right": cost_regular,
  101. "up left": cost_diagonal,
  102. "up right": cost_diagonal,
  103. "down left": cost_diagonal,
  104. "down right": cost_diagonal,
  105. }
  106.  
  107. problem = SRTS(TRACK)
  108. s_root = problem.initial
  109. s_goal = problem.goal
  110.  
  111. while s_root != s_goal:
  112. Comfortables = []
  113. c = best_first(problem, graph_search=True)
  114. t = s_root
  115. if c is not None or problem.is_safe(c):
  116. Comfortables.append(t)
  117. if len(Comfortables) is not 0:
  118. result = greedy(problem, graph_search=True)
  119. break
  120.  
  121. ancestors = [a[1] for a in c.path()]
  122. s_safe = Comfortables[0]
  123. s_target = Comfortables[0]
  124. for a in ancestors:
  125. Comfortables.append(a)
  126.  
  127. if problem.is_safe(Comfortables[0]):
  128. s_target = s_safe
  129.  
  130. elif problem.is_safe(problem.result(s_target,'up')) or \
  131. problem.is_safe(problem.result(s_target, 'up')) or \
  132. problem.is_safe(problem.result(s_target, 'up')) or \
  133. problem.is_safe(problem.result(s_target, 'up')) or \
  134. problem.is_safe(problem.result(s_target, 'up')) or \
  135. problem.is_safe(problem.result(s_target, 'up')) or \
  136. problem.is_safe(problem.result(s_target, 'up')) or \
  137. problem.is_safe(problem.result(s_target, 'up')):
  138. s_target = s_root
  139. else:
  140. print('No Solution')
  141. break
  142.  
  143. problem.initial = s_root
  144. problem.goal = s_target
  145. s_root = s_target
  146.  
  147.  
  148. path = [x[1] for x in result.path()]
  149. print()
  150. for y in range(len(TRACK)):
  151. for x in range(len(TRACK[y])):
  152. if (x, y) == problem.initial:
  153. print('o', end='')
  154. elif (x, y) == problem.goal:
  155. print('x', end='')
  156. elif (x, y) in path:
  157. print('·', end='')
  158. else:
  159. print(TRACK[y][x], end='')
  160.  
  161. print()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement