def _collect_aspect_highlights(_aspect_name, aspect_lst, num_highlights):
    global aspect_group, highlights_dirs, ag_idx

    highlights = []
    aspect_idx = 0
    total_highlights = 0
    highlight_idx = 0

    # until highlight budget is not reached
    group_name = aspect_group[_aspect_name]
    while total_highlights < num_highlights and aspect_idx < len(aspect_lst):

        # tries to load highlight for the aspect
        s, *_ = aspect_lst[aspect_idx]
        highlight_file = join(
            highlights_dirs[ag_idx], group_name,
            '{}-{}-video{}.{}'.format(_aspect_name, s, highlight_idx,
                                      NUMPY_EXTENSION))
        if exists(highlight_file):
            highlights.append(np.load(highlight_file))
            total_highlights += 1
            highlight_idx += 1
            print_line('Loaded highlight from: {}'.format(highlight_file),
                       log_file)
        else:
            # advances aspect element
            aspect_idx += 1
            highlight_idx = 0
    return highlights
예제 #2
0
    def save_stats(self, path, clear=True, img_format='pdf'):
        super().save_stats(path, clear)

        # collects and prints final stats to file
        e = self.config.num_episodes
        with open(join(path, 'results.log'), 'w') as file:
            print_line('\nStats (avg. of {} episodes):'.format(e), file)
            var_names = list(self.stats_collector.all_variables())
            var_names.sort()
            self._print_stats(e, var_names, file)
예제 #3
0
 def _print_stats(self, e, selected_var_names, file=None):
     """
     Prints the current stats of a selected number of variables to the screen.
     :param int e: the number of the episode.
     :param list selected_var_names: a list of the names of the variables for which to print the statistics.
     :param stream file: the file on which to save the message line.
     :return:
     """
     for var_name in selected_var_names:
         avg, std = self.stats_collector.get_mean(var_name, e)
         print_line(
             'Avg {}: {:.2f} ± {:.2f}'.format(var_name.lower(), avg, std),
             file)
def _save_highlights_for_aspect(sub_dir, _aspect_name, aspect_lst,
                                num_highlights, agent_name):
    global log_file

    print_line('==============================================', log_file)
    print_line(
        'Collecting and saving highlights for aspect {} of \'{}\' in sub-dir: \'{}\''
        .format(_aspect_name, agent_name, sub_dir), log_file)

    # collect and saves highlights for this aspect
    highlights = _collect_aspect_highlights(_aspect_name, aspect_lst,
                                            num_highlights)
    _save_highlights_video(highlights, sub_dir, agent_name)
예제 #5
0
def print_variable_evo_comparison(_output_dir, _stats, _var_name):
    print_line('\n==============================================', log_file)
    print_line(
        'Processing comparison of evolution for \'{}\'...\n'.format(
            _var_name.lower()), log_file)

    # plots and creates data frame from trial data for each variable
    plt.figure()
    ag_names = list(_stats.keys())
    color_map = plt.cm.get_cmap(COLOR_SET_NAME)(np.arange(len(ag_names)))

    var_data = OrderedDict()
    for _ag_idx, _ag_name in enumerate(ag_names):
        means, stds, counts = _stats[_ag_name][_var_name]
        var_data['{} Mean'.format(_ag_name)] = means
        var_data['{} Std'.format(_ag_name)] = stds
        var_data['{} Count'.format(_ag_name)] = counts
        errs = stds / np.sqrt(counts)

        plt.plot(smooth_data(means), label=_ag_name, c=color_map[_ag_idx])
        plt.fill_between(np.arange(len(means)),
                         smooth_data(means - errs),
                         smooth_data(means + errs),
                         color=color_map[_ag_idx],
                         alpha=0.5)

    df = pd.DataFrame(var_data)
    df.to_csv(join(subdir, '{}-trial-data.csv'.format(_var_name.lower())))

    # gets y label adjust for base 10
    exp = get_y_label_adjust_base(df.max().max())
    y_label = _var_name.title()

    # configures axes and legend
    plt.legend(fontsize=16)
    plt.xlabel('Episode', fontweight='bold', fontsize=14)
    if abs(exp) >= 2:
        y_label = '{} $\\mathregular{{\\left(\\times10^{}\\right)}}$'.format(
            y_label, exp)
        plt.gca().yaxis.set_major_formatter(
            FuncFormatter(lambda x, _: '{}'.format(int(x / (10**exp)))))
    plt.ylabel(y_label, fontweight='bold', fontsize=14)

    plt.xlim(0, df.shape[0])

    # saves figure
    plt.savefig(join(_output_dir, 'evo {}.pdf'.format(_var_name.lower())),
                pad_inches=.02,
                bbox_inches='tight')
    plt.close()
예제 #6
0
 def _print_actions(title, diff_actions, table, file, write_console):
     print_line('\t\t{}:\t['.format(title), file, write_console, False)
     for a in range(len(table)):
         print_line(
             str(round(table[a], 3)).rjust(10), file, write_console, False)
         print_line('*' if a in diff_actions else ' ', file, write_console,
                    False)
     print_line(']\n', file, write_console, False)
def _save_highlights_for_aspects(sub_dir, _aspect_names, aspect_lsts,
                                 num_highlights, agent_name):
    global log_file

    print_line('==============================================', log_file)
    print_line(
        'Collecting and saving highlights for {} aspects of \'{}\' in sub-dir: \'{}\'...'
        .format(len(_aspect_names), agent_name, sub_dir), log_file)

    # collect and saves highlights for the different aspects
    highlights = []
    for _i in range(len(_aspect_names)):
        highlights.extend(
            _collect_aspect_highlights(_aspect_names[_i], aspect_lsts[_i],
                                       num_highlights[_i]))
    _save_highlights_video(highlights, sub_dir, agent_name)
def _save_highlights_video(highlights, sub_dir_name, _file_name):
    global log_file, output_dir

    sub_dir = join(output_dir, sub_dir_name)
    if not exists(sub_dir):
        makedirs(sub_dir)

    # tries to save highlight compact to video file
    if len(highlights) > 0:
        highlights_frames = []
        for frames in highlights:
            highlights_frames.extend(frames[frames.files[0]])
        video_file_path = join(
            sub_dir, '{}.{}'.format(_file_name.lower(), VIDEO_EXTENSION))
        record_video(highlights_frames, video_file_path, HIGHLIGHTS_FPS)
        print_line('Saved highlights compact in: {}'.format(video_file_path),
                   log_file)
def _save_sequence(sub_dir_name, sequence_lst, agent_name):
    global output_dir, log_file, ag_idx

    sub_dir = join(output_dir, sub_dir_name)
    if not exists(sub_dir):
        makedirs(sub_dir)

    print_line('==============================================', log_file)
    print_line(
        'Collecting and saving sequence for \'{}\' in sub-dir: \'{}\''.format(
            agent_name, sub_dir), log_file)

    # tries to get the longest recorded sequences
    for seq_idx, seq_info in enumerate(sequence_lst):
        seq_name, _ = SequencesExplainer.get_sequence_name(seq_idx, seq_info)
        seq_file_name = join(
            sequences_dirs[ag_idx],
            SequencesExplainer.get_sequence_file_name(seq_name, 0))

        # if file exists, copy to results dir
        if exists(seq_file_name):
            video_file_path = join(
                sub_dir, '{}.{}'.format(agent_name.lower(), VIDEO_EXTENSION))
            shutil.copyfile(seq_file_name, video_file_path)
            print_line(
                'Saved sequence by copying from: {}\n\tto: {}'.format(
                    seq_file_name, video_file_path), log_file)
            return
예제 #10
0
    def _save_report(self, file, write_console):

        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        print_line('====================================', file, write_console)
        print_line(
            '{} certain sequences to sub-goals found (min. prob > 0):'.format(
                len(self.certain_seqs_to_subgoal)), file, write_console)

        for s, n, seq, prob in self.certain_seqs_to_subgoal:
            self._print_sequence(s, n, seq, prob, feats_nbins, action_names,
                                 file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} states with an uncertain future found:'.format(
                len(self.uncertain_future_states)), file, write_console)

        for s, n in self.uncertain_future_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{} (count: {})'.format(feats_labels, n), file,
                       write_console)
예제 #11
0
def print_variable_mean_comparison(_output_dir, _stats, _var_name):
    print_line('\n==============================================', log_file)
    print_line(
        'Processing comparison of means for \'{}\'...\n'.format(
            _var_name.lower()), log_file)

    # reorganizes data by stats x agent
    ag_names = list(_stats.keys())
    var_data = np.zeros((3, len(_stats)))
    for _ag_idx, _ag_name in enumerate(ag_names):
        means, stds, counts = _stats[_ag_name][_var_name]
        mean, std, count = get_combined_mean(means, stds, counts)
        var_data[:, _ag_idx] = [mean, std, count]

    # convert variable stats to pandas data-frame
    df = pd.DataFrame(var_data,
                      index=['Mean', 'Std', 'Count'],
                      columns=ag_names)

    # saves to CSV
    file_name = _var_name.lower()
    df.to_csv(join(_output_dir, '{}.csv'.format(file_name)))

    # print to screen
    print_line(str(df), log_file)

    x_axis = np.arange(len(ag_names))
    means = [var_data[0][i] for i in x_axis]
    errors = [var_data[1][i] / np.sqrt(var_data[2][i]) for i in x_axis]
    # errors = [var_data[1][i] for i in x_axis]
    color_map = plt.cm.get_cmap(COLOR_SET_NAME)(x_axis)

    # gets y label adjust for base 10
    exp = get_y_label_adjust_base(max(means) + max(errors))
    y_label = 'Mean {}'.format(_var_name).title()

    # print bar chart of means to image file (may not work well for all variables)
    plt.bar(x_axis,
            means,
            yerr=errors,
            capsize=10,
            align='center',
            color=color_map)
    plt.xticks(x_axis, ag_names)
    plt.xlabel('Agent', fontweight='bold', fontsize=14)
    if abs(exp) >= 2:
        y_label = '{} $\\mathregular{{\\left(\\times10^{}\\right)}}$'.format(
            y_label, exp)
        plt.gca().yaxis.set_major_formatter(
            FuncFormatter(lambda x, _: '{}'.format(int(x / (10**exp)))))
    plt.ylabel(y_label, fontweight='bold', fontsize=14)
    plt.savefig(join(_output_dir, '{}.pdf'.format(file_name)),
                pad_inches=.02,
                bbox_inches='tight')
    plt.close()
예제 #12
0
 def _print_sequence(self, s, n, seq, prob, feats_nbins, action_names, file,
                     write_console):
     obs_vec = get_features_from_index(s, feats_nbins)
     feats_labels = self.helper.get_features_labels(obs_vec, True)
     print_line(
         '\t{}-{} (count: {}, prob. reaching target: {:.3e})'.format(
             s, feats_labels, n, prob), file, write_console)
     for a, ns in seq:
         obs_vec1 = get_features_from_index(ns, feats_nbins)
         feats_labels = self.helper.get_features_labels(obs_vec1, True)
         obs_vec_diff = self._vec_diff(obs_vec1, obs_vec)
         feats_labels = [feats_labels[i] for i in obs_vec_diff]
         obs_vec = obs_vec1
         print_line(
             '\t\t{} -> {}-{}'.format(action_names[a], ns, feats_labels),
             file, write_console)
     print_line('____________________________________', file, write_console)
예제 #13
0
    def _save_report(self, file, write_console):

        feats_nbins = self.helper.get_features_bins()

        print_line('====================================', file, write_console)
        print_line('Total states visited: {}'.format(self.total_count), file,
                   write_console)
        print_line(
            'Coverage of the state-space: {:.2f}%'.format(
                self.state_coverage * 100.), file, write_console)
        print_line(
            'Dispersion of the visits to the state-space: {:.3f}'.format(
                self.state_dispersion), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} frequent states found (min. support: {}):'.format(
                len(self.freq_states), self.min_state_count), file,
            write_console)

        for s, n in self.freq_states:
            rf = n / float(self.total_count)
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line(
                '\t{}-{} (count: {}, freq.: {:.3f})'.format(
                    s, feats_labels, n, rf), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} infrequent states found (max. support: {}):'.format(
                len(self.infreq_states), self.max_state_count), file,
            write_console)

        for s, n in self.infreq_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {})'.format(s, feats_labels, n), file,
                       write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} frequent feature-sets found (min. support: {}, min. assoc. strength: {}):'
            .format(len(self.freq_feature_sets), self.min_feat_set_count,
                    self.min_feat_set_assoc), file, write_console)

        num_feats = len(feats_nbins)
        for feat_set, jacc in self.freq_feature_sets:
            feats_labels = self._get_feats_labels(feat_set, num_feats)
            print_line('\t{} (jacc: {:.3f})'.format(feats_labels, jacc), file,
                       write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} interesting feature-rules found (min. confidence: {}):'.format(
                len(self.freq_feature_rules), self.min_feat_rule_conf), file,
            write_console)

        for ant, cons, n, conf in self.freq_feature_rules:
            n /= float(self.total_count)
            ant = self._get_feats_labels(ant, num_feats)
            cons = self._get_feats_labels(cons, num_feats)
            print_line(
                '\t{} => {} (freq: {:.3f}, conf: ({:.3f}))'.format(
                    ant, cons, n, conf), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} infrequent feature-sets found (max. assoc. strength: {}):'.
            format(len(self.infreq_feature_sets),
                   self.max_feat_set_assoc), file, write_console)

        for feat_set in self.infreq_feature_sets:
            feats_labels = self._get_feats_labels(feat_set, num_feats)
            print_line('\t{}'.format(feats_labels), file, write_console)
예제 #14
0
    def _save_report(self, file, write_console):
        print_line('\n===================================================================', file, write_console)
        print_line('TRANSITION ANALYSIS', file, write_console)
        self.trans_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('REWARD ANALYSIS', file, write_console)
        self.rwd_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('STATE FREQUENCY ANALYSIS', file, write_console)
        self.state_freq_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('STATE-ACTION FREQUENCY ANALYSIS', file, write_console)
        self.state_action_freq_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('VALUE ANALYSIS', file, write_console)
        self.value_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('RECENCY ANALYSIS', file, write_console)
        self.recency_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('TRANSITION VALUE ANALYSIS', file, write_console)
        self.trans_value_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('SEQUENCE ANALYSIS', file, write_console)
        self.sequence_analysis._save_report(file, write_console)

        print_line('\n===================================================================', file, write_console)
        print_line('CONTRADICTION ANALYSIS', file, write_console)
        self.contradiction_analysis._save_report(file, write_console)
예제 #15
0
    def _save_report(self, file, write_console):

        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        rwd_avg, rwd_std, n = self.avg_reward
        print_line('====================================', file, write_console)
        print_line(
            'Average overall reward: {:.3f} ± {:.3f} (count: {})'.format(
                rwd_avg, rwd_std, n), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} state-action outliers found (outlier threshold: {}):'.format(
                len(self.state_action_outliers), self.state_outlier_stds),
            file, write_console)

        for s, a, rwd, n in self.state_action_outliers:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec)
            print_line(
                '\t{}-{} - {} (mean reward: {:.3f}, count: {})'.format(
                    s, feats_labels, action_names[a], rwd, n), file,
                write_console)

        print_line('====================================', file, write_console)
        print_line('Actions\' average rewards:', file, write_console)

        for a in range(len(self.action_rwds_avg)):
            rwd_avg, rwd_std, n = self.action_rwds_avg[a]
            print_line(
                '\t{}: {:.3f} ± {:.3f} (count: {})'.format(
                    action_names[a], rwd_avg, rwd_std, n), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} feature-action outliers found (outlier threshold: {}):'.format(
                len(self.feature_action_outliers), self.feature_outlier_stds),
            file, write_console)

        for f, v, a, rwd_avg, rwd_std, n in self.feature_action_outliers:
            obs_vec = np.zeros(len(feats_nbins), np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} - {} (avg. reward: {:.3f} ± {:.3f}, count: {})'.format(
                    feat_label, action_names[a], rwd_avg, rwd_std, n), file,
                write_console)
예제 #16
0
    def _save_report(self, file, write_console):
        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        print_line(
            'Min. state-action support: {}'.format(self.min_state_count), file,
            write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} certain state transitions found (max. dispersion: {}):'.format(
                len(self.certain_trans), self.certain_trans_max_disp), file,
            write_console)

        for s, a, supp, disp in self.certain_trans:
            feats_labels = self.helper.get_features_labels(
                get_features_from_index(s, feats_nbins), True)
            print_line(
                '\t({}-{}, {}) (supp: {}, disp: {:.3f})'.format(
                    s, feats_labels, action_names[a], supp, disp), file,
                write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} uncertain state transitions found (min. dispersion: {}):'.
            format(len(self.uncertain_trans),
                   self.uncertain_trans_min_disp), file, write_console)

        for s, a, supp, disp in self.uncertain_trans:
            feats_labels = self.helper.get_features_labels(
                get_features_from_index(s, feats_nbins), True)
            print_line(
                '\t({}-{}, {}) (supp: {}, disp: {:.3f})'.format(
                    s, feats_labels, action_names[a], supp, disp), file,
                write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} certain actions found (max. dispersion: {}):'.format(
                len(self.certain_actions), self.certain_action_max_disp), file,
            write_console)

        for a, disp in self.certain_actions:
            print_line('\t{} (disp: {:.3f})'.format(action_names[a], disp),
                       file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} uncertain actions found (min. dispersion: {}):'.format(
                len(self.uncertain_actions), self.uncertain_action_min_disp),
            file, write_console)

        for a, disp in self.uncertain_actions:
            print_line('\t{} (disp: {:.3f})'.format(action_names[a], disp),
                       file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} certain state feature-action pairs found (max disp: {}):'.
            format(len(self.certain_feats),
                   self.certain_feat_max_disp), file, write_console)

        num_feats = len(feats_nbins)
        for f, v, a, disp in self.certain_feats:
            obs_vec = np.zeros(num_feats, np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} - {} (disp: {:.3f})'.format(feat_label, action_names[a],
                                                  disp), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} uncertain state feature-action pairs found (min. disp: {}):'.
            format(len(self.uncertain_feats),
                   self.uncertain_feat_min_disp), file, write_console)

        for f, v, a, disp in self.uncertain_feats:
            obs_vec = np.zeros(num_feats, np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} - {} (disp: {:.3f})'.format(feat_label, action_names[a],
                                                  disp), file, write_console)
예제 #17
0
    def _save_report(self, file, write_console):
        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        print_line('====================================', file, write_console)
        print_line('{} total time-steps'.format(self.total_time_steps), file,
                   write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} earlier states found (min. support: {}, max time-step: {}):'.
            format(len(self.earlier_states), self.min_state_count,
                   int(self.state_max_time_step * self.total_time_steps)),
            file, write_console)

        for s, t, n in self.earlier_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line(
                '\t{}-{} (count: {}, last time-step: {})'.format(
                    s, feats_labels, n, t), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} earlier state-action pairs found (min. support: {}, max time-step: {}):'
            .format(len(self.earlier_actions), self.min_state_count,
                    int(self.action_max_time_step * self.total_time_steps)),
            file, write_console)

        for s, a, t, n in self.earlier_actions:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line(
                '\t{}-{} - {} (count: {}, last time-step: {})'.format(
                    s, feats_labels, action_names[a], n, t), file,
                write_console)
예제 #18
0
    def _save_report(self, file, write_console):
        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        print_line('====================================', file, write_console)
        print_line(
            'Action JS divergence threshold: {} per action:'.format(
                self.action_dist_div_threshold / self.config.num_actions),
            file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} contradictory values states found):'.format(
                len(self.contradictory_value_states)), file, write_console)

        for s, n, jsd, diff_actions in self.contradictory_value_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            action_labels = [action_names[a] for a in diff_actions]
            print_line(
                '\t{}-{} (jsd: {:.3f}, count: {})'.format(
                    s, feats_labels, jsd, n), file, write_console)
            print_line('\t\tDivergent actions: {}'.format(action_labels), file,
                       write_console)
            self._print_actions('Rwd. dist', diff_actions,
                                self.state_rewards[s], file, write_console)
            self._print_actions('Val. dist', diff_actions,
                                self.state_values[s], file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} contradictory count states found):'.format(
                len(self.contradictory_count_states)), file, write_console)

        for s, n, jsd, diff_actions in self.contradictory_count_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            action_labels = [action_names[a] for a in diff_actions]
            print_line(
                '\t{}-{} (jsd: {:.3f}, count: {})'.format(
                    s, feats_labels, jsd, n), file, write_console)
            print_line('\t\tDivergent actions: {}'.format(action_labels), file,
                       write_console)
            self._print_actions('Val. dist', diff_actions,
                                self.state_values[s], file, write_console)
            self._print_actions('Count dist', diff_actions,
                                self.state_counts[s], file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} contradictory goal states found):'.format(
                len(self.contradictory_goal_states)), file, write_console)

        for s, n in self.contradictory_goal_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {})'.format(s, feats_labels, n), file,
                       write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} contradictory feature-action associations found):'.format(
                len(self.contradictory_feature_actions)), file, write_console)

        for f, v, a in self.contradictory_feature_actions:
            obs_vec = np.zeros(len(feats_nbins), np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line('\t{} - {}'.format(feat_label, action_names[a]), file,
                       write_console)
    def _save_report(self, file, write_console):
        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        print_line('====================================', file, write_console)
        print_line(
            'Coverage of the state-action space (for visited states): {:.2f}%'.
            format(self.state_action_coverage * 100.), file, write_console)

        mean, std, n = self.mean_action_dispersion
        print_line(
            'Mean dispersion of the execution of actions in visited states: {:.3f} ± {:.3f} (count: {})'
            .format(mean, std, n), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} certain states found (max. dispersion: {}):'.format(
                len(self.certain_states), self.certain_state_max_disp), file,
            write_console)

        for s, disp, max_actions in self.certain_states:
            max_action_labels = [action_names[a] for a in max_actions]
            feats_labels = self.helper.get_features_labels(
                get_features_from_index(s, feats_nbins), True)
            print_line(
                '\t{}-{} (mean disp.: {:.3f}, max actions {})'.format(
                    s, feats_labels, disp, max_action_labels), file,
                write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} uncertain states found (min. dispersion: {}):'.format(
                len(self.uncertain_states), self.uncertain_state_min_disp),
            file, write_console)

        for s, disp in self.uncertain_states:
            feats_labels = self.helper.get_features_labels(
                get_features_from_index(s, feats_nbins), True)
            print_line(
                '\t{}-{} (mean disp.: {:.3f})'.format(s, feats_labels, disp),
                file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} certain state features found (max. dispersion: {}):'.format(
                len(self.certain_feats), self.certain_feat_max_disp), file,
            write_console)

        num_feats = len(feats_nbins)
        for f, v, disp, max_actions in self.certain_feats:
            max_action_labels = [action_names[a] for a in max_actions]
            obs_vec = np.zeros(num_feats, np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} (mean disp.: {:.3f}, max actions: {})'.format(
                    feat_label, disp, max_action_labels), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} uncertain state features found (min. dispersion: {}):'.format(
                len(self.uncertain_feats), self.uncertain_feat_min_disp), file,
            write_console)

        for f, v, disp in self.uncertain_feats:
            obs_vec = np.zeros(num_feats, np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line('\t{} (mean disp.: {:.3f})'.format(feat_label, disp),
                       file, write_console)
예제 #20
0
    def _save_report(self, file, write_console):

        feats_nbins = self.helper.get_features_bins()
        action_names = self.config.get_action_names()

        mean, std, n = self.avg_value
        print_line('====================================', file, write_console)
        print_line(
            'Average overall value: {:.3f} ± {:.3f} (count: {})'.format(
                mean, std, n), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} state-action value outliers found (min. support: {}, outlier threshold: {}):'
            .format(len(self.mean_val_state_action_outliers),
                    self.min_state_count, self.state_value_outlier_stds), file,
            write_console)

        for s, actions, val, n in self.mean_val_state_action_outliers:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            act_labels = [action_names[a] for a in actions]
            print_line(
                '\t{}-{} (value: {:.3f}, count: {})\n\t\tMax actions: {}'.
                format(s, feats_labels, val, n,
                       act_labels), file, write_console)

        mean, std, n = self.avg_pred_error
        print_line('====================================', file, write_console)
        print_line(
            'Average overall prediction error: {:.3f} ± {:.3f} (count: {})'.
            format(mean, std, n), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} state-action prediction error outliers found (min. support: {}, outlier threshold: {}):'
            .format(len(self.pred_error_state_action_outliers),
                    self.min_state_count, self.pred_error_outlier_stds), file,
            write_console)

        for s, a, pred_error, n in self.pred_error_state_action_outliers:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line(
                '\t{}-{} - {} (avg. prediction error: {:.3f}, count: {})'.
                format(s, feats_labels, action_names[a], pred_error,
                       n), file, write_console)

        print_line('====================================', file, write_console)
        print_line('Actions\' average values:', file, write_console)

        for a, avg, std in self.action_vals_avg:
            print_line(
                '\t{}: {:.3f} ± {:.3f}'.format(action_names[a], avg, std),
                file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} feature value outliers found (outlier threshold: {}):'.format(
                len(self.mean_val_feature_outliers),
                self.feature_outlier_stds), file, write_console)

        for f, v, avg, std in self.mean_val_feature_outliers:
            obs_vec = np.zeros(len(feats_nbins), np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} (avg. value: {:.3f} ± {:.3f})'.format(
                    feat_label, avg, std), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} feature pred. error outliers found (outlier threshold: {}):'.
            format(len(self.pred_error_feature_outliers),
                   self.feature_outlier_stds), file, write_console)

        for f, v, a, avg, std in self.pred_error_feature_outliers:
            obs_vec = np.zeros(len(feats_nbins), np.uint32)
            obs_vec[f] = v
            feat_label = self.helper.get_features_labels(obs_vec)[f]
            print_line(
                '\t{} - {} (avg. pred. error: {:.3f} ± {:.3f})'.format(
                    feat_label, action_names[a], avg, std), file,
                write_console)
    def _save_report(self, file, write_console):

        feats_nbins = self.helper.get_features_bins()

        print_line('====================================', file, write_console)
        print_line('{} local minima states found (min. transition support: {}):'.format(
            len(self.local_minima_states), self.min_transition_count), file, write_console)

        for s, val, ns_val_avg, n in self.local_minima_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {}, value: {:.3f} <= avg next values: {:.3f})'.format(
                s, feats_labels, n, val, ns_val_avg), file, write_console)

        print_line('====================================', file, write_console)
        print_line('{} absolute minima states found (min. transition support: {}):'.format(
            len(self.absolute_minima_states), self.min_transition_count), file, write_console)

        for s, val, ns_val_avg, n in self.absolute_minima_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {}, value: {:.3f} <= avg next values: {:.3f})'.format(
                s, feats_labels, n, val, ns_val_avg), file, write_console)

        print_line('====================================', file, write_console)
        print_line('{} local maxima states found (min. transition support: {}):'.format(
            len(self.local_maxima_states), self.min_transition_count), file, write_console)

        for s, val, ns_val_avg, n in self.local_maxima_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {}, value: {:.3f} >= avg next values: {:.3f})'.format(
                s, feats_labels, n, val, ns_val_avg), file, write_console)

        print_line('====================================', file, write_console)
        print_line('{} absolute maxima states found (min. transition support: {}):'.format(
            len(self.absolute_maxima_states), self.min_transition_count), file, write_console)

        for s, val, ns_val_avg, n in self.absolute_maxima_states:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {}, value: {:.3f} >= avg next values: {:.3f})'.format(
                s, feats_labels, n, val, ns_val_avg), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} value difference mean state outliers found (min. transition support: {}, outlier stds: {}):'.format(
                len(self.val_diff_mean_action_outliers), self.min_transition_count, self.state_diff_outlier_stds),
            file, write_console)

        action_names = self.config.get_action_names()
        for s, a, diff, n in self.val_diff_mean_action_outliers:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} - {} (trans. count: {}, mean value diff.: {:.3f})'.format(
                s, feats_labels, action_names[a], n, diff), file, write_console)

        print_line('====================================', file, write_console)
        print_line(
            '{} value difference variance state-action outliers found (min. transition support: {}, outlier stds: {}):'.
                format(len(self.val_diff_variance_state_outliers), self.min_transition_count,
                       self.state_diff_outlier_stds), file, write_console)

        for s, diff, n in self.val_diff_variance_state_outliers:
            obs_vec = get_features_from_index(s, feats_nbins)
            feats_labels = self.helper.get_features_labels(obs_vec, True)
            print_line('\t{}-{} (count: {}, value diff. variance: {:.3f})'.format(
                s, feats_labels, n, diff), file, write_console)
예제 #22
0
    # creates log file
    log_file = open(join(output_dir, 'log.txt'), 'w')
    print()

    # ============================================================
    # PART 1 : collects performance stats across trials/episodes per agent
    subdir = join(output_dir, 'performance')
    clear_dir(subdir)

    stats = OrderedDict()
    var_names = list(helpers[0][0].stats_collector.all_variables())
    for ag_idx, ag_helpers in enumerate(helpers):

        ag_name = ag_names[ag_idx]
        print_line(
            'Collecting stats across {} trials of {} episodes for agent \'{}\'...'
            .format(len(ag_helpers), ag_helpers[0].config.num_episodes,
                    ag_name), log_file)

        # saves / copies configs to file
        ag_helpers[0].config.save_json(
            join(output_dir, 'config-{}.json'.format(ag_name.lower())))

        # collects stats for all trials
        stats[ag_name] = {}
        stats_collectors = [helper.stats_collector for helper in ag_helpers]
        for var_name in var_names:
            ag_stats = StatsCollector.get_mean_trials_stats(
                stats_collectors, var_name, ag_helpers[0].config.num_episodes)
            stats[ag_name][var_name] = ag_stats

    # prints comparison (csv, screen, bar-chart) between agents for the several variables