MARL TD3
robot_nav.models.MARL.marlTD3
Actor
Bases: Module
Policy network for MARL, with an attention mechanism for multi-robot coordination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_dim
|
int
|
Number of action dimensions. |
required |
embedding_dim
|
int
|
Dimensionality of agent feature embeddings. |
required |
Attributes:
Name | Type | Description |
---|---|---|
attention |
Attention
|
Encodes agent state and computes attention. |
policy_head |
Sequential
|
MLP for mapping attention output to actions. |
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|
forward(obs, detach_attn=False)
Forward pass through the actor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
Tensor
|
Observation input of shape (batch, n_agents, obs_dim). |
required |
detach_attn
|
bool
|
If True, detach attention output from computation graph. |
False
|
Returns:
Name | Type | Description |
---|---|---|
tuple |
(action, hard_logits, pair_d, mean_entropy, hard_weights, combined_weights) |
Source code in robot_nav/models/MARL/marlTD3.py
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Critic
Bases: Module
Critic (value) network for MARL, with twin Q-outputs and attention encoding.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action_dim
|
int
|
Number of action dimensions. |
required |
embedding_dim
|
int
|
Dimensionality of agent feature embeddings. |
required |
Attributes:
Name | Type | Description |
---|---|---|
attention |
Attention
|
Encodes agent state and computes attention. |
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|
forward(embedding, action)
Forward pass through both Q-networks using attention on agent embeddings.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
embedding
|
Tensor
|
Input agent embeddings (batch, n_agents, state_dim). |
required |
action
|
Tensor
|
Actions (batch * n_agents, action_dim). |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
(Q1, Q2, mean_entropy, hard_logits, unnorm_rel_dist, hard_weights) Q1, Q2 (Tensor): Twin Q-value estimates (batch * n_agents, 1) mean_entropy (Tensor): Soft attention entropy (scalar). hard_logits (Tensor): Hard attention logits (batch * n_agents, n_agents-1). unnorm_rel_dist (Tensor): Unnormalized inter-agent distances. hard_weights (Tensor): Hard attention weights (batch, n_agents, n_agents-1). |
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|
TD3
Bases: object
TD3 (Twin Delayed Deep Deterministic Policy Gradient) agent for multi-agent reinforcement learning.
Wraps actor and critic networks, optimizer setup, exploration, training, and saving/loading utilities.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state_dim
|
int
|
State vector length per agent. |
required |
action_dim
|
int
|
Number of action dimensions. |
required |
max_action
|
float
|
Maximum action value for clipping. |
required |
device
|
device
|
Torch device. |
required |
num_robots
|
int
|
Number of robots/agents. |
required |
lr_actor
|
float
|
Learning rate for actor optimizer. |
0.0001
|
lr_critic
|
float
|
Learning rate for critic optimizer. |
0.0003
|
save_every
|
int
|
Save model every N train iterations (0 = disable). |
0
|
load_model
|
bool
|
If True, load model from checkpoint. |
False
|
save_directory
|
Path
|
Path for saving model files. |
Path('robot_nav/models/MARL/checkpoint')
|
model_name
|
str
|
Base name for saved models. |
'marlTD3'
|
load_model_name
|
str or None
|
Name for loading saved model files. |
None
|
load_directory
|
Path
|
Path for loading model files. |
Path('robot_nav/models/MARL/checkpoint')
|
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|
act(state)
Computes the deterministic action from the actor network for a given state.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
ndarray
|
State (n_agents, state_dim). |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
(action, connection_logits, combined_weights) action (np.ndarray): Action(s) (flattened). connection_logits (Tensor): Hard attention logits. combined_weights (Tensor): Final soft attention weights. |
Source code in robot_nav/models/MARL/marlTD3.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
|
get_action(obs, add_noise)
Computes an action (with optional exploration noise) for a given observation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obs
|
ndarray
|
State vector (n_agents, state_dim) or batch. |
required |
add_noise
|
bool
|
Whether to add exploration noise. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
(action, connection_logits, combined_weights) action (np.ndarray): Action(s) (n_agents, action_dim). connection_logits (Tensor): Hard attention logits. combined_weights (Tensor): Final soft attention weights. |
Source code in robot_nav/models/MARL/marlTD3.py
218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 |
|
load(filename, directory)
Loads model parameters from the specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Base filename for saved files. |
required |
directory
|
Path
|
Path to load the model files from. |
required |
Source code in robot_nav/models/MARL/marlTD3.py
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
|
prepare_state(poses, distance, cos, sin, collision, action, goal_positions)
Formats raw environment state for learning.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
poses
|
list
|
Each agent's global pose [x, y, theta]. |
required |
distance
|
list
|
Distance to goal for each agent. |
required |
cos
|
list
|
Cosine of angle to goal. |
required |
sin
|
list
|
Sine of angle to goal. |
required |
collision
|
list
|
Collision flags per agent. |
required |
action
|
list
|
Last action taken [lin_vel, ang_vel]. |
required |
goal_positions
|
list
|
Each agent's goal [x, y]. |
required |
Returns:
Name | Type | Description |
---|---|---|
tuple |
states (list): List of processed state vectors. terminal (list): 1 if collision or goal reached, else 0. |
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|
save(filename, directory)
Saves the current model parameters to the specified directory.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
filename
|
str
|
Base filename for saved files. |
required |
directory
|
Path
|
Path to save the model files. |
required |
Source code in robot_nav/models/MARL/marlTD3.py
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 |
|
train(replay_buffer, iterations, batch_size, discount=0.99, tau=0.005, policy_noise=0.2, noise_clip=0.5, policy_freq=2, bce_weight=0.1, entropy_weight=1, connection_proximity_threshold=4)
Runs a full TD3 training cycle using sampled experiences.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
replay_buffer
|
Experience replay buffer. |
required | |
iterations
|
int
|
Training steps. |
required |
batch_size
|
int
|
Batch size. |
required |
discount
|
float
|
Discount factor (gamma). |
0.99
|
tau
|
float
|
Target network soft update factor. |
0.005
|
policy_noise
|
float
|
Noise std for policy smoothing. |
0.2
|
noise_clip
|
float
|
Max policy smoothing noise. |
0.5
|
policy_freq
|
int
|
Frequency of actor/policy updates. |
2
|
bce_weight
|
float
|
Loss weight for connection prediction BCE. |
0.1
|
entropy_weight
|
float
|
Loss weight for attention entropy term. |
1
|
connection_proximity_threshold
|
float
|
Threshold for true binary connection label. |
4
|
Returns:
Type | Description |
---|---|
None |
Source code in robot_nav/models/MARL/marlTD3.py
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 |
|