-
Notifications
You must be signed in to change notification settings - Fork 0
/
norm_swarm.py
341 lines (298 loc) · 13.3 KB
/
norm_swarm.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
# Copyright (c) 2022-2023.
# ProrokLab (https://www.proroklab.org/)
# All rights reserved.
import typing
from typing import Dict, Callable, List
import torch
from torch import Tensor
from vmas import render_interactively
from vmas.simulator.core import Agent, Landmark, Sphere, World, Entity
from vmas.simulator.heuristic_policy import BaseHeuristicPolicy
from vmas.simulator.scenario import BaseScenario
from vmas.simulator.sensors import Lidar
from vmas.simulator.utils import Color, X, Y, ScenarioUtils
if typing.TYPE_CHECKING:
from vmas.simulator.rendering import Geom
class Scenario(BaseScenario):
def make_world(self, batch_dim: int, device: torch.device, **kwargs):
self.n_agents = kwargs.get("n_agents", 4)
self.n_targets = kwargs.get("n_targets", 1)
self._min_dist_between_entities = kwargs.get("min_dist_between_entities", 0.2)
self._lidar_range = kwargs.get("lidar_range", 0.35)
self._covering_range = kwargs.get("covering_range", 0.25)
self._agents_per_target = kwargs.get("agents_per_target", 2)
self.targets_respawn = kwargs.get("targets_respawn", True)
self.shared_reward = kwargs.get("shared_reward", False)
self.agent_collision_penalty = kwargs.get("agent_collision_penalty", 0)
self.covering_rew_coeff = kwargs.get("covering_rew_coeff", 1.0)
self.time_penalty = kwargs.get("time_penalty", 0)
self._comms_range = self._lidar_range
self.min_collision_distance = 0.005
self.agent_radius = 0.05
self.target_radius = self.agent_radius
self.viewer_zoom = 1
self.target_color = Color.RED
# Make world
world = World(
batch_dim,
device,
x_semidim=1,
y_semidim=1,
collision_force=500,
substeps=2,
drag=0.25,
)
# Add agents
entity_filter_agents: Callable[[Entity], bool] = lambda e: e.name.startswith(
"agent"
)
entity_filter_targets: Callable[[Entity], bool] = lambda e: e.name.startswith(
"target"
)
for i in range(self.n_agents):
# Constraint: all agents have same action range and multiplier
agent = Agent(
name=f"agent_{i}",
collide=True,
shape=Sphere(radius=self.agent_radius),
sensors=[
# Lidar(
# world,
# angle_start=0.05,
# angle_end=2 * torch.pi + 0.05,
# n_rays=12,
# max_range=self._lidar_range,
# entity_filter=entity_filter_agents,
# render_color=Color.BLUE,
# ),
Lidar(
world,
n_rays=12,
max_range=self._lidar_range,
entity_filter=entity_filter_targets,
render_color=Color.BLUE,
),
],
)
agent.collision_rew = torch.zeros(batch_dim, device=device)
agent.covering_reward = agent.collision_rew.clone()
world.add_agent(agent)
self._targets = []
for i in range(self.n_targets):
target = Landmark(
name=f"target_{i}",
collide=True,
movable=False,
shape=Sphere(radius=self.target_radius),
color=self.target_color,
)
world.add_landmark(target)
self._targets.append(target)
self.covered_targets = torch.zeros(batch_dim, self.n_targets, device=device)
self.shared_covering_rew = torch.zeros(batch_dim, device=device)
return world
def reset_world_at(self, env_index: int = None):
placable_entities = self._targets[: self.n_targets] + self.world.agents
if env_index is None:
self.all_time_covered_targets = torch.full(
(self.world.batch_dim, self.n_targets), False, device=self.world.device
)
else:
self.all_time_covered_targets[env_index] = False
ScenarioUtils.spawn_entities_randomly(
entities=placable_entities,
world=self.world,
env_index=env_index,
min_dist_between_entities=self._min_dist_between_entities,
x_bounds=(-self.world.x_semidim, self.world.x_semidim),
y_bounds=(-self.world.y_semidim, self.world.y_semidim),
)
for target in self._targets[self.n_targets :]:
target.set_pos(self.get_outside_pos(env_index), batch_index=env_index)
def reward(self, agent: Agent):
is_first = agent == self.world.agents[0]
is_last = agent == self.world.agents[-1]
if is_first:
self.time_rew = torch.full(
(self.world.batch_dim,), self.time_penalty, device=self.world.device
)
self.agents_pos = torch.stack(
[a.state.pos for a in self.world.agents], dim=1
)
self.targets_pos = torch.stack([t.state.pos for t in self._targets], dim=1)
self.agents_targets_dists = torch.cdist(self.agents_pos, self.targets_pos)
self.agents_per_target = torch.sum(
(self.agents_targets_dists < self._covering_range).type(torch.int),
dim=1,
)
self.covered_targets = self.agents_per_target >= self._agents_per_target
self.shared_covering_rew[:] = 0
for a in self.world.agents:
self.shared_covering_rew += self.agent_reward(a)
self.shared_covering_rew[self.shared_covering_rew != 0] /= 2
# Avoid collisions with each other
agent.collision_rew[:] = 0
for a in self.world.agents:
if a != agent:
agent.collision_rew[
self.world.get_distance(a, agent) < self.min_collision_distance
] += self.agent_collision_penalty
if is_last:
if self.targets_respawn:
occupied_positions_agents = [self.agents_pos]
for i, target in enumerate(self._targets):
occupied_positions_targets = [
o.state.pos.unsqueeze(1)
for o in self._targets
if o is not target
]
occupied_positions = torch.cat(
occupied_positions_agents + occupied_positions_targets, dim=1
)
pos = ScenarioUtils.find_random_pos_for_entity(
occupied_positions,
env_index=None,
world=self.world,
min_dist_between_entities=self._min_dist_between_entities,
x_bounds=(-self.world.x_semidim, self.world.x_semidim),
y_bounds=(-self.world.y_semidim, self.world.y_semidim),
)
target.state.pos[self.covered_targets[:, i]] = pos[
self.covered_targets[:, i]
].squeeze(1)
else:
self.all_time_covered_targets += self.covered_targets
for i, target in enumerate(self._targets):
target.state.pos[self.covered_targets[:, i]] = self.get_outside_pos(
None
)[self.covered_targets[:, i]]
covering_rew = (
agent.covering_reward
if not self.shared_reward
else self.shared_covering_rew
)
return agent.collision_rew + covering_rew + self.time_rew
def get_outside_pos(self, env_index):
return torch.empty(
(1, self.world.dim_p)
if env_index is not None
else (self.world.batch_dim, self.world.dim_p),
device=self.world.device,
).uniform_(-1000 * self.world.x_semidim, -10 * self.world.x_semidim)
def agent_reward(self, agent):
if "target" not in agent.name:
agent_index = self.world.agents.index(agent)
agent.covering_reward[:] = 0
targets_covered_by_agent = (
self.agents_targets_dists[:, agent_index] < self._covering_range
)
num_covered_targets_covered_by_agent = (
targets_covered_by_agent * self.covered_targets
).sum(dim=-1)
agent.covering_reward += (
num_covered_targets_covered_by_agent * self.covering_rew_coeff
)
return agent.covering_reward
else:
return 0
def observation(self, agent: Agent):
lidar_1_measures = agent.sensors[0].measure()
# lidar_2_measures = agent.sensors[1].measure()
return torch.cat(
[
agent.state.pos,
agent.state.vel,
torch.zeros_like(agent.state.pos),
lidar_1_measures,
# lidar_2_measures,
],
dim=-1,
)
def info(self, agent: Agent) -> Dict[str, Tensor]:
info = {
"covering_reward": agent.covering_reward
if not self.shared_reward
else self.shared_covering_rew,
"collision_rew": agent.collision_rew,
"targets_covered": self.covered_targets.sum(-1),
}
return info
def done(self):
return self.all_time_covered_targets.all(dim=-1)
def extra_render(self, env_index: int = 0) -> "List[Geom]":
from vmas.simulator import rendering
geoms: List[Geom] = []
# Target ranges
for i, target in enumerate(self._targets):
range_circle = rendering.make_circle(self._covering_range, filled=False)
xform = rendering.Transform()
xform.set_translation(*target.state.pos[env_index])
range_circle.add_attr(xform)
range_circle.set_color(*self.target_color.value)
geoms.append(range_circle)
# Communication lines
for i, agent1 in enumerate(self.world.agents):
for j, agent2 in enumerate(self.world.agents):
if j <= i:
continue
agent_dist = torch.linalg.vector_norm(
agent1.state.pos - agent2.state.pos, dim=-1
)
if agent_dist[env_index] <= self._comms_range:
color = Color.BLACK.value
line = rendering.Line(
(agent1.state.pos[env_index]),
(agent2.state.pos[env_index]),
width=1,
)
xform = rendering.Transform()
line.add_attr(xform)
line.set_color(*color)
geoms.append(line)
return geoms
class HeuristicPolicy(BaseHeuristicPolicy):
def compute_action(self, observation: torch.Tensor, u_range: float) -> torch.Tensor:
assert self.continuous_actions
# First calculate the closest point to a circle of radius circle_radius given the current position
circle_origin = torch.zeros(1, 2, device=observation.device)
circle_radius = 0.75
current_pos = observation[:, :2]
v = current_pos - circle_origin
closest_point_on_circ = (
circle_origin + v / torch.linalg.norm(v, dim=1).unsqueeze(1) * circle_radius
)
# calculate the normal vector of the vector from the origin of the circle to that closest point
# on the circle. Adding this scaled normal vector to the other vector gives us a target point we
# try to reach, thus resulting in a circular motion.
closest_point_on_circ_normal = torch.stack(
[closest_point_on_circ[:, Y], -closest_point_on_circ[:, X]], dim=1
)
closest_point_on_circ_normal /= torch.linalg.norm(
closest_point_on_circ_normal, dim=1
).unsqueeze(1)
closest_point_on_circ_normal *= 0.1
des_pos = closest_point_on_circ + closest_point_on_circ_normal
# Move away from other agents within visibility range
lidar_agents = observation[:, 4:16]
agent_visible = torch.any(lidar_agents < 0.15, dim=1)
_, agent_dir_index = torch.min(lidar_agents, dim=1)
agent_dir = agent_dir_index / lidar_agents.shape[1] * 2 * torch.pi
agent_vec = torch.stack([torch.cos(agent_dir), torch.sin(agent_dir)], dim=1)
des_pos_agent = current_pos - agent_vec * 0.1
des_pos[agent_visible] = des_pos_agent[agent_visible]
# Move towards targets within visibility range
lidar_targets = observation[:, 16:28]
target_visible = torch.any(lidar_targets < 0.3, dim=1)
_, target_dir_index = torch.min(lidar_targets, dim=1)
target_dir = target_dir_index / lidar_targets.shape[1] * 2 * torch.pi
target_vec = torch.stack([torch.cos(target_dir), torch.sin(target_dir)], dim=1)
des_pos_target = current_pos + target_vec * 0.1
des_pos[target_visible] = des_pos_target[target_visible]
action = torch.clamp(
(des_pos - current_pos) * 10,
min=-u_range,
max=u_range,
)
return action
if __name__ == "__main__":
render_interactively(__file__, control_two_agents=True)