-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree_search.py
137 lines (103 loc) · 3.75 KB
/
tree_search.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
# Students:
# Bárbara Nóbrega Galiza - 105937
# Pedro Daniel Fidalgo de Pinho - 109986
"""
Using a somewhat modified version of the tree_search provided to us
during the practical classes
Credit :
(c) Luis Seabra Lopes
Introducao a Inteligencia Artificial, 2012-2019,
Inteligência Artificial, 2014-2019
"""
from abc import ABC, abstractmethod
import time
class SearchDomain(ABC):
# construtor
@abstractmethod
def __init__(self):
pass
# lista de accoes possiveis num estado
@abstractmethod
def actions(self, state):
pass
# resultado de uma accao num estado, ou seja, o estado seguinte
@abstractmethod
def result(self, action):
pass
# custo de uma accao num estado
@abstractmethod
def cost(self, action):
pass
# custo estimado de chegar de um estado a outro
@abstractmethod
def heuristic(self, state, goal):
pass
# test if the given "goal" is satisfied in "state"
@abstractmethod
def satisfies(self, state, goal):
pass
class SearchProblem:
def __init__(self, domain, initial, goal):
self.domain = domain
self.initial = initial
self.goal = goal
def goal_test(self, state):
return self.domain.satisfies(state,self.goal)
class SearchNode:
def __init__(self, state, parent, depth, cost, heuristic):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
self.heuristic = heuristic
def in_parent(self, newstate):
if(self.state[0] == newstate[0] and self.state[1] == newstate[1]):
return True
if(self.parent == None): # é a root
return False
return self.parent.in_parent(newstate)
def __str__(self):
return "no(" + str(self.state) + "," + str(self.parent) + ")"
def __repr__(self):
return str(self)
class SearchTree:
def __init__(self,problem):
self.problem = problem
root = SearchNode(problem.initial, None, 0, 0, problem.domain.heuristic(problem.initial, problem.goal))
self.open_nodes = [root]
self.solution = None
self.non_terminals = 0
self._total_depth = 0
self.start_time = round(time.time() * 1000)
# obter o caminho (sequencia de estados) da raiz ate um no
def get_path(self,node):
if node.parent == None:
return [node.state]
path = self.get_path(node.parent)
path += [node.state]
return(path)
# procurar a solucao
def search(self, limit = None):
while self.open_nodes != []:
node = self.open_nodes.pop(0)
if self.problem.goal_test(node.state):
self.solution = node
return self.get_path(node)
self.non_terminals += 1
lnewnodes = []
for a in self.problem.domain.actions(node.state):
if self.start_time + 300 < round(time.time() * 1000):
return None
newstate = self.problem.domain.result(a)
if not node.in_parent(newstate) and (limit == None or node.depth < limit):
cost = node.cost + self.problem.domain.cost(a)
heuristic = self.problem.domain.heuristic(newstate, self.problem.goal)
newnode = SearchNode(newstate,node, node.depth + 1, cost, heuristic)
lnewnodes.append(newnode)
self._total_depth += newnode.depth
self.add_to_open(lnewnodes)
return None
# juntar novos nos a lista de nos abertos
def add_to_open(self,lnewnodes):
self.open_nodes.extend(lnewnodes)
self.open_nodes.sort(key = lambda x : x.heuristic + x.cost)