Last active
March 15, 2017 11:49
-
-
Save dfbarrero/70138ef2b76c51dade887eb4f7f06fa8 to your computer and use it in GitHub Desktop.
Support files for the practical "Understanding parameters settings in Evolutionary Algorithms". This code is based on examples taken from inpyred source code.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import Random | |
| from time import time | |
| import inspyred | |
| # Do not touch this value | |
| maxEvaluations=8000 | |
| # Customize these parameters | |
| popSize = X | |
| mutRate = X | |
| elitism = X | |
| tourSize = X | |
| xoverPoints = X | |
| def showStatistics(population, num_generations, num_evaluations, args): | |
| stats = inspyred.ec.analysis.fitness_statistics(population) | |
| print('Generation {0}, best fit {1}, avg. fit {2}'.format( | |
| num_generations, stats['best'], stats['mean'])) | |
| def main(prng=None, display=False): | |
| if prng is None: | |
| prng = Random() | |
| prng.seed(time()) | |
| problem = inspyred.benchmarks.Binary(inspyred.benchmarks.Schwefel(2), | |
| dimension_bits=30) | |
| ea = inspyred.ec.GA(prng) | |
| ea.terminator = inspyred.ec.terminators.evaluation_termination | |
| ea.observer = showStatistics | |
| ea.selector = inspyred.ec.selectors.tournament_selection | |
| final_pop = ea.evolve(generator=problem.generator, | |
| evaluator=problem.evaluator, | |
| pop_size=popSize, | |
| maximize=problem.maximize, | |
| bounder=problem.bounder, | |
| max_evaluations=maxEvaluations, | |
| num_elites=elitism, | |
| tournament_size=tourSize, | |
| mutation_rate=mutRate, | |
| num_crossover_points=xoverPoints) | |
| if display: | |
| best = max(final_pop) | |
| print('Best Solution: \n{0}'.format(str(best))) | |
| return ea | |
| if __name__ == '__main__': | |
| main(display=True) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from random import Random | |
| from time import time | |
| import inspyred | |
| chrLength = 15 # Chromosome length | |
| popSize = 50 # Population size | |
| maxGenerations = 15 # Max. generations | |
| mutRate = 0.1 # Mutation rate | |
| elite=0 # Elitism size | |
| def onemax_fitness(candidates, args): | |
| fitness = [] | |
| for cs in candidates: | |
| fit = sum(cs) | |
| fitness.append(fit) | |
| print("Best fit: {0}, avg. fit: {1}".format(max(fitness), | |
| float(sum(fitness))/len(fitness))) | |
| return fitness | |
| def generator(random, args): | |
| return [random.choice([0, 1]) for _ in range(chrLength)] | |
| prng = Random() | |
| prng.seed(time()) | |
| ea = inspyred.ec.GA(prng) | |
| ea.terminator = inspyred.ec.terminators.generation_termination | |
| final_pop = ea.evolve(generator=generator, | |
| evaluator=onemax_fitness, | |
| pop_size=popSize, | |
| num_elites=elite, | |
| max_generations=maxGenerations, | |
| mutation_rate=mutRate) | |
| best = max(final_pop) | |
| print('Best solution: \n{0}'.format(str(best))) | |
| if (best.fitness == chrLength): | |
| print("Solution found!") | |
| else: | |
| print("Solution NOT found") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Support files for the practical "Understanding parameters settings in Evolutionary Algorithms". This code is based on examples taken from inpyred source code. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #start_imports | |
| from random import Random | |
| from time import time | |
| from math import cos | |
| from math import pi | |
| from inspyred import ec | |
| from inspyred.ec import terminators | |
| #end_imports | |
| def generate_rastrigin(random, args): | |
| size = args.get('num_inputs', 10) | |
| return [random.uniform(-5.12, 5.12) for i in range(size)] | |
| def evaluate_rastrigin(candidates, args): | |
| fitness = [] | |
| for cs in candidates: | |
| fit = 10 * len(cs) + sum([((x - 1)**2 - 10 * cos(2 * pi * (x - 1))) for x in cs]) | |
| fitness.append(fit) | |
| return fitness | |
| #start_main | |
| rand = Random() | |
| rand.seed(int(time())) | |
| es = ec.ES(rand) | |
| es.terminator = terminators.evaluation_termination | |
| final_pop = es.evolve(generator=generate_rastrigin, | |
| evaluator=evaluate_rastrigin, | |
| pop_size=100, | |
| maximize=False, | |
| bounder=ec.Bounder(-5.12, 5.12), | |
| max_evaluations=20000, | |
| mutation_rate=0.25, | |
| num_inputs=3) | |
| # Sort and print the best individual, who will be at index 0. | |
| final_pop.sort(reverse=True) | |
| print(final_pop[0]) | |
| #end_main |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment