-
Notifications
You must be signed in to change notification settings - Fork 0
/
GeneticAlg.py
67 lines (56 loc) · 2.05 KB
/
GeneticAlg.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
63
64
65
66
67
from random import randint
from MyChromosome import MyChromosome
class GA:
def __init__(self, fitness_func, param=None):
self.__param = param
self.__fitness_function = fitness_func
self.__population = []
@property
def population(self):
return self.__population
def initialisation(self, network):
for _ in range(0, self.__param['popSize']):
c = MyChromosome()
c.init_representation(network)
self.__population.append(c)
def evaluation(self, network):
for crom in self.__population:
crom.fitness = self.__fitness_function(crom.representation, network)
def best_chromosome(self):
bestc = self.__population[0]
for crom in self.__population:
if crom.fitness < bestc.fitness:
bestc = crom
return bestc
def worstChromosome(self):
best = self.__population[0]
for c in self.__population:
if (c.fitness > best.fitness):
best = c
return best
def averageFitness(self):
avg = 0.0
for c in self.__population:
avg += c.fitness
return avg/self.__param['popSize']
def selection(self):
pos_1 = randint(0, self.__param['popSize'] - 1)
pos_2 = randint(0, self.__param['popSize'] - 1)
if self.__population[pos_1].fitness < self.__population[pos_2].fitness:
return pos_1
else:
return pos_2
def one_generation(self, network):
new_population = []
for _ in range(self.__param['popSize']//2):
crom_1 = self.__population[self.selection()]
crom_2 = self.__population[self.selection()]
off = crom_1.crossover(crom_2)
off1 = off[0]
off2 = off[1]
off1.mutation(self.__param['mutFactor'])
off2.mutation(self.__param['mutFactor'])
new_population.append(off1)
new_population.append(off2)
self.__population = new_population
self.evaluation(network)