test_aula4 finished

Signed-off-by: Tiago Garcia <tiago.rgarcia@ua.pt>
This commit is contained in:
Tiago Garcia 2024-10-13 19:26:19 +01:00
parent 7f5313608e
commit bb7c5a1acb
Signed by: TiagoRG
GPG Key ID: DFCD48E3F420DB42
2 changed files with 135 additions and 125 deletions

View File

@ -16,6 +16,7 @@ class Cidades(SearchDomain):
def __init__(self,connections, coordinates): def __init__(self,connections, coordinates):
self.connections = connections self.connections = connections
self.coordinates = coordinates self.coordinates = coordinates
def actions(self,city): def actions(self,city):
actlist = [] actlist = []
for (C1,C2,D) in self.connections: for (C1,C2,D) in self.connections:
@ -24,12 +25,19 @@ class Cidades(SearchDomain):
elif (C2==city): elif (C2==city):
actlist += [(C2,C1)] actlist += [(C2,C1)]
return actlist return actlist
def result(self,city,action): def result(self,city,action):
(C1,C2) = action (C1,C2) = action
if C1==city: if C1==city:
return C2 return C2
def cost(self, city, action): def cost(self, city, action):
pass (C1,C2) = action
for (X,Y,D) in self.connections:
if X==C1 and Y==C2:
return D
if X==C2 and Y==C1:
return D
def heuristic(self, city, goal_city): def heuristic(self, city, goal_city):
pass pass
def satisfies(self, city, goal_city): def satisfies(self, city, goal_city):
@ -115,5 +123,3 @@ def search_path(c1,c2,strategy):
my_tree.strategy = strategy my_tree.strategy = strategy
return my_tree.search() return my_tree.search()

View File

@ -62,12 +62,15 @@ class SearchProblem:
# Nos de uma arvore de pesquisa # Nos de uma arvore de pesquisa
class SearchNode: class SearchNode:
def __init__(self,state,parent, depth): def __init__(self,state,parent, depth, cost=0):
self.state = state self.state = state
self.parent = parent self.parent = parent
self.depth = depth self.depth = depth
self.cost = cost
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):
return str(self) return str(self)
@ -92,6 +95,10 @@ class SearchTree:
def avg_branching(self): def avg_branching(self):
return ((self.terminals + self.non_terminals) - 1) / self.non_terminals if self.non_terminals > 0 else None return ((self.terminals + self.non_terminals) - 1) / self.non_terminals if self.non_terminals > 0 else None
@property
def cost(self):
return self.solution.cost if self.solution 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):
if node.parent == None: if node.parent == None:
@ -114,11 +121,8 @@ class SearchTree:
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)
if newstate not in self.get_path(node): if newstate not in self.get_path(node):
newnode = SearchNode(newstate,node,node.depth+1) newnode = SearchNode(newstate,node,node.depth+1,node.cost+self.problem.domain.cost(node.state,a))
if limit != None and self.strategy == 'depth': if not (limit != None and self.strategy == 'depth' and newnode.depth > limit):
if newnode.depth <= limit:
lnewnodes.append(newnode)
else:
lnewnodes.append(newnode) lnewnodes.append(newnode)
self.add_to_open(lnewnodes) self.add_to_open(lnewnodes)
return None return None
@ -130,5 +134,5 @@ class SearchTree:
elif self.strategy == 'depth': elif self.strategy == 'depth':
self.open_nodes[:0] = lnewnodes self.open_nodes[:0] = lnewnodes
elif self.strategy == 'uniform': elif self.strategy == 'uniform':
pass self.open_nodes = sorted(self.open_nodes + lnewnodes, key=lambda node: node.cost)