-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeneticAlgorithmSerial.py
More file actions
57 lines (43 loc) · 1.78 KB
/
geneticAlgorithmSerial.py
File metadata and controls
57 lines (43 loc) · 1.78 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import time
from utilityFunctions import City, Individual, Population, create_gene_pool, create_plot
def average_fitness(individuals):
"""
Calculate the average fitness score for a generation
"""
fitness_num = 0
for individual in individuals:
fitness = individual.get_fitness()
fitness_num += fitness
return fitness_num / len(individuals)
def main():
AMOUNT_OF_GENERATIONS = 50
SIZE_OF_POPULATION = 500
MUTATION_RATE = 0.2
start = time.monotonic()
# Create gene pool and beginning population
gene_pool = create_gene_pool()
population = Population(gene_pool=gene_pool, size_of_population=SIZE_OF_POPULATION)
generation = []
fitness_counts = []
generation_count = 0
# while population.size_of_population > 1:
while generation_count <= AMOUNT_OF_GENERATIONS:
# Get all the individuals in the population
generation.append(generation_count)
individuals = population.get_population()
# Calculate the average for the population in order to graph
fitness_counts.append(average_fitness(individuals))
#Create the next generation by mating the best half of the parent population and keeping the best parents
print('mating', generation_count)
next_generation = population.mate()
print('done')
# Create a new population as the next generation and apply mutation to them
population = Population(gene_pool=gene_pool, individuals=next_generation)
population.mutate(MUTATION_RATE)
# Increment generation
generation_count += 1
print(time.monotonic() - start)
# Plot the average fitness score per generation
create_plot(generation, fitness_counts)
if __name__ == '__main__':
main()