193:     def reset(self,
194:               key: chex.PRNGKey, params={'random_reset_fn': 'reset_all'}):
195: 
196:         jitted_reset = lambda k: self.custom_reset(k, layout=self.layout, random_reset=False, shuffle_inv_and_pot=self.shuffle_inv_and_pot)
197:         jitted_reset = jax.jit(jitted_reset)
198: 
199:         @jax.jit
200:         def random_og_5(key):
201:             def reset_sub_dict(key, fn):
202:                 key, subkey = jax.random.split(key)
203:                 sampled_layout_dict = fn(subkey, ik=True)
204:                 temp_o, temp_s = self.custom_reset(key, layout=sampled_layout_dict, random_reset=False, shuffle_inv_and_pot=self.shuffle_inv_and_pot)
205:                 key, subkey = jax.random.split(key)
206:                 return (temp_o, temp_s), key
207:             
208:             def sampled_0(key, sampled_num):
209:                 return reset_sub_dict(key, make_asymm_advantages_9x9)
210:             def sampled_1(key, sampled_num):
211:                 def reset_coord_ring(key, sampled_num):
212:                     return reset_sub_dict(key, make_coord_ring_9x9)
213:                 return jax.lax.cond(sampled_num==1, reset_coord_ring, sampled_2, key, sampled_num)
214:             def sampled_2(key, sampled_num):
215:                 def reset_counter_circuit(key, sampled_num):
216:                     return reset_sub_dict(key, make_counter_circuit_9x9)
217:                 return jax.lax.cond(sampled_num==2, reset_counter_circuit, sampled_3, key, sampled_num)
218:             def sampled_3(key, sampled_num):
219:                 def reset_forced_coord(key, sampled_num):
220:                     return reset_sub_dict(key, make_forced_coord_9x9)
221:                 return jax.lax.cond(sampled_num==3, reset_forced_coord, sampled_4, key, sampled_num)
222:             def sampled_4(key, sampled_num):
223:                 return reset_sub_dict(key, make_cramped_room_9x9)
224: 
225:             # sample an index from 0 to 4
226:             index = jax.random.randint(key, (), minval=0, maxval=5)
227:             sampled_reset, key = jax.lax.cond(index == 0, sampled_0, sampled_1, key, index)
228:             return sampled_reset
229: 
230:         @jax.jit
231:         def random_counter_circuit(key):
232:             def reset_sub_dict(key, fn):
233:                 key, subkey = jax.random.split(key)
234:                 sampled_layout_dict = fn(subkey, ik=True)
235:                 temp_o, temp_s = self.custom_reset(key, layout=sampled_layout_dict, random_reset=False, shuffle_inv_and_pot=self.shuffle_inv_and_pot)
236:                 key, subkey = jax.random.split(key)
237:                 return (temp_o, temp_s), key
238:             
239:             counter_circuit_reset, key = reset_sub_dict(key, make_counter_circuit_9x9)
240:             return counter_circuit_reset
241:         
242:         
243:         def check_match(state_):  # says whether or not the observation is in the held out set
244:             goal_match = lambda g_pos: jnp.all(g_pos == state_.goal_pos)
245:             pot_match = lambda p_pos: jnp.all(p_pos == state_.pot_pos)
246:             wall_match = lambda w_map: jnp.all(w_map == state_.wall_map)
247:             some_goal_match = jax.vmap(goal_match)(self.held_out_goal)
248:             some_pot_match = jax.vmap(pot_match)(self.held_out_pot)
249:             some_wall_match = jax.vmap(wall_match)(self.held_out_wall)
250:             temp = jnp.stack([some_goal_match, some_pot_match, some_wall_match], axis=0) # 3 x 100
251:             some_match = jnp.all(temp, axis=0).any()
252:             return some_match
253: 
254:         random_reset_fn = lambda k: jax.lax.cond(params['random_reset_fn'] == 'reset_all', random_og_5, random_counter_circuit, k)
255:         obs, state = jax.lax.cond(self.random_reset, random_reset_fn, jitted_reset, key)
256:         key = jax.random.split(key)[0]
257:         (obs, state) = jax.lax.cond(jnp.logical_and(check_match(state), self.check_held_out), random_og_5, lambda k: (obs, state), key)
258:         
259:         return lax.stop_gradient(obs), lax.stop_gradient(state)

451:         key, subkey = jax.random.split(key)
452:         # Pot status is determined by a number between 0 (inclusive) and 24 (exclusive)
453:         # 23 corresponds to an empty pot (default)
454:         pot_status = jax.random.randint(subkey, (pot_idx.shape[0],), 0, 24)
455:         pot_status = pot_status * shuffle_inv_and_pot + (1-shuffle_inv_and_pot) * jnp.ones((pot_idx.shape[0])) * 23

500:         # agent inventory (empty by default, can be randomized)
501:         key, subkey = jax.random.split(key)
502:         possible_items = jnp.array([OBJECT_TO_INDEX['empty'], OBJECT_TO_INDEX['onion'],
503:                           OBJECT_TO_INDEX['plate'], OBJECT_TO_INDEX['dish']])
504:         random_agent_inv = jax.random.choice(subkey, possible_items, shape=(num_agents,), replace=True)
505:         
506: 
507:         agent_inv = shuffle_inv_and_pot * random_agent_inv + \
508:                     (1-shuffle_inv_and_pot) * jnp.array([OBJECT_TO_INDEX['empty'], OBJECT_TO_INDEX['empty']])

792:     def process_interact(
793:             self,
794:             maze_map: chex.Array,
795:             wall_map: chex.Array,
796:             fwd_pos_all: chex.Array,
797:             inventory_all: chex.Array,
798:             player_idx: int):
799:         """Assume agent took interact actions. Result depends on what agent is facing and what it is holding."""
800:         
801:         fwd_pos = fwd_pos_all[player_idx]
802:         inventory = inventory_all[player_idx]
803: 
804:         shaped_reward = 0.
805: 
806:         height = self.obs_shape[1]
807:         padding = (maze_map.shape[0] - height) // 2
808: 
809:         # Get object in front of agent (on the "table")
810:         maze_object_on_table = maze_map.at[padding + fwd_pos[1], padding + fwd_pos[0]].get()
811:         object_on_table = maze_object_on_table[0]  # Simple index
812: 
813:         # Booleans depending on what the object is
814:         object_is_pile = jnp.logical_or(object_on_table == OBJECT_TO_INDEX["plate_pile"], object_on_table == OBJECT_TO_INDEX["onion_pile"])
815:         object_is_pot = jnp.array(object_on_table == OBJECT_TO_INDEX["pot"])
816:         object_is_goal = jnp.array(object_on_table == OBJECT_TO_INDEX["goal"])
817:         object_is_agent = jnp.array(object_on_table == OBJECT_TO_INDEX["agent"])
818:         object_is_pickable = jnp.logical_or(
819:             jnp.logical_or(object_on_table == OBJECT_TO_INDEX["plate"], object_on_table == OBJECT_TO_INDEX["onion"]),
820:             object_on_table == OBJECT_TO_INDEX["dish"]
821:         )
822:         # Whether the object in front is counter space that the agent can drop on.
823:         is_table = jnp.logical_and(wall_map.at[fwd_pos[1], fwd_pos[0]].get(), ~object_is_pot)
824: 
825:         table_is_empty = jnp.logical_or(object_on_table == OBJECT_TO_INDEX["wall"], object_on_table == OBJECT_TO_INDEX["empty"])
826: 
827:         # Pot status (used if the object is a pot)
828:         pot_status = maze_object_on_table[-1]
829: 
830:         # Get inventory object, and related booleans
831:         inv_is_empty = jnp.array(inventory == OBJECT_TO_INDEX["empty"])
832:         object_in_inv = inventory
833:         holding_onion = jnp.array(object_in_inv == OBJECT_TO_INDEX["onion"])
834:         holding_plate = jnp.array(object_in_inv == OBJECT_TO_INDEX["plate"])
835:         holding_dish = jnp.array(object_in_inv == OBJECT_TO_INDEX["dish"])
836: 
837:         # Interactions with pot. 3 cases: add onion if missing, collect soup if ready, do nothing otherwise
838:         case_1 = (pot_status > POT_FULL_STATUS) * holding_onion * object_is_pot
839:         case_2 = (pot_status == POT_READY_STATUS) * holding_plate * object_is_pot
840:         case_3 = (pot_status > POT_READY_STATUS) * (pot_status <= POT_FULL_STATUS) * object_is_pot
841:         else_case = ~case_1 * ~case_2 * ~case_3
842: 
843:         # give reward for placing onion in pot, and for picking up soup
844:         shaped_reward += case_1 * BASE_REW_SHAPING_PARAMS["PLACEMENT_IN_POT_REW"]
845:         shaped_reward += case_2 * BASE_REW_SHAPING_PARAMS["SOUP_PICKUP_REWARD"]
846: 
847:         # Update pot status and object in inventory
848:         new_pot_status = \
849:             case_1 * (pot_status - 1) \
850:             + case_2 * POT_EMPTY_STATUS \
851:             + case_3 * pot_status \
852:             + else_case * pot_status
853:         new_object_in_inv = \
854:             case_1 * OBJECT_TO_INDEX["empty"] \
855:             + case_2 * OBJECT_TO_INDEX["dish"] \
856:             + case_3 * object_in_inv \
857:             + else_case * object_in_inv
858: 
859:         # Interactions with onion/plate piles and objects on counter
860:         # Pickup if: table, not empty, room in inv & object is not something unpickable (e.g. pot or goal)
861:         successful_pickup = is_table * ~table_is_empty * inv_is_empty * jnp.logical_or(object_is_pile, object_is_pickable)
862:         successful_drop = is_table * table_is_empty * ~inv_is_empty
863:         successful_delivery = is_table * object_is_goal * holding_dish
864:         no_effect = jnp.logical_and(jnp.logical_and(~successful_pickup, ~successful_drop), ~successful_delivery)
865: 
866:         # Update object on table
867:         new_object_on_table = \
868:             no_effect * object_on_table \
869:             + successful_delivery * object_on_table \
870:             + successful_pickup * object_is_pile * object_on_table \
871:             + successful_pickup * object_is_pickable * OBJECT_TO_INDEX["wall"] \
872:             + successful_drop * object_in_inv
873: 
874:         # Update object in inventory
875:         new_object_in_inv = \
876:             no_effect * new_object_in_inv \
877:             + successful_delivery * OBJECT_TO_INDEX["empty"] \
878:             + successful_pickup * object_is_pickable * object_on_table \
879:             + successful_pickup * (object_on_table == OBJECT_TO_INDEX["plate_pile"]) * OBJECT_TO_INDEX["plate"] \
880:             + successful_pickup * (object_on_table == OBJECT_TO_INDEX["onion_pile"]) * OBJECT_TO_INDEX["onion"] \
881:             + successful_drop * OBJECT_TO_INDEX["empty"]
882: 
883:         # Apply inventory update
884:         has_picked_up_plate = successful_pickup*(new_object_in_inv == OBJECT_TO_INDEX["plate"])
885:         
886:         # number of plates in player hands < number ready/cooking/partially full pot
887:         num_plates_in_inv = jnp.sum(inventory == OBJECT_TO_INDEX["plate"])
888:         pot_loc_layer = jnp.array(maze_map[padding:-padding, padding:-padding, 0] == OBJECT_TO_INDEX["pot"], dtype=jnp.uint8)
889:         padded_map = maze_map[padding:-padding, padding:-padding, 2] 
890:         num_notempty_pots = jnp.sum((padded_map!=POT_EMPTY_STATUS)* pot_loc_layer)
891:         is_dish_picku_useful = num_plates_in_inv < num_notempty_pots
892: 
893:         plate_loc_layer = jnp.array(maze_map == OBJECT_TO_INDEX["plate"], dtype=jnp.uint8)
894:         no_plates_on_counters = jnp.sum(plate_loc_layer) == 0
895:         
896:         shaped_reward += no_plates_on_counters*has_picked_up_plate*is_dish_picku_useful*BASE_REW_SHAPING_PARAMS["PLATE_PICKUP_REWARD"]
897: 
898:         inventory = new_object_in_inv
899:         
900:         # Apply changes to maze
901:         new_maze_object_on_table = \
902:             object_is_pot * OBJECT_INDEX_TO_VEC[new_object_on_table].at[-1].set(new_pot_status) \
903:             + ~object_is_pot * ~object_is_agent * OBJECT_INDEX_TO_VEC[new_object_on_table] \
904:             + object_is_agent * maze_object_on_table
905: 
906:         maze_map = maze_map.at[padding + fwd_pos[1], padding + fwd_pos[0], :].set(new_maze_object_on_table)
907: 
908:         # Reward of 20 for a soup delivery
909:         reward = jnp.array(successful_delivery, dtype=float)*DELIVERY_REWARD
910:         return maze_map, inventory, reward, shaped_reward