Documentation Index
Fetch the complete documentation index at: https://mintlify.com/salesforce/ai-economist/llms.txt
Use this file to discover all available pages before exploring further.
FoundationEnvWrapper wraps a Foundation BaseEnvironment instance and decides whether environment reset and step operations run on the CPU or the GPU.
- CPU mode (
use_cuda=False): everyreset()andstep()call runs on the host using the standard Python environment. - GPU mode (
use_cuda=True, requires WarpDrive): the firstreset()runs on the CPU, copies all relevant data to the GPU, and all subsequent steps execute as CUDA kernels.
observation_space and action_space attributes to the wrapped environment, making it compatible with RLlib and other Gym-style training frameworks.
recursive_obs_dict_to_spaces_dict()
gym.spaces.Dict of observation spaces. Used internally by FoundationEnvWrapper.__init__ to populate env.observation_space.
Each leaf value is converted according to these rules:
| Python type | Gym space |
|---|---|
np.ndarray | Box(low=-BIG_NUMBER, high=BIG_NUMBER, shape=v.shape, dtype=v.dtype) |
list | Converted to np.ndarray, then treated as above |
int / float / np.integer / np.floating | Wrapped in a 1-D np.array, then treated as above |
dict | Recurses into a nested spaces.Dict |
BIG_NUMBER bound is 1e20, halved iteratively until the Box low/high values are numerically valid.
Parameters
A dictionary of observations keyed by agent index, as returned by a multi-agent Foundation environment’s
reset() call.Returns
A
gym.spaces.Dict whose structure mirrors the input observation dictionary, with every leaf replaced by an appropriate Box space.FoundationEnvWrapper
Constructor
env_obj or the triple (env_name, env_config, env_registrar).
Parameters
A fully constructed Foundation environment instance. When provided,
env_name, env_config, and env_registrar are ignored.Name of an environment registered on the WarpDrive environment registrar. Required when
env_obj is not provided.Keyword arguments passed to the environment class retrieved from
env_registrar. Required when env_obj is not provided.Number of parallel environments to run simultaneously. Only relevant when
use_cuda=True.When
True, runs environment steps on the GPU via WarpDrive CUDA kernels. Requires a CUDA-capable GPU and the rl-warp-drive package.WarpDrive
EnvironmentRegistrar object that provides customized environment info (such as CUDA source paths) needed for the GPU build. Required when env_obj is not provided or when use_cuda=True.Optional multiprocessing
Event used to synchronise the CUDA build when multiple worker processes are launched simultaneously.Integer identifier of the process running WarpDrive. Used to avoid build collisions in multi-process setups.
What the constructor does
Attach the environment
Stores the environment as
self.env. If env_obj is provided it is used directly; otherwise the environment is instantiated from the registrar.Build observation space
Calls
obs_at_reset() (which calls env.reset() once on the CPU) and passes the result to recursive_obs_dict_to_spaces_dict(). The resulting gym.spaces.Dict is stored on env.observation_space.Build action space
Iterates over mobile agents and the planner. Each agent whose
multi_action_mode is True receives a MultiDiscrete space; otherwise a Discrete space. All spaces are stored in env.action_space keyed by agent index string (e.g. "0", "1", …, "p").Properties
observation_space
gym.spaces.Dict keyed by agent index strings (e.g. "0", "1", …, "p"). Built from the initial observation returned by the first reset() call. Attached to wrapper.env during construction.
action_space
Discrete(n)whenagent.multi_action_modeisFalseMultiDiscrete([n1, n2, ...])whenagent.multi_action_modeisTrue
"p". All spaces have dtype = np.int32.
Methods
reset()
reset_all_envs(). Use this in CPU mode or when writing code that is agnostic to the execution backend.
Returns
Initial observation dictionary, identical in structure to what
env.reset() returns (after reformatting collated agent observations).step()
step_all_envs(). In CPU mode, requires actions. In GPU mode, steps happen on the CUDA device and None is returned.
Parameters
Dictionary
{agent_idx: action} of agent actions. Required in CPU mode. Ignored in GPU mode (actions are managed on-device).Returns
In CPU mode:
(obs, rew, done, info) tuple with the same structure as BaseEnvironment.step().In GPU mode: None — arrays are updated in place on the device.reset_all_envs()
- If
reset_on_hostisTrue: callsobs_at_reset()on the CPU. In GPU mode this also copies all data tensors to the device (expanding along the environment dimension fornum_envs > 1) and then setsreset_on_host = Falseso all future resets run on the GPU. - If
reset_on_hostisFalse(GPU mode only): invokesCUDAEnvironmentResetwithmode="force_reset"and returns an empty dict.
Returns
Initial observation dictionary on the first (CPU) reset. An empty dictionary
{} for subsequent GPU resets.reset_only_done_envs()
True, leaving others running. Requires use_cuda=True and a completed first reset (reset_on_host=False).
Calling this method in CPU mode or before the first GPU reset raises an
AssertionError.Returns
Always returns an empty dictionary
{} — done-env resets modify device arrays in place.step_all_envs()
- CPU mode: requires
actions. Callsenv.step(actions), reformats collated observations and rewards, and returns(obs, rew, done, info). - GPU mode: steps each component via
component.component_step(), callsenv.scenario_step(), then callsenv.generate_rewards(). ReturnsNone.
Parameters
Action dictionary. Must be provided in CPU mode; ignored in GPU mode.
Returns
(obs, rew, done, info) in CPU mode; None in GPU mode.obs_at_reset()
env.reset() on the CPU and applies _reformat_obs(). Used internally during construction to build the observation space and during the first GPU reset to populate device data.
Returns
Initial observation dictionary after reformatting.
GPU mode
GPU-accelerated simulation requires therl-warp-drive package and a CUDA-capable GPU.
GPUtil. If no GPUs are found, all simulation falls back to CPU automatically.
Internal helper methods
_reformat_obs()
"a" present), expands each agent’s slice into a separate str(agent_id) key and removes the "a" key. Returns the reformatted dictionary unchanged otherwise.
_reformat_rew()
"a" present), expands each agent’s scalar into a separate str(agent_id) key and removes the "a" key. Returns the dictionary unchanged otherwise.