-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.py
75 lines (56 loc) · 2.9 KB
/
agent.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
import numpy as np
from numpy.linalg import norm
class Agent:
def __init__(self, index, simulation):
self.simulation = simulation
self.position = np.random.uniform(low=-simulation.init_size,
high=simulation.init_size,
size=(1, simulation.dim))
self.index = index
self.velocity = np.zeros((1, simulation.dim))
self.momentum = 0
self.local_grad = np.zeros((simulation.n, simulation.dim))
self.error = np.zeros((simulation.n, simulation.dim))
self.cooldown = 0
self.mu = .3
#print(f"agent {index} initial position: {self.position}")
# Loss functions for gradient estimation, with respect to the pursued source
def loss(self, source):
return .5 * (norm(self.position - source.position) ** 2)
def loss_plus(self, source, u):
return .5 * (norm(self.position + self.mu * u - (source.position + .5 * source.velocity)) ** 2)
# Loss functions for gradient estimation, with respect to neighboring agents
# (used for the regularization terms)
def loss_reg(self, neighbor):
return self.simulation.Lambda * (norm(self.position - neighbor.position)**2 - self.simulation.r**2)
def loss_reg_plus(self, neighbor, u):
return self.simulation.Lambda * (norm(self.position +
self.mu * u -
(neighbor.position + .5 * neighbor.velocity))**2 -
self.simulation.r**2)
# Computes the local gradient of the agent
def compute_grad(self, source):
u = np.random.standard_normal((1, self.simulation.dim))
grad_i = u * (self.loss_plus(source, u) - self.loss(source)) / self.mu
if not self.simulation.benchmark:
local_grad = np.zeros((self.simulation.n, self.simulation.dim))
local_grad[self.index, :] = grad_i
for neighbor in self.simulation.detected_neighbors[self.index]:
u = np.random.standard_normal((1, self.simulation.dim))
grad_j = u * (self.loss_reg_plus(neighbor, u) - self.loss_reg(neighbor)) / self.mu
local_grad[neighbor.index, :] = -grad_j
else:
local_grad = grad_i / norm(grad_i)
self.local_grad = local_grad
class CircularAgent(Agent):
def __init__(self, index, simulation, source):
super(CircularAgent, self).__init__(index, simulation)
rotate = source.rotate
# initialize agent from 1 rad movement behind
rotate[0, 1] *= -1
rotate[1, 0] *= -1
centered_pos = source.position - source.center
self.position = np.matmul(rotate, centered_pos.T).T + source.center
# velocity was removed
def loss_plus(self, source, u):
return .5 * (norm(self.position + self.mu * u - source.position) ** 2)