improvements on EA

This commit is contained in:
Jonas Weinz 2017-06-25 19:33:08 +02:00
parent c62e9fc2d8
commit 69e7be0501
3 changed files with 50 additions and 3 deletions

View File

@ -0,0 +1,28 @@
# Evolutionary Algorithm Simulator
## run the program:
```
usage: main.py [-h] [--offspringSize OFFSPRINGSIZE] [--P P] [--L L] [--f F]
[--epsilon EPSILON] [--cycles CYCLES]
evolutionary algorithm simulation
optional arguments:
-h, --help show this help message and exit
--offspringSize OFFSPRINGSIZE
size for new offspring
--P P start population size
--L L genome length
--f F fitness function in python syntax. x[0] - x[L] are the arguments
--epsilon EPSILON epsilon for random mutation
--cycles CYCLES cycles to calculate
```
arguments can also be adjusted by the user at runtime. Example for a 2 dimensional fitness function:
```
./main.py --f "-abs(x[0] - 6) - abs(x[1] +5)" --L 2 --cycles 1000
```

View File

@ -55,7 +55,7 @@ class EvolutionaryPopulation(object):
externalSelectionFunction=lambda fitness: list(range(len(fitness))), # keep whole population externalSelectionFunction=lambda fitness: list(range(len(fitness))), # keep whole population
parentSelectionFunction=lambda population, fitness: list(range(len(population))) # all individuals are parents parentSelectionFunction=lambda population, fitness: list(range(len(population))) # all individuals are parents
): ):
self.L = 3 self.L = L
self.fitnessFunction = fitnessFunction self.fitnessFunction = fitnessFunction
self.inheritanceFunction = inheritanceFunction self.inheritanceFunction = inheritanceFunction
self.mutationFunction = mutationFunction self.mutationFunction = mutationFunction
@ -97,6 +97,7 @@ class EvolutionaryPopulation(object):
newIndividual = Individual(self.fitnessFunction, self.mutationFunction, self.inheritanceFunction, parents) newIndividual = Individual(self.fitnessFunction, self.mutationFunction, self.inheritanceFunction, parents)
newIndividual.mutate() newIndividual.mutate()
self.population.append(newIndividual) self.population.append(newIndividual)
# update fitness:
self.fitness.append(newIndividual.evaluateFitness()) self.fitness.append(newIndividual.evaluateFitness())
def printPopulation(self): def printPopulation(self):

View File

@ -51,13 +51,22 @@ def parsingArguments():
# easy adjustable functions for the ea-cycle. Will be packed in lambda objects in main() # easy adjustable functions for the ea-cycle. Will be packed in lambda objects in main()
def inheritance(parents): def inheritance(parents):
'''
:param parents: list of Individuals. Their genome can be accessed by parents[i].genome
:return: genome for new offspring individual
'''
# just copy genome from first parent: # just copy genome from first parent:
return parents[0].genome return parents[0].genome
def mutation(genome, e): def mutation(genome, e):
'''
:param genome: list of length L of real values: the genome to mutate
:param e: epsilon value (for random range)
:return: the mutated genome
'''
# mutate new genome by equally distributed random value in range [-e:e] # mutate new genome by equally distributed random value in range [-e:e]
newGenome = [] newGenome = []
@ -68,12 +77,21 @@ def mutation(genome, e):
return newGenome return newGenome
def externalSelection(fitness): def externalSelection(fitness):
'''
:param fitness: list with fitness values
:return: list of indices of surviving individuals
'''
# only keep the fittest # only keep the fittest
return [np.argmax(fitness)] return [np.argmax(fitness)]
def parentSelection(population, fitness): def parentSelection(population, fitness):
'''
parent selection method for one individual
:param population: list of individuals which survived external selection
:param fitness: list of fitness values for given population
:return: list of parents for one new offspring individual
'''
# only first (and only individual so far) is parent # only first (and only individual so far) is parent
return [0] return [0]