Level-Based Foraging
Bases: Environment[State, MultiDiscreteArray, Observation]
An implementation of the Level-Based Foraging environment where agents need to cooperate to collect food and split the reward.
Original implementation: https://github.com/semitable/lb-foraging
-
observation
:Observation
agent_views
: Depending on theobserver
passed to__init__
, it can be aGridObserver
or aVectorObserver
.GridObserver
: Returns an agent's view with a shape of (num_agents, 3, 2 * fov + 1, 2 * fov +1).VectorObserver
: Returns an agent's view with a shape of (num_agents, 3 * (num_food + num_agents).
action_mask
: JAX array (bool) of shape (num_agents, 6) indicating for each agent which size actions (no-op, up, down, left, right, load) are allowed.step_count
: int32, the number of steps since the beginning of the episode.
-
action
: JAX array (int32) of shape (num_agents,). The valid actions for each agent are (0: noop, 1: up, 2: down, 3: left, 4: right, 5: load). -
reward
: JAX array (float) of shape (num_agents,) When one or more agents load food, the food level is rewarded to the agents, weighted by the level of each agent. The reward is then normalized so that, at the end, the sum of the rewards (if all food items have been picked up) is one. -
Episode Termination:
- All food items have been eaten.
- The number of steps is greater than the limit.
-
state
:State
agents
: Stacked Pytree ofAgent
objects of lengthnum_agents
.Agent
:id
: JAX array (int32) of shape ().position
: JAX array (int32) of shape (2,).level
: JAX array (int32) of shape ().loading
: JAX array (bool) of shape ().
food_items
: Stacked Pytree ofFood
objects of lengthnum_food
.Food
:id
: JAX array (int32) of shape ().position
: JAX array (int32) of shape (2,).level
: JAX array (int32) of shape ().eaten
: JAX array (bool) of shape ().
step_count
: JAX array (int32) of shape (), the number of steps since the beginning of the episode.key
: JAX array (uint) of shape (2,) JAX random generation key. Ignored since the environment is deterministic.
Example:
1 2 3 4 5 6 7 8 |
|
Initialization Args:
- generator
: A Generator
object that generates the initial state of the environment.
Defaults to a RandomGenerator
with the following parameters:
- grid_size
: 8
- fov
: 8 (full observation of the grid)
- num_agents
: 2
- num_food
: 2
- max_agent_level
: 2
- force_coop
: True
- time_limit
: The maximum number of steps in an episode. Defaults to 200.
- grid_observation
: If True
, the observer generates a grid observation (default is False
).
- normalize_reward
: If True
, normalizes the reward (default is True
).
- penalty
: The penalty value (default is 0.0).
- viewer
: Viewer to render the environment. Defaults to LevelBasedForagingViewer
.
Source code in jumanji/environments/routing/lbf/env.py
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 |
|
action_spec: specs.MultiDiscreteArray
cached
property
#
Returns the action spec for the Level Based Foraging environment.
Returns:
Type | Description |
---|---|
MultiDiscreteArray
|
specs.MultiDiscreteArray: Action spec for the environment with shape (num_agents,). |
discount_spec: specs.BoundedArray
cached
property
#
Describes the discount returned by the environment.
Returns:
Name | Type | Description |
---|---|---|
discount_spec |
BoundedArray
|
a |
observation_spec: specs.Spec[Observation]
cached
property
#
Specifications of the observation of the environment.
The spec's shape depends on the observer
passed to __init__
.
The GridObserver returns an agent's view with a shape of
(num_agents, 3, 2 * fov + 1, 2 * fov +1).
The VectorObserver returns an agent's view with a shape of
(num_agents, 3 * num_food + 3 * num_agents).
See a more detailed description of the observations in the docs
of GridObserver
and VectorObserver
.
Returns:
Type | Description |
---|---|
Spec[Observation]
|
specs.Spec[Observation]: Spec for the |
Spec[Observation]
|
action_mask, and step_count. |
reward_spec: specs.Array
cached
property
#
Returns the reward specification for the LevelBasedForaging
environment.
Since this is a multi-agent environment each agent gets its own reward.
Returns:
Type | Description |
---|---|
Array
|
specs.Array: Reward specification, of shape (num_agents,) for the environment. |
animate(states, interval=200, save_path=None)
#
Creates an animation from a sequence of states.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
states
|
Sequence[State]
|
Sequence of |
required |
interval
|
int
|
Delay between frames in milliseconds, default to 200. |
200
|
save_path
|
Optional[str]
|
The path where the animation file should be saved. |
None
|
Returns:
Type | Description |
---|---|
FuncAnimation
|
matplotlib.animation.FuncAnimation: Animation object that can be saved as a GIF, MP4, |
FuncAnimation
|
or rendered with HTML. |
Source code in jumanji/environments/routing/lbf/env.py
305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 |
|
close()
#
Perform any necessary cleanup.
Source code in jumanji/environments/routing/lbf/env.py
324 325 326 |
|
get_reward(food_items, adj_loading_agents_levels, eaten_this_step)
#
Returns a reward for all agents given all food items.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
food_items
|
Food
|
All the food items in the environment. |
required |
adj_loading_agents_levels
|
Array
|
The level of all agents adjacent to all foods. |
required |
eaten_this_step
|
Array
|
Whether the food was eaten or not (this step). |
required |
Source code in jumanji/environments/routing/lbf/env.py
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 |
|
render(state)
#
Renders the current state of the LevelBasedForaging
environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
The current environment state to be rendered. |
required |
Returns:
Type | Description |
---|---|
Optional[NDArray]
|
Optional[NDArray]: Rendered environment state. |
Source code in jumanji/environments/routing/lbf/env.py
294 295 296 297 298 299 300 301 302 303 |
|
reset(key)
#
Resets the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
key
|
PRNGKey
|
Used to randomly generate the new |
required |
Returns:
Type | Description |
---|---|
State
|
Tuple[State, TimeStep]: |
TimeStep
|
of the environment and |
Source code in jumanji/environments/routing/lbf/env.py
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
|
step(state, actions)
#
Simulate one step of the environment.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
state
|
State
|
State containing the dynamics of the environment. |
required |
actions
|
Array
|
Array containing the actions to take for each agent. |
required |
Returns:
Type | Description |
---|---|
State
|
Tuple[State, TimeStep]: |
TimeStep
|
|
Source code in jumanji/environments/routing/lbf/env.py
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 |
|