forked from RodrigoAGM/SI404-artificial-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenetic_algorithm.py
151 lines (107 loc) · 3.86 KB
/
genetic_algorithm.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
###################################################################
##### Genetic algorithm applied to solve the N-Queens problem #####
###################################################################
import random
N = 8
INIT_POP = 4
MUTATION_PROB = 10
def maxAttacks():
return N*(N-1)/2
def getRandomBetween(minVal, maxVal):
return random.randint(minVal, maxVal)
def fitness(gen:str):
attacks = 0
index = 0
for g in gen:
#First we add the horizontal matches
attacks = attacks + (gen.count(g)-1)
#Then we create another for to get diagonal matches
subindex = 0
for c in gen:
#We skip the evaluation for the same column
if index != subindex:
coef = abs(index - subindex)
if int(g) + coef == int(c) or int(g) - coef == int(c):
attacks = attacks + 1
subindex = subindex + 1
index = index + 1
#Return the fitness result of pair matches
return maxAttacks() - (attacks / 2)
def generateGen():
gen = ""
for _ in range(N):
gen = gen + str(random.randint(1,N))
return gen
def calculateSelectionProbabilities(population:list, fitnessValues:list):
selectionProbabilities = []
limits = []
limitSum = 0
totalFitness = sum(fitnessValues)
for gen in fitnessValues:
value = gen/totalFitness
selectionProbabilities.append(value)
limits.append(limitSum)
limitSum = limitSum + value
return selectionProbabilities, limits
def getRandomGen(population:list, limits:list):
randValue = random.uniform(0,100)
lastGen = population[0]
index = 0
for gen in population:
if limits[index] > randValue:
break
lastGen = gen
index = index + 1
return lastGen
def reproduce(x:str, y:str):
c = getRandomBetween(1, N-1)
child1 = x[0:c] + y[c:]
child2 = y[0:c] + x[c:]
return child1, child2
def mutate(x:str):
index = getRandomBetween(0,N-1)
#print("OldX " + x)
x = x[0:index] + str(getRandomBetween(1,N)) + x[index+1:]
#print("NewX " + x)
return x
def geneticAlgorithm():
population = []
fitnessValues = []
limits = []
generation = 0
for _ in range(INIT_POP):
gen = generateGen()
population.append(gen)
fitnessValues.append(fitness(gen))
while True:
print("Genration: ", generation)
print("Population: ", population)
print("Values ", fitnessValues)
_, limits = calculateSelectionProbabilities(population, fitnessValues)
if maxAttacks() in fitnessValues:
print("Solution found at generation: " + str(generation))
print(population[fitnessValues.index(maxAttacks())])
break
newPopulation = []
newfitnessValues = []
for _ in range(int(INIT_POP/2)):
x = getRandomGen(population, limits)
y = getRandomGen(population, limits)
while x == y:
y = getRandomGen(population, limits)
#print("XY values:" + x + "," + y)
child1, child2 = reproduce(x,y)
#print(child1 + "," + child2)
if getRandomBetween(1,100) <= MUTATION_PROB:
child1 = mutate(child1)
child2 = mutate(child2)
newPopulation.append(child1)
newPopulation.append(child2)
newfitnessValues.append(fitness(child1))
newfitnessValues.append(fitness(child2))
population = newPopulation
fitnessValues = newfitnessValues
#print(fitnessValues)
generation = generation + 1
random.seed(a=None, version=2)
geneticAlgorithm()