From 68ae32eff8416d13fdcf5b890976790934cc22b8 Mon Sep 17 00:00:00 2001 From: Giuseppe Chindemi Date: Mon, 9 May 2016 11:07:47 +0200 Subject: [PATCH] Added support for elitism --- bluepyopt/deapext/algorithms.py | 14 +++++++++++++- bluepyopt/deapext/optimisations.py | 9 +++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/bluepyopt/deapext/algorithms.py b/bluepyopt/deapext/algorithms.py index bd247a2..e4f5f3f 100644 --- a/bluepyopt/deapext/algorithms.py +++ b/bluepyopt/deapext/algorithms.py @@ -28,6 +28,7 @@ import deap.algorithms import deap.tools import pickle +import numpy logger = logging.getLogger('__main__') @@ -69,6 +70,14 @@ def _get_offspring(parents, toolbox, cxpb, mutpb): return deap.algorithms.varAnd(parents, toolbox, cxpb, mutpb) +def _get_elite(halloffame, nelite): + if nelite > 0 and halloffame is not None: + normsorted_idx = numpy.argsort([ind.fitness.norm for ind in halloffame]) + # Return nelite best individuals + return [halloffame[idx] for idx in normsorted_idx[:nelite]] + return list() + + def eaAlphaMuPlusLambdaCheckpoint( population, toolbox, @@ -78,6 +87,7 @@ def eaAlphaMuPlusLambdaCheckpoint( ngen, stats=None, halloffame=None, + nelite=0, cp_frequency=1, cp_filename=None, continue_cp=False): @@ -92,6 +102,8 @@ def eaAlphaMuPlusLambdaCheckpoint( ngen(int): Total number of generation to run stats(deap.tools.Statistics): generation of statistics halloffame(deap.tools.HallOfFame): hall of fame + nelite(int): Total number of individuals added to population at every + generation from hall of fame cp_frequency(int): generations between checkpoints cp_filename(string): path to checkpoint filename continue_cp(bool): whether to continue @@ -131,7 +143,7 @@ def eaAlphaMuPlusLambdaCheckpoint( _record_stats(stats, logbook, gen, population, invalid_count) # Select the next generation parents - parents = toolbox.select(population, mu) + parents = toolbox.select(population + _get_elite(halloffame, nelite), mu) logger.info(logbook.stream) diff --git a/bluepyopt/deapext/optimisations.py b/bluepyopt/deapext/optimisations.py index 3985ef6..f17625c 100644 --- a/bluepyopt/deapext/optimisations.py +++ b/bluepyopt/deapext/optimisations.py @@ -25,6 +25,7 @@ import random import logging import functools +import numpy import deap import deap.base @@ -62,6 +63,11 @@ def sum(self): """Weighted sum of values""" return sum(self.values) + @property + def norm(self): + """Frobenius norm of values""" + return numpy.linalg.norm(self.values) + def __le__(self, other): return self.weighted_sum <= other.weighted_sum @@ -96,6 +102,7 @@ def __init__(self, evaluator=None, use_scoop=False, seed=1, offspring_size=10, + elite_size=0, eta=10, mutpb=1.0, cxpb=1.0, @@ -107,6 +114,7 @@ def __init__(self, evaluator=None, self.use_scoop = use_scoop self.seed = seed self.offspring_size = offspring_size + self.elite_size = elite_size self.eta = eta self.cxpb = cxpb self.mutpb = mutpb @@ -254,6 +262,7 @@ def run(self, max_ngen, stats=stats, halloffame=hof, + nelite=self.elite_size, cp_frequency=cp_frequency, continue_cp=continue_cp, cp_filename=cp_filename)