def build_world_step(self, env, floor, floor_size): default_name = 'static_cylinder' if self.make_static else 'moveable_cylinder' diameter = env._random_state.uniform(self.diameter[0], self.diameter[1]) height = env._random_state.uniform(self.height[0], self.height[1]) obj_size = (diameter, height, 0) successful_placement = True for i in range(self.n_objects): geom = Geom('cylinder', obj_size, name=f'{default_name}{i}', rgba=self.rgba) if self.make_static: geom.mark_static() if self.placement_fn is not None: _placement_fn = (self.placement_fn[i] if isinstance( self.placement_fn, list) else self.placement_fn) pos, _ = rejection_placement(env, _placement_fn, floor_size, diameter * np.ones(2)) if pos is not None: floor.append(geom, placement_xy=pos) else: successful_placement = False else: floor.append(geom) return successful_placement
def _get_new_sim(self, seed): world_params = WorldParams(size=(self.floorsize, self.floorsize, 2.5), num_substeps=self.n_substeps) builder = WorldBuilder(world_params, seed) floor = Floor() builder.append(floor) # Walls wallsize = 0.1 wall = Geom('box', (wallsize, self.floorsize, 0.5), name="wall1") wall.mark_static() floor.append(wall, placement_xy=(0, 0)) wall = Geom('box', (wallsize, self.floorsize, 0.5), name="wall2") wall.mark_static() floor.append(wall, placement_xy=(1, 0)) wall = Geom('box', (self.floorsize - wallsize*2, wallsize, 0.5), name="wall3") wall.mark_static() floor.append(wall, placement_xy=(1/2, 0)) wall = Geom('box', (self.floorsize - wallsize*2, wallsize, 0.5), name="wall4") wall.mark_static() floor.append(wall, placement_xy=(1/2, 1)) # Add agents obj = ObjFromXML("particle", name="agent0") floor.append(obj) obj.mark(f"object0") # Add food sites for i in range(self.n_food): floor.mark(f"food{i}", (.5, .5, 0.05), rgba=(0., 1., 0., 1.)) sim = builder.get_sim() # Cache constants for quicker lookup later self.food_ids = np.array([sim.model.site_name2id(f'food{i}') for i in range(self.n_food)]) return sim
def get_sim(seed): world_params = WorldParams(size=(10., 6., 4.5)) builder = WorldBuilder(world_params, seed) floor = Floor() builder.append(floor) obj = ObjFromXML("particle_hinge") floor.append(obj) floorsize = 4. # Walls wallsize = 0.1 wall = Geom('box', (wallsize, floorsize, 0.5), name="wall1") wall.mark_static() floor.append(wall, placement_xy=(0, 0)) wall = Geom('box', (wallsize, floorsize, 0.5), name="wall2") wall.mark_static() floor.append(wall, placement_xy=(1, 0)) wall = Geom('box', (floorsize - wallsize * 2, wallsize, 0.5), name="wall3") wall.mark_static() floor.append(wall, placement_xy=(1 / 2, 0)) wall = Geom('box', (floorsize - wallsize * 2, wallsize, 0.5), name="wall4") wall.mark_static() floor.append(wall, placement_xy=(1 / 2, 1)) # Add agents obj = ObjFromXML("particle", name="agent0") floor.append(obj) obj.mark("object") floor.mark("target", (.5, .5, 0.05)) return builder.get_sim()
def walls_to_mujoco(floor, floor_size, grid_size, walls, friction=None): ''' Take a list of walls in grid frame and add them to the floor in the worldgen frame. Args: floor (worldgen.Floor): floor floor_size (float): size of floor grid_size (int): size of placement grid walls (Wall list): list of walls friction (float): wall friction ''' wall_width = floor_size / grid_size / 2 grid_cell_length = floor_size / grid_size for i, wall in enumerate(walls): if wall.is_vertical: wall_length_grid = (wall.pt2[1] - wall.pt1[1] + 1) offset = np.array([-1, 1]) else: wall_length_grid = (wall.pt2[0] - wall.pt1[0] + 1) offset = np.array([1, -1]) # Convert to mujoco frame wall_length = wall_length_grid * grid_cell_length # Subtract 1 grid_cell_length such that walls originate and end in the center of a grid cell # Subtract 1 wall_width such that perpendicular walls do not intersect at the center of a # grid cell wall_length -= grid_cell_length + wall_width if wall.is_vertical: size = (wall_width, wall_length, wall.height) else: size = (wall_length, wall_width, wall.height) # Position of object should be in the middle of a grid cell (add 0.5) shifted by # the wall width such that corners don't overlap pos = np.array([wall.pt1[0] + 0.5, wall.pt1[1] + 0.5]) / grid_size pos += offset * wall_width / floor_size / 2 # Convert from mujoco to worldgen scale scale_x = (floor_size - size[0]) / floor_size scale_y = (floor_size - size[1]) / floor_size pos = pos / np.array([scale_x, scale_y]) geom = Geom('box', size, name=f"wall{i}") geom.mark_static() geom.add_transform(set_geom_attr_transform('rgba', wall.rgba)) geom.add_transform(set_geom_attr_transform('group', 1)) if friction is not None: geom.add_transform(set_geom_attr_transform('friction', friction)) floor.append(geom, placement_xy=pos)