-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate_agent.py
More file actions
214 lines (195 loc) · 8.09 KB
/
evaluate_agent.py
File metadata and controls
214 lines (195 loc) · 8.09 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
# Helper functions for agent motion
from numba import njit
import math
@njit
def plot_agent(img, shape,position, color):
agent_width = shape.shape[0]
agent_height = shape.shape[1]
agent_left = - int(agent_width / 2)
agent_right = agent_left + agent_width
agent_bottom = - int(agent_height / 2)
agent_top = agent_bottom + agent_height
x1 = 0
for x in range(agent_left,agent_right, 1):
y1 = 0
for y in range(agent_bottom, agent_top, 1):
if shape[x1, y1] > 0:
if position[1] + y >= 0 and position[1] + y < img.shape[0] and position[0] + x >= 0 and position[0] + x < img.shape[1]:
img[position[1] + y, position[0] + x, 0] = color[0]
img[position[1] + y, position[0] + x, 1] = color[1]
img[position[1] + y, position[0] + x, 2] = color[2]
y1 += 1
x1 += 1
@njit
def erase_agent(img, shape, position):
agent_width = shape.shape[0]
agent_height = shape.shape[1]
agent_left = - int(agent_width / 2)
agent_right = agent_left + agent_width
agent_bottom = - int(agent_height / 2)
agent_top = agent_bottom + agent_height
x1 = 0
for x in range(agent_left,agent_right, 1):
y1 = 0
for y in range(agent_bottom, agent_top, 1):
if shape[x1, y1] > 0:
if position[1] + y >= 0 and position[1] + y < img.shape[0] and position[0] + x >= 0 and position[0] + x < img.shape[1]:
img[position[1] + y, position[0] + x, 0] = 0
img[position[1] + y, position[0] + x, 1] = 0
img[position[1] + y, position[0] + x, 2] = 0
y1 += 1
x1 += 1
@njit
def check_if_equal_fast(pos, xyxy):
if pos[0] != xyxy[0] or pos[1] != xyxy[1]:
return False
return True
@njit
def check_collison_fast(img, position, agent_left, agent_right, agent_bottom, agent_top, shape, color0, color1, color2):
x1 = 0
for x in range(agent_left, agent_right, 1):
y1 = 0
for y in range(agent_bottom, agent_top, 1):
if position[1] + y >= 0 and position[1] + y < img.shape[0] and position[0] + x >= 0 and position[0] + x < \
img.shape[1]:
if shape[x1, y1] > 0:
if (img[position[1] + y, position[0] + x, 0] > 0 and img[position[1] + y, position[0] + x, 0] != color0) \
or (img[position[1] + y, position[0] + x, 1] > 0 and img[position[1] + y, position[0] + x, 1] != color1) \
or (img[position[1] + y, position[0] + x, 2] > 0 and img[position[1] + y, position[0] + x, 2] != color2):
return True
else:
return True
y1 += 1
x1 += 1
return False
@njit
def signum(x):
if x < 0:
return -1
if x > 0:
return 1
return 0
@njit
def check_collison(img, position, shape, color):
agent_width = shape.shape[0]
agent_height = shape.shape[1]
agent_left = - int(agent_width / 2)
agent_right = agent_left + agent_width
agent_bottom = - int(agent_height / 2)
agent_top = agent_bottom + agent_height
#position = np.array(position)
return check_collison_fast(img, position, agent_left, agent_right, agent_bottom, agent_top,
shape, color[0], color[1], color[2])
@njit
def check_position(position, xyxy, img, shape, color):
if not check_if_equal_fast(position, xyxy) and not check_collison(img, xyxy, shape, color):
return True
return False
import numpy as np
@njit
def calculate_move(force_field, force_field_wall, force_field_agents, img, position, shape, color, rd, xyxy, max_distance):
speed = 1
# self.img_with_agents = np.copy(self.img)
#img_with_agents = img
pos_arr = np.zeros((int(max_distance) + 2, 2))
pos_arr_id = 0
if position[0] < 0 or position[0] >= force_field.shape[0] or position[1] < 0 or position[1] >= \
force_field.shape[1]:
#return position
return pos_arr
"""OK
v_xy_force_field = 5 * force_field[position[0], position[1], 2:4]
v_xy_wall = 0.25 * force_field_wall[position[0], position[1]]
v_xy_agents = 8 * force_field_agents[12, 12]
"""
#EXP
"""
v_xy_force_field = 5 * force_field[position[0], position[1], 2:4]
v_xy_wall = 0.25 * force_field_wall[position[0], position[1]]
v_xy_agents = 8 * force_field_agents[12, 12]
"""
# EXP 2
v_xy_force_field = 5 * force_field[position[0], position[1], 2:4]
v_xy_wall = 0.25 * force_field_wall[position[0], position[1]]
#v_xy_agents = 8 * force_field_agents[12, 12]
v_xy_agents = 8 * force_field_agents[12, 12]
found = True
v_xy = v_xy_force_field + v_xy_wall + v_xy_agents
#v_xy = v_xy_agents
v_xy_copy = np.copy(v_xy)
if np.linalg.norm(v_xy) > speed:
v_xy = speed * v_xy / np.linalg.norm(v_xy)
distance_so_far = 0
position_new = np.copy(position)
while distance_so_far < max_distance and found:
found = False
#ii += 1
xyxy[0] = int(round(position_new[0] + v_xy[0]))
xyxy[1] = int(round(position_new[1] + v_xy[1]))
#znaleziony kierunek
if check_position(position_new, xyxy, img, shape, color):
hx = int(round(v_xy[0]))
hy = int(round(v_xy[1]))
distance_so_far += math.sqrt(hx * hx + hy * hy)
found = True
if not found:
#dluzszy wektor
if math.fabs(v_xy[0]) >= math.fabs(v_xy[1]):
xyxy[0] = position_new[0] + speed * signum(v_xy[0])
xyxy[1] = position_new[1]
if check_position(position_new, xyxy, img, shape, color):
distance_so_far += 1
found = True
else:
xyxy[0] = position_new[0]
xyxy[1] = position_new[1] + speed * signum(v_xy[1])
if check_position(position_new, xyxy, img, shape, color):
distance_so_far += 1
found = True
if not found:
#skos
xyxy[0] = position_new[0] + speed * signum(v_xy[0])
xyxy[1] = position_new[1] + speed * signum(v_xy[1])
if not check_if_equal_fast(position_new, xyxy) and not check_collison(img, xyxy, shape, color):
distance_so_far += math.sqrt(2)
found = True
if not found:
#dol-gora
xyxy[0] = position_new[0] + speed * signum(v_xy[0])
xyxy[1] = position_new[1]
if not check_if_equal_fast(position_new, xyxy) and not check_collison(img, xyxy, shape, color):
distance_so_far += 1
found = True
if not found:
#prawo-lewo
xyxy[0] = position_new[0]
xyxy[1] = position_new[1] + speed * signum(v_xy[1])
if not check_if_equal_fast(position_new, xyxy) and not check_collison(img, xyxy, shape, color):
distance_so_far += 1
# agent_xy = xyxy
found = True
##############################
# try one random steps in all directions
#rd = random_dir()
a = 0
while not found and a < len(rd):
_rd = rd[a]
# xyxy = [self.position[0] + _rd[0], self.position[1] + _rd[1]]
xyxy[0] = position_new[0] + speed * _rd[0]
xyxy[1] = position_new[1] + speed * _rd[1]
if not check_if_equal_fast(position_new, xyxy) and not check_collison(img, xyxy, shape, color):
distance_so_far += math.sqrt(_rd[0] * _rd[0] + _rd[1] * _rd[1])
found = True
a += 1
if found:
position_new[0] = xyxy[0]
position_new[1] = xyxy[1]
pos_arr[pos_arr_id, 0] = xyxy[0]
pos_arr[pos_arr_id, 1] = xyxy[1]
pos_arr_id += 1
if found:
#return xyxy
return pos_arr
else:
#return position_new
return np.zeros((int(max_distance) + 2, 2))