Compare commits

..

No commits in common. "2d498eb23162177eb833879081e7b075caea4a51" and "7f5313608ec0b988da0e37a4c9c7b387e65c8f13" have entirely different histories.

2 changed files with 125 additions and 161 deletions

View File

@ -16,7 +16,6 @@ class Cidades(SearchDomain):
def __init__(self,connections, coordinates):
self.connections = connections
self.coordinates = coordinates
def actions(self,city):
actlist = []
for (C1,C2,D) in self.connections:
@ -25,26 +24,14 @@ class Cidades(SearchDomain):
elif (C2==city):
actlist += [(C2,C1)]
return actlist
def result(self,city,action):
(C1,C2) = action
if C1==city:
return C2
def cost(self, city, action):
(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
pass
def heuristic(self, city, goal_city):
coords_city = cidades_portugal.coordinates[city]
coords_goal_city = cidades_portugal.coordinates[goal_city]
return ((coords_city[0] - coords_goal_city[0])**2 + (coords_city[1] - coords_goal_city[1])**2)**0.5
pass
def satisfies(self, city, goal_city):
return goal_city==city
@ -128,3 +115,5 @@ def search_path(c1,c2,strategy):
my_tree.strategy = strategy
return my_tree.search()

View File

@ -62,16 +62,12 @@ class SearchProblem:
# Nos de uma arvore de pesquisa
class SearchNode:
def __init__(self,state,parent, depth, cost=0, heuristic=0):
def __init__(self,state,parent, depth):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
self.heuristic = heuristic
def __str__(self):
return "no(" + str(self.state) + "," + str(self.parent) + ")"
def __repr__(self):
return str(self)
@ -87,8 +83,6 @@ class SearchTree:
self.solution = None
self.terminals = 0
self.non_terminals = 0
self.highest_cost_nodes = [root]
self.average_depth = 0
@property
def length(self):
@ -98,10 +92,6 @@ class SearchTree:
def avg_branching(self):
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
def get_path(self,node):
if node.parent == None:
@ -115,10 +105,8 @@ class SearchTree:
while self.open_nodes != []:
self.terminals = len(self.open_nodes)
node = self.open_nodes.pop(0)
if self.problem.goal_test(node.state):
self.solution = node
self.average_depth = self.average_depth / (self.terminals + self.non_terminals)
return self.get_path(node)
self.non_terminals += 1
@ -126,22 +114,13 @@ class SearchTree:
for a in self.problem.domain.actions(node.state):
newstate = self.problem.domain.result(node.state,a)
if newstate not in self.get_path(node):
newnode = SearchNode(
newstate,
node,
node.depth+1,
node.cost+self.problem.domain.cost(node.state,a),
self.problem.domain.heuristic(newstate,self.problem.goal)
)
if not (limit != None and self.strategy == 'depth' and newnode.depth > limit):
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)
if newnode.cost > self.highest_cost_nodes[0].cost:
self.highest_cost_nodes = [newnode]
elif newnode.cost == self.highest_cost_nodes[0].cost:
self.highest_cost_nodes.append(newnode)
self.average_depth += newnode.depth
self.add_to_open(lnewnodes)
return None
# juntar novos nos a lista de nos abertos de acordo com a estrategia
@ -151,9 +130,5 @@ class SearchTree:
elif self.strategy == 'depth':
self.open_nodes[:0] = lnewnodes
elif self.strategy == 'uniform':
self.open_nodes = sorted(self.open_nodes + lnewnodes, key=lambda node: node.cost)
elif self.strategy == 'greedy':
self.open_nodes = sorted(self.open_nodes + lnewnodes, key=lambda node: node.heuristic)
elif self.strategy == 'a*':
self.open_nodes = sorted(self.open_nodes + lnewnodes, key=lambda node: node.cost + node.heuristic)
pass