示例#1
0
def test_cumulative_cost_plotter_label():
    plotter1 = ph.CumulativeCostPlotter(num_simulation_steps=10,
                                        time_interval=1.0,
                                        plot_frequency=2,
                                        discounted=True)
    assert plotter1.y_label == 'Cumulative Disc Cost'
    assert [*plotter1.line_labels] == ['Total Cost']

    plotter2 = ph.CumulativeCostPlotter(num_simulation_steps=10,
                                        time_interval=1.0,
                                        plot_frequency=2,
                                        discounted=False)
    assert plotter2.y_label == 'Cumulative Cost'
    assert [*plotter2.line_labels] == ['Total Cost']
示例#2
0
def test_cumulative_cost_plotter_discounted_small_plot_frequency():
    plotter = ph.CumulativeCostPlotter(num_simulation_steps=10,
                                       time_interval=1.0,
                                       plot_frequency=2,
                                       discounted=True)

    cost = [np.array([1]), np.array([10]), np.array([20])]
    data_dict = {'cost': cost}
    reporter_cache = {'discount_factor': [np.array([[0.5]])]}
    step = 2

    for s in range(step):
        plotter.handle(data_dict=data_dict,
                       reporter_cache=reporter_cache,
                       step=s)

    cumul = plotter.get_new_line_data(data_dict=data_dict,
                                      reporter_cache=reporter_cache,
                                      step=step)
    assert np.all(cumul == np.array([1, 6, 11]))
示例#3
0
def get_handlers(server_mode: bool,
                 num_sim_steps: int,
                 plot_freq: int,
                 time_interval: int,
                 is_hedgehog: bool,
                 is_routing: bool,
                 reproduce_mode: bool = False) -> List[Handler]:
    """
    Get the handlers used to validate the examples.

    :param server_mode: when experiment runs locally, this flag controls whether to show live plots
        and wait for input before closing them at the end of the simulation.
    :param num_sim_steps: the number of steps the simulation runs.
    :param plot_freq: number of timesteps after which the plots are updated.
    :param time_interval: the time interval of the environment.
    :param is_hedgehog: whether the use hedgehog specific handlers (eg for workload).
    :param is_routing: whether the environment is a routing model.
    :param reproduce_mode: flag indicating whether we are live plotting or reproducing from already
        stored data.

    :return: handlers: List of reporting handlers.
    """
    if server_mode:
        return []

    handlers = []  # type: List[Handler]
    compact_panel = False
    if compact_panel:
        rows = 3 if is_hedgehog else 2
        plots = 6 if is_hedgehog else 4

        fig = plt.figure(figsize=(12, 4 * rows))
        axs = [
            fig.add_subplot('{}2{}'.format(rows, i + 1)) for i in range(plots)
        ]

        handlers.extend([
            hand.StateCostPlotter(num_sim_steps,
                                  time_interval,
                                  plot_freq,
                                  ax=axs[0],
                                  do_plot_cost=False,
                                  reproduce_mode=reproduce_mode),
            hand.StateCostPlotter(num_sim_steps,
                                  time_interval,
                                  plot_freq,
                                  ax=axs[1],
                                  do_plot_state=False,
                                  reproduce_mode=reproduce_mode),
            hand.CumulativeCostPlotter(num_sim_steps,
                                       time_interval,
                                       plot_freq,
                                       ax=axs[3],
                                       discounted=False,
                                       reproduce_mode=reproduce_mode),
        ])
        if is_hedgehog:
            handlers.extend([
                hand.HedgingThresholdPlotter(num_sim_steps,
                                             time_interval,
                                             plot_freq,
                                             ax=axs[2],
                                             reproduce_mode=reproduce_mode),
                hand.EffectiveCostErrorPlotter(num_sim_steps,
                                               time_interval,
                                               plot_freq,
                                               ax=axs[5],
                                               eff_cost_err_method='absolute',
                                               reproduce_mode=reproduce_mode),
                hand.IdlingPlotter(num_sim_steps,
                                   time_interval,
                                   plot_freq,
                                   ax=axs[4],
                                   reproduce_mode=reproduce_mode),
            ])
    else:
        rows = 3 if is_hedgehog else 2
        plots = 9 if is_hedgehog else 4
        plot_per_row = 3 if is_hedgehog else 2
        width = 17 if is_hedgehog else 12

        fig = plt.figure(figsize=(width, 3 * rows))
        axs = [
            fig.add_subplot(f'{rows}{plot_per_row}{i + 1}')
            for i in range(plots)
        ]

        handlers.extend([
            hand.StateCostPlotter(num_sim_steps,
                                  time_interval,
                                  plot_freq,
                                  ax=axs[0],
                                  do_plot_cost=False,
                                  reproduce_mode=reproduce_mode),
            hand.StateCostPlotter(num_sim_steps,
                                  time_interval,
                                  plot_freq,
                                  ax=axs[1],
                                  do_plot_state=False,
                                  reproduce_mode=reproduce_mode),
            hand.CumulativeCostPlotter(num_sim_steps,
                                       time_interval,
                                       plot_freq,
                                       ax=axs[2],
                                       discounted=False,
                                       reproduce_mode=reproduce_mode),
            hand.ArrivalsPlotter(num_sim_steps,
                                 time_interval,
                                 plot_freq,
                                 ax=axs[3],
                                 reproduce_mode=reproduce_mode)
        ])
        if is_hedgehog:
            handlers.extend([
                hand.WorkloadPlotter(num_sim_steps,
                                     time_interval,
                                     plot_freq,
                                     ax=axs[4],
                                     plot_fluid_model=False,
                                     plot_hedging=False,
                                     reproduce_mode=reproduce_mode),
                hand.HedgingThresholdPlotter(num_sim_steps,
                                             time_interval,
                                             plot_freq,
                                             ax=axs[5],
                                             reproduce_mode=reproduce_mode),
                hand.EffectiveCostErrorPlotter(num_sim_steps,
                                               time_interval,
                                               plot_freq,
                                               ax=axs[6],
                                               eff_cost_err_method='absolute',
                                               reproduce_mode=reproduce_mode),
                hand.IdlingPlotter(num_sim_steps,
                                   time_interval,
                                   plot_freq,
                                   ax=axs[7],
                                   reproduce_mode=reproduce_mode),
                hand.ActionsToFluidPlotter(num_sim_steps,
                                           time_interval,
                                           plot_freq,
                                           ax=axs[8])
            ])
            if not reproduce_mode:
                handlers.append(PrintStateHedging(plot_freq))
                if not is_routing:
                    handlers.append(PrintInverseLoadings(plot_freq))
                handlers.append(PrintActualtoFluidRatio(plot_freq))

    return handlers