Commit 21c7d512 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

Only scenarios of last iteration used.

Options added for disabling output
parent c77ff897
......@@ -90,7 +90,7 @@ class MCMC:
def scenario_scores(patches, indicators):
return indicators.compute_indicators(patches)
def step(self, iter_nb, nb_particles, scenarios):
def step(self, iter_nb, nb_particles, scenarios, write_data=True, disable_progress=False):
'''
Sample new scenarios, evaluate their scores and retain only pareto front.
......@@ -99,28 +99,30 @@ class MCMC:
:param scenarios: (ScenariosStack) list of the scenarios used as base for sampling the new scenarios.
'''
# Loop of sampling and scoring
for index, scenario in tqdm(scenarios.sample(nb_particles, self.rng).iterrows(), total=nb_particles):
for index, scenario in tqdm(scenarios.sample(nb_particles, self.rng).iterrows(), total=nb_particles, disable=disable_progress):
scenario = scenarios.reallocate(index)
if scenario is None:
scenarios.setFullReallocated(index, True)
else:
scenarios.append(iter_nb, scenario, index)
scenarios.retain(scenarios.scores['iteration']==iter_nb)
# Computing pareto front
start_time = time.time()
pareto_mask = MCMC.is_pareto_efficient(scenarios.scores[['Resilience','Proximity','Productivity','Biodiversity','Social','TargetDelta']].values)
scenarios.scores['pareto'] = pareto_mask
elapsed_time = time.time() - start_time
print(' Fully reallocated scenarios {}/{} - '.format(scenarios.nbFullReallocated(), len(scenarios.scores)), end="", flush=True)
print('{:.2f}% on pareto front (computed in {}) - '.format(100*np.sum(pareto_mask)/len(pareto_mask), time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
if (len(scenarios.scores)>0):
print('{:.2f}% on pareto front (computed in {}) - '.format(100*np.sum(pareto_mask)/len(pareto_mask), time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
# Writing output data
write_times=[]
start_time = time.time()
scenarios.scores.to_csv(self.outputdir+'/scores_iter_{0:03d}.csv'.format(iter_nb), index=True)
write_times.append(time.time() - start_time)
start_time = time.time()
scenarios.cultgeopat.to_hdf(self.outputdir+'/cult_iter_{0:03d}.h5'.format(iter_nb), 'cultgeopat', index=True, complevel=1)
write_times.append(time.time() - start_time)
if self.mcmc_config.get('write_data', False):
if write_data:
write_times=[]
start_time = time.time()
scenarios.scores.to_csv(self.outputdir+'/scores_iter_{0:03d}.csv'.format(iter_nb), index=True)
write_times.append(time.time() - start_time)
start_time = time.time()
scenarios.cultgeopat.to_hdf(self.outputdir+'/cult_iter_{0:03d}.h5'.format(iter_nb), 'cultgeopat', index=True, complevel=1)
write_times.append(time.time() - start_time)
start_time = time.time()
try:
pairplot = sns.pairplot(scenarios.scores, vars=['Resilience','Proximity','Productivity','Biodiversity','Social','TargetDelta'],
......@@ -134,8 +136,6 @@ class MCMC:
pass
write_times.append(time.time() - start_time)
print('Scores written in {}/{}/{} '.format(*map(lambda x: time.strftime("%M:%S", time.gmtime(x)), write_times)), end="", flush=True)
else:
print('Scores written in {}/{} '.format(*map(lambda x: time.strftime("%M:%S", time.gmtime(x)), write_times)), end="", flush=True)
print()
# Retaining only optimal particules
scenarios.retain(pareto_mask)
......@@ -172,8 +172,7 @@ class MCMC:
for i in range(self.mcmc_config['max_iterations']):
nb_iteration=i+1
print('Iteration #{}'.format(nb_iteration))
# Write SHP only for the last iteration
scenarios = self.step(nb_iteration, nb_particles, scenarios)
scenarios = self.step(nb_iteration, nb_particles, scenarios, write_data=self.mcmc_config.get('write_data', False))
if scenarios.allFullReallocated():
print('All scenarios are fully reallocated. Simulation finished.')
break
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment