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

@ -1,119 +1,125 @@
# #
# Module: cidades # Module: cidades
# #
# Implements a SearchDomain for find paths between cities # Implements a SearchDomain for find paths between cities
# using the tree_search module # using the tree_search module
# #
# (c) Luis Seabra Lopes # (c) Luis Seabra Lopes
# Introducao a Inteligencia Artificial, 2012-2020 # Introducao a Inteligencia Artificial, 2012-2020
# Inteligência Artificial, 2014-2023 # Inteligência Artificial, 2014-2023
# #
from tree_search import * from tree_search import *
class Cidades(SearchDomain): 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):
actlist = [] def actions(self,city):
for (C1,C2,D) in self.connections: actlist = []
if (C1==city): for (C1,C2,D) in self.connections:
actlist += [(C1,C2)] if (C1==city):
elif (C2==city): actlist += [(C1,C2)]
actlist += [(C2,C1)] elif (C2==city):
return actlist actlist += [(C2,C1)]
def result(self,city,action): return actlist
(C1,C2) = action
if C1==city: def result(self,city,action):
return C2 (C1,C2) = action
def cost(self, city, action): if C1==city:
pass return C2
def heuristic(self, city, goal_city):
pass def cost(self, city, action):
def satisfies(self, city, goal_city): (C1,C2) = action
return goal_city==city for (X,Y,D) in self.connections:
if X==C1 and Y==C2:
return D
cidades_portugal = Cidades( if X==C2 and Y==C1:
# Ligacoes por estrada return D
[ def heuristic(self, city, goal_city):
('Coimbra', 'Leiria', 73), pass
('Aveiro', 'Agueda', 35), def satisfies(self, city, goal_city):
('Porto', 'Agueda', 79), return goal_city==city
('Agueda', 'Coimbra', 45),
('Viseu', 'Agueda', 78),
('Aveiro', 'Porto', 78), cidades_portugal = Cidades(
('Aveiro', 'Coimbra', 65), # Ligacoes por estrada
('Figueira', 'Aveiro', 77), [
('Braga', 'Porto', 57), ('Coimbra', 'Leiria', 73),
('Viseu', 'Guarda', 75), ('Aveiro', 'Agueda', 35),
('Viseu', 'Coimbra', 91), ('Porto', 'Agueda', 79),
('Figueira', 'Coimbra', 52), ('Agueda', 'Coimbra', 45),
('Leiria', 'Castelo Branco', 169), ('Viseu', 'Agueda', 78),
('Figueira', 'Leiria', 62), ('Aveiro', 'Porto', 78),
('Leiria', 'Santarem', 78), ('Aveiro', 'Coimbra', 65),
('Santarem', 'Lisboa', 82), ('Figueira', 'Aveiro', 77),
('Santarem', 'Castelo Branco', 160), ('Braga', 'Porto', 57),
('Castelo Branco', 'Viseu', 174), ('Viseu', 'Guarda', 75),
('Santarem', 'Evora', 122), ('Viseu', 'Coimbra', 91),
('Lisboa', 'Evora', 132), ('Figueira', 'Coimbra', 52),
('Evora', 'Beja', 105), ('Leiria', 'Castelo Branco', 169),
('Lisboa', 'Beja', 178), ('Figueira', 'Leiria', 62),
('Faro', 'Beja', 147), ('Leiria', 'Santarem', 78),
# extra ('Santarem', 'Lisboa', 82),
('Braga', 'Guimaraes', 25), ('Santarem', 'Castelo Branco', 160),
('Porto', 'Guimaraes', 44), ('Castelo Branco', 'Viseu', 174),
('Guarda', 'Covilha', 46), ('Santarem', 'Evora', 122),
('Viseu', 'Covilha', 57), ('Lisboa', 'Evora', 132),
('Castelo Branco', 'Covilha', 62), ('Evora', 'Beja', 105),
('Guarda', 'Castelo Branco', 96), ('Lisboa', 'Beja', 178),
('Lamego','Guimaraes', 88), ('Faro', 'Beja', 147),
('Lamego','Viseu', 47), # extra
('Lamego','Guarda', 64), ('Braga', 'Guimaraes', 25),
('Portalegre','Castelo Branco', 64), ('Porto', 'Guimaraes', 44),
('Portalegre','Santarem', 157), ('Guarda', 'Covilha', 46),
('Portalegre','Evora', 194) ], ('Viseu', 'Covilha', 57),
('Castelo Branco', 'Covilha', 62),
# City coordinates ('Guarda', 'Castelo Branco', 96),
{ 'Aveiro': (41,215), ('Lamego','Guimaraes', 88),
'Figueira': ( 24, 161), ('Lamego','Viseu', 47),
'Coimbra': ( 60, 167), ('Lamego','Guarda', 64),
'Agueda': ( 58, 208), ('Portalegre','Castelo Branco', 64),
'Viseu': ( 104, 217), ('Portalegre','Santarem', 157),
'Braga': ( 61, 317), ('Portalegre','Evora', 194) ],
'Porto': ( 45, 272),
'Lisboa': ( 0, 0), # City coordinates
'Santarem': ( 38, 59), { 'Aveiro': (41,215),
'Leiria': ( 28, 115), 'Figueira': ( 24, 161),
'Castelo Branco': ( 140, 124), 'Coimbra': ( 60, 167),
'Guarda': ( 159, 204), 'Agueda': ( 58, 208),
'Evora': (120, -10), 'Viseu': ( 104, 217),
'Beja': (125, -110), 'Braga': ( 61, 317),
'Faro': (120, -250), 'Porto': ( 45, 272),
#extra 'Lisboa': ( 0, 0),
'Guimaraes': ( 71, 300), 'Santarem': ( 38, 59),
'Covilha': ( 130, 175), 'Leiria': ( 28, 115),
'Lamego' : (125,250), 'Castelo Branco': ( 140, 124),
'Portalegre': (130,170) } 'Guarda': ( 159, 204),
) 'Evora': (120, -10),
'Beja': (125, -110),
'Faro': (120, -250),
#extra
'Guimaraes': ( 71, 300),
p = SearchProblem(cidades_portugal,'Braga','Faro') 'Covilha': ( 130, 175),
t = SearchTree(p,'breadth') 'Lamego' : (125,250),
'Portalegre': (130,170) }
print(t.search()) )
# Atalho para obter caminho de c1 para c2 usando strategy:
def search_path(c1,c2,strategy):
my_prob = SearchProblem(cidades_portugal,c1,c2) p = SearchProblem(cidades_portugal,'Braga','Faro')
my_tree = SearchTree(my_prob) t = SearchTree(p,'breadth')
my_tree.strategy = strategy
return my_tree.search() print(t.search())
# Atalho para obter caminho de c1 para c2 usando strategy:
def search_path(c1,c2,strategy):
my_prob = SearchProblem(cidades_portugal,c1,c2)
my_tree = SearchTree(my_prob)
my_tree.strategy = strategy
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)