Multiple environments#
IR-SIM supports creating and running multiple environments simultaneously. Each environment maintains its own isolated state, including world parameters, objects, and simulation time. This is useful for:
Parallel simulations: Running multiple scenarios simultaneously
Comparison studies: Comparing different algorithms or configurations
Training: Running multiple instances for reinforcement learning
Creating Multiple Environments#
import irsim
# Create two separate environments
env1 = irsim.make('scenario_a.yaml')
env2 = irsim.make('scenario_b.yaml')
# Each environment has its own state
print(f"Env1 robots: {env1.robot_number}")
print(f"Env2 robots: {env2.robot_number}")
Isolated Parameters#
Each environment has completely separate parameter instances:
import irsim
env1 = irsim.make('world1.yaml')
env2 = irsim.make('world2.yaml')
# World parameters are isolated
env1.world_param.control_mode = 'keyboard'
print(f"Env1 control mode: {env1.world_param.control_mode}") # keyboard
print(f"Env2 control mode: {env2.world_param.control_mode}") # auto (unchanged)
# Simulation time is independent
for _ in range(10):
env1.step()
for _ in range(5):
env2.step()
print(f"Env1 time: {env1.time}") # 1.0 (10 steps * 0.1)
print(f"Env2 time: {env2.time}") # 0.5 (5 steps * 0.1)
Running Multiple Environments#
import irsim
env1 = irsim.make('scenario_a.yaml', display=True)
env2 = irsim.make('scenario_b.yaml', display=True)
# Run both environments in the same loop
for i in range(500):
# Step both environments
env1.step()
env2.step()
# Render both (creates two windows)
env1.render(0.01)
env2.render(0.01)
# Check completion independently
if env1.done() and env2.done():
break
env1.end()
env2.end()
import irsim
# Create multiple headless environments for training
num_envs = 4
envs = [
irsim.make(f'training_world.yaml', display=False, seed=i)
for i in range(num_envs)
]
# Run training loop
for episode in range(100):
# Reset all environments
for env in envs:
env.reset()
# Collect experiences from all environments
for step in range(1000):
actions = [get_action(env) for env in envs] # Your policy
for env, action in zip(envs, actions):
env.step(action)
# Check if any environment is done
if all(env.done() for env in envs):
break
# Clean up
for env in envs:
env.end()
Parameter Isolation Details#
Each environment instance creates and binds its own parameter objects:
world_param - Simulation state and settings:
Attribute |
Type |
Default |
Description |
|---|---|---|---|
|
|
|
Current simulation time |
|
|
|
Control mode ( |
|
|
|
Collision handling mode |
|
|
|
Time step duration (seconds) |
|
|
|
Simulation step counter |
env_param - Environment objects and utilities:
Attribute |
Type |
Description |
|---|---|---|
|
|
List of all objects in the environment |
|
|
Logger instance for this environment |
|
|
Spatial index for collision detection |
|
|
Operating system name |
path_param - File path management:
Attribute |
Type |
Description |
|---|---|---|
|
|
Path to irsim package directory |
|
|
Path for animation frame buffer |
|
|
Path for saved animations |
|
|
Path for saved figures |
Objects within each environment reference their own environment’s parameters:
Note
When creating multiple environments with display=True, each environment opens its own visualization window. For training or batch simulations, use display=False to disable rendering and improve performance.