Exemple #1
0
def play(folder, nb_eps, seed, save=False):
    """
    Replaying script.

    Parameters
    ----------
    folder: str
        path to result folder.
    nb_eps: int
        Number of episodes to be replayed.
    seed: int
    save: bool
        Whether to save figures.

    """

    algorithm, cost_function, env, params = setup_for_replay(folder, seed)

    goal = np.array([45000, 20])
    for i_ep in range(nb_eps):
        res, costs = algorithm.evaluate(n=1, best=False, goal=goal)#np.array([0.4]))#p.array([0.5, 1, 1]))
        # print('----------------')
        # for k in res.keys():
        #     print(k + ': {:.2f}'.format(res[k]))
        stats = env.unwrapped.get_data()

        labs = []
        for l in stats['model_states_labels']:
            if l == 'I':
                labs.append(l)
            else:
                labs.append(l + r' $\mathbf{(\times 10^4)}$')
        # plot model states
        plot_stats(t=stats['history']['env_timesteps'],
                   states=np.array(stats['history']['model_states']).transpose() / np.array([1e4,1e4,1,1e4,1e4,1e4]).reshape(-1, 1),
                   labels=labs,#stats['model_states_labels'],
                   lockdown=np.array(stats['history']['lockdown']),
                   icu_capacity=stats['icu_capacity'],
                   time_jump=stats['time_jump'])
        if save:
            plt.savefig(folder + 'plots/model_states_ep_{}_{}.pdf'.format(i_ep, goal))
            plt.close('all')
        plot_stats(t=stats['history']['env_timesteps'][1:],
                   states=stats['stats_run']['to_plot'],
                   labels=stats['stats_run']['labels'],
                   legends=stats['stats_run']['legends'],
                   title=stats['title'],
                   lockdown=np.array(stats['history']['lockdown']),
                   time_jump=stats['time_jump'],
                   show=False if save else i_ep == (nb_eps - 1)
                   )
        if save:
            plt.savefig(folder + 'plots/rl_states_ep_{}_{}.pdf'.format(i_ep, goal))
            plt.close('all')
                      simulation_horizon=simulation_horizon))  #params 이거 맞나?
    env.reset()

    actions = np.random.choice(
        [0, 1, 2, 3, 4, 5],
        size=53)  # need to change to have 6 actions 0, 1, 2, 3, 4, 5
    t = 0
    r = 0
    done = False
    while not done:
        out = env.step(actions[t])
        t += 1
        r += out[1]
        done = out[2]
    stats = env.unwrapped.get_data()

    # plot model states
    plot_stats(t=stats['history']['env_timesteps'],
               states=np.array(stats['history']['model_states']).transpose(),
               labels=stats['model_states_labels'],
               distancing=np.array(stats['history']['distancing']),
               time_jump=stats['time_jump'])
    plot_stats(t=stats['history']['env_timesteps'][1:],
               states=stats['stats_run']['to_plot'],
               labels=stats['stats_run']['labels'],
               legends=stats['stats_run']['legends'],
               title=stats['title'],
               distancing=np.array(stats['history']['distancing']),
               time_jump=stats['time_jump'],
               show=True)
        print(r)

    ax1 = None
    ax2 = None
    fig2 = None

    # STEP 6: Plot results
    for i in range(10):
        stats = all_stats[i]
        # plot model states
        labs = [l + r' $(\times 10^3)$' for l in stats['model_states_labels']]
        labs[2] = 'I'
        ax1, fig1 = plot_stats(t=stats['history']['env_timesteps'],
                               states=np.array(stats['history']['model_states']).transpose(),
                               labels= stats['model_states_labels'],
                               lockdown=np.array(stats['history']['lockdown']),
                               time_jump=stats['time_jump'],
                               icu_capacity=stats['icu_capacity'],
                               axs=ax1)
        plt.savefig('/home/flowers/Desktop/distrib.pdf')

        ax2, fig2 = plot_stats(t=stats['history']['env_timesteps'][1:],
                               states=stats['stats_run']['to_plot'],
                               labels=stats['stats_run']['labels'],
                               title=stats['title'],
                               lockdown=np.array(stats['history']['lockdown']),
                               time_jump=stats['time_jump'],
                               axs=ax2,
                               fig=fig2
                               )
    plt.show()
        print(r)

    ax1 = None
    ax2 = None
    fig2 = None

    # STEP 6: Plot results
    for i in range(10):
        stats = all_stats[i]
        # plot model states
        labs = [l + r' $(\times 10^3)$' for l in stats['model_states_labels']]
        labs[2] = 'I'
        ax1, fig1 = plot_stats(
            t=stats['history']['env_timesteps'],
            states=np.array(stats['history']['model_states']).transpose(),
            labels=stats['model_states_labels'],
            distancing=np.array(stats['history']['distancing']),
            time_jump=stats['time_jump'],
            axs=ax1)
        plt.savefig('/home/flowers/Desktop/distrib.pdf')

        ax2, fig2 = plot_stats(t=stats['history']['env_timesteps'][1:],
                               states=stats['stats_run']['to_plot'],
                               labels=stats['stats_run']['labels'],
                               title=stats['title'],
                               distancing=np.array(
                                   stats['history']['distancing']),
                               time_jump=stats['time_jump'],
                               axs=ax2,
                               fig=fig2)
    plt.show()
Exemple #5
0
                  model=model,
                  simulation_horizon=simulation_horizon)
    env.reset()

    actions = np.random.choice([0, 1], size=53)
    t = 0
    r = 0
    done = False
    while not done:
        out = env.step(actions[t])
        t += 1
        r += out[1]
        done = out[2]
    stats = env.unwrapped.get_data()

    # plot model states
    plot_stats(t=stats['history']['env_timesteps'],
               states=np.array(stats['history']['model_states']).transpose(),
               labels=stats['model_states_labels'],
               lockdown=np.array(stats['history']['lockdown']),
               icu_capacity=stats['icu_capacity'],
               time_jump=stats['time_jump'])
    plot_stats(t=stats['history']['env_timesteps'][1:],
               states=stats['stats_run']['to_plot'],
               labels=stats['stats_run']['labels'],
               legends=stats['stats_run']['legends'],
               title=stats['title'],
               lockdown=np.array(stats['history']['lockdown']),
               time_jump=stats['time_jump'],
               show=True)