Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.88 KB | None | 0 0
  1. # Modulo: tree_search
  2. #
  3. # Fornece um conjunto de classes para suporte a resolucao de
  4. # problemas por pesquisa em arvore:
  5. # SearchDomain - dominios de problemas
  6. # SearchProblem - problemas concretos a resolver
  7. # SearchNode - nos da arvore de pesquisa
  8. # SearchTree - arvore de pesquisa, com metodos para
  9. # a respectiva construcao
  10. #
  11. # (c) Luis Seabra Lopes, Introducao a Inteligencia Artificial, 2012-2014
  12.  
  13.  
  14. # Dominios de pesquisa
  15. # Permitem calcular
  16. # as accoes possiveis em cada estado, etc
  17. class SearchDomain:
  18. # construtor
  19. def __init__(self):
  20. abstract
  21.  
  22. # lista de accoes possiveis num estado
  23. def actions(self, state):
  24. abstract
  25.  
  26. # resultado de uma accao num estado, ou seja, o estado seguinte
  27. def result(self, state, action):
  28. abstract
  29.  
  30. # custo de uma accao num estado
  31. def cost(self, state, action):
  32. abstract
  33.  
  34. # custo estimado de chegar de um estado a outro
  35. def heuristic(self, state, goal_state):
  36. abstract
  37.  
  38.  
  39. # Problemas concretos a resolver
  40. # dentro de um determinado dominio
  41. class SearchProblem:
  42. def __init__(self, domain, initial, goal):
  43. self.domain = domain
  44. self.initial = initial
  45. self.goal = goal
  46.  
  47. def goal_test(self, state):
  48. return state == self.goal
  49.  
  50.  
  51. # Nos de uma arvore de pesquisa
  52. class SearchNode:
  53. def __init__(self, state, parent, depth, heuristica, cost):
  54. self.state = state
  55. self.parent = parent
  56. self.depth = depth
  57. self.heuristica = heuristica
  58. self.cost = cost
  59.  
  60. def __str__(self):
  61. return "no(" + str(self.state) + "," + str(self.parent) + ")"
  62.  
  63. def __repr__(self):
  64. return str(self)
  65.  
  66.  
  67. # Arvores de pesquisa
  68. class SearchTree:
  69. # construtor
  70. def __init__(self, problem, strategy='greedy', limit=500):
  71. self.problem = problem
  72. self.limit = limit
  73. root = SearchNode(problem.initial, None, 0, 0, 0)
  74. self.open_nodes = [root]
  75. self.strategy = strategy
  76.  
  77. # obter o caminho (sequencia de estados) da raiz ate um no
  78. def get_path(self, node):
  79. if node.parent == None:
  80. return ([], 0)
  81. path = self.get_path(node.parent)
  82. return (path[0] + [node.state], node.depth)
  83.  
  84. # procurar a solucao
  85. def search(self):
  86. opened_states={}
  87. possiveis=[]
  88. while self.open_nodes != [] and len(possiveis)<10:
  89. node = self.open_nodes[0]
  90. if (((node.depth > self.limit) and self.limit != -1) or (self.problem.goal_test(node.state))):
  91. #return self.get_path(node)
  92. possiveis.append( node )
  93. continue
  94. self.open_nodes[0:1] = []
  95. lnewnodes = []
  96. for a in self.problem.domain.actions(node.state):
  97. newstate = self.problem.domain.result(node.state, a)
  98. if (opened_states.get(newstate) != True):
  99. lnewnodes += [SearchNode(newstate,
  100. node, node.depth + 1,
  101. self.problem.domain.heuristic(newstate, self.problem.goal),
  102. node.cost + self.problem.domain.cost(node.state, a)
  103. )]
  104. self.add_to_open(lnewnodes)
  105. opened_states[node.state] = True
  106. if possiveis != []:
  107. possiveis.sort(key = lambda x : x.cost)
  108. return self.get_path(possiveis[0])
  109. return None
  110.  
  111. # juntar novos nos a lista de nos abertos de acordo com a estrategia
  112. def add_to_open(self, lnewnodes):
  113. self.open_nodes.extend(lnewnodes)
  114. # Greedy
  115. # self.open_nodes.sort(key = lambda x : x.heuristica)
  116. # A*
  117. self.open_nodes.sort(key=lambda x: x.cost + x.heuristica*0.6)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement