forked from arthursrr/GraphMinimalColoringBRKGA
-
Notifications
You must be signed in to change notification settings - Fork 1
/
individuo.py
72 lines (62 loc) · 2.55 KB
/
individuo.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
68
69
70
71
72
from random import randint
from random import shuffle
class Individuo:
def __init__(self, numVert, numColor):
self.numVert = numVert
self.chromosome = [-99999 for x in range(numVert)]
self.maxColors = numColor
self.fitness = 0
self.colors = 0
def setChromosome(self, cromossomo):
self.chromosome = cromossomo
def setColorChromosome(self, index, cor):
self.chromosome[index] = cor
self.fitness = 0
self.colors = 0
def generateIndividual(self):
for i in range(self.numVert):
self.setColorChromosome(i, randint(0, self.maxColors))
chromosome = self.chromosome
shuffle(chromosome)
self.setChromosome(chromosome)
def getColorChromosome(self, index):
return self.chromosome[index]
def getColors(self):
tempColors = []
length = len(self.chromosome)
tempColors.append(self.getColorChromosome(0))
for i in range(length):
flag = True
for j in range(len(tempColors)):
if tempColors[j] == self.getColorChromosome(i):
flag = False
break
if flag:
tempColors.append(self.getColorChromosome(i))
self.colors = len(tempColors)
return self.colors
def getFitness(self):
if self.fitness == 0:
self.fitness = self.getColors()
return self.fitness
def correctGenome(self, edges):
for i in range(self.numVert):
for j in range(self.numVert):
if edges[i][j] == 1:
color1 = self.getColorChromosome(i)
color2 = self.getColorChromosome(j)
if color1 == color2:
greaterColor = color2
lowerColor = color2
for k in range(self.numVert):
if edges[j][k] == 1:
if greaterColor < self.getColorChromosome(k):
tmp = greaterColor
greaterColor = self.getColorChromosome(k)
lowerColor = tmp
if lowerColor > 0:
color2 = lowerColor - 1
self.setColorChromosome(j, color2)
else:
color2 = greaterColor + 1
self.setColorChromosome(j, color2)