Repository navigation
Expand file tree
/
Copy pathexplorer.py
More file actions
205 lines (183 loc) · 8.39 KB
/
Copy pathexplorer.py
File metadata and controls
205 lines (183 loc) · 8.39 KB
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
## EXPLORER AGENT
### @Author: Tacla, UTFPR
### It walks randomly in the environment looking for victims.
# IMPORTS
import sys
import os
import random
from abstract_agent import AbstractAgent
from physical_agent import PhysAgent
from abc import ABC, abstractmethod
from map import Map, THERE_VICTIM, UNTRIED_ACTIONS, UNBACKTRACKED
from stack import Stack
# MACROS
DELTA = [(0,-1),(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1)]
VISIT = 1
# Functions
def updatePosition(position: list, delta: tuple) -> list:
return [position[0] + delta[0], position[1] + delta[1]]
def reverse(delta: list | tuple) -> list:
return [delta[0]*-1, delta[1]*-1]
class Explorer(AbstractAgent):
def __init__(self, env, config_file, resc):
""" Construtor do agente random on-line
@param env referencia o ambiente
@config_file: the absolute path to the explorer's config file
@param resc referencia o rescuer para poder acorda-lo
"""
super().__init__(env, config_file)
# Specific initialization for the rescuer
self.resc = resc # reference to the rescuer agent
self.rtime = self.TLIM # remaining time to explore
self.map = Map() # Map of environment
self.map_vision = Map() # Map of Vision Não confirma as vítimas
self.backtracked = Map() # Stack com delta daquela posição, só é executado quando untried retorna None
self.position = [0,0] # [x,y]
self.action = [0,0] # ultima ação tomada
def deliberate(self) -> bool:
""" The agent chooses the next action. The simulator calls this
method at each cycle. Must be implemented in every agent"""
dx = 0
dy = 0
# No more actions, time almost ended
if self.rtime < 10.0:
# time to wake up the rescuer
# pass the walls and the victims (here, they're empty)
print(f"{self.NAME} I believe I've remaining time of {self.rtime:.1f}")
self.resc.go_save_victims([],[])
return False
if not self.map.knowPosition(tuple(self.position)):
actions_results = self.createActionsResults(self.position) # list
content: list = [PhysAgent.CLEAR] + actions_results
self.map.addPosition(tuple(self.position), content)
untried_actions: Stack = content[1]
unbacktracked = None
if self.backtracked.knowPosition(tuple(self.position)):
unbacktracked: Stack = self.backtracked.getContent(tuple(self.position))
if not untried_actions.empty():
self.action = list(untried_actions.pop())
dx = self.action[0]
dy = self.action[1]
elif(unbacktracked is not None):
if not unbacktracked.empty():
self.action = list(unbacktracked.pop())
dx = self.action[0]
dy = self.action[1]
else:
dx = 0
dy = 0
else:
dx = 0
dy = 0
self.position = updatePosition(self.position, self.action)
if self.backtracked.knowPosition(tuple(self.position)):
self.backtracked.getContent(tuple(self.position)).push(reverse(self.action))
else:
backaction = Stack()
backaction.push(reverse(self.action))
self.backtracked.addPosition(tuple(self.position), backaction)
else:
content: list = self.map.getContent(tuple(self.position)) # [obstacle, untried_actions(Pilha), Result(dict)]
actions: Stack = content[1]
if(not actions.empty()):
self.action = actions.pop()
dx = self.action[0]
dy = self.action[1]
self.position = updatePosition(self.position, self.action)
if self.backtracked.knowPosition(tuple(self.position)):
self.backtracked.getContent(tuple(self.position)).push(reverse(self.action))
else:
backaction = Stack()
backaction.push(reverse(self.action))
self.backtracked.addPosition(tuple(self.position), backaction)
else:
unbacktracked: Stack = None
if self.backtracked.knowPosition(tuple(self.position)):
unbacktracked: Stack = self.backtracked.getContent(tuple(self.position))
if(unbacktracked is not None):
if(not unbacktracked.empty()):
print("Unbacktracked")
self.action = unbacktracked.pop()
dx = self.action[0]
dy = self.action[1]
self.position = updatePosition(self.position, self.action)
else:
dx = 0 # Aleatorio
dy = 0
else:
dx = 0
dy = 0
'''
if(dx == 0 and dy == 0):
print("Aleatório")
dx = random.choice([-1, 0, 1])
if dx == 0:
dy = random.choice([-1, 1])
else:
dy = random.choice([-1, 0, 1])
'''
# Check the neighborhood obstacles
# obstacles = self.body.check_obstacles()
# Moves the body to another position
result = self.body.walk(dx, dy)
# Update remaining time
if dx != 0 and dy != 0:
self.rtime -= self.COST_DIAG
else:
self.rtime -= self.COST_LINE
# Test the result of the walk action
if result == PhysAgent.BUMPED:
walls = 1 # build the map- to do
# print(self.name() + ": wall or grid limit reached")
if result == PhysAgent.EXECUTED:
# check for victim returns -1 if there is no victim or the sequential
# the sequential number of a found victim
seq = self.body.check_for_victim()
if seq >= 0:
vs = self.body.read_vital_signals(seq)
self.rtime -= self.COST_READ
# print("exp: read vital signals of " + str(seq))
# print(vs)
return True
'''
Métodos do algoritmo DFS-on-line
'''
# Result[delta] = [result of the delta, VISIT]
def getResult(self, position: tuple) -> dict:
result = {}
obstacle = self.body.check_obstacles()
for i,d in enumerate(DELTA):
if obstacle[i] == PhysAgent.CLEAR:
result[d] = (position[0] + d[0], position[1] + d[1])
else:
result[d] = position
return result
def createActionsResults(self, position: list) -> list:
ans = []
result = {}
highest_priority_action = Stack()
lower_priority_action = Stack()
obstacle = self.body.check_obstacles()
for i,d in enumerate(DELTA):
if obstacle[i] == PhysAgent.CLEAR:
result[d] = tuple([position[0] + d[0], position[1] + d[1]])
if self.map.knowPosition(result[d]):
lower_priority_action.push(d)
else:
highest_priority_action.push(d)
else:
result[d] = tuple(position)
if not self.map.knowPosition(tuple([position[0] + d[0], position[1] + d[1]])):
self.map.addPosition(position = tuple([position[0] + d[0], position[1] + d[1]]), content=obstacle[i])
while(not highest_priority_action.empty()):
lower_priority_action.push(highest_priority_action.pop()) # embaixo da pilha esta de baixa prioridade e em cima da pilha está de alta prioridade
ans.append(lower_priority_action)
ans.append(result)
return ans
from typing import Optional
def Untried(self, position: list) -> Optional[tuple]:
if self.map.knowPosition(tuple(position)):
stack_actions: Stack = self.map.getContent()[UNTRIED_ACTIONS]
if not stack_actions.empty():
return stack_actions.pop()
return None