Advertisement
Guest User

Untitled

a guest
Nov 19th, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. import copy
  2.  
  3.  
  4. def find_solution(front, closed, method):
  5. if goal_state(front[0]):
  6. print('_GOAL_FOUND_')
  7. print(front[0])
  8.  
  9. elif not front:
  10. print('_NO_SOLUTION_FOUND_')
  11.  
  12. elif front[0] in closed:
  13. new_front = copy.deepcopy(front)
  14. new_front.pop(0)
  15. find_solution(new_front, closed, method)
  16.  
  17. else:
  18. closed.append(front[0])
  19. front_copy = copy.deepcopy(front)
  20. front_children = expand_front(front_copy, method)
  21. closed_copy = copy.deepcopy(closed)
  22. find_solution(front_children, closed_copy, method)
  23.  
  24.  
  25. def find_children(state):
  26.  
  27. children = []
  28.  
  29. right_state = copy.deepcopy(state)
  30. child_right = move_right(right_state)
  31.  
  32. left_state = copy.deepcopy(state)
  33. child_left = move_left(left_state)
  34.  
  35. eat_state = copy.deepcopy(state)
  36. child_eat = eat(eat_state)
  37.  
  38. if not child_eat == state:
  39. children.append(child_eat)
  40.  
  41. if not child_left == state:
  42. children.append(child_left)
  43.  
  44. if not child_right == state:
  45. children.append(child_right)
  46.  
  47. return children
  48.  
  49.  
  50. def make_front(state):
  51. return [state]
  52.  
  53.  
  54. def can_move_left(state):
  55. print("Move left state is:")
  56. print(not state[0] == ['p'])
  57. return not state[0] == ['p']
  58.  
  59.  
  60. def can_move_right(state):
  61. print("Move right state is:")
  62. print(not state[len(state) - 1] == ['p'])
  63. return not state[len(state) - 1] == ['p']
  64.  
  65.  
  66. def expand_front(front, method):
  67. if method == 'DFS':
  68. if front:
  69. print("Front:")
  70. for i in range(len(front)):
  71. print(front[i])
  72. node = front.pop(0)
  73. for child in find_children(node):
  74. front.insert(0, child)
  75.  
  76. elif method == 'BFS':
  77. if front:
  78. print("Front:")
  79. for i in range(len(front)):
  80. print(front[i])
  81. node = front.pop(0)
  82. for child in find_children(node):
  83. front.append(child)
  84. """
  85. elif method= = 'BestFS':
  86. else: "other methods to be added"
  87. """
  88. return front
  89.  
  90.  
  91. def can_eat(state):
  92. for i in range(len(state)):
  93. print("Eat state is:")
  94. print(state[i] == ['p', 'f'])
  95. if state[i] == ['p', 'f']:
  96. return 1
  97.  
  98.  
  99. def move_left(state):
  100. if can_move_left(state):
  101. for i in range(len(state)):
  102. if state[i] == ['p']:
  103. print("Move left state")
  104. print(state[i])
  105. state[i].pop() # pop() when we move
  106. print("Remove pac-man")
  107. print(state[i])
  108. state[i-1].insert(0, 'p') # insert() to where we move
  109. print("Insert the new pac-man")
  110. print(state[i-1])
  111. return state
  112. else:
  113. return state
  114.  
  115.  
  116. def move_right(state):
  117. if can_move_right(state):
  118. for i in range(len(state)):
  119. if state[i] == ['p']:
  120. print("Move right state")
  121. print(state[i])
  122. state[i].pop() # pop() when we move
  123. print("Remove pac-man")
  124. print(state[i])
  125. state[i+1].insert(0, 'p') # insert(0, 'p') to where we move
  126. print("Insert the new pac-man")
  127. print(state[i - 1])
  128. return state
  129. else:
  130. return state
  131.  
  132.  
  133. def eat(state):
  134. if can_eat(state):
  135. for i in range(len(state)):
  136. if state[i] == ['p', 'f']:
  137. state[i].pop()
  138. return state
  139. else:
  140. return state
  141.  
  142.  
  143. def goal_state(state):
  144. print("Goal state length:")
  145. print(len(state))
  146. for i in range(len(state)):
  147. if state[i] == ['f']:
  148. return 0
  149. return 1
  150.  
  151.  
  152. def main():
  153. initial_state = [['p', 'f'], ['f'], ['f'], []]
  154. method = "DFS"
  155. print('____BEGIN__SEARCHING____')
  156. find_solution(make_front(initial_state), [], method)
  157.  
  158.  
  159. if __name__ == "__main__":
  160. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement