Commit 899059fd authored by Dumoulin Nicolas's avatar Dumoulin Nicolas
Browse files

fix in fully reallocated management and enhancement of messages

parent dddb4182
......@@ -101,12 +101,14 @@ class MCMC:
scenario = scenarios.reallocate(index, self.rng, self.patches, self.targetPAT, self.mcmc_config['ratio_patches_to_modify'])
if not scenario is None:
scenarios.append(iter_nb, scenario)
scenarios.setFullReallocated(index, True)
# 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('Pareto front computed ({:.2f}% keeped) in {} - '.format(100*np.sum(pareto_mask)/len(pareto_mask), time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
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)
# Writing output data
start_time = time.time()
scenarios.scores.to_csv(self.outputdir+'/scores_iter_{0:03d}.csv'.format(iter_nb), index=True)
......@@ -122,7 +124,7 @@ class MCMC:
# and an error is triggered. Here, we ignore this error.
pass
elapsed_time = time.time() - start_time
print('Scores of {} scenarios written in {} '.format(len(scenarios.scores), time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
print('Scores written in {} '.format(time.strftime("%M:%S", time.gmtime(elapsed_time))), end="", flush=True)
print()
# Retaining only optimal particules
scenarios.retain(pareto_mask)
......@@ -134,15 +136,18 @@ class MCMC:
start_time = time.time()
shp_dir = self.outputdir + '/patches_iter_{0:03d}'.format(iter_nb)
os.makedirs(shp_dir)
counter = 0
for index,scenario in tqdm(scenarios.cultgeopat.iterrows(), total=len(scenarios.cultgeopat)):
concat = pd.concat([self.patches, scenario.to_frame("newcult")], axis=1)
concat = concat[concat['init_cult']!=concat['newcult']]
if len(concat)>0:
for c in ['newcult','cultgeopat','init_cult']:
patutils.decode_cultgeopat(concat, c)
concat.to_file(shp_dir+'/{:03d}_{}'.format(iter_nb, index), encoding='utf-8')
if scenarios.scores.loc[index,'full_reallocated']:
counter += 1
concat = pd.concat([self.patches, scenario.to_frame("newcult")], axis=1)
concat = concat[concat['init_cult']!=concat['newcult']]
if len(concat)>0:
for c in ['newcult','cultgeopat','init_cult']:
patutils.decode_cultgeopat(concat, c)
concat.to_file(shp_dir+'/{:03d}_{}'.format(iter_nb, index), encoding='utf-8')
elapsed_time = time.time() - start_time
print(' Shape files written in {}'.format(time.strftime("%M:%S", time.gmtime(elapsed_time))))
print(' {} shapefiles (only scenario fully reallocated) written in {}'.format(counter, time.strftime("%M:%S", time.gmtime(elapsed_time))))
def run(self):
......
......@@ -39,7 +39,9 @@ class ScenariosStack:
self.cultgeopat = self.cultgeopat.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, False, False]], columns=self.indicators.indicators_names+['iteration','pareto','full_reallocated'], dtype='float64')
self.scores = pd.DataFrame([self.indicators.compute_indicators(initial_patches) + [0, False, False]],
columns=self.indicators.indicators_names+['iteration','pareto','full_reallocated'],
dtype='float64')
for c in ['pareto','full_reallocated']:
self.scores[c] = self.scores[c].astype('bool')
self.scores['iteration'] = self.scores['iteration'].astype('int')
......@@ -60,6 +62,9 @@ class ScenariosStack:
def isFullReallocated(self, id_scen):
return self.scores.loc[id_scen,'full_reallocated']
def nbFullReallocated(self):
return len(self.scores[self.scores['full_reallocated']==True])
def allFullReallocated(self):
return self.scores[self.scores['full_reallocated']==False].empty
......
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