Commit 467a8e7a authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

New storage during iteration (faster) + new config for different nb of...

New storage during iteration (faster) + new config for different nb of particles during init (first step) and following iteration
The scenario are stored in raw array and then in a new dataframe
parent d7304e86
......@@ -99,13 +99,19 @@ class MCMC:
:param scenarios: (ScenariosStack) list of the scenarios used as base for sampling the new scenarios.
'''
# Loop of sampling and scoring
data={'scenarios_patches':[], 'scenarios_cult':[], 'previous_indexes':[]}
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)
data['scenarios_patches'].append(scenario)
data['scenarios_cult'].append(scenario['cultgeopat'].to_list())
data['previous_indexes'].append(index)
start_time = time.time()
scenarios.reconstitute(iter_nb, **data)
elapsed_time = time.time() - start_time
print('scenarios evaluated and stored in {} - '.format(time.strftime("%M:%S", time.gmtime(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','TargetDelta']].values)
......@@ -113,7 +119,7 @@ class MCMC:
elapsed_time = time.time() - start_time
print(' Fully reallocated scenarios {}/{} - '.format(scenarios.nbFullReallocated(), len(scenarios.scores)), 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)
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
if write_data:
write_times=[]
......@@ -135,7 +141,7 @@ class MCMC:
# and an error is triggered. Here, we ignore this error.
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)
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)
......@@ -163,8 +169,6 @@ class MCMC:
def run(self):
# Initial sampling and evaluation
nb_particles = self.mcmc_config['initial_nb_particles']
# Start with initial scenario
scenarios = ScenariosStack(self.indicators, self.reallocator, self.patches)
# Iteration
......@@ -172,6 +176,10 @@ class MCMC:
for i in range(self.mcmc_config['max_iterations']):
nb_iteration=i+1
print('Iteration #{}'.format(nb_iteration))
if nb_iteration==1:
nb_particles = self.mcmc_config['initial_nb_particles']
else:
nb_particles = self.mcmc_config['step_nb_particles']
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.')
......
......@@ -3,7 +3,8 @@ target: ../resources/targetPAT.csv
output_dir: ../output/MCMC
write_data: True
initial_nb_particles: 1000
max_iterations: 10
step_nb_particles: 1000
max_iterations: 20
multiprocessing: False
ratio_patches_to_modify: 0.01
indicators_config:
......
......@@ -4,6 +4,7 @@
import pandas as pd
import itertools
from patutils import load_pat_patches
from tqdm import tqdm
class Reallocator:
def __init__(self, rng, patches, targetPAT, ratioNbPatches):
......@@ -44,12 +45,15 @@ class ScenariosStack:
self.indicators = indicators
self.reallocator = reallocator
self.initial_patches = initial_patches
self.cultgeopat = initial_patches[['cultgeopat']]
self.cultgeopat = self.cultgeopat.T
self.cultgeopat = initial_patches['cultgeopat'].to_frame().T
# Now we have the id_parcel as columns, and each row will represent a scenario
self.cultgeopat.rename(index={'cultgeopat':scenario_id}, inplace=True)
self.scores = pd.DataFrame([self.indicators.compute_indicators(initial_patches) + [0, 0, -1, False, False]],
self.initscores([self.indicators.compute_indicators(initial_patches) + [0, 0, -1, False, False]], [scenario_id])
def initscores(self, scores, indexes):
self.scores = pd.DataFrame(scores,
columns=self.indicators.indicators_names+['iteration','cumulated_it', 'previous_index', 'pareto','full_reallocated'],
index=indexes,
dtype='float64')
for c in ['pareto','full_reallocated']:
self.scores[c] = self.scores[c].astype('bool')
......@@ -58,15 +62,26 @@ class ScenariosStack:
def append(self, nb_iter, patches_cult, previous_index):
id = next(self._counter)
self.cultgeopat.loc[id] = patches_cult['cultgeopat'].values
newdf = patches_cult['cultgeopat'].to_frame().T
newdf.rename(index={'cultgeopat':id}, inplace=True)
self.cultgeopat = pd.concat([self.cultgeopat,newdf])
self.scores.loc[id] = self.indicators.compute_indicators(patches_cult) + [nb_iter, self.scores.loc[previous_index]['cumulated_it']+1, previous_index, False, False]
for c in ['iteration','cumulated_it', 'previous_index']:
self.scores[c] = self.scores[c].astype('int')
return id
def reconstitute(self, iter_nb, scenarios_patches, scenarios_cult, previous_indexes, disable_progress=False):
indexes = [next(self._counter) for id in range(len(scenarios_cult))]
self.cultgeopat = pd.DataFrame(scenarios_cult, index=indexes, columns=self.cultgeopat.columns)
list_scores = []
for i, previous_index in tqdm(enumerate(previous_indexes), total=len(previous_indexes), disable=disable_progress):
scenario_patch = scenarios_patches[i]
list_scores.append(self.indicators.compute_indicators(scenario_patch) + [iter_nb, self.scores.loc[previous_index]['cumulated_it']+1, previous_index, False, False])
self.initscores(list_scores, indexes)
def retain(self, mask):
self.cultgeopat = self.cultgeopat[mask | (self.scores['full_reallocated'])]
self.scores = self.scores[mask | (self.scores['full_reallocated'])]
self.cultgeopat = self.cultgeopat[mask | (self.scores['full_reallocated'].to_list())]
self.scores = self.scores[mask | (self.scores['full_reallocated'].to_list())]
def setFullReallocated(self, id_scen, full_reallocated):
self.scores.loc[id_scen,'full_reallocated'] = full_reallocated
......
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