Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Renaming of parameters #65

Merged
merged 2 commits into from
Mar 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions bin/l2l-fun-ga.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,11 @@ def main():
optimizee = FunctionGeneratorOptimizee(traj, benchmark_function, seed=optimizee_seed)

## Outerloop optimizer initialization
parameters = GeneticAlgorithmParameters(seed=0, popsize=50, CXPB=0.5,
MUTPB=0.3, NGEN=100, indpb=0.02,
tournsize=15, matepar=0.5,
mutpar=1
parameters = GeneticAlgorithmParameters(seed=0, pop_size=50, cx_prob=0.5,
mut_prob=0.3, n_iteration=100,
ind_prob=0.02,
tourn_size=15, mate_par=0.5,
mut_par=1
)

optimizer = GeneticAlgorithmOptimizer(traj, optimizee_create_individual=optimizee.create_individual,
Expand Down
41 changes: 21 additions & 20 deletions l2l/optimizers/evolution/optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,18 @@
logger = logging.getLogger("l2l-ga")

GeneticAlgorithmParameters = namedtuple('GeneticAlgorithmParameters',
['seed', 'popsize', 'CXPB', 'MUTPB', 'NGEN', 'indpb', 'tournsize', 'matepar',
'mutpar'])
['seed', 'pop_size', 'cx_prob', 'mut_prob', 'n_iteration', 'ind_prob', 'tourn_size', 'mate_par',
'mut_par'])
GeneticAlgorithmParameters.__doc__ = """
:param seed: Random seed
:param popsize: Size of the population
:param CXPB: Crossover probability
:param MUTPB: Mutation probability
:param NGEN: Number of generations simulation should run for
:param indpb: Probability of mutation of each element in individual
:param tournsize: Size of the tournamaent used for fitness evaluation and selection
:param matepar: Paramter used for blending two values during mating
:param pop_size: Size of the population
:param cx_prob: Crossover probability
:param mut_prob: Mutation probability
:param n_iteration: Number of generations simulation should run for
:param ind_prob: Probability of mutation of each element in individual
:param tourn_size: Size of the tournamaent used for fitness evaluation and selection
:param mate_par: Parameter used for blending two values during mating
:param mut_par: Standard deviation for the gaussian addition mutation.
"""


Expand Down Expand Up @@ -53,13 +54,13 @@ def __init__(self, traj,
__, self.optimizee_individual_dict_spec = dict_to_list(optimizee_create_individual(), get_dict_spec=True)

traj.f_add_parameter('seed', parameters.seed, comment='Seed for RNG')
traj.f_add_parameter('popsize', parameters.popsize, comment='Population size') # 185
traj.f_add_parameter('CXPB', parameters.CXPB, comment='Crossover term')
traj.f_add_parameter('MUTPB', parameters.MUTPB, comment='Mutation probability')
traj.f_add_parameter('n_iteration', parameters.NGEN, comment='Number of generations')
traj.f_add_parameter('pop_size', parameters.pop_size, comment='Population size') # 185
traj.f_add_parameter('cx_prob', parameters.cx_prob, comment='Crossover term')
traj.f_add_parameter('mut_prob', parameters.mut_prob, comment='Mutation probability')
traj.f_add_parameter('n_iteration', parameters.n_iteration, comment='Number of generations')

traj.f_add_parameter('indpb', parameters.indpb, comment='Mutation parameter')
traj.f_add_parameter('tournsize', parameters.tournsize, comment='Selection parameter')
traj.f_add_parameter('ind_prob', parameters.ind_prob, comment='Mutation parameter')
traj.f_add_parameter('tourn_size', parameters.tourn_size, comment='Selection parameter')

# ------- Create and register functions with DEAP ------- #
# delay_rate, slope, std_err, max_fraction_active
Expand Down Expand Up @@ -92,15 +93,15 @@ def bounding_wrapper(*args, **kwargs):

return bounding_wrapper

toolbox.register("mate", tools.cxBlend, alpha=parameters.matepar)
toolbox.register("mate", tools.cxBlend, alpha=parameters.mate_par)
toolbox.decorate("mate", bounding_decorator)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=parameters.mutpar, indpb=traj.indpb)
toolbox.register("mutate", tools.mutGaussian, mu=0, sigma=parameters.mut_par, indpb=traj.ind_prob)
toolbox.decorate("mutate", bounding_decorator)
toolbox.register("select", tools.selTournament, tournsize=traj.tournsize)
toolbox.register("select", tools.selTournament, tournsize=traj.tourn_size)

# ------- Initialize Population and Trajectory -------- #
# NOTE: The Individual object implements the list interface.
self.pop = toolbox.population(n=traj.popsize)
self.pop = toolbox.population(n=traj.pop_size)
self.eval_pop_inds = [ind for ind in self.pop if not ind.fitness.valid]
self.eval_pop = [list_to_dict(ind, self.optimizee_individual_dict_spec)
for ind in self.eval_pop_inds]
Expand All @@ -116,7 +117,7 @@ def post_process(self, traj, fitnesses_results):
"""
See :meth:`~l2l.optimizers.optimizer.Optimizer.post_process`
"""
CXPB, MUTPB, NGEN = traj.CXPB, traj.MUTPB, traj.n_iteration
CXPB, MUTPB, NGEN = traj.cx_prob, traj.mut_prob, traj.n_iteration

logger.info(" Evaluating %i individuals" % len(fitnesses_results))

Expand Down
8 changes: 4 additions & 4 deletions l2l/tests/test_ga_optimizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ class GAOptimizerTestCase(OptimizerTestCase):

def test_setup(self):

optimizer_parameters = GeneticAlgorithmParameters(seed=0, popsize=1, CXPB=0.5,
MUTPB=0.3, NGEN=1, indpb=0.02,
tournsize=1, matepar=0.5,
mutpar=1
optimizer_parameters = GeneticAlgorithmParameters(seed=0, pop_size=1, cx_prob=0.5,
mut_prob=0.3, n_iteration=1, ind_prob=0.02,
tourn_size=1, mate_par=0.5,
mut_par=1
)

optimizer = GeneticAlgorithmOptimizer(self.trajectory, optimizee_create_individual=self.optimizee.create_individual,
Expand Down