32: def initialize_environment(config):
33:     layout_name = config["ENV_KWARGS"]["layout"]
34:     config['layout_name'] = layout_name
35:     config["ENV_KWARGS"]["layout"] = overcooked_layouts[layout_name]
36:     env = jaxmarl.make(config["ENV_NAME"], **config["ENV_KWARGS"])
37: 
38:     if config["ENV_NAME"] == "overcooked":
39:         def reset_env(key):
40:             def reset_sub_dict(key, fn):
41:                 key, subkey = jax.random.split(key)
42:                 sampled_layout_dict = fn(subkey, ik=True)
43:                 temp_o, temp_s = env.custom_reset(key, layout=sampled_layout_dict, random_reset=False, shuffle_inv_and_pot=False)
44:                 key, subkey = jax.random.split(key)
45:                 return (temp_o, temp_s), key
46:                 
47:             asymm_reset, key = reset_sub_dict(key, make_asymm_advantages_9x9)
48:             coord_ring_reset, key = reset_sub_dict(key, make_coord_ring_9x9)
49:             counter_circuit_reset, key = reset_sub_dict(key, make_counter_circuit_9x9)
50:             forced_coord_reset, key = reset_sub_dict(key, make_forced_coord_9x9)
51:             cramped_room_reset, key = reset_sub_dict(key, make_cramped_room_9x9)
52:             layout_resets = [asymm_reset, coord_ring_reset, counter_circuit_reset, forced_coord_reset, cramped_room_reset]
53:             # stack all layouts
54:             stacked_layout_reset = jax.tree_map(lambda *x: jnp.stack(x), *layout_resets)
55:             # sample an index from 0 to 4
56:             index = jax.random.randint(key, (), minval=0, maxval=5)
57:             sampled_reset = jax.tree_map(lambda x: x[index], stacked_layout_reset)
58:             return sampled_reset
59:         @scan_tqdm(100)
60:         def gen_held_out(runner_state, unused):
61:             (i,) = runner_state
62:             _, ho_state = reset_env(jax.random.key(i))
63:             res = (ho_state.goal_pos, ho_state.wall_map, ho_state.pot_pos)
64:             carry = (i+1,)
65:             return carry, res
66:         carry, res = jax.lax.scan(gen_held_out, (0,), jnp.arange(100), 100)
67:         ho_goal, ho_wall, ho_pot = [], [], []
68:         for layout_name, layout_dict in overcooked_layouts.items():  # add hand crafted ones to heldout set
69:             if "9" in layout_name:
70:                 _, ho_state = env.custom_reset(jax.random.PRNGKey(0), random_reset=False, shuffle_inv_and_pot=False, layout=layout_dict)
71:                 ho_goal.append(ho_state.goal_pos)
72:                 ho_wall.append(ho_state.wall_map)
73:                 ho_pot.append(ho_state.pot_pos)
74:         ho_goal = jnp.stack(ho_goal, axis=0)
75:         ho_wall = jnp.stack(ho_wall, axis=0)
76:         ho_pot = jnp.stack(ho_pot, axis=0)
77:         ho_goal = jnp.concatenate([res[0], ho_goal], axis=0)
78:         ho_wall = jnp.concatenate([res[1], ho_wall], axis=0)
79:         ho_pot = jnp.concatenate([res[2], ho_pot], axis=0)
80:         env.held_out_goal, env.held_out_wall, env.held_out_pot = (ho_goal, ho_wall, ho_pot)
81:     elif config["ENV_NAME"] == "ToyCoop":
82:         # Generate 100 held-out states for ToyCoop
83:         @scan_tqdm(100)
84:         def gen_held_out_toycoop(runner_state, unused):
85:             (i,) = runner_state
86:             key = jax.random.key(i)
87:             state = env.custom_reset_fn(key, random_reset=True)
88:             res = (state.agent_pos, state.goal_pos, state.other_goal_pos)
89:             carry = (i+1,)
90:             return carry, res
91:         
92:         carry, res = jax.lax.scan(gen_held_out_toycoop, (0,), jnp.arange(100), 100)
93:         ho_agent_pos, ho_goal_pos, ho_other_goal_pos = res
94:         
95:         # Set the held-out states in the environment
96:         env.held_out_agent_pos = ho_agent_pos
97:         env.held_out_goal_pos = ho_goal_pos
98:         env.held_out_other_goal_pos = ho_other_goal_pos
99:     config["obs_dim"] = env.observation_space(env.agents[0]).shape
100:     return env