Advertisement
Guest User

Untitled

a guest
May 20th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. class Node: # Node has only PARENT_NODE, STATE, DEPTH
  2. def __init__(self, state, parent=None, depth=0, heuristic=0, cost=0):
  3. self.STATE = state
  4. self.PARENT_NODE = parent
  5. self.DEPTH = depth
  6. self.COST = cost
  7. self.HEURISTIC = heuristic
  8. self.HEURICOST = heuristic + cost
  9.  
  10. def path(self): # Create a list of nodes from the root to this node.
  11. current_node = self
  12. path = [self]
  13. while current_node.PARENT_NODE: # while current node has parent
  14. current_node = current_node.PARENT_NODE # make parent the current node
  15. path.append(current_node) # add current node to path
  16. return path
  17.  
  18. def display(self):
  19. print(self)
  20.  
  21. def __repr__(self):
  22. return 'State: ' + str(self.STATE) + ' - Depth: ' + str(self.DEPTH) + ' - Cost: ' + str(self.COST) + ' - Heuristic: ' + str(self.HEURISTIC) + ' - Heuricost: ' + str(self.HEURICOST)
  23.  
  24. #Takes a list and finds the lowest note
  25. def FIND_LOWEST_COST(open):
  26. lowestNode = Node(INITIAL_STATE)
  27. lowestNode.COST = 0
  28. lowestNode.HEURISTIC = 0
  29. lowestNode.HEURICOST = 1000
  30. iterableNode = Node(INITIAL_STATE)
  31.  
  32. for node in open:
  33. iterableNode = node
  34. iterableNode = POPULATE_NODE(iterableNode)
  35. # print("<LOWEST_COST> Iterable node: " + iterableNode.__str__())
  36. if int(iterableNode.HEURICOST) <= int(lowestNode.HEURICOST):
  37. lowestNode = iterableNode
  38. return lowestNode
  39.  
  40.  
  41. #based on a state, it will populate the node
  42. def POPULATE_NODE(node):
  43. # print("<POPULATE_NODE> node recieved is: " + node.__str__())
  44. a, b, c = node
  45. # print("<POPULATE_NODE> A from state: " + a.__str__())
  46. # print("<POPULATE_NODE> B from state: " + b.__str__())
  47. # print("<POPULATE_NODE> C from state: " + c.__str__())
  48. node = Node(node)
  49. node.HEURISTIC = c
  50. # print("<FIND_COST> Node Heuristic after set: " + node.HEURISTIC.__str__())
  51. node.COST = b
  52. # print("<FIND_COST> Node Cost after set: " + node.COST.__str__())
  53. node.HEURICOST = int(b) + int(c)
  54. # print("<FIND_COST> Node Heuricost after set: " + node.HEURICOST.__str__())
  55. return node
  56.  
  57.  
  58. def DISTANCE_TO_CHILD(mother, child):
  59. mother = int(mother)
  60. child = int(child)
  61. distance = 0
  62. distance = child - mother
  63. return distance
  64.  
  65.  
  66. def FINAL_PATH(cameFrom, current):
  67. # print("<FINAL_PATH> cameFrom at start " + cameFrom.__str__())
  68. # print("<FINAL_PATH> current at start " + current.__str__())
  69. total_path = current.STATE
  70.  
  71. check = current.STATE
  72. while check in cameFrom:
  73. current = cameFrom[current]
  74. total_path.append(current)
  75. return total_path
  76.  
  77.  
  78. def ASTAR_SEARCH():
  79. closedSet = []
  80. openSet=[]
  81. firstNode = POPULATE_NODE(INITIAL_STATE)
  82. cameFrom = {}
  83. INSERT(firstNode.STATE, openSet)
  84. while openSet is not None:
  85. current = FIND_LOWEST_COST(openSet)
  86. print("<A STAR> current " + current.__str__())
  87. # print("<A STAR> lowest " + current.__str__())
  88. if current.STATE in GOAL_STATE:
  89. return FINAL_PATH(cameFrom, current)
  90.  
  91. INSERT(current.STATE, closedSet)
  92. openSet.remove(current.STATE)
  93. # print("<A STAR> open " + openSet.__str__())
  94.  
  95. for succesor in successor_fn(current.STATE):
  96. if succesor in closedSet:
  97. continue
  98. if succesor not in openSet:
  99. INSERT(succesor, openSet)
  100. continue
  101. succesor = POPULATE_NODE(succesor)
  102. # distance = DISTANCE_TO_CHILD(current.COST, succesor.COST)
  103. # tempCost = int(current.COST)
  104. tempCost = succesor.COST + succesor.HEURISTIC
  105.  
  106. # tempPathCost = tempCost + distance
  107. if int(succesor.COST) >= int(succesor.COST):
  108. continue
  109. print("<A STAR> succesor " + succesor.__str__())
  110. print("<A STAR> current " + current.__str__())
  111. print("<A STAR> succesor " + succesor.__str__())
  112. cameFrom[succesor.STATE] = current.STATE
  113. succesor.COST = tempCost
  114. succesor.HEURISTIC = str(succesor.HEURISTIC) + str(succesor.COST)
  115.  
  116. return print("Error, Open List is empty")
  117.  
  118.  
  119. '''
  120. Insert node in to the queue (fringe).
  121. '''
  122. def INSERT(node, queue):
  123. #queue.insert(0, node) # DFS
  124. queue.append(node) # BFS
  125. return queue
  126.  
  127.  
  128. '''
  129. Insert list of nodes into the fringe
  130. '''
  131. def INSERT_ALL(list, queue):
  132. for node in list:
  133. INSERT(node, queue)
  134. return queue
  135.  
  136.  
  137. '''
  138. Remove first element from fringe
  139. '''
  140. def REMOVE_FIRST(queue):
  141. if len(queue) != 0:
  142. return queue.pop(0)
  143. return []
  144.  
  145.  
  146. '''
  147. Successor function, mapping the nodes to its successors
  148. '''
  149. def successor_fn(state): # Lookup list of successor states
  150. return STATE_SPACE[state] # successor_fn( 'C' ) returns ['F', 'G']
  151.  
  152.  
  153. #initial state, path count of 0, cost = heuristic
  154. INITIAL_STATE = ('A', '0', '6')
  155.  
  156. #Initial State A - end point is either K or L
  157. #Current state, path, cost
  158. GOAL_STATE = [('L', '11', '0'),('L', '13', '0'),('L', '10', '0'),('K', '13', '0'),('K', '11', '0'),('K', '12', '0'),('K', '14', '0')]
  159. #GOAL_STATE = ('L', '11', '0')
  160. #GOAL_STATE2 = ('K', '13', '0')
  161.  
  162. '''
  163. Complete state space including loops back to current state
  164. '''
  165. # Current State - Actions:
  166. STATE_SPACE = {('A', '0', '6'): [('B', '1', '5'), ('C', '2', '5'), ('D', '4', '2')],
  167. ('B', '1', '5'): [('F', '6', '5'), ('E', '5', '4')],
  168. ('C', '2', '5'): [('E', '3', '4')],
  169. ('D', '4', '2'): [('J', '6', '1'), ('I', '8', '2'), ('H', '5', '1')],
  170.  
  171. ('F', '6', '5'): [('G', '7', '4')],
  172.  
  173. ('E', '3', '4'): [('G', '5', '4'), ('H', '6', '1')],
  174. ('E', '5', '4'): [('G', '7', '4'), ('H', '8', '1')],
  175. ('I', '8', '2'): [('L', '11', '0')],
  176. ('J', '6', '1'): [],
  177.  
  178. ('G', '7', '4'): [('K', '13', '0')],
  179. ('G', '5', '4'): [('A', '15', '0')],
  180.  
  181. ('H', '6', '1'): [('L', '11', '0'), ('K', '12', '0')],
  182. ('H', '8', '1'): [('L', '13', '0'), ('K', '14', '0')],
  183. ('H', '5', '1'): [('L', '10', '0'), ('K', '11', '0')],
  184.  
  185. ('L', '11', '0'): [],
  186. ('L', '13', '0'): [],
  187. ('L', '10', '0'): [],
  188.  
  189. ('K', '13', '0'): [],
  190. ('K', '11', '0'): [],
  191. ('K', '12', '0'): [],
  192. ('K', '14', '0'): []
  193. }
  194.  
  195. '''
  196. Run Astar and display the nodes in the path to goal node
  197. '''
  198. def run():
  199. path = ASTAR_SEARCH()
  200.  
  201.  
  202.  
  203.  
  204.  
  205. if __name__ == '__main__':
  206. run()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement