def __init__(self, seed, robot_id, sim, comm_range, map_filename,
                 duration, disc_method, disc, log_filename, teammates_id, n_robots, ref_dist,
                 env_filename, comm_dataset_filename, resize_factor, tiling, errors_filename,
                 communication_model):

        rospy.loginfo(str(robot_id) + ' - Leader - starting!')
        if not (os.path.exists(env_filename)):
            print "Creating new environment."
            f = open(env_filename, "wb")
            self.env = Environment(map_filename, disc_method, disc, resize_factor, comm_range,communication_model)
            pickle.dump(self.env, f)
            f.close()
        else:
            f = open(env_filename, "rb")
            self.env = pickle.load(f)
            f.close()

        super(Leader, self).__init__(seed, robot_id, True, sim, comm_range, map_filename,
                                     duration, log_filename, comm_dataset_filename, teammates_id, n_robots,
                                     ref_dist, resize_factor, errors_filename)

        rospy.loginfo(str(self.robot_id) + ' - Created environment variable')


        self.comm_map = GPmodel(self.env.dimX, self.env.dimY, comm_range, tiling, self.comm_module.comm_model, self.log_filename)
        self.comm_maps = [] #for logging

        #for sending commands to the follower
        #the name of the server is the name of the robot itself
        self.clients_signal = {}
        for teammate_id in teammates_id:
            self.clients_signal[teammate_id] = actionlib.SimpleActionClient('/robot_' + str(teammate_id) + '/main_robot', SignalMappingAction)
            rospy.loginfo(str(self.robot_id) + ' - Leader - waiting for follower server ' + str(teammate_id))
            self.clients_signal[teammate_id].wait_for_server()
            rospy.loginfo(str(self.robot_id) + ' - Done.')
Beispiel #2
0
def evaluate(pr, d_list, tot_proc, environment, num_robots, num_runs,
             is_simulation, comm_model_path, granularity, mission_duration,
             plot_comm_map, fixed_robot, test_set_size, log_folder):
    """Create pickle files containing data to be plotted.

    It processes the dat files containing the collected data from the robots.
    It outputs a pickle file, containing errors, variances, and GP comm_map,
    according to run, environment, robot.

    Args:
        pr (int): identification number of the process.
        d_list (dict): shared dictionary between processes.
        tot_proc (int): total number of created processes.
        environment (str): name of environment used for saving data.
        num_robots (int): number of robots.
        num_runs (int): number of repeated experiments.
        is_simulation(bool): True, if data generated from simulation.
        comm_model_path (str): path to the XML file containing communication model parameters.
        granularity (int): second for each epoch to plot data.
        mission_duration (int): seconds for total mission.
        plot_comm_map (bool): If True, plot communication map.
        fixed_robot (tuple of float): x,y of robot in meters.
        test_set_size (int): number of samples in the test set.
        log_folder (str): folder where logs are saved.

    """

    log_folder = os.path.expanduser(log_folder)

    # Reading of the communication parameters necessary to produce correct test set.
    comm_model = CommModel(comm_model_path)

    # Reading environment image.
    environment_yaml_path = os.getcwd() + '/envs/' + environment + '.yaml'

    im_array, resolution = read_environment(environment_yaml_path)
    im_array = specular(im_array)

    # Generation of test set.
    if is_simulation:
        random.seed(0)
        np.random.seed(0)
        dimX, dimY, XTest, YTest = create_test_set(im_array, comm_model,
                                                   test_set_size, resolution)

    runs = range(num_runs)

    for proc in range(tot_proc):
        if proc == pr:
            runs = np.array_split(
                runs, tot_proc
            )[proc]  #splitting the runs list in order to parallelize its analysis
            print "proc: " + str(pr) + ", runs: " + str(runs)

    time.sleep(pr)

    errors = {}
    variances_all = {}
    times_all = {}

    for run in runs:
        all_signal_data = []

        for robot in range(num_robots):
            dataset_filename = log_folder + str(
                run) + '_' + environment + '_' + str(
                    robot) + '_' + str(num_robots) + '_' + str(
                        int(comm_model.COMM_RANGE)) + '.dat'
            all_signal_data += parse_dataset(dataset_filename)

        errors[run] = []
        variances_all[run] = []
        times_all[run] = []

        all_secs = range(granularity, mission_duration + 1, granularity)
        for secs in all_secs:
            cur_signal_data = []
            for datum in all_signal_data:
                if datum.timestep <= secs:
                    cur_signal_data.append(datum)

            print "Run: " + str(run) + " - number of data: ", len(
                cur_signal_data)

            start = time.time()
            comm_map = GPmodel(dimX, dimY, comm_model.COMM_RANGE, False)
            comm_map.update_model(cur_signal_data)
            end = time.time()

            predictions_all = comm_map.predict(XTest)
            predictions = map(lambda x: x[0], predictions_all)
            variances = map(lambda x: x[1], predictions_all)
            std_devs = map(lambda x: math.sqrt(x), variances)
            conf_95 = map(lambda x: 1.96 * x, std_devs)

            errors[run].append(
                (mean_squared_error(YTest, predictions),
                 math.sqrt(mean_squared_error(YTest, predictions))))
            variances_all[run].append(
                (np.mean(variances), np.std(variances), np.mean(std_devs),
                 np.std(std_devs), np.mean(conf_95), np.std(conf_95)))
            times_all[run].append(end - start)

            if plot_comm_map:
                if filter:
                    extension = '_C.png'
                else:
                    extension = '.png'

                print "Drawing the CM..."
                communication_figures = plot_prediction_from_xy_center_3d(
                    im_array, fixed_robot, comm_map, dimX, dimY, comm_model,
                    resolution, True, cur_signal_data)
                communication_map_figure_filename = os.getcwd(
                ) + '/figs/COMM_MAP' + str(
                    num_robots) + '_' + environment + '_' + str(
                        int(comm_model.COMM_RANGE)) + '_' + str(
                            run) + '_' + str(secs) + extension
                communication_figures[0].savefig(
                    communication_map_figure_filename, bbox_inches='tight')
                plt.close(communication_figures[0])
                print "Done."
                if len(communication_figures) > 1:
                    print "Drawing the Variance map..."
                    communication_map_figure_filename = os.getcwd(
                    ) + '/figs/COMM_MAP' + str(
                        num_robots) + '_' + environment + '_' + str(
                            int(comm_model.COMM_RANGE)) + '_' + str(
                                run) + '_' + str(
                                    secs) + '_' + 'VAR' + extension
                    communication_figures[1].savefig(
                        communication_map_figure_filename, bbox_inches='tight')
                    plt.close(communication_figures[1])
                    print "Done."

                #cleaning stuff
                plt.close('all')
                gc.collect()

    er = d_list[0].copy()
    er.update(errors)
    d_list[0] = er.copy()

    v = d_list[1].copy()
    v.update(variances_all)
    d_list[1] = v.copy()

    t = d_list[2].copy()
    t.update(times_all)
    d_list[2] = t.copy()
Beispiel #3
0
def evaluate(environment, num_robots, num_runs, is_simulation, comm_model_path,
             granularity, mission_duration, plot_comm_map, fixed_robot,
             test_set_size, log_folder):
    """Create pickle files containing data to be plotted.

    It processes the dat files containing the collected data from the robots.
    It outputs a pickle file, containing errors, variances, and GP comm_map,
    according to run, environment, robot.

    Args:
        environment (str): name of environment used for saving data.
        num_robots (int): number of robots.
        num_runs (int): number of repeated experiments.
        is_simulation(bool): True, if data generated from simulation.
        comm_model_path (str): path to the XML file containing communication 
            model parameters.
        granularity (int): second for each epoch to plot data.
        mission_duration (int): seconds for total mission.
        plot_comm_map (bool): If True, plot communication map.
        fixed_robot (tuple of float): x,y of robot in meters.
        filter (bool): If True, consider ony information found by Pairing TSP algorithm
        test_set_size (int): number of samples in the test set
        log_folder (str): folder where logs are saved.
    """
    log_folder = os.path.expanduser(log_folder)

    # Reading of the communication parameters necessary to produce correct test set.
    comm_model = CommModel(comm_model_path)

    # Reading environment image.
    environment_yaml_path = os.getcwd() + '/envs/' + environment + '.yaml'

    sel_pol = gflags.FLAGS.point_selection_policy

    im_array, resolution = read_environment(environment_yaml_path)
    im_array = specular(im_array)

    test_type = gflags.FLAGS.test_type

    # Generation of test set.
    if is_simulation:
        if test_type != "discarded_set":
            random.seed(0)
            np.random.seed(0)
            dimX, dimY, XTest, YTest = create_test_set(
                im_array, comm_model, test_set_size,
                False if test_type == "random" else True, resolution)

    runs = range(num_runs)

    errors = {}
    variances_all = {}
    times_all = {}

    for set in sets:
        #if set == "complete": continue

        print "Set: ", set

        filter = True if set == "filtered" else False

        errors[set] = {}
        variances_all[set] = {}
        times_all[set] = {}

        for run in runs:
            if run != 0 and plot_comm_map: break

            parsed = []
            if test_type == "discarded_set" and set == "filtered":
                parsed_test_set = []

            for robot in range(num_robots):
                dataset_filename = log_folder + str(run) + '_' + environment + \
                                   '_' + str(robot) + '_' + str(num_robots) + \
                                   '_' + str(int(comm_model.COMM_RANGE)) + \
                                   '_' + sel_pol + '.dat'
                parsed += parse_dataset(
                    dataset_filename, filter,
                    True if test_type == "normalized" else False)

                if test_type == "discarded_set" and set == "filtered":
                    parsed_test_set += parse_dataset(dataset_filename, False,
                                                     False)

            all_signal_data = create_dataset(parsed, set)

            if test_type == "discarded_set":
                dimX, dimY, XTest, YTest = create_test(
                    im_array, parsed if set != "filtered" else parsed_test_set,
                    resolution)

            print "Set length: " + str(len(all_signal_data))

            if not plot_comm_map and \
                    len(all_signal_data) >= 10000: #with 5 runs and 10k samples for run, 64GB of RAM/swap memory are not enough
                print "Too many samples: the GP training would be too heavy. Discarding this set."
                break

            errors[set][run] = []
            variances_all[set][run] = []
            times_all[set][run] = []

            all_secs = range(granularity, mission_duration + 1, granularity)
            for secs in all_secs:
                cur_signal_data = []
                for datum in all_signal_data:
                    if datum.timestep <= secs:
                        cur_signal_data.append(datum)

                print "Run: " + str(run) + " - number of data: ", len(
                    cur_signal_data)

                start = time.time()
                comm_map = GPmodel(dimX, dimY, comm_model.COMM_RANGE, False)
                comm_map.update_model(cur_signal_data)
                end = time.time()

                predictions_all = comm_map.predict(XTest)
                predictions = map(lambda x: x[0], predictions_all)
                variances = map(lambda x: x[1], predictions_all)
                std_devs = map(lambda x: math.sqrt(x), variances)
                conf_95 = map(lambda x: 1.96 * x, std_devs)

                errors[set][run].append(
                    (mean_squared_error(YTest, predictions),
                     math.sqrt(mean_squared_error(YTest, predictions))))
                variances_all[set][run].append(
                    (np.mean(variances), np.std(variances), np.mean(std_devs),
                     np.std(std_devs), np.mean(conf_95), np.std(conf_95)))
                times_all[set][run].append(end - start)

                if plot_comm_map:
                    print "Drawing the Communication map..."

                    if set == "complete":
                        extension = '.png'
                    elif set == "pre_processing":
                        extension = '_P.png'
                    else:
                        extension = '_C.png'

                    communication_figures = plot_prediction_from_xy_center_3d(
                        im_array, fixed_robot, comm_map, dimX, dimY,
                        comm_model, resolution, True, cur_signal_data)
                    communication_map_figure_filename = os.getcwd() + '/figs/COMM_MAP_' + str(num_robots) + \
                                                        '_' + environment + '_' + str(int(comm_model.COMM_RANGE)) + \
                                                        '_' + str(run) + '_' + str(secs) + '_' + sel_pol + extension
                    communication_figures[0].savefig(
                        communication_map_figure_filename, bbox_inches='tight')
                    print "Done."
                    if len(communication_figures) > 1:
                        print "Drawing the Variance map..."
                        communication_map_figure_filename = os.getcwd() + '/figs/COMM_MAP_' + str(num_robots) + \
                                                            '_'  + environment + '_' + str(int(comm_model.COMM_RANGE)) + \
                                                            '_' + str(run) + '_' + str(secs) + '_' + sel_pol + \
                                                            '_' + 'VAR' + extension
                        communication_figures[1].savefig(
                            communication_map_figure_filename,
                            bbox_inches='tight')
                        print "Done."

    print '----------------------------------------------------------------------------'
    print errors
    print '----------------------------------------------------------------------------'
    print variances_all
    print '----------------------------------------------------------------------------'
    print times_all
    print '----------------------------------------------------------------------------'

    f = open(
        log_folder + str(num_robots) + '_' + environment + '_' +
        str(int(comm_model.COMM_RANGE)) + '_' +
        gflags.FLAGS.point_selection_policy + '.dat', "wb")
    pickle.dump((errors, variances_all, times_all), f)
    f.close()
Beispiel #4
0
def evaluate(environment, num_robots, num_runs, is_simulation,
    comm_model_path, granularity, mission_duration,
    plot_comm_map, fixed_robot, test_set_size, log_folder):
    """Create pickle files containing data to be plotted.

    It processes the dat files containing the collected data from the robots.
    It outputs a pickle file, containing errors, variances, and GP comm_map,
    according to run, environment, robot.

    Args:
        environment (str): name of environment used for saving data.
        num_robots (int): number of robots.
        num_runs (int): number of repeated experiments.
        is_simulation(bool): True, if data generated from simulation.
        comm_model_path (str): path to the XML file containing communication 
            model parameters.
        granularity (int): second for each epoch to plot data.
        mission_duration (int): seconds for total mission.
        plot_comm_map (bool): If True, plot communication map.
        fixed_robot (tuple of float): x,y of robot in meters.
        filter (bool): If True, consider ony information found by Carlo algorithm
        test_set_size (int): number of samples in the test set
        log_folder (str): folder where logs are saved.
    """
    log_folder = os.path.expanduser(log_folder)
    
    # Reading of the communication parameters necessary to produce correct test set.
    comm_model = CommModel(comm_model_path)

    # Reading environment image.
    environment_yaml_path = os.getcwd() + '/envs/' + environment + '.yaml'

    im_array, resolution = read_environment(environment_yaml_path)
    im_array = specular(im_array)

    #parsing filter
    filter = gflags.FLAGS.filter_dat
    
    # Generation of test set.
    if is_simulation:
        # In simulation.
        random.seed(0)
        np.random.seed(0)
        dimX, dimY, XTest, YTest = create_test_set(im_array, comm_model,test_set_size, resolution)
    else: #only to not have warnings, setting is useful only for real robots
        dimX = []
        dimY = []
        XTest = []
        YTest = []

    runs = range(num_runs)

    errors = {}
    variances_all = {}
    times_all = {}

    for run in runs:
        all_signal_data = []

        for robot in range(num_robots):
            dataset_filename = log_folder + str(run) + '_' + environment + '_' + str(robot) + '_' + str(num_robots) + '_' + str(int(comm_model.COMM_RANGE)) + '.dat'
            all_signal_data += parse_dataset(dataset_filename)

        #print "Length: " + str(len(all_signal_data))

        errors[run] = []
        variances_all[run] = []
        times_all[run] = []

        all_secs = range(granularity, mission_duration + 1, granularity)
        for secs in all_secs:
            cur_signal_data = []
            for datum in all_signal_data:
                if datum.timestep <= secs:
                    cur_signal_data.append(datum)

            print "Run: " + str(run) + " - number of data: ", len(cur_signal_data)

            start = time.time()
            comm_map = GPmodel(dimX, dimY, comm_model.COMM_RANGE, False)
            comm_map.update_model(cur_signal_data)
            end = time.time()

            predictions_all = comm_map.predict(XTest)
            predictions = map(lambda x: x[0], predictions_all)
            variances = map(lambda x: x[1], predictions_all)
            std_devs = map(lambda x: math.sqrt(x), variances)
            conf_95 = map(lambda x: 1.96 * x, std_devs)

            errors[run].append((mean_squared_error(YTest, predictions), math.sqrt(mean_squared_error(YTest, predictions))))
            variances_all[run].append((np.mean(variances), np.std(variances), np.mean(std_devs), np.std(std_devs),np.mean(conf_95), np.std(conf_95)))
            times_all[run].append(end - start)

            if plot_comm_map:
                if filter:
                    extension = '_C.png'
                else:
                    extension = '.png'

                print "Drawing the CM..."
                communication_figures = plot_prediction_from_xy_center_3d(im_array, fixed_robot, comm_map, dimX, dimY,
                                                                          comm_model, resolution, True, cur_signal_data)
                communication_map_figure_filename = os.getcwd() + '/figs/COMM_MAP' + str(num_robots) + \
                                                    '_' + environment + '_' + str(int(comm_model.COMM_RANGE)) + \
                                                    '_' + str(run) + '_' + str(secs) + extension
                communication_figures[0].savefig(communication_map_figure_filename, bbox_inches='tight')
                #plt.close(communication_figures[0])
                print "Done."
                if len(communication_figures) > 1:
                    print "Drawing the Variance map..."
                    communication_map_figure_filename = os.getcwd() + '/figs/COMM_MAP' + str(num_robots) +\
                                                        '_'  + environment + '_' + str(int(comm_model.COMM_RANGE)) + \
                                                        '_' + str(run) + '_' + str(secs) + '_' + 'VAR' + extension
                    communication_figures[1].savefig(communication_map_figure_filename, bbox_inches='tight')
                    #plt.close(communication_figures[1])
                    print "Done."

                # cleaning stuff
                plt.close('all')
                gc.collect()

    print '----------------------------------------------------------------------------'
    print errors
    print '----------------------------------------------------------------------------'
    print variances_all
    print '----------------------------------------------------------------------------'
    print times_all
    print '----------------------------------------------------------------------------'

    f = open(log_folder + str(num_robots) + '_' + environment + '_' + str(int(comm_model.COMM_RANGE)) + '.dat', "wb")
    pickle.dump((errors, variances_all, times_all), f)
    f.close()