示例#1
0
def run_calibration(n_rounds, current_period=0, verbose=0):
    if not os.path.isdir('../calibrations'):
        os.makedirs('../calibrations')
    memory_error = False
    map = Map()
    best_score = None
    for i in range(n_rounds):
        if i%10 == 0:
            print(f'round {i}...')
        evaluations = []
        array_params, pdict = build_parameters(current_period, verbose)
        try:
            map.from_arrays(**array_params)
        except:
            print('Memory error')
            memory_error = True
            pass
        if memory_error:
            memory_error = False
            continue

        for prd in range(N_PERIODS):
            for _ in range(pdict['n_moves_per_period']):
                map.make_move(prop_cont_factor=pdict['prop_cont_factor'])
            map.forward_all_cells()
            state_ids, state_numbers = map.get_states_numbers()
            # Check if we are already in the good range at mid time
            evaluation = (state_ids, state_numbers)
            if prd == 6:
                ind_asymptomatic = np.where(evaluation[0] == 1)[0][0].astype(np.uint32)
                n_asymptomatic = evaluation[1][ind_asymptomatic]
            evaluations.append(evaluation)

        score, progressions = evaluate_move(evaluations)

        if best_score is None or score < best_score:
            fpath = os.path.join(CALIBRATION_DIR, f'move_3e_{i}.npy')
            to_save = {'score': score, 'params': array_params, 'pdict': pdict}
            print(f'New best score found: {score}, saved under {fpath}')
            print(f'Corresponding progressions: {progressions}')
            print(f'corresponding params:')
            pprint(pdict)
            best_score = score
            np.save(fpath, to_save)
示例#2
0
# agents move now `f_unmove`x less than before lockdown
f_unmove = 1
p_moves = np.load(os.path.join(map_path, 'p_moves.npy')).flatten()
n_p_moves = p_moves.shape[0]
p_move = pdict['avg_p_move'] / f_unmove
p_moves = get_p_moves(n_p_moves, p_move)
print(f'DEBUG: mean of p_move: {np.mean(p_moves)}')

res = {}
map = Map()
map.load(map_path)
# map.set_verbose(3)

map.set_p_moves(p_moves)
map.set_unsafeties(unsafeties)

new_hosps = []
for i in range(N_PERIODS):
    res[i] = {}
    for _ in range(n_moves_per_period):
        map.make_move(p_mask=.3)
    new_states = map.forward_all_cells(tracing_rate=0)
    new_hosp = new_states[new_states == 4].shape[0]
    new_hosps.append(new_hosp)
    state_ids, state_numbers = map.get_states_numbers()
    for j in range(state_ids.shape[0]):
        res[i][state_ids[j]] = state_numbers[j]

pprint(res)
print(new_hosps)
示例#3
0
new_state_id = 1

print(
    f'Injecting {N_INFECTED_AGENTS_START} contaminated agents out of {N_AGENTS} in map'
)

map.change_state_agents(np.array([infected_agent_id]),
                        np.array([new_state_id]))

stats = {}
t_start = time()

for i in range(N_PERIODS):
    print(f'starting period {i}...')
    t0 = time()
    for j in range(N_MOVES_PER_PERIOD):
        t_ = time()
        map.make_move()
    map.forward_all_cells()
    states_ids, state_numbers = map.get_states_numbers()
    stats[i] = {
        states_ids[k]: state_numbers[k]
        for k in range(len(states_ids))
    }
    if 5 in stats:
        print()
    print(f'period {i} computed in {time() - t0}s')

print(f'duration: {time() - t_start}s')
print(stats)