Commit 60ed93dc authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

Bugfix in RNG distribution

parent 777fbbfc
......@@ -28,7 +28,7 @@ class MCMC:
dirname += suffix
return dirname
def __init__(self, mcmc_config_filename, output=True, suffix='', outputDirName=None):
def __init__(self, mcmc_config_filename, indicator, output=True, suffix='', outputDirName=None):
if not os.path.isfile(mcmc_config_filename):
print('Error: file not found "{}"'.format(mcmc_config_filename))
print('Please copy the template file "MCMC_config.sample.yml" and adjust to your settings and run again this program')
......@@ -107,9 +107,8 @@ class MCMC:
def getRNGState(self, iter_nb, scen_nb):
result = 1 # First RNG is the default one for local sampling
if iter_nb==1:
result += scen_nb
else:
result += scen_nb
if iter_nb > 1:
result += self.mcmc_config['initial_nb_particles'] + (iter_nb - 2) * self.mcmc_config['step_nb_particles']
return result
......@@ -130,26 +129,29 @@ class MCMC:
start_time = time.time()
if num_cpus>1:
# multiprocessing
loop_data = [[self.getRNGState(iter_nb, loop_index), scen_id] for loop_index,scen_id in enumerate(scenarios.sample(nb_particles, self.rng).index)]
import signal
mp.set_start_method('fork', force=True)
self.mp_pool = mp.Pool(self.mcmc_config['num_cpus'],
# for catching correctly Ctrl-C keyboard interruption
initializer=lambda:signal.signal(signal.SIGINT, signal.SIG_IGN))
samples_indexes = scenarios.sample(nb_particles, self.rng).index
loop_data = [[self.getRNGState(iter_nb, loop_index), scen_id] for loop_index,scen_id in enumerate(samples_indexes)]
results = list(tqdm(self.mp_pool.imap(partial(MCMC.reallocate_mp), loop_data, chunksize=10), total=len(loop_data)))
self.mp_pool.terminate()
self.mp_pool.join()
# Consolidate data
scenarios.reconstitute_mp(iter_nb, results, disable_progress=disable_progress)
else:
# Sequential
data={'scenarios_patches':[], 'scenarios_cult':[], 'previous_indexes':[], 'fullreallocated':[]}
for loop_index, [index, scenario] in tqdm(enumerate(scenarios.sample(nb_particles, self.rng).iterrows()), total=nb_particles, disable=disable_progress):
rng = np.random.RandomState(self.getRNGState(iter_nb, loop_index))
[fullreallocated,scenario] = scenarios.reallocate(index, rng)
data['fullreallocated'].append(fullreallocated)
data['scenarios_patches'].append(scenario)
data['scenarios_cult'].append(scenario['cultgeopat'])
data['previous_indexes'].append(index)
scenarios.reconstitute(iter_nb, **data, disable_progress=disable_progress)
loop_data = [[self.getRNGState(iter_nb, loop_index), scen_id] for loop_index,scen_id in enumerate(scenarios.sample(nb_particles, self.rng).index)]
results = [MCMC.reallocate_mp(d) for d in tqdm(loop_data)]
# Consolidate data
scenarios.reconstitute_mp(iter_nb, results, disable_progress=disable_progress)
elapsed_time = time.time() - start_time
print('Iteration duration: {} - {:.2f} scen/s '.format(time.strftime("%M:%S", time.gmtime(elapsed_time)), nb_particles/elapsed_time), end="", flush=True)
# Computing pareto front
start_time = time.time()
pareto_mask = MCMC.is_pareto_efficient(scenarios.scores[['Resilience','Proximity','Productivity','Biodiversity','Social']].values)
pareto_mask = MCMC.is_pareto_efficient(scenarios.scores[self.indicators.indicators_names].values)
scenarios.scores['pareto'] = pareto_mask
elapsed_time = time.time() - start_time
print(' Fully reallocated scenarios {}/{} - '.format(scenarios.nbFullReallocated(), nb_particles), end="", flush=True)
......@@ -166,7 +168,7 @@ class MCMC:
write_times.append(time.time() - start_time)
start_time = time.time()
try:
pairplot = sns.pairplot(scenarios.scores, vars=['Resilience','Proximity','Productivity','Biodiversity','Social','TargetDelta'],
pairplot = sns.pairplot(scenarios.scores, vars=self.indicators.indicators_names+['TargetDelta'],
diag_kind="kde",hue="pareto"
)
pairplot.savefig(self.outputdir+'/mcmc_iter_{0:03d}.png'.format(iter_nb))
......@@ -180,19 +182,12 @@ class MCMC:
print()
# Retaining only optimal particules
scenarios.retain(pareto_mask)
return scenarios
def run(self):
# Start with initial scenario
global scenarios
scenarios = ScenariosStack(self.indicators, self.reallocator, self.patches)
num_cpus = self.mcmc_config.get('num_cpus', 1)
if num_cpus > 1:
import signal
mp.set_start_method('fork', force=True)
self.mp_pool = mp.Pool(self.mcmc_config['num_cpus'],
# for catching correctly Ctrl-C keyboard interruption
initializer=lambda:signal.signal(signal.SIGINT, signal.SIG_IGN))
# Iteration
nb_iteration=0
for i in range(self.mcmc_config['max_iterations']):
......@@ -206,7 +201,6 @@ class MCMC:
if scenarios.allFullReallocated():
print('All scenarios are fully reallocated. Simulation finished.')
break
return scenarios
if __name__ == '__main__':
suffix=''
......@@ -218,6 +212,11 @@ if __name__ == '__main__':
outputdir = MCMC.buildOutputDirName(config, suffix)
mcmc = MCMC('MCMC_config.yml', suffix=suffix, outputDirName=outputdir)
scenarios = mcmc.run()
# for indicator in patutils.indicators:
# outputdir = MCMC.buildOutputDirName(config, suffix+'_'+indicator)
# mcmc = MCMC('MCMC_config.yml', indicator=indicator, suffix=suffix+'_'+indicator, outputDirName=outputdir)
# mcmc.run()
except KeyboardInterrupt:
if mcmc and hasattr(mcmc, 'mp_pool'):
mcmc.mp_pool.terminate()
......
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