def test_execute_with_temporary(generate_pilot_list): # Test the execute function with a temporarily created PlaygroundState object. # This is to test for any cpp lifetime management weirdness. p1, p2 = generate_pilot_list assert np.allclose( p1.execute(PlaygroundState(Map(10, 10, 1.0)), 0).data, [1, 2, 3]) assert np.allclose( p2.execute(PlaygroundState(Map(10, 10, 1.0)), 0).data, [1] * 10)
def generate_map_list(): # List of maps constructed in different ways. # Also tests initialization. map1 = Map(width=2, height=2, resolution=0.1) map2 = Map([[1, 2], [3, 4], [5, 6]], 0.5) map3 = Map(((0, 1, 0), (2, -1, 0)), 1) map4 = Map(data=np.eye(100), resolution=10) return map1, map2, map3, map4
def test_state_write(generate_playground, generate_robot_list): # Make sure that the returned state is a reference and the value of the # actual internal state can be changed through this state. playground = generate_playground robot1, robot2 = generate_robot_list playground_state = playground.state # Change time. playground_state.time = 0.01 assert playground.state.time == 0.01 playground_state.map = Map(np.ones([500, 500]), 0.1) # The state value in the Playground object should have changed. assert np.allclose(playground.state.map.data, np.ones([500, 500])) # Adding a robot through the state. Note that this should never be carried # out unless its for testing purposes. Playground's add_robot function # must always be used. playground_state.add_robot(robot1, 0) playground_state.add_robot(robot2, 1) # Make sure that the state object and internal playground state both have # two robots. assert playground_state.num_robots == 2 assert playground.state.num_robots == 2
def test_canvas_from_map_without_obstacles(): env_map = Map(width=20, height=20, resolution=0.01) canvas = canvas_from_map(env_map) # Make sure that the canvas is valid. _valid_canvas(canvas)
def test_execute(generate_pilot_list): # Make sure that the derived Pilot class performs the right operation # during an execute call. p1, p2 = generate_pilot_list # PlaygroundState object to be used. playground_state = PlaygroundState(Map(10, 10, 1.0)) assert np.allclose(p1.execute(playground_state, 0).data, [1, 2, 3]) assert np.allclose(p2.execute(playground_state, 0).data, [1] * 10)
def test_evolve_with_polygonal_obstacle(): env_map = Map(width=50.0, height=50.0, resolution=0.1) evolved_env_map = evolve_map_with_polygonal_obstacle( env_map, [[0.0, 50.0], [10.0, 50.0], [10.0, 47.5], [0.0, 47.5]]) # The top left region would be filled by the circle. assert np.allclose(evolved_env_map.data[:25, :100], MapConstants.OBSTACLE * np.ones([25, 100])) # Also make sure that the original map is not mutated. assert np.allclose(env_map.data[:25, :100], MapConstants.EMPTY * np.ones([25, 100]))
def test_evolve_with_circular_obstacle(): env_map = Map(width=50.0, height=50.0, resolution=0.1) evolved_env_map = evolve_map_with_circular_obstacle( env_map, CircleShape(5.0, center=[0.0, 50.0])) # The top left region would be filled by the circle. assert np.allclose(evolved_env_map.data[:35, :35], MapConstants.OBSTACLE * np.ones([35, 35])) # Also make sure that the original map is not mutated. assert np.allclose(env_map.data[:35, :35], MapConstants.EMPTY * np.ones([35, 35]))
def test_map(generate_playground_state_list): ps1, ps2 = generate_playground_state_list # Test map retrieval. assert ps1.map.width == 10.0 assert ps1.map.height == 10.0 assert ps1.map.resolution == 0.1 assert np.all(ps1.map.data == 0.0) assert ps2.map.width == 20.0 assert ps2.map.height == 20.0 assert ps2.map.resolution == 0.1 assert np.all(ps2.map.data == 1.0) # Test map setting. ps1.map = Map(10.0, 10.0, 0.5) ps2.map = Map(20.0, 20.0, 0.8) assert ps1.map.resolution == 0.5 assert ps2.map.resolution == 0.8 assert np.all(ps2.map.data == 0.0)
def run(robot_type): # Convert the type from a string to a RobotType object. assert isinstance(robot_type, str) # Check if the type is valid and convert to a RobotType object. try: robot_type = RobotType(robot_type) except (ValueError): raise MorphacLogicError( f"Invalid robot type. Please try again.\n{HELP_PROMPT}") # Parameters. dt = 0.02 display_ratio = 1.0 metric_type = "time" metric_limit = 100.0 # Create the environment. env_map = Map(width=20.0, height=20.0, resolution=0.02) env_map = evolve_map_with_circular_obstacle( env_map, CircleShape(radius=1.0, center=[10.0, 10.0])) # Create the playground. playground_spec = PlaygroundSpec(name="constant_controller_playground", dt=dt) playground = Playground(playground_spec, env_map) # Get the required robot. if robot_type not in robot_bank: raise MorphacLogicError("Invalid robot type.") robot = robot_bank[robot_type] # Construct the pilot. pilot = ConstantPilot(ConstantController( constant_control_bank[robot_type])) # Add the robot to the playground. playground.add_robot(robot, pilot, IntegratorType.EULER_INTEGRATOR, uid=0) # Create the playground visualizer. playground_visualizer_spec = PlaygroundVisualizerSpec( display_ratio=display_ratio) playground_visualizer = PlaygroundVisualizer( spec=playground_visualizer_spec, playground=playground) # Simulate. playground_visualizer.run(metric_type, metric_limit)
def test_canvas_from_map_with_obstacles(): map_data = MapConstants.EMPTY * np.ones([1000, 1000], dtype=np.int32) # Left half of the map as an obstacle. map_data[:, :500] = MapConstants.OBSTACLE env_map = Map(map_data, resolution=0.01) canvas = canvas_from_map(env_map) _valid_canvas(canvas) # Making sure that the canvas is painted correctly. for i in range(3): assert np.allclose(canvas[:, :500, i], MapColors.BLACK[i] * np.ones([1000, 500])) assert np.allclose(canvas[:, 500:, i], MapColors.WHITE[i] * np.ones([1000, 500]))
def generate_playground_visualizer(): env_map = Map(width=20.0, height=20.0, resolution=0.02) # Create the playground. playground_spec = PlaygroundSpec(name="playground_spec", dt=0.01) playground = Playground(playground_spec, env_map) robot = Robot(AckermannModel(1.0, 1.0)) # Construct the pilot. pilot = ZeroPilot(ZeroController(2)) # Add the robot to the playground. playground.add_robot(robot, pilot, IntegratorType.EULER_INTEGRATOR, uid=0) # Create the playground visualizer. spec = PlaygroundVisualizerSpec(display_ratio=1.0) playground_visualizer = PlaygroundVisualizer(spec, playground) return playground_visualizer
def generate_playground(): playground_spec = PlaygroundSpec("playground", 0.01) playground = Playground(playground_spec, Map(50, 50, 0.1)) return playground
def generate_playground_state_list(): ps1 = PlaygroundState(Map(10.0, 10.0, 0.1)) ps2 = PlaygroundState(map=Map(np.ones([200, 200], dtype=np.float), 0.1)) return ps1, ps2