forked from arthursrr/GraphMinimalColoringBRKGA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
BRKGA.py
52 lines (44 loc) · 1.77 KB
/
BRKGA.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
from random import randint
import populacao as pp
class BRKGA:
taxaCruzamento = 0.8
taxaMutacao = 0.2
taxaSobrevivencia = 0.4
ELITISM = True
elite = []
n_VERT = 0
def Evoluir(self, Populacao, tamPopulacao, numVert, numColor, edges):
n_VERT = numVert
n_COLOR = numColor
novaPopulacao = pp.Population(tamPopulacao, True, numVert, numColor, edges)
sobrevive = 0
if (self.ELITISM):
elite = novaPopulacao.getEliteFromFitness(self.taxaSobrevivencia)
for i in range(len(elite)):
novaPopulacao.coloring[i] = elite[i]
sobrevive += 1
for i in range(sobrevive, len(Populacao.coloring)):
taxa = int(100*self.taxaCruzamento)
if randint(0, 100) < taxa:
positPai = randint(0, (len(elite)-1))
ind1 = elite[positPai]
ind2 = Populacao.getColoring(i)
filho = self.crossover(ind1, ind2)
novaPopulacao.saveColoring(filho)
for i in range(sobrevive, novaPopulacao.tamPopulacao):
taxa = int(100*self.taxaMutacao)
if randint(0, 100) < taxa:
self.mutacao(novaPopulacao.getColoring(i), n_COLOR)
return novaPopulacao
def mutacao(self, ind, n_COLOR):
for i in range(len(ind.chromosome)):
taxa = int(100*self.taxaMutacao)
if randint(0, 100) < taxa:
color = randint(0, n_COLOR-1)
ind.setColorChromosome(i, color)
def crossover(self, ind1, ind2):
filho = ind1
for i in range(len(ind1.chromosome)):
if i%2 == 0:
filho.setColorChromosome(i, ind2.chromosome[i])
return filho