Exemplo n.º 1
0
def tournament_worker(graph_manager, number_of_trials, task_parameters, s3_writers, is_continuous):
    """ Tournament worker function

    Arguments:
        graph_manager {[MultiAgentGraphManager]} -- [Graph manager of multiagent graph manager]
        number_of_trials {[int]} -- [Number of trails you want to run the evaluation]
        task_parameters {[TaskParameters]} -- [Information of the checkpoint, gpu/cpu, framework etc of rlcoach]
        s3_writers {[S3Writer]} -- [Information to upload to the S3 bucket all the simtrace and mp4]
        is_continuous {bool} -- [The termination condition for the car]
    """
    checkpoint_dirs = list()
    agent_names = list()
    subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic = list(), list()
    subscribe_to_save_mp4, unsubscribe_from_save_mp4 = list(), list()
    for agent_param in graph_manager.agents_params:
        _checkpoint_dir = task_parameters.checkpoint_restore_path if len(graph_manager.agents_params) == 1 \
            else os.path.join(task_parameters.checkpoint_restore_path, agent_param.name)
        agent_names.append(agent_param.name)
        checkpoint_dirs.append(_checkpoint_dir)
        racecar_name = 'racecar' if len(agent_param.name.split("_")) == 1 \
            else "racecar_{}".format(agent_param.name.split("_")[1])
        subscribe_to_save_mp4_topic.append("/{}/save_mp4/subscribe_to_save_mp4".format(racecar_name))
        unsubscribe_from_save_mp4_topic.append("/{}/save_mp4/unsubscribe_from_save_mp4".format(racecar_name))
    wait_for_checkpoints(checkpoint_dirs, graph_manager.data_store)
    modify_checkpoint_variables(checkpoint_dirs, agent_names)

    # Make the clients that will allow us to pause and unpause the physics
    rospy.wait_for_service('/gazebo/pause_physics')
    rospy.wait_for_service('/gazebo/unpause_physics')
    pause_physics = ServiceProxyWrapper('/gazebo/pause_physics', Empty)
    unpause_physics = ServiceProxyWrapper('/gazebo/unpause_physics', Empty)

    for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic):
        rospy.wait_for_service(mp4_sub)
        rospy.wait_for_service(mp4_unsub)
    for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic):
        subscribe_to_save_mp4.append(ServiceProxyWrapper(mp4_sub, Empty))
        unsubscribe_from_save_mp4.append(ServiceProxyWrapper(mp4_unsub, Empty))

    graph_manager.create_graph(task_parameters=task_parameters, stop_physics=pause_physics,
                               start_physics=unpause_physics, empty_service_call=EmptyRequest)
    unpause_physics(EmptyRequest())
    graph_manager.reset_internal_state(True)

    is_save_mp4_enabled = rospy.get_param('MP4_S3_BUCKET', None)
    if is_save_mp4_enabled:
        for subscribe_mp4 in subscribe_to_save_mp4:
            subscribe_mp4(EmptyRequest())
    if is_continuous:
        graph_manager.evaluate(EnvironmentSteps(1))
    else:
        for _ in range(number_of_trials):
            graph_manager.evaluate(EnvironmentSteps(1))
    if is_save_mp4_enabled:
        for unsubscribe_mp4 in unsubscribe_from_save_mp4:
            unsubscribe_mp4(EmptyRequest())
    for s3_writer in s3_writers:
        s3_writer.upload_to_s3()
    time.sleep(1)
    pause_physics(EmptyRequest())
def tournament_worker(graph_manager, number_of_trials, task_parameters,
                      s3_writers, is_continuous, park_positions):
    """ Tournament worker function

    Arguments:
        graph_manager(MultiAgentGraphManager): Graph manager of multiagent graph manager
        number_of_trials(int): Number of trails you want to run the evaluation
        task_parameters(TaskParameters): Information of the checkpoint, gpu/cpu,
            framework etc of rlcoach
        s3_writers(S3Writer): Information to upload to the S3 bucket all the simtrace and mp4
        is_continuous(bool): The termination condition for the car
        park_positions(list of tuple): list of (x, y) for cars to park at
    """
    # Collect profiler information only IS_PROFILER_ON is true
    with utils.Profiler(s3_bucket=PROFILER_S3_BUCKET,
                        s3_prefix=PROFILER_S3_PREFIX,
                        output_local_path=ROLLOUT_WORKER_PROFILER_PATH,
                        enable_profiling=IS_PROFILER_ON):
        checkpoint_dirs = list()
        agent_names = list()
        subscribe_to_save_mp4_topic, unsubscribe_from_save_mp4_topic = list(
        ), list()
        subscribe_to_save_mp4, unsubscribe_from_save_mp4 = list(), list()
        for agent_param in graph_manager.agents_params:
            _checkpoint_dir = task_parameters.checkpoint_restore_path if len(graph_manager.agents_params) == 1 \
                else os.path.join(task_parameters.checkpoint_restore_path, agent_param.name)
            agent_names.append(agent_param.name)
            checkpoint_dirs.append(_checkpoint_dir)
            racecar_name = 'racecar' if len(agent_param.name.split("_")) == 1 \
                                     else "racecar_{}".format(agent_param.name.split("_")[1])
            subscribe_to_save_mp4_topic.append(
                "/{}/save_mp4/subscribe_to_save_mp4".format(racecar_name))
            unsubscribe_from_save_mp4_topic.append(
                "/{}/save_mp4/unsubscribe_from_save_mp4".format(racecar_name))
        wait_for_checkpoints(checkpoint_dirs, graph_manager.data_store)
        modify_checkpoint_variables(checkpoint_dirs, agent_names)

        # Make the clients that will allow us to pause and unpause the physics
        rospy.wait_for_service('/gazebo/pause_physics')
        rospy.wait_for_service('/gazebo/unpause_physics')
        pause_physics = ServiceProxyWrapper('/gazebo/pause_physics', Empty)
        unpause_physics = ServiceProxyWrapper('/gazebo/unpause_physics', Empty)

        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            rospy.wait_for_service(mp4_sub)
            rospy.wait_for_service(mp4_unsub)
        for mp4_sub, mp4_unsub in zip(subscribe_to_save_mp4_topic,
                                      unsubscribe_from_save_mp4_topic):
            subscribe_to_save_mp4.append(ServiceProxyWrapper(mp4_sub, Empty))
            unsubscribe_from_save_mp4.append(
                ServiceProxyWrapper(mp4_unsub, Empty))

        graph_manager.create_graph(task_parameters=task_parameters,
                                   stop_physics=pause_physics,
                                   start_physics=unpause_physics,
                                   empty_service_call=EmptyRequest)
        logger.info(
            "Graph manager successfully created the graph: Unpausing physics")
        unpause_physics(EmptyRequest())

        is_save_mp4_enabled = rospy.get_param('MP4_S3_BUCKET', None)
        if is_save_mp4_enabled:
            for subscribe_mp4 in subscribe_to_save_mp4:
                subscribe_mp4(EmptyRequest())

        configure_environment_randomizer()
        track_data = TrackData.get_instance()

        # Before each evaluation episode (single lap for non-continuous race and complete race for
        # continuous race), a new copy of park_positions needs to be loaded into track_data because
        # a park position will be pop from park_positions when a racer car need to be parked.
        if is_continuous:
            track_data.park_positions = park_positions
            graph_manager.evaluate(EnvironmentSteps(1))
        else:
            for _ in range(number_of_trials):
                track_data.park_positions = park_positions
                graph_manager.evaluate(EnvironmentSteps(1))
        if is_save_mp4_enabled:
            for unsubscribe_mp4 in unsubscribe_from_save_mp4:
                unsubscribe_mp4(EmptyRequest())
        for s3_writer in s3_writers:
            s3_writer.upload_to_s3()
        time.sleep(1)
        pause_physics(EmptyRequest())