Exemplo n.º 1
0
def train(config, expe_name, trial_id, beta_default):
    """
    Main training script. Defines the cost function and epidemiological model to form the environment model.

    Parameters
    ----------
    config: str
        Identifier of the configuration parameters.
    expe_name: str
        String to describe your experiment, will be used for the logging directory.
    trial_id: int
        Trial identifier for logging path.
    beta_default: float
        Additional parameter for our experiments.

    """
    # Get the configuration
    params = get_params(config_id=config, expe_name=expe_name)
    params.update(trial_id=trial_id)
    params['cost_params']['beta_default'] = beta_default

    # Set seeds
    set_seeds(params['seed'])

    # Get the epidemiological model
    model = get_model(model_id=params['model_id'],
                      params=params['model_params'])

    # Update cost function params
    params['cost_params']['N_region'] = int(
        model.pop_sizes[params['model_params']['region']])
    params['cost_params']['N_country'] = int(
        sum(list(model.pop_sizes.values())))

    # Get cost function
    cost_function = get_cost_function(cost_function_id=params['cost_id'],
                                      params=params['cost_params'])

    # Create the optimization problem as a Gym-like environment
    env = get_env(env_id=params['env_id'],
                  cost_function=cost_function,
                  model=model,
                  simulation_horizon=params['simulation_horizon'],
                  seed=params['seed'])

    # Setup logdir
    params = get_logdir(params=params)

    # Get DQN algorithm parameterized by beta
    algorithm = get_algorithm(algo_id=params['algo_id'],
                              env=env,
                              params=params)

    # Run the training loop
    algorithm.learn(num_train_steps=params['num_train_steps'])
Exemplo n.º 2
0
def setup_for_replay(folder,
                     seed=np.random.randint(1e6),
                     deterministic_model=False):
    from epidemioptim.environments.models import get_model
    from epidemioptim.environments.cost_functions import get_cost_function
    from epidemioptim.environments.gym_envs import get_env
    from epidemioptim.optimization import get_algorithm

    # print('Replaying: ', folder)
    with open(folder + 'params.json', 'r') as f:
        params = json.load(f)

    if deterministic_model:
        params['model_params']['stochastic'] = False
    params[
        'logdir'] = None  #get_repo_path() + 'data/results/experiments' + params['logdir'].split('EpidemicDiscrete-v0')[1]
    model = get_model(model_id=params['model_id'],
                      params=params['model_params'])

    # update reward params
    params['cost_params']['N_region'] = int(
        model.pop_sizes[params['model_params']['region']])
    params['cost_params']['N_country'] = int(
        np.sum(list(model.pop_sizes.values())))

    set_seeds(seed)

    cost_function = get_cost_function(cost_function_id=params['cost_id'],
                                      params=params['cost_params'])

    # Form the Gym-like environment
    env = get_env(env_id=params['env_id'],
                  cost_function=cost_function,
                  model=model,
                  simulation_horizon=params['simulation_horizon'],
                  seed=seed)

    # Get DQN algorithm parameterized by beta
    algorithm = get_algorithm(algo_id=params['algo_id'],
                              env=env,
                              params=params)

    if params['algo_id'] == 'NSGAII':
        algorithm.load_model(folder + 'res_eval.pk')
    else:
        algorithm.load_model(folder + 'models/best_model.cp')

    return algorithm, cost_function, env, params
Exemplo n.º 3
0
    #model = get_model(model_id='prague_seirah', params=dict(region=region, stochastic=stochastic))

    model = get_model(model_id='sqeir', params=dict(stochastic=stochastic))

    #N_region = model.pop_sizes[region]
    #N_country = np.sum(list(model.pop_sizes.values()))
    #ratio_death_to_R = 0.005
    ratio_death_to_R = 0.02

    # cost_function = get_cost_function(cost_function_id='multi_cost_death_gdp_controllable', params=dict(N_region=N_region,
    #                                                                                       N_country=N_country,
    #                                                                                       ratio_death_to_R=ratio_death_to_R)
    #                                   )

    cost_function = get_cost_function(
        cost_function_id='korea_multi_cost_death_gdp_controllable',
        params=dict(ratio_death_to_R=ratio_death_to_R))

    env = get_env(env_id='KoreaEpidemicDiscrete-v0',
                  params=dict(
                      cost_function=cost_function,
                      model=model,
                      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
Exemplo n.º 4
0
from epidemioptim.environments.gym_envs import get_env
if __name__ == '__main__':

    simulation_horizon = 364
    stochastic = True
    region = 'IDF'

    model = get_model(model_id='prague_seirah', params=dict(region=region,
                                                      stochastic=stochastic))

    N_region = model.pop_sizes[region]
    N_country = np.sum(list(model.pop_sizes.values()))
    ratio_death_to_R = 0.005

    cost_function = get_cost_function(cost_function_id='multi_cost_death_gdp_controllable', params=dict(N_region=N_region,
                                                                                                        N_country=N_country,
                                                                                                        ratio_death_to_R=ratio_death_to_R)
                                      )

    env = get_env(env_id='EpidemicDiscrete-v0',
                  cost_function=cost_function,
                  model=model,
                  simulation_horizon=simulation_horizon)


    all_stats = []
    for i_loop in range(10):
        env.reset()

        # STEP 4: Initialize the scripted sequence of actions
        # This is useful only here for simulation, learning algorithm will replace this.
        # * use a action_type corresponding to your env (on/off, sticky_one_week, switch or commit)
Exemplo n.º 5
0
def setup_for_replay(folder,
                     seed=np.random.randint(1e6),
                     deterministic_model=False):
    from epidemioptim.environments.models import get_model
    from epidemioptim.environments.cost_functions import get_cost_function
    from epidemioptim.environments.gym_envs import get_env
    from epidemioptim.optimization import get_algorithm

    print('Replaying: ', folder)
    with open(folder + 'params.json', 'r') as f:
        params = json.load(f)

    if deterministic_model:
        params['model_params']['stochastic'] = False
    params[
        'logdir'] = None  #get_repo_path() + 'data/results/experiments' + params['logdir'].split('EpidemicDiscrete-v0')[1]
    model = get_model(model_id=params['model_id'],
                      params=params['model_params'])

    set_seeds(seed)

    cost_function = get_cost_function(cost_function_id=params['cost_id'],
                                      params=params['cost_params'])

    # Form the Gym-like environment
    env = get_env(env_id=params['env_id'],
                  cost_function=cost_function,
                  model=model,
                  simulation_horizon=params['simulation_horizon'],
                  seed=seed)

    # Get DQN algorithm parameterized by beta
    algorithm = get_algorithm(algo_id=params['algo_id'],
                              env=env,
                              params=params)

    # if params['algo_id'] == 'NGSA':
    #     algorithm.load_model(folder + 'res_eval.pk')
    # else:
    algorithm.load_model(folder + 'models/best_model.cp')

    return algorithm, cost_function, env, params


# Compute the real SIDEP incidence from a CSV file
# def get_incidence():
#     PATH_TO_DATA = get_repo_path() + '/data/jane_model_data/inci.csv'
#     data = pd.read_csv(PATH_TO_DATA, delimiter=";")
#     true_data = pd.DataFrame(data['jour'])
#     true_data['T']=data['P']
#     week_grouped = true_data.groupby(['jour'])['T'].sum()/2

#     data = week_grouped.values.tolist()
#     #pre = np.zeros(132).tolist()
#     #post = np.zeros(214).tolist()
#     #pre.extend(data)
#     #pre.extend(post)
#     yhat = scipy.signal.savgol_filter(data, 53, 3)
#     # for i in range(455, 731):
#     #     yhat[i] = 0
#     return yhat
Exemplo n.º 6
0
        data['stats_run'] = stats_run
        data['title'] = 'Death Cost: {}, Aggregated Cost: {:.2f}'.format(
            int(cumulative_death[-1]),
            np.sum(self.history['aggregated_costs']))
        return data


if __name__ == '__main__':
    from epidemioptim.utils import plot_stats, plot_preds
    from epidemioptim.environments.cost_functions import get_cost_function
    from epidemioptim.environments.models import get_model

    simulation_horizon = 360
    model = get_model(model_id='heffernan_model',
                      params=dict(stochastic=False))
    cost_func = get_cost_function(cost_function_id='one_cost_death_toll',
                                  params=dict(ratio_death_to_R=0.0001))
    env = gym.make('EpidemicVaccination-v0',
                   cost_function=cost_func,
                   model=model,
                   simulation_horizon=simulation_horizon)
    env.reset()

    model_states = []
    env.model.current_state, env.model.current_internal_params = env.initialize_model_for_vaccine(
    )
    # Actions
    actions = ([0, 0, 1], [0, 0, 1], [0, 1, 1], [0, 1, 1], [0, 0, 1],
               [0, 1, 1], [0, 1, 1], [0, 1, 1], [0, 1, 1], [0, 1,
                                                            1], [0, 1,
                                                                 1], [0, 1, 1])
    #actions = ([0,0,1], [0,0,1], [0,0,1], [0,1,1],[1,0,1], [1,1,1], [1,1,1], [1,1,1], [1,1,1], [0,1,1], [1,1,1], [1,1,0])