This repository has been archived by the owner on Apr 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tree_search.py
183 lines (138 loc) · 5.69 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# código retirado do exemplo das aulas práticas
from abc import ABC, abstractmethod
import time
import asyncio
import queue
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, state, action):
pass
# custo de uma accao num estado
@abstractmethod
def cost(self, state, 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, action, keeper, movements, counter, hash_):
self.state = state
self.parent = parent
self.depth = depth
self.cost = cost
self.heuristic = heuristic
self.action = action
self.keeper = keeper
self.movements = movements
self.counter = counter
self.hash_ = hash_
def __str__(self):
return "no(" + str(self.state) + "," + str(self.parent) + ")"
def __repr__(self):
return str(self)
def __lt__(self, node):
return self.counter > node.counter
class SearchTree:
# construtor
def __init__(self,problem, keeper_coord):
self.problem = problem
root = SearchNode(problem.initial, None, 0, 0, problem.domain.heuristic(problem.initial, problem.goal), None, keeper_coord, '', 0, hash(''))
self.open_nodes = queue.PriorityQueue()
self.open_nodes.put((0, root))
self.terminals = 0
self.non_terminals = 1
self.visited = set()
self.in_queue = {hash('')}
self.counter = 0
@property
def length(self):
return self.solution.depth
@property
def cost(self):
return self.solution.cost
@property
def avg_branching(self):
return (self.terminals + self.non_terminals - 1) / self.non_terminals
# 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
def get_plan(self, node):
if node.parent == None:
return ''
plan = self.get_plan(node.parent)
plan += node.movements + node.action
return plan
@property
def plan(self):
return self.get_plan(self.solution)
async def search(self):
while not self.open_nodes.empty():
await asyncio.sleep(0)
priority, node = self.open_nodes.get()
if self.problem.goal_test(node.state):
self.solution = node
self.terminals = self.open_nodes.qsize()
return True
self.visited.add(node.hash_)
self.in_queue.remove(node.hash_)
self.non_terminals += 1
for a, mov in self.problem.domain.actions(node):
newstate = a.result
state_hash = hash(str(sorted(newstate)) + str(a.args))
if state_hash in self.visited or state_hash in self.in_queue:
continue
self.in_queue.add(state_hash)
heuristica = self.problem.domain.heuristic(newstate, self.problem.goal)
newnode = SearchNode(newstate, node, node.depth + 1, 1,
heuristica, a.__class__.__name__.lower(), a.args, mov, self.counter, state_hash)
self.open_nodes.put((heuristica, newnode))
self.counter += 1
return None
def searchSync(self, limit=None):
while not self.open_nodes.empty():
priority, node = self.open_nodes.get()
if self.problem.goal_test(node.state):
self.solution = node
self.terminals = self.open_nodes.qsize()
return self.get_path(node)
self.non_terminals += 1
self.visited.add(node.hash_)
self.in_queue.remove(node.hash_)
for a, mov in self.problem.domain.actions(node):
newstate = a.result
state_hash = hash(str(sorted(newstate)) + str(a.args))
if state_hash in self.visited or state_hash in self.in_queue:
continue
self.in_queue.add(state_hash)
custo = node.cost + self.problem.domain.cost(node.state, a)
heuristica = self.problem.domain.heuristic(newstate, self.problem.goal)
newnode = SearchNode(newstate, node, node.depth + 1, custo,
heuristica, a.__class__.__name__.lower(), a.args, mov, self.counter, state_hash)
self.open_nodes.put((custo + heuristica, newnode))
self.counter += 1
return None