示例#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'])
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
示例#3
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