コード例 #1
0
class AnalyzeCompleteLapPercentage(GraphAnalyzer):
    def __init__(self, guru_parent_redraw,
                 matplotlib_canvas: FigureCanvasTkAgg,
                 control_frame: tk.Frame):

        super().__init__(guru_parent_redraw, matplotlib_canvas, control_frame)

        self._episode_control = EpisodeCheckButtonControl(
            guru_parent_redraw, control_frame, True)

    def build_control_frame(self, control_frame):
        self._episode_control.add_to_control_frame()

    def add_plots(self):
        if not self.all_episodes:
            return

        axes: Axes = self.graph_figure.add_subplot()

        # Plot data

        if self._episode_control.show_all():
            self._add_plot_for_episodes(axes, "All Episodes",
                                        self.all_episodes, "C1")

        if self.filtered_episodes and self._episode_control.show_filtered():
            self._add_plot_for_episodes(axes, "Filtered Episodes",
                                        self.filtered_episodes, "C2")

        if self._episode_control.show_evaluations():
            add_plot_for_evaluations(axes, "Evaluations",
                                     self.evaluation_phases, "C3")

        # Format the plot
        axes.set_title("Complete Lap Percentage")
        axes.set_xlabel("Training Iteration")

        if axes.has_data():
            axes.legend(frameon=True, framealpha=0.8, shadow=True)

    def _add_plot_for_episodes(self, axes: Axes, label, episodes, colour):
        (plot_x, plot_y) = get_plot_data_for_episodes(episodes)
        axes.plot(plot_x, plot_y, colour, label=label)
        self._plot_solo_items(axes, plot_x, plot_y, colour)
コード例 #2
0
class AnalyzeTrainingProgress(GraphAnalyzer):
    def __init__(self, guru_parent_redraw,
                 matplotlib_canvas: FigureCanvasTkAgg,
                 control_frame: tk.Frame):

        super().__init__(guru_parent_redraw, matplotlib_canvas, control_frame)

        self.episode_control = EpisodeCheckButtonControl(
            guru_parent_redraw, control_frame, True)
        self._stats_control = StatsControl(guru_parent_redraw, control_frame)
        self._scale_control = GraphScaleControl(guru_parent_redraw,
                                                control_frame)
        self._line_fitting_control = GraphLineFittingControl(
            guru_parent_redraw, control_frame)
        self._evaluation_pairs_control = EvaluationPairsControl(
            guru_parent_redraw, control_frame)

    def build_control_frame(self, control_frame):
        self.episode_control.add_to_control_frame()
        self._stats_control.add_to_control_frame()
        self._scale_control.add_to_control_frame()
        self._line_fitting_control.add_to_control_frame()
        self._evaluation_pairs_control.add_to_control_frame()

    def add_plots(self):
        if not self.all_episodes:
            return

        gs = GridSpec(1, 2)
        axes_left: Axes = self.graph_figure.add_subplot(gs[0, 0])
        axes_right: Axes = self.graph_figure.add_subplot(gs[0, 1])

        self.create_plot_iteration_vs_total_reward(axes_left)
        self.create_plot_iteration_vs_percent_complete(axes_right)

    def create_plot_iteration_vs_total_reward(self, axes):
        # Plot data

        if self.all_episodes and self.episode_control.show_all():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_total_reward(
                    axes, "All - Median", self.all_episodes, np.median, "C5")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_total_reward(
                    axes, "All - Mean", self.all_episodes, np.mean, "C6")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_total_reward(
                    axes, "All - Best", self.all_episodes, np.max, "C7")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_total_reward(
                    axes, "All - Worst", self.all_episodes, np.min, "C8")

        if self.filtered_episodes and self.episode_control.show_filtered():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_total_reward(
                    axes, "Filtered - Median", self.filtered_episodes,
                    np.median, "C1")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_total_reward(
                    axes, "Filtered - Mean", self.filtered_episodes, np.mean,
                    "C2")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_total_reward(
                    axes, "Filtered - Best", self.filtered_episodes, np.max,
                    "C3")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_total_reward(
                    axes, "Filtered - Worst", self.filtered_episodes, np.min,
                    "C4")

        if self.episode_control.show_evaluations():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_evaluation_total_reward(
                    axes, "Evaluations - Median", self.evaluation_phases,
                    np.median, "C9")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_evaluation_total_reward(
                    axes, "Evaluations - Mean", self.evaluation_phases,
                    np.mean, "C10")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_evaluation_total_reward(
                    axes, "Evaluations - Best", self.evaluation_phases, np.max,
                    "C11")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_evaluation_total_reward(
                    axes, "Evaluations - Worst", self.evaluation_phases,
                    np.min, "C12")

        # Format the plot
        axes.set_title("Total Reward")
        axes.set_xlabel("Training Iteration")

        if self.log_meta and self.is_fixed_scale():
            best = self.log_meta.episode_stats.best_reward
            worst = self.log_meta.episode_stats.worst_reward
            if best != worst:
                border = 0.02 * (best - worst)
                axes.set_ybound(worst - border, best + border)

        if axes.has_data():
            axes.legend(frameon=True, framealpha=0.8, shadow=True)

    def create_plot_iteration_vs_percent_complete(self, axes):

        # Plot data
        if self.episode_control.show_all():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "All - Median", self.all_episodes, np.median, "C5")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "All - Mean", self.all_episodes, np.mean, "C6")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "All - Best", self.all_episodes, np.max, "C7")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "All - Worst", self.all_episodes, np.min, "C8")

        if self.filtered_episodes and self.episode_control.show_filtered():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "Filtered - Median", self.filtered_episodes,
                    np.median, "C1")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "Filtered - Mean", self.filtered_episodes, np.mean,
                    "C2")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "Filtered - Best", self.filtered_episodes, np.max,
                    "C3")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_percent_complete(
                    axes, "Filtered - Worst", self.filtered_episodes, np.min,
                    "C4")

        if self.episode_control.show_evaluations():
            if self._stats_control.show_median():
                self.add_plot_iteration_vs_evaluation_percent_complete(
                    axes, "Evaluations - Median", self.evaluation_phases,
                    np.median, "C9")
            if self._stats_control.show_mean():
                self.add_plot_iteration_vs_evaluation_percent_complete(
                    axes, "Evaluations - Mean", self.evaluation_phases,
                    np.mean, "C10")
            if self._stats_control.show_best():
                self.add_plot_iteration_vs_evaluation_percent_complete(
                    axes, "Evaluations - Best", self.evaluation_phases, np.max,
                    "C11")
            if self._stats_control.show_worst():
                self.add_plot_iteration_vs_evaluation_percent_complete(
                    axes, "Evaluations - Worst", self.evaluation_phases,
                    np.min, "C12")

        # Format the plot
        axes.set_title("Track Completion")
        axes.set_xlabel("Training Iteration")
        axes.yaxis.set_major_formatter(PercentFormatter())

        if self.is_fixed_scale():
            axes.set_ybound(0, 105)

        if axes.has_data():
            axes.legend(frameon=True, framealpha=0.8, shadow=True)

    def is_fixed_scale(self):
        return self._scale_control.fixed_scale()

    def add_plot_iteration_vs_total_reward(self, axes: Axes, label, episodes,
                                           stat_method, colour):
        (plot_x, plot_y) = get_plot_data_iteration_vs_total_reward(
            episodes, stat_method)
        self.plot_data_or_smooth_it(axes, label, plot_x, plot_y, colour)

    def add_plot_iteration_vs_evaluation_total_reward(self, axes: Axes, label,
                                                      evaluation_phases,
                                                      stat_method, colour):
        (plot_x,
         plot_y) = self.get_plot_data_iteration_vs_evaluation_total_reward(
             evaluation_phases, stat_method)
        self.plot_data_or_smooth_it(axes, label, plot_x, plot_y, colour)

    def add_plot_iteration_vs_percent_complete(self, axes: Axes, label,
                                               episodes, stat_method, colour):
        (plot_x, plot_y) = get_plot_data_iteration_vs_percent_complete(
            episodes, stat_method)
        self.plot_data_or_smooth_it(axes, label, plot_x, plot_y, colour)

    def add_plot_iteration_vs_evaluation_percent_complete(
            self, axes: Axes, label, evaluation_phases, stat_method, colour):
        (plot_x,
         plot_y) = self.get_plot_data_iteration_vs_evaluation_percent_complete(
             evaluation_phases, stat_method)
        self.plot_data_or_smooth_it(axes, label, plot_x, plot_y, colour)

    def plot_data_or_smooth_it(self, axes: Axes, label: str,
                               plot_x: np.ndarray, plot_y: np.ndarray,
                               colour: str):
        if self._line_fitting_control.linear_fitting():
            (x_values, y_values, r) = get_linear_regression(plot_x, plot_y)
        elif self._line_fitting_control.quadratic_fitting():
            (x_values,
             y_values) = get_polynomial_quadratic_regression(plot_x, plot_y)
        else:
            x_values = plot_x
            y_values = plot_y

        axes.plot(x_values, y_values, colour, label=label)
        self._plot_solo_items(axes, x_values, y_values, colour)

    def get_plot_data_iteration_vs_evaluation_total_reward(
            self, evaluation_phases, stat_method):
        plot_iteration = []
        plot_data = []

        previous_eval = None
        for i, this_eval in enumerate(evaluation_phases):
            if self._evaluation_pairs_control.show_combined() and i % 2 == 1:
                plot_value = stat_method(
                    np.concatenate((this_eval.rewards, previous_eval.rewards)))
                plot_data.append(plot_value)
                plot_iteration.append(i - 0.5)
            elif ((self._evaluation_pairs_control.show_odd() and i % 2 == 1) or
                  (self._evaluation_pairs_control.show_even() and i % 2 == 0)
                  or self._evaluation_pairs_control.show_separate()):

                plot_data.append(stat_method(this_eval.rewards))
                plot_iteration.append(i)

            previous_eval = this_eval

        return np.array(plot_iteration), np.array(plot_data)

    def get_plot_data_iteration_vs_evaluation_percent_complete(
            self, evaluation_phases, stat_method):
        plot_iteration = []
        plot_data = []

        previous_eval = None
        for i, this_eval in enumerate(evaluation_phases):
            if self._evaluation_pairs_control.show_combined() and i % 2 == 1:
                plot_value = stat_method(
                    np.concatenate(
                        (this_eval.progresses, previous_eval.progresses)))
                plot_data.append(plot_value)
                plot_iteration.append(i - 0.5)
            elif ((self._evaluation_pairs_control.show_odd() and i % 2 == 1) or
                  (self._evaluation_pairs_control.show_even() and i % 2 == 0)
                  or self._evaluation_pairs_control.show_separate()):

                plot_data.append(stat_method(this_eval.progresses))
                plot_iteration.append(i)

            previous_eval = this_eval

        return np.array(plot_iteration), np.array(plot_data)