Commit 96f78df6 authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

multiprocessing feature and order of indicators columns fixed

parent 617998bd
......@@ -15,6 +15,8 @@ from social import Social
from tqdm import tqdm
from patutils import md5sum, load_pat_patches
import seaborn as sns
import multiprocessing as mp
from functools import partial
class Scenario:
def __init__(self, patches):
......@@ -66,11 +68,11 @@ class CulturalIndicator(Indicator):
class Indicators:
def __init__(self, config, initial_patches, patches_md5sum, targetPAT):
self.indicators_names = ['Proximity', 'Resilience', 'Productivity', 'Biodiversity', 'Social']
self._indicators = {
indicator.lower(): eval(indicator)(config.get(indicator.lower()), initial_patches, patches_md5sum, targetPAT)
for indicator in ['Proximity', 'Resilience', 'Productivity', 'Biodiversity', 'Social']
indicator: eval(indicator)(config.get(indicator.lower()), initial_patches, patches_md5sum, targetPAT)
for indicator in self.indicators_names
}
self.indicators_names = list(self._indicators.keys())
for cultgeopat in targetPAT.index.tolist():
self._indicators[cultgeopat] = CulturalIndicator(cultgeopat)
self.indicators_names.append(cultgeopat)
......@@ -91,6 +93,7 @@ class MCMC:
print('Please copy the template file "MCMC_config.sample.yml" and adjust to your settings and run again this program')
sys.exit(1)
self.mcmc_config = yaml.load(open(mcmc_config_filename,'r'))
self.multiprocessing = self.mcmc_config.get('multiprocessing', False)
self.patches_md5sum = md5sum(self.mcmc_config['patches'])
if 'rng_seed' in self.mcmc_config:
self.rng = np.random.RandomState(self.mcmc_config['rng_seed'])
......@@ -157,6 +160,9 @@ class MCMC:
else:
return is_efficient
def scenario_scores(patches, indicators):
return indicators.compute_indicators(patches)
def step(self, iter_nb, nb_particles, scenarios, scores=None):
'''
Sample new scenarios, evaluate their scores and retain only pareto front.
......@@ -174,7 +180,10 @@ class MCMC:
scenario = Scenario(patches[0].copy())
scenario.reallocate(self.rng, self.targetPAT, self.mcmc_config['ratio_patches_to_modify']) # 3.8 ms
new_scenarios.append(scenario.patches)
new_scores.append(self.indicators.compute_indicators(scenario.patches))
if not self.multiprocessing:
new_scores.append(self.indicators.compute_indicators(scenario.patches))
if self.multiprocessing:
new_scores = self.pool.map(partial(MCMC.scenario_scores, indicators=self.indicators), new_scenarios)
# merging with precedent data
new_scores = pd.DataFrame(new_scores, columns=self.indicators.indicators_names)
new_scores['iteration'] = iter_nb
......@@ -185,12 +194,12 @@ class MCMC:
scores = scores.append(new_scores, ignore_index=True, sort=False)
scenarios = scenarios.append(pd.DataFrame(new_scenarios), ignore_index=True, sort=False)
# Computing pareto front
pareto_mask = MCMC.is_pareto_efficient(scores[['resilience','proximity','productivity','biodiversity','social']].values)
pareto_mask = MCMC.is_pareto_efficient(scores[['Resilience','Proximity','Productivity','Biodiversity','Social']].values)
scores['pareto'] = pareto_mask
# Writing output data
scores.to_csv(self.outputdir+'/mcmc_iter_{}.csv'.format(iter_nb), index=False)
try:
sns.pairplot(scores, vars=['resilience','proximity','productivity','biodiversity','social'],
sns.pairplot(scores, vars=['Resilience','Proximity','Productivity','Biodiversity','Social'],
diag_kind="kde",hue="pareto"
).savefig(self.outputdir+'/mcmc_iter_{}.png'.format(iter_nb))
except (np.linalg.linalg.LinAlgError, ValueError) as e:
......@@ -199,7 +208,8 @@ class MCMC:
pass
return [scenarios[pareto_mask], scores[pareto_mask]]
def run(self):
def run(self, nb_processes=mp.cpu_count()):
self.pool = mp.Pool(processes=nb_processes)
# Initial sampling and evaluation
nb_particles = self.mcmc_config['initial_nb_particles']
scenarios_init = pd.DataFrame([self.patches])
......@@ -212,6 +222,7 @@ class MCMC:
# Storing variation of indicators
init_var = scores.std()
# sequential optimization loop
self.pool.close()
return [scenarios,scores]
if __name__ == '__main__':
......
......@@ -2,6 +2,7 @@ patches: ../output/PAT_patches/PAT_patches.shp
target: ../resources/targetPAT.csv
output_dir: ../output/MCMC
initial_nb_particles: 10000
multiprocessing: False
ratio_patches_to_modify: 0.01
indicators_config:
proximite:
......
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