/
Population.py
63 lines (51 loc) · 2.06 KB
/
Population.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from Individual import Individual
from FitnessCalc import FitnessCalc
from fpl import fpl
class Population():
def __init__(self, population_size, initialise):
self.individuals = []
#Creates the individuals
if (initialise):
for i in range(population_size):
new_individual = Individual()
self.individuals.append(new_individual)
# Determine the fitness of an individual
def get_fitness(self, individual_passed):
fitness = individual_passed.get_score()
if fitness == -1:
fitness = fpl.scoreteam(individual_passed.genes)
individual_passed.set_score(fitness)
return fitness
# Get the score of the fittest individual in a population
def fitness_of_the_fittest(self):
fitness_of_the_fittest = self.get_fitness(self.get_fittest())
return fitness_of_the_fittest
# Get the fittest individual in a population
def get_fittest(self):
fittest = self.individuals[0]
for i in range(len(self.individuals)):
if self.get_fitness(fittest) <= self.get_fitness(self.individuals[i]) :
fittest = self.individuals[i]
return fittest
# Get the average fitness of a population
def get_average_fitness(self):
totalFitness = 0
scores = []
for i in range(len(self.individuals)):
score = self.individuals[i].get_score()
totalFitness += score
scores.append(score)
return totalFitness / len(self.individuals)
# Get the size of a population
def size(self):
return len(self.individuals)
# Get a specific member of a population
def get_individual(self, index):
return self.individuals[index]
# Set a specific member of a population
def save_individual(self, index, individual_passed):
self.individuals[index] = individual_passed
# Output the fittest individual in a population
def OutputFittest(self):
fittest = self.get_fittest()
fpl.scoreteam(fittest.genes,True)