test_aula3 finished
Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
parent
790453a712
commit
7f5313608e
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
# Module: tree_search
|
# Module: tree_search
|
||||||
#
|
#
|
||||||
# This module provides a set o classes for automated
|
# This module provides a set o classes for automated
|
||||||
# problem solving through tree search:
|
# problem solving through tree search:
|
||||||
# SearchDomain - problem domains
|
# SearchDomain - problem domains
|
||||||
|
@ -62,9 +62,10 @@ class SearchProblem:
|
||||||
|
|
||||||
# Nos de uma arvore de pesquisa
|
# Nos de uma arvore de pesquisa
|
||||||
class SearchNode:
|
class SearchNode:
|
||||||
def __init__(self,state,parent):
|
def __init__(self,state,parent, depth):
|
||||||
self.state = state
|
self.state = state
|
||||||
self.parent = parent
|
self.parent = parent
|
||||||
|
self.depth = depth
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
return "no(" + str(self.state) + "," + str(self.parent) + ")"
|
return "no(" + str(self.state) + "," + str(self.parent) + ")"
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
@ -74,12 +75,22 @@ class SearchNode:
|
||||||
class SearchTree:
|
class SearchTree:
|
||||||
|
|
||||||
# construtor
|
# construtor
|
||||||
def __init__(self,problem, strategy='breadth'):
|
def __init__(self,problem, strategy='breadth'):
|
||||||
self.problem = problem
|
self.problem = problem
|
||||||
root = SearchNode(problem.initial, None)
|
root = SearchNode(problem.initial, None, 0)
|
||||||
self.open_nodes = [root]
|
self.open_nodes = [root]
|
||||||
self.strategy = strategy
|
self.strategy = strategy
|
||||||
self.solution = None
|
self.solution = None
|
||||||
|
self.terminals = 0
|
||||||
|
self.non_terminals = 0
|
||||||
|
|
||||||
|
@property
|
||||||
|
def length(self):
|
||||||
|
return self.solution.depth if self.solution else None
|
||||||
|
|
||||||
|
@property
|
||||||
|
def avg_branching(self):
|
||||||
|
return ((self.terminals + self.non_terminals) - 1) / self.non_terminals if self.non_terminals > 0 else None
|
||||||
|
|
||||||
# obter o caminho (sequencia de estados) da raiz ate um no
|
# obter o caminho (sequencia de estados) da raiz ate um no
|
||||||
def get_path(self,node):
|
def get_path(self,node):
|
||||||
|
@ -90,17 +101,25 @@ class SearchTree:
|
||||||
return(path)
|
return(path)
|
||||||
|
|
||||||
# procurar a solucao
|
# procurar a solucao
|
||||||
def search(self):
|
def search(self,limit=None):
|
||||||
while self.open_nodes != []:
|
while self.open_nodes != []:
|
||||||
|
self.terminals = len(self.open_nodes)
|
||||||
node = self.open_nodes.pop(0)
|
node = self.open_nodes.pop(0)
|
||||||
if self.problem.goal_test(node.state):
|
if self.problem.goal_test(node.state):
|
||||||
self.solution = node
|
self.solution = node
|
||||||
return self.get_path(node)
|
return self.get_path(node)
|
||||||
|
|
||||||
|
self.non_terminals += 1
|
||||||
lnewnodes = []
|
lnewnodes = []
|
||||||
for a in self.problem.domain.actions(node.state):
|
for a in self.problem.domain.actions(node.state):
|
||||||
newstate = self.problem.domain.result(node.state,a)
|
newstate = self.problem.domain.result(node.state,a)
|
||||||
newnode = SearchNode(newstate,node)
|
if newstate not in self.get_path(node):
|
||||||
lnewnodes.append(newnode)
|
newnode = SearchNode(newstate,node,node.depth+1)
|
||||||
|
if limit != None and self.strategy == 'depth':
|
||||||
|
if newnode.depth <= limit:
|
||||||
|
lnewnodes.append(newnode)
|
||||||
|
else:
|
||||||
|
lnewnodes.append(newnode)
|
||||||
self.add_to_open(lnewnodes)
|
self.add_to_open(lnewnodes)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue