-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimulation_rough_set_loop_all.py
More file actions
329 lines (270 loc) · 12.5 KB
/
simulation_rough_set_loop_all.py
File metadata and controls
329 lines (270 loc) · 12.5 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
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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# ORDER OF RUN: #7
# Simulation rough set, 1,000 agents
import cv2
from agent import Agent
import random
import numpy as np
from calculate_simulation_helper import plot_room, calculate_room_force_field_and_distance, is_color_correct, \
order_agents, calculate_local_force_field_agents, calculate_wall_force_field, prepare_local_force_field_array
image_size = [512,512]
#for gap_size in [9,10,12,14,16,18,20]:
for gap_size in [20]:
for eps in [0.5]:
denisty_plot = np.ones(image_size)
local_force_field_size = [25, 25]
file_save_plot_data = "results/simulation_rough/plot_data" + str(gap_size) + '0_' + str(eps) + '_' + str(eps) + '.rs'
file_save_time_data = "results/simulation_rough/time_data" + str(gap_size) + '0_' + str(eps) + '_' + str(eps) + '.rs'
white = (255, 255, 255)
file_name = 'data/rough_setb' + str(gap_size) + '0_' + str(eps) + '_' + str(eps) + '.rs'
denisty_plot = np.ones(image_size)
local_force_field_size = [25, 25]
#image_size = [1000,1000]
agents_x = 50
agents_y = 20
# Calculate static fields
img = plot_room(image_size, gap_size)
force_field_wall= calculate_wall_force_field(img)
[force_field, distance_to_goal] = calculate_room_force_field_and_distance(image_size, img)
[local_force_field_size, local_force_field, local_force_field_size_half_x, local_force_field_size_half_y] \
= prepare_local_force_field_array(local_force_field_size)
def stop_criterium(position):
if position[1] > img.shape[1]-10:
return True
rnd = random.Random()
def random_dir():
directions = [(1, 1), (1, -1), (-1, 1), (-1, -1), (0, 1), (0, -1), (1, 0), (-1, 0)]
rnd.shuffle(directions)
return directions
#############################################################################
# Initialize agents
Agents = []
Agents_done = []
rnd.seed(1)
agent_size = np.ones((5, 5))
agent_size[0,0] = 0
agent_size[4,4] = 0
agent_size[0,4] = 0
agent_size[4,0] = 0
id = 0
for _x in range(agents_x):
for _y in range(agents_y):
a = Agent()
a.position = np.array([_x * 10 + 3, _y * 10 + 3])
#a.position = np.array([_x * 8 + 100, _y * 8 + 5])
#a.position = np.array([_x * 10 + 5, _y * 10 + 125])
a.color = np.array((rnd.randint(30, 255), rnd.randint(30, 255), rnd.randint(30, 255)))
#do not allow same color of two agents
while is_color_correct(Agents, a):
a.color = np.array((rnd.randint(30, 255), rnd.randint(30, 255), rnd.randint(30, 255)))
a.shape = agent_size
a.random_dir = random_dir
a.id = id
id += 1
Agents.append(a)
from find_directions_rough import find_directions, genarate_sets, discretization, calculate_dir
import pickle
#[all_rough_sets, columns_to_take, t_dir_r, df, ranges] = genarate_sets('results_to_file_imp_no_zero.txt', eps0=0.5, eps1=0.5)
file = open(file_name, 'rb')
data = pickle.load(file)
[all_rough_sets, columns_to_take, t_dir_r, df, df_sampled, ranges] = data
file.close()
def calculate_ff_dir(x, y):
xr = round(x)
yr = round(y)
if xr == 0 and yr == 0:
return 0.0
if xr == 1 and yr == 0:
return 1.
if xr == 1 and yr == 1:
return 2.
if xr == 0 and yr == 1:
return 3.
if xr == -1 and yr == 1:
return 4.
if xr == -1 and yr == 0:
return 5.
if xr == -1 and yr == -1:
return 6.
if xr == 0 and yr == -1:
return 7.
if xr == 1 and yr == -1:
return 8.
return 0.
from evaluate_agent import check_position
def calculate_move_rough(force_field, force_field_wall, local_force_field_grad, img_a, position, shape, color, rd, xyxy, max_distance):
new_pos = []
xy_c = position
v_xy_force_field = force_field[xy_c[0], xy_c[1], 2:4]
v_xy_wall = force_field_wall[xy_c[0], xy_c[1]]
v_xy_agents = local_force_field_grad[12, 12]
ff_dir = calculate_ff_dir(v_xy_force_field[0], v_xy_force_field[1])
fw_x = v_xy_wall[0]
fw_y = v_xy_wall[1]
fa_x = v_xy_agents[0]
fa_y = v_xy_agents[1]
my_coord_val = [str(ff_dir), fw_x, fw_y, fa_x, fa_y]
my_coord = discretization(my_coord_val, ranges)
dir_help = find_directions(my_coord, all_rough_sets, columns_to_take, t_dir_r)
move_done = False
b = 0
while not move_done and b < len(dir_help):
a = 0
while not move_done and a < len(dir_help[b]):
dd = dir_help[b][a][0]
v_xy = calculate_dir(float(dd))
position_new = np.copy(position)
xyxy[0] = position_new[0] + v_xy[0]
xyxy[1] = position_new[1] + v_xy[1]
#xyxy[0] = position_new[0] + 0
#xyxy[1] = position_new[1] + 1
if check_position(position_new, xyxy, img_a, shape, color):
new_pos = xyxy
move_done = True
a = a + 1
b = b + 1
if not move_done:
new_pos = position_new
return new_pos
def get_random(rd):
my_sum = 0
for a in range(len(rd)):
my_sum += rd[a][1]
ran_sum = random.randint(0, my_sum)
sum = 0
id = 0
while sum < ran_sum:
sum += rd[id][1]
id += 1
return id - 1
def calculate_move_rough_random(force_field, force_field_wall, local_force_field_grad, img_a, position, shape, color, rd, xyxy, max_distance):
new_pos = []
xy_c = position
v_xy_force_field = force_field[xy_c[0], xy_c[1], 2:4]
v_xy_wall = force_field_wall[xy_c[0], xy_c[1]]
v_xy_agents = local_force_field_grad[12, 12]
ff_dir = calculate_ff_dir(v_xy_force_field[0], v_xy_force_field[1])
fw_x = v_xy_wall[0]
fw_y = v_xy_wall[1]
fa_x = v_xy_agents[0]
fa_y = v_xy_agents[1]
my_coord_val = [str(ff_dir), fw_x, fw_y, fa_x, fa_y]
#print(my_coord_val)
my_coord = discretization(my_coord_val, ranges)
dir_help = find_directions(my_coord, all_rough_sets, columns_to_take, t_dir_r)
move_done = False
b = 0
while not move_done and b < len(dir_help):
a = 0
dir_help_random = []
while not move_done and len(dir_help[b]) > 0:
id = get_random(dir_help[b])
dd = dir_help[b].pop(id)[0]
#dd = dir_help[0][a][0]
v_xy = calculate_dir(float(dd))
position_new = np.copy(position)
xyxy[0] = position_new[0] + v_xy[0]
xyxy[1] = position_new[1] + v_xy[1]
#xyxy[0] = position_new[0] + 0
#xyxy[1] = position_new[1] + 1
if check_position(position_new, xyxy, img_a, shape, color):
new_pos = xyxy
move_done = True
a = a + 1
b = b + 1
if not move_done:
#new_pos = position_new
new_pos = position
return new_pos
from numba import njit
@njit
def update_denisty_plot(denisty_plot, img_a):
for a in range(denisty_plot.shape[0]):
for b in range(denisty_plot.shape[1]):
if (img_a[a][b][0] != 255 or img_a[a][b][1] != 255 or img_a[a][b][2] != 255) and \
(img_a[a][b][0] > 0 or img_a[a][b][1] > 0 or img_a[a][b][2] > 0):
denisty_plot[a][b] += 1
#from numba import njit
from evaluate_agent import plot_agent, calculate_move, erase_agent
cc = 0
img = plot_room(image_size, gap_size)
import time
start = time.time()
img_a = np.copy(img)
for a in Agents:
plot_agent(img_a, a.shape, a.position, a.color)
# Run simulation until all agents leave area
while len(Agents) > 0:
start = time.time()
# Order agents, closest to goal is first
Agents = order_agents(Agents, distance_to_goal)
img_a = np.copy(img)
for a in Agents:
plot_agent(img_a, a.shape, a.position, a.color)
update_denisty_plot(denisty_plot, img_a)
# For each agent
for a in Agents:
a.img = img_a
dist = 0
a.max_distance
cc = 0
while dist < a.max_distance and cc < 3:
#for cc in range(3):
rd = random_dir()
xyxy = np.zeros(2).astype(int)
# Calculate local force field
local_force_field_grad = calculate_local_force_field_agents(local_force_field, local_force_field_size,
local_force_field_size_half_x,
local_force_field_size_half_y, a.position, img_a, a.color)
# Make move
new_pos = calculate_move_rough_random(force_field, force_field_wall, local_force_field_grad, img_a, a.position, a.shape, a.color, np.array(rd), xyxy, a.max_distance)
# If agent moved (has new position)
dist_help = np.linalg.norm(new_pos - a.position)
if np.linalg.norm(new_pos - a.position) > 0.01:# and check_position(a.position, new_pos, img_a, a.shape, a.color):
#if True:
# Remove agent from map
erase_agent(img_a, a.shape, a.position)
if new_pos[0] < 0: new_pos[0] = 0
if new_pos[1] < 0: new_pos[1] = 0
if new_pos[0] >= img.shape[1]: new_pos[0] = img.shape[1] - 1
if new_pos[1] >= img.shape[0]: new_pos[1] = img.shape[0] - 1
a.old_position.append(a.position)
a.position = new_pos
# Plot agent on map
plot_agent(img_a, a.shape, a.position, a.color)
dist += dist_help
cc += 1
#cv2.imshow("a", cv2.resize(img_a, (512, 512)))
#cv2.waitKey(100)
# Remove each agent which satisfied stop criteria
for a in Agents:
if stop_criterium(a.position):
erase_agent(img_a, a.shape, a.position)
Agents_done.append(a)
Agents.remove(a)
print(time.time() - start)
cv2.imshow("a", cv2.resize(img_a,(512,512)))
#img_a = np.copy(img)
cv2.waitKey(1)
#################################################################################
from matplotlib import pyplot as plt
from matplotlib.colors import LogNorm
#denisty_plot = denisty_plot[150:(512-150),150:(512-150)]
denisty_plot_mirror = np.zeros(denisty_plot.shape)
for a in range(denisty_plot.shape[0]):
for b in range(denisty_plot.shape[1]):
denisty_plot_mirror[a, b] = denisty_plot[denisty_plot.shape[0] - a - 1, b]
denisty_plot = denisty_plot_mirror
#denisty_plot = np.transpose(np.transpose(denisty_plot))
#denisty_plot = np.transpose(denisty_plot)
plt.pcolormesh(denisty_plot, norm=LogNorm(), cmap='inferno')
plt.colorbar()
import pickle
file = open(file_save_plot_data, 'wb')
pickle.dump(denisty_plot,file)
file.close()
file = open(file_save_time_data, 'wb')
agents_paths = []
for a in Agents_done:
agents_paths.append(a.old_position)
pickle.dump(agents_paths,file)
file.close()