523: def make_9x9_layout(rng, layout_grid, rotate=False, num_base_walls=None):
524:     base_layout = jnp.ones((9, 9))
525:     def sub_in_default_cramped_room(target_layout, base_layout, num_rotations, to_flip, flip_axis):
526:         layout_height, layout_width = target_layout.shape
527:         def flip_zero(x):
528:             return jnp.flip(x, axis=0)
529:         def flip_one(x):
530:             return jnp.flip(x, axis=1)
531:         flip_func = lambda x, y: jnp.where(y==0, flip_zero(x), flip_one(x))
532:         target_layout = jnp.where(to_flip==1, flip_func(target_layout, flip_axis), target_layout)
533:         updated_layout = base_layout.at[:layout_height, :layout_width].set(target_layout)
534:         frozen_dict = layout_array_to_dict(updated_layout, num_base_walls=num_base_walls)
535:         return updated_layout, frozen_dict
536:     def sub_in_90_degree_rotation(target_layout, base_layout, num_rotations, to_flip, flip_axis):
537:         def get_effect(target_layout, base_layout, num_rotations, to_flip, flip_axis):
538:             rot_layout = jnp.rot90(target_layout)
539:             def flip_zero(x):
540:                 return jnp.flip(x, axis=0)
541:             def flip_one(x):
542:                 return jnp.flip(x, axis=1)
543:             flip_func = lambda x, y: jnp.where(y==0, flip_zero(x), flip_one(x))
544:             rot_layout = jnp.where(to_flip==1, flip_func(rot_layout, flip_axis), rot_layout)
545:             layout_height, layout_width = rot_layout.shape
546:             updated_layout = base_layout.at[:layout_height, :layout_width].set(rot_layout)
547:             frozen_dict = layout_array_to_dict(updated_layout, num_base_walls=num_base_walls)
548:             return updated_layout, frozen_dict
549:         updated_layout, frozen_dict = jax.lax.cond(num_rotations == 1, get_effect, sub_in_180_degree_rotation, target_layout, base_layout, num_rotations, to_flip, flip_axis)
550:         return updated_layout, frozen_dict
551:     def sub_in_180_degree_rotation(target_layout, base_layout, num_rotations, to_flip, flip_axis):
552:         def get_effect(target_layout, base_layout, num_rotations, to_flip, flip_axis):
553:             rot_layout = jnp.rot90(target_layout, k=2)
554:             def flip_zero(x):
555:                 return jnp.flip(x, axis=0)
556:             def flip_one(x):
557:                 return jnp.flip(x, axis=1)
558:             flip_func = lambda x, y: jnp.where(y==0, flip_zero(x), flip_one(x))
559:             rot_layout = jnp.where(to_flip==1, flip_func(rot_layout, flip_axis), rot_layout)
560:             layout_height, layout_width = rot_layout.shape
561:             updated_layout = base_layout.at[:layout_height, :layout_width].set(rot_layout)
562:             frozen_dict = layout_array_to_dict(updated_layout, num_base_walls=num_base_walls)
563:             return updated_layout, frozen_dict
564:         updated_layout, frozen_dict = jax.lax.cond(num_rotations == 2, get_effect, sub_in_270_degree_rotation, target_layout, base_layout, num_rotations, to_flip, flip_axis)
565:         return updated_layout, frozen_dict
566:     def sub_in_270_degree_rotation(target_layout, base_layout, num_rotations, to_flip, flip_axis):
567:         rot_layout = jnp.rot90(target_layout, k=3)
568:         def flip_zero(x):
569:             return jnp.flip(x, axis=0)
570:         def flip_one(x):
571:             return jnp.flip(x, axis=1)
572:         flip_func = lambda x, y: jnp.where(y==0, flip_zero(x), flip_one(x))
573:         rot_layout = jnp.where(to_flip==1, flip_func(rot_layout, flip_axis), rot_layout)
574:         layout_height, layout_width = rot_layout.shape
575:         updated_layout = base_layout.at[:layout_height, :layout_width].set(rot_layout)
576:         frozen_dict = layout_array_to_dict(updated_layout, num_base_walls=num_base_walls)
577:         return updated_layout, frozen_dict
578: 
579:     num_rotations = jax.random.randint(rng, (), 0, 4)
580:     num_rotations = jnp.where(rotate, num_rotations, 0)
581:     rng, rng_sub = jax.random.split(rng)
582:     to_flip = jax.random.randint(rng_sub, (), 0, 2)
583:     to_flip = jnp.where(rotate, to_flip, 0)
584:     rng, rng_sub = jax.random.split(rng)
585:     flip_axis = jax.random.randint(rng_sub, (), 0, 2)
586:     flip_axis = jnp.where(rotate, flip_axis, 0)
587: 
588:     updated_layout, layout_dict = jax.lax.cond(num_rotations == 0, sub_in_default_cramped_room, sub_in_90_degree_rotation, layout_grid, base_layout, num_rotations, to_flip, flip_axis)
589:     return layout_dict
590: 

815: def make_forced_coord_9x9(rng, ik=False, num_default_walls=67):
816:     forced_coord_array = jnp.array([
817:         [1,1,1,6,1],
818:         [5,0,1,0,6],
819:         [5,2,1,2,1],
820:         [4,0,1,0,1],
821:         [4,1,1,3,3],
822:     ])
823: 
824:     def default_forced_coord(rng, layout=forced_coord_array):
825:         return make_9x9_layout(rng, layout, rotate=False, num_base_walls=num_default_walls)
826: 
827:     def ik_forced_coord(rng, layout=forced_coord_array):
828:         height, width = layout.shape
829:         all_walls = jnp.array([0,1,2,3,4,5,7,9,10,12,14,15,17,19,20,21,22,23,24])
830:         need_one = jnp.array([1,5,10,15,21,17,12,7,3,9,14,19,23])
831:         # get a random permutation of need_one, and take the first 4
832:         need_one_permutation = jax.random.permutation(rng, need_one)[:4]
833:         rng, rng_sub = jax.random.split(rng)
834: 
835:         # Create mask where 1 indicates wall not in need_one_permutation
836:         wall_mask = jnp.ones(len(all_walls))
837:         sorted_all_walls = jnp.sort(all_walls)
838:         wall_mask = wall_mask.at[jnp.searchsorted(sorted_all_walls, need_one_permutation)].set(0)
839:         wall_probs = wall_mask.astype(float) / jnp.sum(wall_mask)
840:         # sample 4 walls from sorted_all_walls
841:         additional = jax.random.choice(rng_sub, sorted_all_walls, shape=(4,), replace=False, p=wall_probs)
842:         rng, rng_sub = jax.random.split(rng)
843: 
844:         # sample agent positions
845:         valid_agent_0_positions = jnp.array([6,11,16])
846:         valid_agent_1_positions = jnp.array([8,13,18])
847:         agent_0_idx = jax.random.choice(rng_sub, valid_agent_0_positions, shape=(1,), replace=False)
848:         rng, rng_sub = jax.random.split(rng)
849:         agent_1_idx = jax.random.choice(rng_sub, valid_agent_1_positions, shape=(1,), replace=False)
850:         agent_idx = jnp.concatenate([agent_0_idx, agent_1_idx])
851:         
852:         item_indices = jnp.concatenate([need_one_permutation, additional, agent_idx])
853:         # convert to 2d coordinates
854:         x_coords, y_coords = jnp.unravel_index(item_indices, (height, width))
855:         stacked_coords = jnp.stack([x_coords, y_coords], axis=1)
856: 
857:         plate_idx = jnp.array([stacked_coords[0], stacked_coords[4]])
858:         onion_idx = jnp.array([stacked_coords[1], stacked_coords[5]])
859:         pot_idx = jnp.array([stacked_coords[2], stacked_coords[6]])
860:         goal_idx = jnp.array([stacked_coords[3], stacked_coords[7]])
861:         agent_idx = jnp.array([stacked_coords[8], stacked_coords[9]])
862: 
863:         def update_map(layout, item_indices, value):
864:             def scan_body(carry, idx):
865:                 layout, item_indices, value = carry
866:                 layout = layout.at[item_indices[idx][0], item_indices[idx][1]].set(value)
867:                 return (layout, item_indices, value), idx
868:             carry, _ = jax.lax.scan(scan_body, (layout, item_indices, value), jnp.arange(len(item_indices)))
869:             return carry[0]
870:         modified_layout = jnp.zeros_like(layout)
871:         # create the basic outline of the ring
872:         wall_coords_x, wall_coords_y = jnp.unravel_index(all_walls, (height, width))
873:         wall_coords = jnp.stack([wall_coords_x, wall_coords_y], axis=1)
874:         modified_layout = update_map(modified_layout, wall_coords, 1)
875: 
876:         modified_layout = update_map(modified_layout, plate_idx, 4)
877:         modified_layout = update_map(modified_layout, onion_idx, 5)
878:         modified_layout = update_map(modified_layout, pot_idx, 6)
879:         modified_layout = update_map(modified_layout, goal_idx, 3)
880:         modified_layout = update_map(modified_layout, agent_idx, 2)
881: 
882:         rng, rng_sub = jax.random.split(rng)
883:         return make_9x9_layout(rng, modified_layout, rotate=True, num_base_walls=num_default_walls)
884: 
885:     return jax.lax.cond(ik, ik_forced_coord, default_forced_coord, rng, forced_coord_array)