-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent_RL.py
623 lines (463 loc) · 24.6 KB
/
agent_RL.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
#!/usr/bin/env python
# coding=utf-8
# ==============================================================================
# title : agent_RL.py
# description : contains neural agents that can be trained in pytorch
# 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 numpy as np
from utils import symmetric_matrix, eucl_distance
import time
from matplotlib import animation
import tkinter as tk
import random
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
from torch.distributions import Categorical
from torch.distributions import Distribution
class Gina(nn.Module):
"""
A neural network RL agent to be trained with policy gradient methods
"""
def __init__(self, device):
super(Gina, self).__init__()
s_size = 2 # observation for left and right eye
a_size = 3 # right, left, forward
h_size = 6 # hidden nodes
self.fc1 = nn.Linear(s_size, h_size)
self.fc2 = nn.Linear(h_size, a_size)
self.device = device
def forward(self, x):
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.softmax(x, dim=1)
def act(self, state):
state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
probs = self.forward(state).cpu()
m = Categorical(probs)
action = m.sample()
return action.item(), m.log_prob(action)
class MultipleGuidos(nn.Module):
"""
A class to parallelize the feedforward run of multiple guido's interacting in the same environment
Arguments
---------
size: how many guidos
other parameters are the same as basic Guido class
"""
def __init__(self, device, fs, frequency = np.array([]), phase_coupling = np.array([]), k = -1, size = 1):
# also execute base initialization of nn.Module
super().__init__()
self.list_of_guidos = nn.ModuleList([Guido(device, fs, frequency, phase_coupling, k) for i in range(int(size))])
self.concatenated_guidos = torch.cat(self.list_of_guidos, dim=1)
def forward(self, input):
"""
input: concatenated inputs of all the agents
output: the concatenated output angles of all the agents
"""
return self.concatenated_guidos(input)
def act(self, state):
"""
state are the concatenated inputs of the agents
returns the output angles of all agents
"""
state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
output_angles = self.forward(state).cpu()
return output_angles
class Guido(nn.Module):
"""
An RL agent with HKB equations instead of neural network layers
Arguments
---------
frequency: np or torch array of length 2
should be [sensory_frequency, motor_frequency]
phase_coupling: np or torch array of length 4
should be [a_sensory, a_ipsilateral, a_contralateral, a_motor]
k: int or float
the proportion of phase coupling vs anti-phase coupling
"""
def __init__(self, device, fs, frequency = np.array([]), phase_coupling = np.array([]), k = -1, n_oscillators = 4):
# also execute base initialization of nn.Module
super().__init__()
self.n_oscillators = n_oscillators
# initialize the frequencies
if frequency.size == 0:
# random values if no input argument
self.frequency = nn.Parameter(torch.Tensor(2))
self.frequency = nn.init.uniform_(self.frequency, a = 0.3, b = 3)
else:
self.frequency = nn.Parameter(torch.tensor(frequency))
# initialize the 'a' phase coupling values
if phase_coupling.size == 0:
self.phase_coupling = nn.Parameter(torch.Tensor(4))
self.phase_coupling = nn.init.uniform_(self.phase_coupling, a = 0.1, b = 5)
else:
self.phase_coupling = nn.Parameter(torch.tensor(phase_coupling))
# initialize k
if k == -1:
# random values
self.k = nn.Parameter(torch.Tensor(1))
self.k = nn.init.uniform_(self.k, a = 1, b = 10)
else:
self.k = nn.Parameter(torch.tensor([float(k)]))
# layer to calculate the next phase of the oscillators
self.full_step_layer = FullStepLayer(fs, self.frequency, self.phase_coupling, self.k, n_oscillators)
# layer to get the probabilities for each action
self.softmax = nn.Softmax(dim=None)
self.device = device
# initialize phases
self.phases = torch.tensor([0., 0., 0., 0.])
self.output_angle = self.phases[2] - self.phases[3]
def forward(self, input):
# transform input to two eyes into a vector
# with one value per oscillator
self.input = torch.squeeze(input)
new_input = torch.zeros(self.n_oscillators)
new_input[0] = self.input[0]
new_input[1] = self.input[1]
# execute forward step of the oscillator phases
self.phase_difference = self.full_step_layer(new_input, self.phases)
self.phases += self.phase_difference
# if the right motor (4) stars turnin faster, angle will become negative and agent will steer to the left
output_angle = self.phases[2] - self.phases[3]
self.output_angle = output_angle # % 2 * torch.pi
a = torch.sqrt(torch.tensor(1/(1 * torch.pi))) # corresponds to a cartoid with area of 1
# define probabilities for taking actions according to the phase angle
# positive values make right turn more probable
# make probabilities by defining a cartoid centered around - 90 deg
prob_left = a * (1 - torch.sin(output_angle))
# centered around 90 deg
prob_right = a * (1 - torch.sin( - output_angle))
# acentered around 0 deg
prob_forward = a * (1 - torch.cos(torch.pi - output_angle))
# print(output_angle)
#prob_right = torch.heaviside(torch.sin(output_angle / 4), torch.tensor([0.]))
# prob_left = torch.heaviside(- torch.sin(output_angle / 4), torch.tensor([0.]))
# prob_forward= torch.heaviside(torch.cos( output_angle / 4), torch.tensor([0.]))
# transform output probabilities with softmax
probs = torch.tensor([prob_right, prob_left, prob_forward])
# print(probs)
probs = torch.unsqueeze(probs, 0)
probs.requires_grad = True
return F.softmax(probs, dim=1)
def act(self, state):
# do forward pass (update update oscillator phases)
state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
probs = self.forward(state).cpu()
# pick an action
m = Categorical(probs)
action = m.sample() # probabilistic policy
#action = torch.argmax(probs) # deterministic policy
return action.item(), m.log_prob(action), self.output_angle
def reset(self, chosen_phases):
self.phases = chosen_phases
class FullStepLayer(nn.Module):
"""
Layer that, given the current phases and inputs, calculates
the phases of the oscillators at the next timestep
by solving the system of differential equations using the Runge-Kutta method
"""
def __init__(self, fs, frequency, phase_coupling, k, n_oscillators):
super().__init__()
self.fs = fs
# initialize the runge kutta step layer that calculates the phase differences for each oscillator
# we will pass the input and phases through this layer 4 times in order to execute the runge kutta method
self.runge_kutta_step = RungeKuttaStepLayer(fs, frequency, phase_coupling, k, n_oscillators)
def forward(self, x, phases):
k1 = self.runge_kutta_step.forward(x, phases) * (1/self.fs)
k2 = self.runge_kutta_step.forward(x, phases + 0.5 * k1) * (1/self.fs)
k3 = self.runge_kutta_step.forward(x, phases + 0.5 * k2) * (1/self.fs)
k4 = self.runge_kutta_step.forward(x, phases + k3) * (1/self.fs)
phase_differences = (1/6) * (k1 + 2 * k2 + 2 * k3 + k4)
return phase_differences
class RungeKuttaStepLayer(nn.Module):
"""
Pytorch layer that that calculates the phase change for each oscillator using the full HKB equations
This layer is used sevaral times when using the Runge Kutta method
"""
def __init__(self, fs, frequency, phase_coupling, k, n_oscillators):
super().__init__()
if n_oscillators == 4:
self.frequency_array = torch.tensor([frequency[0], frequency[0], frequency[1], frequency[1]])
else:
self.frequency_array = torch.tensor([frequency[0], frequency[0], frequency[1], frequency[1], frequency[2]])
# create var for each coupling value for clarity
a_sens = phase_coupling[0]
a_ips_left = phase_coupling[1] # left sensor to left motor
a_ips_right = phase_coupling[2] # right sensor to right motor
a_con_left = phase_coupling[3] # starting from left sensor to right motor
a_con_right = phase_coupling[4] # starting from right sensor to left motor
a_motor = phase_coupling[5]
if n_oscillators == 5:
a_soc_sens_left = phase_coupling[6]
a_soc_sens_right = phase_coupling[7]
a_soc_motor_left = phase_coupling[8]
a_soc_motor_right = phase_coupling[9]
if n_oscillators == 4:
# we initialize couping matrix with the chosen weight
self.phase_coupling_matrix = torch.tensor([[0, a_sens, a_ips_left, a_con_left],
[a_sens, 0, a_con_right, a_ips_right],
[a_ips_left, a_con_right, 0, a_motor],
[a_con_left, a_ips_right, a_motor, 0]])
else:
# if there is a 5th oscillator also include the weights for that one
self.phase_coupling_matrix = torch.tensor([[0, a_sens, a_ips_left, a_con_left, a_soc_sens_left],
[a_sens, 0, a_con_right, a_ips_right, a_soc_sens_right],
[a_ips_left, a_con_right, 0, a_motor, a_soc_motor_left],
[a_con_left, a_ips_right, a_motor, 0, a_soc_motor_right],
[a_soc_sens_left, a_soc_sens_right, a_soc_motor_left, a_soc_motor_right, 0]])
# the anti phase weights are proportional to the phase weights
self.anti_phase_coupling_matrix = self.phase_coupling_matrix / k
# these layers will calculate the mutual influence of the oscillators
self.phase_layer = MutualInfluenceLayer(self.phase_coupling_matrix, torch.tensor([1]))
self.anti_phase_layer = MutualInfluenceLayer(self.anti_phase_coupling_matrix, torch.tensor([2]))
self.fs = fs
def forward(self, x, phases):
# get the phase change for each oscillator
# x is the sensory input to each oscillator
x = 2 * torch.pi * self.frequency_array + x - self.phase_layer(phases) - self.anti_phase_layer(phases)
return x
class MutualInfluenceLayer(nn.Module):
"""
Pytorch layer that calculates the phase change for each oscillator in a network of mutually influencing oscillators
"""
def __init__(self, coupling_matrix, phase_multiplyer):
"""
Arguments:
---------
coupling_matrix: torch.Tensor of dim (n_oscillators, n_oscillators)
contains the coupling weight for each pair of oscillators
phase_multiplyer: scalar
1 for in-phase coupling
2 for anti-phase coupling
"""
super().__init__()
self.weights = torch.flatten(coupling_matrix)
self.phase_multiplyer = phase_multiplyer
self.num_oscillators = coupling_matrix.size(dim=0)
def forward(self, phases):
"""
Arguments:
---------
phases: torch.Tensor of dim n_oscillators
contains the phase of each oscillator in the previous timestep
Returns:
---------
weighted_sums: torch.Tensor of dim n_oscillators
contains the phase change for each oscillators resulting from its (in/anti) phase coupling with the other oscillators
"""
phases_i = torch.unsqueeze(phases, 1)
phases_i = torch.flatten(phases_i.repeat(1, self.num_oscillators))
phases_j = torch.squeeze(phases.repeat(1, self.num_oscillators), 0)
phase_differences = self.phase_multiplyer * (phases_i - phases_j)
weighted_differences = self.weights * torch.sin(phase_differences)
weighted_differences = torch.reshape(weighted_differences, (self.num_oscillators, self.num_oscillators))
weighted_sums = torch.sum(weighted_differences, dim=1)
return weighted_sums
class SocialGuido(nn.Module):
"""
Guido with 5 oscillators and the 5th oscillator takes into account the orientation of the other agents
Arguments
---------
frequency: np or torch array of length 2
should be [sensory_frequency, motor_frequency]
phase_coupling: np or torch array of length 4
should be [a_sensory, a_ipsilateral, a_contralateral, a_motor]
k: int or float
the proportion of phase coupling vs anti-phase coupling
"""
def __init__(self, device, fs, frequency = np.array([]), phase_coupling = np.array([]), k = -1, agent_coupling = -1, social_weight_decay_rate = 0, n_agents = 1, agent_id = 0):
# also execute base initialization of nn.Module
super().__init__()
# initialize the frequencies
if frequency.size == 0:
# random values if no input argument
self.frequency = nn.Parameter(torch.Tensor(2))
self.frequency = nn.init.uniform_(self.frequency, a = 0.3, b = 3)
else:
self.frequency = nn.Parameter(torch.tensor(frequency))
# initialize the 'a' phase coupling values
if phase_coupling.size == 0:
self.phase_coupling = nn.Parameter(torch.Tensor(5))
self.phase_coupling = nn.init.uniform_(self.phase_coupling, a = 0.1, b = 5)
else:
self.phase_coupling = nn.Parameter(torch.tensor(phase_coupling))
# initialize k
if k == -1:
# random values
self.k = nn.Parameter(torch.Tensor(1))
self.k = nn.init.uniform_(self.k, a = 1, b = 10)
else:
self.k = nn.Parameter(torch.tensor([float(k)]))
# initialize agent coupling w
if agent_coupling == -1:
# random values
self.agent_coupling = nn.Parameter(torch.Tensor(1))
self.agent_coupling = nn.init.uniform_(self.k, a = 1, b = 10)
else:
self.agent_coupling = nn.Parameter(torch.tensor([float(agent_coupling)]))
# layer to calculate the next phase of the oscillators
self.social_full_step_layer = SocialFullStepLayer(fs, self.frequency, self.phase_coupling, self.k, self.agent_coupling, social_weight_decay_rate, n_agents, agent_id)
# layer to get the probabilities for each action
self.softmax = nn.Softmax(dim=None)
self.device = device
# initialize phases
self.phases = torch.tensor([0., 0., 0., 0., 0])
self.output_angle = self.phases[3] - self.phases[2]
def forward(self, input, angles, inter_agent_distances):
# transform input to two eyes into a vector
# with one value per oscillator
self.input = torch.squeeze(input)
new_input = torch.zeros(5)
new_input[0] = self.input[0]
new_input[1] = self.input[1]
self.angles = torch.squeeze(angles)
self.inter_agent_distances = torch.squeeze(inter_agent_distances)
# execute forward step of the oscillator phases
self.phase_difference = self.social_full_step_layer(new_input, self.phases, self.angles, self.inter_agent_distances)
self.phases += self.phase_difference
output_angle = self.phases[2] - self.phases[3]
#print(output_angle)
self.output_angle = output_angle # % 2 * torch.pi
#a = torch.sqrt(torch.tensor(1/(6 * torch.pi))) # corresponds to a cartoid with area of 1
angles = torch.linspace(-torch.pi, torch.pi, 360)
probs = (1/(2*torch.pi)) * (1 - torch.cos(output_angle - torch.pi - angles))
# define probabilities for taking actions according to the phase angle
# positive values make right turn more probable
probs = torch.unsqueeze(probs, 0)
#probs.requires_grad = True
return F.softmax(probs, dim=1)
def act(self, state, angles, inter_agent_distances):
# do forward pass (update update oscillator phases)
state = torch.from_numpy(state).float().unsqueeze(0).to(self.device)
angles = torch.from_numpy(angles).float().unsqueeze(0).to(self.device)
inter_agent_distances = torch.from_numpy(inter_agent_distances).float().unsqueeze(0).to(self.device)
probs = self.forward(state, angles, inter_agent_distances).cpu()
# pick an action
m = Categorical(probs)
action = m.sample() # probabilistic policy
#action = torch.argmax(probs) # deterministic policy
return action.item(), m.log_prob(action), self.output_angle
def reset(self, chosen_phases):
self.phases = chosen_phases
class SocialFullStepLayer(nn.Module):
"""
Layer that, given the current phases and inputs, calculates
the phases of the oscillators at the next timestep
by solving the system of differential equations using the Runge-Kutta method
"""
def __init__(self, fs, frequency, phase_coupling, k, agent_coupling, social_weight_decay_rate, n_agents, agent_id):
super().__init__()
self.fs = fs
# initialize the runge kutta step layer that calculates the phase differences for each oscillator
# we will pass the input and phases through this layer 4 times in order to execute the runge kutta method
self.runge_kutta_step = SocialRungeKuttaStepLayer(fs, frequency, phase_coupling, k, agent_coupling, social_weight_decay_rate, n_agents, agent_id)
def forward(self, x, phases, angles, inter_agent_distances):
k1 = self.runge_kutta_step.forward(x, phases, angles, inter_agent_distances) * (1/self.fs)
k2 = self.runge_kutta_step.forward(x, phases + 0.5 * k1, angles, inter_agent_distances) * (1/self.fs)
k3 = self.runge_kutta_step.forward(x, phases + 0.5 * k2, angles, inter_agent_distances) * (1/self.fs)
k4 = self.runge_kutta_step.forward(x, phases + k3, angles, inter_agent_distances) * (1/self.fs)
phase_differences = (1/6) * (k1 + 2 * k2 + 2 * k3 + k4)
return phase_differences
class SocialRungeKuttaStepLayer(nn.Module):
"""
Pytorch layer that that calculates the phase change for each oscillator using the full HKB equations
This layer is used sevaral times when using the Runge Kutta method
"""
def __init__(self, fs, frequency, phase_coupling, k, agent_coupling, social_weight_decay_rate, n_agents, agent_id):
super().__init__()
self.frequency_array = torch.tensor([frequency[0], frequency[0], frequency[1], frequency[1], frequency[2]])
# create var for each coupling value for clarity
a_sens = phase_coupling[0]
a_ips_left = phase_coupling[1] # left sensor to left motor
a_ips_right = phase_coupling[2] # right sensor to right motor
a_con_left = phase_coupling[3] # starting from left sensor to right motor
a_con_right = phase_coupling[4] # starting from right sensor to left motor
a_motor = phase_coupling[5]
a_soc_sens_left = phase_coupling[6]
a_soc_sens_right = phase_coupling[7]
a_soc_motor_left = phase_coupling[8]
a_soc_motor_right = phase_coupling[9]
# if there is a 5th oscillator also include the weights for that one
self.phase_coupling_matrix = torch.tensor([[0, a_sens, a_ips_left, a_con_left, a_soc_sens_left],
[a_sens, 0, a_con_right, a_ips_right, a_soc_sens_right],
[a_ips_left, a_con_right, 0, a_motor, a_soc_motor_left],
[a_con_left, a_ips_right, a_motor, 0, a_soc_motor_right],
[a_soc_sens_left, a_soc_sens_right, a_soc_motor_left, a_soc_motor_right, 0]])
# the anti phase weights are proportional to the phase weights
self.anti_phase_coupling_matrix = self.phase_coupling_matrix / k
# these layers will calculate the mutual influence of the oscillators
self.phase_layer = MutualInfluenceLayer(self.phase_coupling_matrix, torch.tensor([1]))
self.anti_phase_layer = MutualInfluenceLayer(self.anti_phase_coupling_matrix, torch.tensor([2]))
# these layer will calculate social influence between agents
self.phase_social_layer = SocialInfluenceLayer(agent_coupling, social_weight_decay_rate, torch.tensor([1]), agent_id, n_agents)
anti_phase_agent_coupling = agent_coupling / k
self.anti_phase_social_layer = SocialInfluenceLayer(anti_phase_agent_coupling, social_weight_decay_rate, torch.tensor([2]), agent_id, n_agents)
self.fs = fs
def forward(self, input, phases, angles, inter_agent_distances):
# get the phase change for each oscillator
# x is the sensory input to each oscillator
output = 2 * torch.pi * self.frequency_array + input - self.phase_layer(phases) - self.anti_phase_layer(phases) - self.phase_social_layer(angles, inter_agent_distances) - self.anti_phase_social_layer(angles, inter_agent_distances)
#print("full")
#print(output)
#print("social")
#print( - self.phase_social_layer(angles, inter_agent_distances) - self.anti_phase_social_layer(angles, inter_agent_distances))
return output
class SocialInfluenceLayer(nn.Module):
"""
Pytorch layer that calculates the phase change for each oscillator in a network of mutually influencing oscillators
"""
def __init__(self, coupling_weight, social_weight_decay_rate, phase_multiplyer, agent_id, n_agents):
"""
Arguments:
---------
coupling_matrix: torch.Tensor of dim (n_oscillators, n_oscillators)
contains the coupling weight for each pair of oscillators
phase_multiplyer: scalar
1 for in-phase coupling
2 for anti-phase coupling
"""
super().__init__()
self.coupling_weight = coupling_weight
self.social_weight_decay_rate = social_weight_decay_rate
self.phase_multiplyer = phase_multiplyer
# we will use this to get an array of the agent angles from which we can subtract the angles of the other agents
self.angle_filter = torch.zeros(n_agents, n_agents)
self.angle_filter[:, agent_id] = torch.ones(n_agents)
self.distance_filter = torch.zeros(n_agents)
self.distance_filter[agent_id] = 1
# we only want the social information to enter one of the oscillators
n_oscillators = 5 # per definition the Social Guido has 5 oscillators
self.oscillator_filter = torch.zeros(n_oscillators)
self.oscillator_filter[4] = 1
self.agent_id = agent_id
def forward(self, angles, inter_agent_distances):
"""
Arguments:
---------
phases: torch.Tensor of dim n_oscillators
contains the phase of each oscillator in the previous timestep
Returns:
---------
weighted_sums: torch.Tensor of dim n_oscillators
contains the phase change for each oscillators resulting from its (in/anti) phase coupling with the other oscillators
"""
# calcultate the angle differences with the other agents
# first get a vector of your own angles
self_angles = torch.matmul(self.angle_filter, angles)
angle_differencs = self.phase_multiplyer * (self_angles - angles)
distances = torch.matmul(inter_agent_distances, self.distance_filter)
# create proportional weighted sum
weighted_differences = self.coupling_weight * torch.exp( - self.social_weight_decay_rate * distances ) * torch.sin(angle_differencs)
weighted_sum = torch.sum(weighted_differences)
# pass the sum to the correct oscillator
output = self.oscillator_filter * weighted_sum
return output