-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathenvironment.py
792 lines (579 loc) · 29.5 KB
/
environment.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
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#!/usr/bin/env python
# coding=utf-8
# ==============================================================================
# title : environment.py
# description : contains environment class to train agents
# author : Nicolas Coucke
# date : 2022-08-16
# version : 1
# usage : use within training_RL.py
# notes : install the packages with "pip install -r requirements.txt"
# python_version : 3.9.2
# ==============================================================================
import matplotlib.pyplot as plt
import torch
import numpy as np
from utils import eucl_distance, symmetric_matrix, eucl_distance_np
import time
from matplotlib import animation
import tkinter as tk
import cmath
class Environment():
def __init__(self, fs, duration, stimulus_positions, stimulus_ratio, stimulus_decay_rate,
stimulus_scale, stimulus_sensitivity, starting_position, starting_orientation, movement_speed, agent_radius, agent_eye_angle, delta_orientation):
self.fs = fs
self.duration = duration
self.stimulus_positions = stimulus_positions # list of 2-element arrays
self.stimulus_ratio = stimulus_ratio
self.stimulus_decay_rate = stimulus_decay_rate
self.stimulus_scale = stimulus_scale
self.stimulus_sensitivity = stimulus_sensitivity
self.movement_speed = movement_speed
self.agent_radius = agent_radius
self.agent_eye_angle = agent_eye_angle
self.delta_orientation = delta_orientation
self.reset(starting_position, starting_orientation)
def reset(self, starting_position, starting_orientation):
""" For the next episode, train the agent in the same
environment but with a different initial position and orientation"""
self.position = starting_position
self.orientation = starting_orientation
self.right_stimulus_intensity = 0
self.left_stimulus_intensity = 0
self.time = 0
self.position_x = []
self.position_y = []
# the first (left) stimulus is largest if the ratio is less than 1
# i.e. the ratio = stimulus_strenght_left / stimulus_strength_right
self.correct_position = self.stimulus_positions[0]
if len(self.stimulus_positions) > 1:
# if two stimulus
if self.stimulus_ratio > 1:
# if the right stimulus is larger
self.correct_position = self.stimulus_positions[1]
self.distance = eucl_distance_np(self.correct_position, self.position)
return np.array([self.left_stimulus_intensity, self.right_stimulus_intensity])
def step(self, action, food_size):
"""action is moving right, moving left or continuing going forward """
# execute action
if action == 0:
# turn right
self.orientation = self.orientation + self.delta_orientation / self.fs
elif action == 1:
# turn left
self.orientation = self.orientation - self.delta_orientation / self.fs
#elif action == 2:
# keep moving forward
output_angle = np.angle(np.exp(1j*(action)))
# orientation += output_angle #np.sin(action)*self.delta_orientation / self.fs
self.orientation += 50 * output_angle / self.fs
#self.orientation = output_angle
self.position = self.position + np.array([np.sin(self.orientation)
* self.movement_speed * (1/self.fs), np.cos(self.orientation) * self.movement_speed * (1/self.fs)])
# calculate next position according to movement speed and new orientation
self.position_x.append(self.position[0])
self.position_y.append(self.position[1])
# get new state and reward
left_eye_position, right_eye_position = self.eye_positions()
# get gradient directly
left_gradient = self.get_stimulus_concentration(left_eye_position)
right_gradient = self.get_stimulus_concentration(right_eye_position)
# print("left gradient" + str(left_gradient))
# print("right gradient" + str(right_gradient))
# the agent will observe the stimulus gradient at its eyes (state)
state = self.stimulus_sensitivity * np.array([left_gradient, right_gradient]) #* self.fs
# state = self.stimulus_sensitivity * np.array([new_left_stimulus_intensity, new_right_stimulus_intensity])
# the food is the stimulus concentration at the center of the body
food = self.get_stimulus_concentration(self.position)
# punish agent for staying too long away from the food
hunger = 2
# reward is a combination of food and funger
reward = food - hunger
# end the episode when the time taken is too long
if self.time > self.duration:
done = True
else:
done = False
# or when the agent has found the food source
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, self.position))
self.distance = np.min(distances)
if self.distance < 5:
reward = (self.duration - self.time) * self.stimulus_scale
done = True
self.time += 1/self.fs
return state, reward, done
def get_stimulus_concentration(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_concentration = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
stimulus_concentration_1 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_concentration_2 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_concentration = stimulus_concentration_1 + self.stimulus_ratio * stimulus_concentration_2
return stimulus_concentration
def get_stimulus_gradient(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
# distance to stimuli center
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_gradient = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
# if moer than one stimulus
stimulus_gradient_1 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_gradient_2 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_gradient = stimulus_gradient_1 + self.stimulus_ratio * stimulus_gradient_2
return stimulus_gradient
#return self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * self.distance)
def eye_positions(self):
""""
Calculate position of the agent's eyes in world space
based on the orientation and position
Arguments:
-----------
None; uses variables stored in the class
Returns:
----------
left_eye_position (x, y): torch.tensor of length 2
position of the left eye in world space
right_eye_position (x, y): torch.tensor of length 2
position of the right eye in world space
"""
left_eye_position = np.zeros(2)
right_eye_position = np.zeros(2)
left_eye_position[0] = self.position[0] + np.sin(self.orientation - self.agent_eye_angle/2 ) * self.agent_radius
left_eye_position[1] = self.position[1] + np.cos(self.orientation - self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[0] = self.position[0] + np.sin(self.orientation + self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[1] = self.position[1] + np.cos(self.orientation + self.agent_eye_angle/2 ) * self.agent_radius
return left_eye_position, right_eye_position
class Social_environment():
def __init__(self, fs, duration, stimulus_positions, stimulus_decay_rate,
stimulus_scale, stimulus_sensitivity, movement_speed, agent_radius, agent_eye_angle, delta_orientation, stimulus_ratio, n_agents):
"""
starting_positions = list with one tuple per agent
"""
self.fs = fs
self.duration = duration
self.stimulus_positions = stimulus_positions
self.stimulus_decay_rate = stimulus_decay_rate
self.stimulus_scale = stimulus_scale
self.stimulus_sensitivity = stimulus_sensitivity
self.movement_speed = movement_speed
self.agent_radius = agent_radius
self.agent_eye_angle = agent_eye_angle
self.right_stimulus_intensity = 0
self.left_stimulus_intensity = 0
self.delta_orientation = delta_orientation
self.time = 0
self.n_agents = n_agents
self.stimulus_ratio = stimulus_ratio
def reset(self, starting_positions, starting_orientations, n_agents):
""" For the next episode, train the angent in the same
environment but with a different initial position and orientation"""
self.agent_positions = starting_positions
self.agent_new_positions = starting_positions
self.agent_orientations = starting_orientations
self.agent_new_orientations = starting_orientations
self.right_stimulus_intensity = 0
self.left_stimulus_intensity = 0
self.time = 0
self.position_x = []
self.position_y = []
self.save_orientations = []
self.distances = []
# the first (left) stimulus is largest if the ratio is less than 1
# i.e. the ratio = stimulus_strenght_left / stimulus_strength_right
self.correct_position = self.stimulus_positions[0]
if len(self.stimulus_positions) > 1:
# if two stimulus
if self.stimulus_ratio > 1:
# if the right stimulus is larger
self.correct_position = self.stimulus_positions[1]
self.inter_agent_distances = np.zeros((n_agents, n_agents))
states = []
for i in range(n_agents):
self.position_x.append([])
self.position_y.append([])
self.save_orientations.append([])
self.distances.append(eucl_distance_np(self.correct_position, self.agent_positions[i]))
states.append(np.array([self.left_stimulus_intensity, self.right_stimulus_intensity]))
return states
def step(self, actions, food_size):
"""action is moving right, moving left or continuing going forward """
states = []
rewards = []
distances = []
# update the distances between the agents
for i in range(self.n_agents):
for j in range(self.n_agents):
if i != j:
inter_agent_distance = eucl_distance_np(self.agent_positions[i], self.agent_positions[j])
self.inter_agent_distances[i, j] = inter_agent_distance
# loop through all Guidos
for i in range(self.n_agents):
action = actions[i]
orientation = self.agent_orientations[i]
position = self.agent_positions[i]
# if statement is to freeze agents that have arrived at stimulus
# loop through all Guidos
for i in range(len(actions)):
# freeze the agents if they are close to stimulus
if self.distances[i] > 5:
action = actions[i]
orientation = self.agent_orientations[i]
position = self.agent_positions[i]
# execute action
if action == 0:
# turn right
self.agent_new_orientations[i] = orientation + self.delta_orientation / self.fs
elif action == 1:
# turn left
self.agent_new_orientations[i] = orientation - self.delta_orientation / self.fs
#elif action == 2:
# keep moving forward
# new version: do the gradual
output_angle = np.angle(np.exp(1j*(action)))
orientation += 50 * output_angle / self.fs
self.agent_new_positions[i] = np.array(position) + np.array([np.sin(orientation)
* self.movement_speed * (1/self.fs), np.cos(orientation) * self.movement_speed * (1/self.fs)])
self.agent_new_orientations[i] = orientation #% (2 * np.pi)
else:
# remains the same
position = self.agent_positions[i]
orientation = self.agent_new_orientations[i]
self.agent_new_positions[i] = np.array(position)
self.agent_new_orientations[i] = orientation
self.position_x[i].append(position[0])
self.position_y[i].append(position[1])
self.save_orientations[i].append(orientation)
# get new state and reward
left_eye_position, right_eye_position = self.eye_positions(self.agent_new_positions[i], self.agent_new_orientations[i])
# get gradient directly
left_gradient = self.get_stimulus_concentration(left_eye_position)
right_gradient = self.get_stimulus_concentration(right_eye_position)
# the agent will observe the stimulus gradient at its eyes (state)
state = self.stimulus_sensitivity * np.array([left_gradient, right_gradient]) #* self.fs
# state = self.stimulus_sensitivity * np.array([new_left_stimulus_intensity, new_right_stimulus_intensity])
states.append(state)
# the food is the stimulus concentration at the center of the body
food = self.get_stimulus_concentration(position)
# punish agent for staying too long away from the food
hunger = 2
# reward is a combination of food and funger
reward = food - hunger
rewards.append(reward)
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, self.agent_new_positions[i]))
distance = np.min(distances)
self.distances[i] = distance
if distance < 10:
reward = (self.duration - self.time) * self.stimulus_scale
# done = True
# end the episode when the time taken is too long
if self.time > self.duration:
done = True
else:
done = False
# after having calculated the new position and angle for each agent, update them
self.agent_positions = self.agent_new_positions
self.agent_orientations = self.agent_new_orientations
self.time += 1/self.fs
return states, rewards, done
def get_stimulus_concentration(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_concentration = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
stimulus_concentration_1 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_concentration_2 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_concentration = stimulus_concentration_1 + self.stimulus_ratio * stimulus_concentration_2
return stimulus_concentration
def get_stimulus_gradient(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
# distance to stimuli center
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_gradient = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
# if moer than one stimulus
stimulus_gradient_1 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_gradient_2 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_gradient = stimulus_gradient_1 + self.stimulus_ratio * stimulus_gradient_2
return stimulus_gradient
#return self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * self.distance)
def eye_positions(self, position, orientation):
""""
Calculate position of the agent's eyes in world space
based on the orientation and position
Arguments:
-----------
None; uses variables stored in the class
Returns:
----------
left_eye_position (x, y): torch.tensor of length 2
position of the left eye in world space
right_eye_position (x, y): torch.tensor of length 2
position of the right eye in world space
"""
left_eye_position = np.zeros(2)
right_eye_position = np.zeros(2)
left_eye_position[0] = position[0] + np.sin(orientation - self.agent_eye_angle/2 ) * self.agent_radius
left_eye_position[1] = position[1] + np.cos(orientation - self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[0] = position[0] + np.sin(orientation + self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[1] = position[1] + np.cos(orientation + self.agent_eye_angle/2 ) * self.agent_radius
return left_eye_position, right_eye_position
class Social_stimulus_environment():
def __init__(self, fs, duration, stimulus_positions, stimulus_decay_rate,
stimulus_scale, stimulus_sensitivity, movement_speed, agent_radius, agent_eye_angle, delta_orientation, agent_stimulus_scale, agent_stimulus_decay_rate, stimulus_ratio, n_agents):
"""
starting_positions = list with one tuple per agent
"""
self.fs = fs
self.duration = duration
self.stimulus_positions = stimulus_positions
self.stimulus_decay_rate = stimulus_decay_rate
self.stimulus_scale = stimulus_scale
self.agent_stimulus_scale = agent_stimulus_scale
self.agent_stimulus_decay_rate = agent_stimulus_decay_rate
self.stimulus_sensitivity = stimulus_sensitivity
self.movement_speed = movement_speed
self.agent_radius = agent_radius
self.agent_eye_angle = agent_eye_angle
self.right_stimulus_intensity = 0
self.left_stimulus_intensity = 0
self.delta_orientation = delta_orientation
self.time = 0
self.n_agents = n_agents
self.stimulus_ratio = stimulus_ratio
def reset(self, starting_positions, starting_orientations, n_agents):
""" For the next episode, train the angent in the same
environment but with a different initial position and orientation"""
self.agent_positions = starting_positions
self.agent_new_positions = starting_positions
self.agent_orientations = starting_orientations
self.agent_new_orientations = starting_orientations
self.right_stimulus_intensity = 0
self.left_stimulus_intensity = 0
self.time = 0
self.position_x = []
self.position_y = []
self.save_orientations = []
self.distances = []
# the first (left) stimulus is largest if the ratio is less than 1
# i.e. the ratio = stimulus_strenght_left / stimulus_strength_right
self.correct_position = self.stimulus_positions[0]
if len(self.stimulus_positions) > 1:
# if two stimulus
if self.stimulus_ratio > 1:
# if the right stimulus is larger
self.correct_position = self.stimulus_positions[1]
self.inter_agent_distances = np.zeros((n_agents, n_agents))
states = []
for i in range(n_agents):
self.position_x.append([])
self.position_y.append([])
self.save_orientations.append([])
self.distances.append(eucl_distance_np(self.correct_position, self.agent_positions[i]))
states.append(np.array([self.left_stimulus_intensity, self.right_stimulus_intensity]))
return states
def step(self, actions, food_size):
"""action is moving right, moving left or continuing going forward """
states = []
rewards = []
distances = []
# loop through all Guidos
for i in range(len(actions)):
# freeze the agents if they are close to stimulus
if self.distances[i] > 5:
action = actions[i]
orientation = self.agent_orientations[i]
position = self.agent_positions[i]
# execute action
if action == 0:
# turn right
self.agent_new_orientations[i] = orientation + self.delta_orientation / self.fs
elif action == 1:
# turn left
self.agent_new_orientations[i] = orientation - self.delta_orientation / self.fs
#elif action == 2:
# keep moving forward
# new version: do the gradual
output_angle = np.angle(np.exp(1j*(action)))
orientation += 50 * output_angle / self.fs
self.agent_new_positions[i] = np.array(position) + np.array([np.sin(orientation)
* self.movement_speed * (1/self.fs), np.cos(orientation) * self.movement_speed * (1/self.fs)])
self.agent_new_orientations[i] = orientation #% (2 * np.pi)
else:
# remains the same
position = self.agent_positions[i]
orientation = self.agent_new_orientations[i]
self.agent_new_positions[i] = np.array(position)
self.agent_new_orientations[i] = orientation
self.position_x[i].append(position[0])
self.position_y[i].append(position[1])
self.save_orientations[i].append(orientation)
# get new state and reward
left_eye_position, right_eye_position = self.eye_positions(self.agent_new_positions[i], self.agent_new_orientations[i])
# get gradient directly
left_gradient = self.get_stimulus_concentration(left_eye_position)
right_gradient = self.get_stimulus_concentration(right_eye_position)
# get gradient due to agents
left_agent_gradient = self.get_agent_concentration(left_eye_position, i)
right_agent_gradient = self.get_agent_concentration(right_eye_position, i)
# merge the two gradients together
left_gradient += left_agent_gradient
right_gradient += right_agent_gradient
# the agent will observe the stimulus gradient at its eyes (state)
state = self.stimulus_sensitivity * np.array([left_gradient, right_gradient]) #* self.fs
# state = self.stimulus_sensitivity * np.array([new_left_stimulus_intensity, new_right_stimulus_intensity])
states.append(state)
# the food is the stimulus concentration at the center of the body
food = self.get_stimulus_concentration(position)
# punish agent for staying too long away from the food
hunger = 2
# reward is a combination of food and funger
reward = food - hunger
rewards.append(reward)
# end the episode when the time taken is too long
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, self.agent_new_positions[i]))
distance = np.min(distances)
self.distances[i] = distance
if distance < 5:
reward = (self.duration - self.time) * self.stimulus_scale
# done = True
if self.time > self.duration:
done = True
else:
done = False
# after having calculated the new position and angle for each agent, update them
self.agent_positions = self.agent_new_positions
self.agent_orientations = self.agent_new_orientations
self.time += 1/self.fs
return states, rewards, done
def get_stimulus_concentration(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_concentration = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
stimulus_concentration_1 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_concentration_2 = self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_concentration = stimulus_concentration_1 + self.stimulus_ratio * stimulus_concentration_2
return stimulus_concentration
def get_stimulus_gradient(self, location):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
distances = []
for stimulus_position in self.stimulus_positions:
# distance to stimuli center
distances.append(eucl_distance_np(stimulus_position, location))
stimulus_gradient = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
if len(distances) > 1:
# if moer than one stimulus
stimulus_gradient_1 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[0])
stimulus_gradient_2 = self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * distances[1])
stimulus_gradient = stimulus_gradient_1 + self.stimulus_ratio * stimulus_gradient_2
return stimulus_gradient
#return self.stimulus_decay_rate * self.stimulus_scale * np.exp( - self.stimulus_decay_rate * self.distance)
def get_agent_concentration(self, location, i):
"""
Get the concentration of the stimulus at a certain location
Arguments:
----------
location: numpy array of length x
[x position, y position]
Returns:
----------
stimulus_concentration: float
"""
agent_gradient = 0
for a in range(len(self.agent_new_positions)):
if a != i:
distance = eucl_distance_np(self.agent_new_positions[a], location)
agent_gradient += self.agent_stimulus_scale * np.exp( - self.agent_stimulus_decay_rate * distance)
return agent_gradient
def eye_positions(self, position, orientation):
""""
Calculate position of the agent's eyes in world space
based on the orientation and position
Arguments:
-----------
None; uses variables stored in the class
Returns:
----------
left_eye_position (x, y): torch.tensor of length 2
position of the left eye in world space
right_eye_position (x, y): torch.tensor of length 2
position of the right eye in world space
"""
left_eye_position = np.zeros(2)
right_eye_position = np.zeros(2)
left_eye_position[0] = position[0] + np.sin(orientation - self.agent_eye_angle/2 ) * self.agent_radius
left_eye_position[1] = position[1] + np.cos(orientation - self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[0] = position[0] + np.sin(orientation + self.agent_eye_angle/2 ) * self.agent_radius
right_eye_position[1] = position[1] + np.cos(orientation + self.agent_eye_angle/2 ) * self.agent_radius
return left_eye_position, right_eye_position