-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase.py
235 lines (207 loc) · 9.34 KB
/
base.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
from distutils.file_util import move_file
# from bullshitmap import BullshitMap
from pyplotmap import PyplotMap
import random
from drone import Drone
from snake import snake_algoithm_generator
from Pathfinder import pathfollow_algoithm_generator
class BaseStation:
def __init__(self, size_of_the_map_x, size_of_the_map_y, number_of_drones, station_pos_x, station_pos_y, lost_person_x, lost_person_y, search_algorithm="snake", drone_battery_capacity=500):
self.mapX = size_of_the_map_x
self.mapY = size_of_the_map_y
self.stationX = station_pos_x
self.stationY = station_pos_y
self.numberOfDrones = number_of_drones
self.searchingAlgorithm = search_algorithm
self.areaStatus = [["not scouted" for y in range(self.mapY)] for x in range(self.mapX)]
self.areaStatus[self.stationX][self.stationY] = "scouted"
self.coveredArea = {}
self.is_charging_needed_base = False
self.going_to_last_place = False
self.listOfMoves = []
if search_algorithm == "snake":
for i in range(number_of_drones):
width = int((1) * (size_of_the_map_x / number_of_drones))
if i == (number_of_drones - 1):
specificWidth = size_of_the_map_x - (i * width)
moves = snake_algoithm_generator(
specificWidth,
size_of_the_map_y,
station_pos_x,
station_pos_y,
(i * width),
0,
)
else:
moves = snake_algoithm_generator(
width,
size_of_the_map_y,
station_pos_x,
station_pos_y,
(i * width),
0,
)
self.listOfMoves.append(moves)
self.drones = []
for id in range(0, self.numberOfDrones):
drone_pos_X = station_pos_x
drone_pos_Y = station_pos_y
self.areaStatus[drone_pos_X][drone_pos_Y] = "scouted"
# SimpleMap.init_drone(drone_pos_X, drone_pos_Y)
added_drone = Drone(1, drone_battery_capacity, drone_pos_X, drone_pos_Y, lost_person_x, lost_person_y, station_pos_x, station_pos_y, id, moves=self.listOfMoves[id])
self.drones.append(added_drone)
if search_algorithm == "pathfollow":
moves = pathfollow_algoithm_generator(
size_of_the_map_x,
size_of_the_map_y,
station_pos_x,
station_pos_y,
number_of_drones
)
self.listOfMoves.append(moves)
self.drones = []
for id in range(0, self.numberOfDrones):
drone_pos_X = station_pos_x
drone_pos_Y = station_pos_y
self.areaStatus[drone_pos_X][drone_pos_Y] = "scouted"
# SimpleMap.init_drone(drone_pos_X, drone_pos_Y)
added_drone = Drone(1, drone_battery_capacity, drone_pos_X, drone_pos_Y, lost_person_x, lost_person_y, station_pos_x, station_pos_y, id, moves=self.listOfMoves[0][id])
self.drones.append(added_drone)
"""++++++++++++++++++++++++++++++++++++++++++
PUT THE NEW ALGORITHM HERE AND APPEND THE PATH TO THE listOfMoves FOR EVERY DRONE OR GUARANTEE TO RUN THE CODE ONLY WITH VALID PARAMETERS
++++++++++++++++++++++++++++++++++++++++++"""
def getPositionOfDrones(self):
"""Get the positions of the drones
Returns:
[ (int,int) ]: array of the positions of the drones
"""
dronePositions = []
for drone in self.drones:
dronePositions.append(drone.get_position())
return dronePositions
def moveOneTime(self):
"""Move the drones to the next position
Raises:
Exception: The Person has been fund
Returns:
[ (int,int) ]: positions of the drones for plotting
"""
newPositions = []
for drone in self.drones:
try:
pos, temp = drone.next_step()
newPositions.append(pos)
except:
raise Exception("Person has been found!!!!!!!!!!!!!")
return newPositions
# def nextMove(self):
# newPositions = []
# for drone in self.drones:
# oldPos = drone.get_position()
# # GOING TO THE LAST PLACE BEFORE CHARGING
# if self.going_to_last_place:
# realNewPos, self.going_to_last_place = drone.go_to_previous_pos(self.lastPlace)
# if self.going_to_last_place == False:
# realNewPos = self.goingToNewPosOfThePath(drone, oldPos)
# # GOING TO THE STATION FOR CHARGING
# elif self.is_charging_needed_base:
# if drone.get_position() != (self.stationX, self.stationY):
# realNewPos, self.is_charging_needed_base = drone.move("north")
# else:
# self.is_charging_needed_base = False
# realNewPos, self.going_to_last_place = drone.go_to_previous_pos(self.lastPlace)
# # GOING TO THE NEXT NORMAL STEP OF THE PATH
# else:
# realNewPos = self.goingToNewPosOfThePath(drone, oldPos)
# newPositions.append(realNewPos)
# return newPositions
# def goingToNewPosOfThePath(self, drone, oldPos):
# newPos = self.moves.pop(0)
# # newPos = self.randomPath(oldPos[0], oldPos[1])
# realNewPos, self.is_charging_needed_base = drone.move(self.calculateDirection(oldPos, newPos))
# self.areaStatus[realNewPos[0]][realNewPos[1]] = "scouted"
# # self .coveredArea.add(self.drone.get_position())
# if self.is_charging_needed_base:
# self.lastPlace = newPos
# return realNewPos
# if newPos in self.coveredArea:
# return newPos
# else:
# self.coveredArea.add(self.drone.get_position())
# return newPos,
# def randomPath(self, x, y):
# nextMoveValid = False
# while nextMoveValid == False:
# decision = random.randrange(4)
# # go up
# if decision == 0:
# if y < self.mapY - 1:
# if self.areaStatus[x][(y + 1)] == "not scouted":
# return x, y + 1
# # go right
# if decision == 1:
# if x < self.mapX - 1:
# tempx = x + 1
# tempy = y
# if self.areaStatus[tempx][tempy] == "not scouted":
# return x + 1, y
# # go down
# if decision == 2:
# if y > 0:
# if self.areaStatus[x][(y - 1)] == "not scouted":
# return x, y - 1
# # go left
# if decision == 3:
# if x > 0:
# tempx = x - 1
# tempy = y
# if self.areaStatus[(x - 1)][y] == "not scouted":
# return x - 1, y
# # Get out from a place
# try:
# if self.areaStatus[x + 1][(y)] == "scouted" and self.areaStatus[x - 1][(y)] == "scouted" and self.areaStatus[x][(y + 1)] == "scouted" and self.areaStatus[x][(y - 1)] == "scouted":
# if bool(random.getrandbits(1)): # horizontal_or_vertical
# X = x
# if bool(random.getrandbits(1)): # plus_or_minus
# Y = y + 1
# else:
# Y = y
# else:
# Y = y
# if bool(random.getrandbits(1)): # plus_or_minus
# X = x + 1
# else:
# X = x
# if X < self.mapX - 1 and X > 0 and Y < self.mapY and Y > 0:
# return X, Y
# except:
# print("out of range")
# try:
# if self.areaStatus[x][(y + 1)] == "scouted":
# return x, y + 1
# except:
# try:
# if self.areaStatus[x][(y - 1)] == "scouted":
# return x, y - 1
# except:
# try:
# if self.areaStatus[x - 1][(y)] == "scouted":
# return x - 1, y
# except:
# try:
# if self.areaStatus[x + 1][(y)] == "scouted":
# return x + 1, y
# except:
# print("An exception occurred")
# def generate_path(self, droneX, droneY):
# def calculateDirection(self, oldPos, newPos):
# xDiff = newPos[0] - oldPos[0]
# yDiff = newPos[1] - oldPos[1]
# if xDiff == 1:
# return "east"
# elif xDiff == -1:
# return "west"
# elif yDiff == 1:
# return "north"
# elif yDiff == -1:
# return "south"