def create_arena_with_red_wall(t=DEFAULT_TIME_LIMIT): arena = Arena(t=t, items=[]) orientation = np.random.choice(['horizontal', 'vertical']) goal_size = float(np.random.choice([1, 2, 3])) _add_wall_to_arena(arena, orientation='horizontal', position=np.random.randint(10, 30), goal_size=goal_size) if orientation == 'horizontal': arena.items.append( Item(name='GoodGoalMulti', sizes=[Vector3(1, 1, 1)] * 2, positions=[ Vector3(-1, 0, float(np.random.randint(1, 7))), Vector3(-1, 0, 40 - float(np.random.randint(1, 7))) ])) else: arena.items.append( Item(name='GoodGoalMulti', sizes=[Vector3(1, 1, 1)] * 2, positions=[ Vector3(float(np.random.randint(1, 7)), 0, -1), Vector3(40 - float(np.random.randint(1, 7)), 0, -1) ])) return arena
def build_y_mazes(number_of_conf=2): """ Build config files with Y mazes. Even numbers have the good goal on the left and odd numbers on the right. +----------+ |\ \ / /| | \ \/ / | | | | | | | | | +----------+ wall[0] : lvw : left vertical wall wall[1] : rvw : right vertical wall wall[2] : ldwo : left vertical wall outside wall[3] : rdwo : right diagonal wall outside wall[4] : ldwi : left diagonal wall inside wall[5] : rdwi : right diagonal wall inside """ min_x_ref = 10 max_x_ref = 10 min_z_ref = 10 max_z_ref = 10 colors = [RGB(153, 153, 153) for i in range(6)] for i in range(number_of_conf): x_ref = random.randint(min_x_ref, max_x_ref) z_ref = random.randint(min_x_ref, max_z_ref) t_ref = 40 - 2 * x_ref thickness = 0.1 height = 5 walls_pos = [None for i in range(6)] walls_pos[0] = Vector3(x_ref, 0, z_ref) walls_pos[1] = Vector3(x_ref + t_ref, 0, z_ref) walls_pos[2] = Vector3(x_ref / 2, 0, z_ref + 20) walls_pos[3] = Vector3((40 + x_ref + t_ref) / 2, 0, z_ref + 20) walls_pos[4] = Vector3((x_ref / 2) + 10, 0, z_ref + 20) walls_pos[5] = Vector3((x_ref + t_ref) / 2 + 10, 0, z_ref + 20) walls_sizes = [None for i in range(6)] walls_sizes[0] = Vector3(thickness, height, z_ref * 2) walls_sizes[1] = Vector3(thickness, height, z_ref * 2) walls_sizes[2] = walls_sizes[3] = walls_sizes[4] = walls_sizes[5] =\ Vector3(thickness, height, math.sqrt(x_ref ** 2 + (40 - 2 * z_ref) ** 2) - 4 * thickness) walls_rot = [0 for i in range(6)] angle = math.degrees(math.atan((x_ref / 2) / (20 - z_ref))) walls_rot[2] = walls_rot[4] = 360 - angle walls_rot[3] = walls_rot[5] = angle left_food_pos = Vector3((x_ref + 10) / 2, 0, z_ref + 20) right_food_pos = Vector3((30 + t_ref + x_ref) / 2, 0, z_ref + 20) good_goal_pos = left_food_pos if i % 2 == 0 else right_food_pos bad_goal_pos = right_food_pos if i % 2 == 0 else left_food_pos items = [ Item(name="Wall", positions=walls_pos, rotations=walls_rot, sizes=walls_sizes, colors=colors), Item(name="GoodGoal", positions=[good_goal_pos]), Item(name="BadGoal", positions=[bad_goal_pos]), Item(name="Agent", positions=[Vector3(20, 0, 5)]), ] save_arena(items, "y_maze_{}".format(i))
def create_arena_with_small_goal(t): arena = Arena(t=t, items=[]) if np.random.randint(0, 2): arena.items.append( Item(name='GoodGoalMulti', sizes=[Vector3(0.5, 0.5, 0.5)])) else: arena.items.append( Item(name='GoodGoal', sizes=[Vector3(0.5, 0.5, 0.5)])) return arena
def create_arena_with_bouncing_goal(t): arena = Arena(t=t, items=[]) size = float(np.random.choice([0.5, 1])) if np.random.randint(0, 2): arena.items.append( Item(name='GoodGoalMultiBounce', sizes=[Vector3(size, size, size)])) else: arena.items.append( Item(name='GoodGoalBounce', sizes=[Vector3(size, size, size)])) return arena
def tunnel_tasks(number_of_conf=10): for i in range(number_of_conf): x_ref = random.randint(5, 20) z_ref = random.randint(5, 20) goal_pos = Vector3(x_ref, 0, z_ref) goal_item = Item(name="GoodGoal", positions=[goal_pos]) if i % 2 == 0: wall_item = Item(name="CylinderTunnelTransparent", positions=[goal_pos]) else: wall_item = Item(name="CylinderTunnel", positions=[goal_pos], colors=[RGB(153, 153, 153)]) items = [wall_item, goal_item] save_arena(items, "tunnel_tasks_{}".format(i))
def create_arena_with_red_houses(t=DEFAULT_TIME_LIMIT): arena = Arena(t=t, items=[]) centers, radiuses = [], [] for idx in range(4): not_good_center = True while not_good_center: not_good_center = False radius = np.random.uniform(2, 4) center = np.random.randint(5, 35, 2) for _center, _radius in zip(centers, radiuses): distance = np.sqrt(np.sum((center - _center)**2)) if distance < radius + _radius + 4: not_good_center = True centers.append(center) radiuses.append(radius) goal_size = float(np.random.uniform(1, 2)) _add_red_circle_to_arena(arena, center=center, radius=radius, goal_size=goal_size) if idx < 2: arena.items.append( Item(name='GoodGoalMulti', sizes=[Vector3(1, 1, 1)], positions=[Vector3(center[0], 0, center[1])])) return arena
def _create_random_box(): movable_objects = ['Cardbox1', 'Cardbox2'] name = str(np.random.choice(movable_objects)) x, z = np.random.randint(1, 6, 2).tolist() y = float(np.random.randint(1, 3)) sizes = [Vector3(x, y, z)] item = Item(name=name, sizes=sizes) return item
def _create_random_platform(): name = 'Wall' colors = [BLUE] x, z = np.random.randint(4, 8, 2).tolist() y = float(np.random.randint(1, 3)) sizes = [Vector3(x, y, z)] item = Item(name=name, sizes=sizes, colors=colors, rotations=[0]) return item
def _split_arena_in_four(arena): inmovable_objects = ['Wall', 'WallTransparent'] name = str(np.random.choice(inmovable_objects)) colors = [GRAY] wall_position = float(np.random.randint(13, 16)) wall_width = float(np.random.randint(19, 22)) wall_thickness = 1 sizes = [ Vector3(float(wall_width), float(np.random.randint(2, 10)), float(wall_thickness)) ] positions = [Vector3(wall_width / 2, 0, wall_position)] item = Item(name=name, sizes=sizes, colors=colors, positions=positions, rotations=[0]) arena.items.append(item) positions = [Vector3(40 - wall_width / 2, 0, 40 - wall_position)] item = Item(name=name, sizes=sizes, colors=colors, positions=positions, rotations=[0]) arena.items.append(item) sizes = [ Vector3(float(wall_thickness), float(np.random.randint(2, 10)), float(wall_width)) ] positions = [Vector3(wall_position, 0, 40 - wall_width / 2)] item = Item(name=name, sizes=sizes, colors=colors, positions=positions, rotations=[0]) arena.items.append(item) positions = [Vector3(40 - wall_position, 0, wall_width / 2)] item = Item(name=name, sizes=sizes, colors=colors, positions=positions, rotations=[0]) arena.items.append(item)
def _add_walls(arena, n_cells, wall_thickness): maze = Maze() maze = maze.generate(n_cells, n_cells) for cell in maze.cells: positions, sizes = _get_cell_walls_positions_and_sizes( cell, wall_thickness, n_cells, WALL_HEIGHT) if positions: wall = Item(name='Wall', positions=positions, rotations=[0]*len(positions), sizes=sizes) arena.items.append(wall)
def _create_random_zone(zone_types=None): if zone_types is None: zone_types = ['DeathZone', 'HotZone'] name = str(np.random.choice(zone_types)) sizes = [Vector3(*np.random.randint(2, 10, 3).tolist())] item = Item(name=name, sizes=sizes, rotations=[float(np.random.randint(0, 360))]) return item
def _create_random_cillinder(): inmovable_objects = ['CylinderTunnelTransparent', 'CylinderTunnel'] name = str(np.random.choice(inmovable_objects)) if name == 'CylinderTunnel': colors = [GRAY] else: colors = [] sizes = [Vector3(*np.random.randint(3, 10, 3).tolist())] item = Item(name=name, sizes=sizes, colors=colors) return item
def _add_agent_to_arena(arena): while 1: try: x, z = np.random.uniform(1, 39, 2).tolist() goal = Item(name='Agent', sizes=[Vector3(*[1] * 3)]) goal.positions = [Vector3(x, 0, z)] detect_collisions(goal, arena.items) arena.items.append(goal) break except CollisionDetected: pass
def _add_center_blocking_wall(arena): inmovable_objects = ['Wall', 'WallTransparent'] name = str(np.random.choice(inmovable_objects)) colors = [GRAY] sizes = [ Vector3(float(np.random.randint(15, 25)), float(np.random.randint(2, 10)), float(np.random.randint(15, 25))) ] positions = [Vector3(20, 0, 20)] item = Item(name=name, sizes=sizes, colors=colors, positions=positions) arena.items.append(item)
def _add_wall_to_arena(arena, orientation, position, goal_size): x_range = np.linspace(goal_size / 2, 40 - goal_size / 2, int((40 - goal_size) / (goal_size + 0.5))).tolist() x_range.pop(np.random.randint(len(x_range))) if orientation == 'horizontal': positions = [Vector3(float(position), 0, float(z)) for z in x_range] elif orientation == 'vertical': positions = [Vector3(float(x), 0, float(position)) for x in x_range] else: raise Exception('Unknown orientation: %s' % orientation) sizes = [Vector3(goal_size, goal_size, goal_size)] * len(positions) goal = Item(name='BadGoal', sizes=sizes, positions=positions) arena.items.append(goal)
def _add_simple_goal(arena): while 1: try: x, z = np.random.uniform(1, 39, 2).tolist() goal = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)], rotations=[0]) goal.positions = [Vector3(x, 0, z)] detect_collisions(goal, arena.items) arena.items.append(goal) break except CollisionDetected: pass
def create_death_zone_arena(size, t): arena_config = ArenaConfig() # Put green reward item_goal = Item(name="GoodGoal", positions=None, rotations=None, sizes=[Vector3(x=1, y=1, z=1)], colors=None) # Put Death Zone item_death_zone = Item(name="DeathZone", positions=None, rotations=None, sizes=[size], colors=None) items = [] items.append(item_goal) items.append(item_death_zone) arena = Arena(t=t, items=items, blackouts=None) return arena
def _add_reward_to_arena(arena, reward=DEFAULT_REWARD): remaining_reward = reward while remaining_reward: new_reward = np.random.uniform(0, remaining_reward) if new_reward < 0.5: new_reward = 0.5 if remaining_reward - new_reward < 0.5: new_reward = remaining_reward remaining_reward -= new_reward goal = Item(name='GoodGoalMulti', sizes=[Vector3(new_reward, new_reward, new_reward)]) arena.items.append(goal) return arena
def _create_ramp_for_platform(platform, rotation=None): """ Creates a ramp that allows to climb the platform Parameters ---------- platform : Item rotation : int If given the ramp will be placed with that orientation, otherwise it will be randomly sampled from 0, 90, 180, 270 """ position = platform.positions[0] size = platform.sizes[0] if rotation is None: rotation = float(np.random.choice([0, 90, 180, 270])) else: assert rotation in [0, 90, 180, 270] sizes = platform.sizes ramp_length = size.y * np.random.uniform(1, 3) if rotation == 0: sizes = [Vector3(size.x, size.y, ramp_length)] displacement = (size.z + ramp_length) / 2 positions = [ Vector3(position.x, position.y, position.z + displacement) ] elif rotation == 180: sizes = [Vector3(size.x, size.y, ramp_length)] displacement = (size.z + ramp_length) / 2 positions = [ Vector3(position.x, position.y, position.z - displacement) ] elif rotation == 90: sizes = [Vector3(size.z, size.y, ramp_length)] displacement = (size.x + ramp_length) / 2 positions = [ Vector3(position.x + displacement, position.y, position.z) ] elif rotation == 270: sizes = [Vector3(size.z, size.y, ramp_length)] displacement = (size.x + ramp_length) / 2 positions = [ Vector3(position.x - displacement, position.y, position.z) ] item = Item(name='Ramp', sizes=sizes, colors=[PINK], rotations=[rotation], positions=positions) return item
def _add_red_circle_to_arena(arena, center, radius, goal_size): theta_range = np.linspace(0, np.pi * 2, int((2 * np.pi * radius) / (goal_size + 0.5)), endpoint=False) theta_range += np.random.uniform(0, np.pi) theta_range = theta_range.tolist() theta_range.pop(np.random.randint(len(theta_range))) positions = [ Vector3(float(radius * np.cos(theta) + center[0]), 0, float(radius * np.sin(theta) + center[1])) for theta in theta_range ] sizes = [Vector3(goal_size, goal_size, goal_size)] * len(positions) goal = Item(name='BadGoal', sizes=sizes, positions=positions) arena.items.append(goal)
def _add_goal_on_top_of_box(arena): while 1: try: box = _create_random_box() border_distance = np.max([box.sizes[0].x, box.sizes[0].z]) * 1.2 x, z = np.random.uniform(border_distance, 40 - border_distance, 2).tolist() box.positions = [Vector3(x, 0, z)] detect_collisions(box, arena.items) arena.items.append(box) break except CollisionDetected: pass goal = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)], positions=[Vector3(x, box.sizes[0].y, z)]) arena.items.append(goal)
def _add_goal_above_hot_zone(arena): while 1: try: zone = _create_random_zone(['HotZone']) border_distance = np.max([zone.sizes[0].x, zone.sizes[0].z]) * 1.2 x, z = np.random.uniform(border_distance, 40 - border_distance, 2).tolist() zone.positions = [Vector3(x, 0, z)] detect_collisions(zone, arena.items) arena.items.append(zone) break except CollisionDetected: pass goal = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)], positions=[Vector3(x, 0, z)]) arena.items.append(goal)
def _create_goal_in_front_of_agent(x, z, angle, goal_type='BadGoalBounce', min_distance=15, max_distance=20, size=-1): while 1: distance = np.random.randint(min_distance, max_distance) x_new, z_new = get_position_in_front_of_agent(x, z, angle, distance) if x_new > 0 + size / 2 and x_new < 40 - size / 2 and z_new > 0 + size / 2 and z_new < 40 - size / 2: break goal = Item(name=goal_type, positions=[Vector3(x_new, 0, z_new)], sizes=[Vector3(size, size, size)], rotations=[normalize_angle(angle + 180)]) return goal
def _add_goals_and_agent_to_platform_maze(arena, n_cells, wall_thickness): centers = np.linspace(0, 40, n_cells, endpoint=False).tolist() wall_length = (40 - n_cells*wall_thickness)/n_cells if n_cells % 2 == 1: x_indexes = [0, n_cells//2, n_cells -1] else: x_indexes = [0, np.random.choice([n_cells//2, n_cells//2-1]), n_cells -1] z_indexes = x_indexes.copy() np.random.shuffle(x_indexes) np.random.shuffle(z_indexes) for idx in range(3): x = float(centers[x_indexes[idx]] + wall_length/2 + wall_thickness/2) z = float(centers[z_indexes[idx]] + wall_length/2 + wall_thickness/2) item = Item(name='GoodGoalMulti', sizes=[Vector3(*[1]*3)], positions=[Vector3(x, PLATFORM_HEIGHT, z)]) if not idx: item.name = 'Agent' arena.items.append(item)
def _add_goal_inside_cillinder(arena): while 1: try: cillinder = _create_random_cillinder() border_distance = np.max( [cillinder.sizes[0].x, cillinder.sizes[0].z]) * 1.2 x, z = np.random.uniform(border_distance, 40 - border_distance, 2).tolist() cillinder.positions = [Vector3(x, 0, z)] detect_collisions(cillinder, arena.items) arena.items.append(cillinder) break except CollisionDetected: pass goal = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)], positions=[Vector3(x, 0, z)]) arena.items.append(goal)
def _add_random_inmovable_object(arena): inmovable_objects = [ 'Wall', 'WallTransparent', 'Ramp', 'CylinderTunnelTransparent', 'CylinderTunnel' ] name = str(np.random.choice(inmovable_objects)) if name in ['Wall', 'CylinderTunnel']: colors = [GRAY] elif name == 'Ramp': colors = [PINK] else: colors = [] if 'Wall' in name or name == 'Ramp': sizes = [Vector3(*np.random.randint(1, 10, 3).tolist())] else: sizes = [Vector3(*np.random.randint(3, 10, 3).tolist())] item = Item(name=name, sizes=sizes, colors=colors) arena.items.append(item)
def _create_wall_in_front_of_agent(x, z, angle, wall_type='WallTransparent', min_distance=15, max_distance=20): size = 1 while 1: distance = np.random.randint(min_distance, max_distance) x_new, z_new = get_position_in_front_of_agent(x, z, angle, distance) if x_new > 0 + size / 2 and x_new < 40 - size / 2 and z_new > 0 + size / 2 and z_new < 40 - size / 2: break height = np.random.uniform(5, 10) width = np.random.uniform(10, 20) thickness = np.random.uniform(1, 2) wall = Item(name=wall_type, positions=[Vector3(x_new, 0, z_new)], sizes=[Vector3(width, height, thickness)], rotations=[normalize_angle(angle)]) return wall
def _add_goal_on_top_of_platform(arena, empty_platform=False): while 1: try: platform = _create_random_platform() border_distance = np.max( [platform.sizes[0].x, platform.sizes[0].z]) * 1.2 x, z = np.random.uniform(border_distance, 40 - border_distance, 2).tolist() platform.positions = [Vector3(x, 0, z)] ramp = _create_ramp_for_platform(platform) detect_collisions(ramp, arena.items) detect_collisions(platform, arena.items) arena.items.append(platform) arena.items.append(ramp) break except CollisionDetected: pass if not empty_platform: goal = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)], positions=[Vector3(x, platform.sizes[0].y, z)]) arena.items.append(goal)
def create_arena_with_platform_maze(t, difficulty=None): arena = Arena(t=t, items=[]) if difficulty is None: difficulty = np.random.choice(DIFFICULTY_LEVELS) else: assert difficulty in DIFFICULTY_LEVELS if difficulty == 'easy': n_cells = 3 elif difficulty == 'medium': n_cells = np.random.randint(3, 5) elif difficulty == 'hard': n_cells = 4 else: raise Exception('Unknown difficulty: %s' % difficulty) wall_thickness = 6 _add_platform_maze(arena, n_cells=n_cells, wall_thickness=wall_thickness) _apply_color_to_platform_maze(arena) _add_goals_and_agent_to_platform_maze(arena, n_cells, wall_thickness) item = Item(name='DeathZone', sizes=[Vector3(40, 0, 40)], positions=[Vector3(20, 0, 20)], rotations=[0]) arena.items.append(item) return arena
def _add_simple_goal(arena): item = Item(name='GoodGoalMulti', sizes=[Vector3(*[1] * 3)]) arena.items.append(item)