/
GA.py
47 lines (35 loc) · 1.33 KB
/
GA.py
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
from FitnessCalc import FitnessCalc
from Population import Population
from Algorithm import Algorithm
from time import time
from fpl import fpl
# Import the player data from a file
fpl.getplayerdata()
# Time the algorithm
start = time()
# Set solution value higher than highest possible score.
# Look for the best possible answer
FitnessCalc.set_solution(1000)
# Create the initial random population
my_pop = Population(10000, True)
# Loop until number of generations is complete
generation_count = 0
while my_pop.fitness_of_the_fittest() < FitnessCalc.get_max_fitness():
generation_count += 1
# Output the best solution every 100 generations
if generation_count % 100 == 0:
print("Outputting Current Status")
my_pop.OutputFittest()
# Quit after a set number of generations
if generation_count == 500:
break
# Output current generation data and produce next generation
print("Average Fitness : %s" % my_pop.get_average_fitness())
print("Generation : %s Fittest : %s " % (generation_count, my_pop.fitness_of_the_fittest()))
my_pop = Algorithm.evolve_population(my_pop)
print("******************************************************")
# Output algorithm run time
finish = time()
print ("Time elapsed : %s " % (finish - start))
# Output the best solution found
my_pop.OutputFittest()