Parallel Base Agent
The ParallelBaseAgent class extends the concepts of the base agent to manage connections to multiple NetSecGame server instances simultaneously, enabling parallel environment interaction.
Unlike BaseAgent (which manages a single socket), this class maintains one TCP socket per environment and exposes vectorized versions of methods like register(), make_step(), and request_game_reset() that operate on lists of actions and observations.
netsecgame.agents.parallel_base_agent.ParallelBaseAgent
Agent that manages connections to multiple NetSecGame server instances simultaneously, enabling parallel environment interaction.
Unlike BaseAgent (which manages a single socket), this class maintains
one TCP socket per environment and exposes vectorized versions of
register(), make_step(), and request_game_reset() that
operate on lists of actions/observations.
For a concrete example of extending and using this class,
see examples/agents/random_attacker.py.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
game_hosts
|
str | List[str]
|
Host address(es). A single string is broadcast to all
|
required |
game_ports
|
int | List[int]
|
Port(s) — one per environment instance. A single int creates a one-environment agent. |
required |
role
|
AgentRole
|
The agent role shared across all environments. |
required |
max_workers
|
Optional[int]
|
Maximum threads in the pool. Defaults to |
None
|
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
all_done
property
True when every episode has ended.
connected
property
Per-environment connection status.
done_mask
property
Current done mask. A single bool in single-env mode,
otherwise a list of bools (one per env).
logger
property
Returns the logger instance for this agent.
num_envs
property
Number of environments (always equals len(game_ports)).
__del__
Close any remaining sockets and shut down the thread pool when the object is garbage-collected.
Source code in netsecgame/agents/parallel_base_agent.py
103 104 105 106 | |
_communicate_single
Send action to environment env_idx and return the response.
Source code in netsecgame/agents/parallel_base_agent.py
196 197 198 199 200 201 202 203 204 205 206 | |
_make_step_single
Execute a single step on one environment.
Source code in netsecgame/agents/parallel_base_agent.py
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 | |
_receive_data
staticmethod
Block until a full response is received from sock.
Source code in netsecgame/agents/parallel_base_agent.py
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
_register_single
Register on a single environment.
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
_request_game_reset_single
Reset a single environment.
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
_run_parallel
Submit fn(env_idx, ...) for each index and collect results in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[[int, ...], Any]
|
Callable whose first argument is the environment index. |
required |
env_indices
|
Optional[List[int]]
|
Which envs to dispatch to. Defaults to all connected envs. |
None
|
args_per_env
|
Optional[Dict[int, tuple]]
|
Optional extra positional args per env index. |
None
|
Returns:
| Type | Description |
|---|---|
List[Any]
|
List of results, one per |
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
_send_data
staticmethod
Send a JSON-encoded message over sock.
Source code in netsecgame/agents/parallel_base_agent.py
167 168 169 170 171 | |
make_step
Execute one step in every active environment in parallel.
Note: If you need to access the boolean done statuses across
all environments, you can use the self.done_mask property.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
actions
|
Action | List[Action]
|
In single-env mode a single |
required |
Returns:
| Type | Description |
|---|---|
Observation | None | List[Optional[Observation]]
|
A single |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the number of actions doesn't match |
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
register
Register in all connected environments in parallel.
Returns:
| Type | Description |
|---|---|
Observation | None | List[Optional[Observation]]
|
The initial |
Source code in netsecgame/agents/parallel_base_agent.py
365 366 367 368 369 370 371 372 373 374 375 376 | |
request_game_reset
Reset all environments in parallel.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
request_trajectory
|
bool
|
If True, request episode trajectory from the server. |
False
|
randomize_topology
|
bool
|
If True, randomize the network topology. |
False
|
seed
|
Optional[int]
|
RNG seed. Required when |
None
|
Returns:
| Type | Description |
|---|---|
Observation | None | List[Optional[Observation]]
|
The initial |
Source code in netsecgame/agents/parallel_base_agent.py
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 | |
terminate_connection
Close all sockets and shut down the thread pool.
Source code in netsecgame/agents/parallel_base_agent.py
108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |