-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEightQueens_0.py
234 lines (164 loc) · 5.62 KB
/
EightQueens_0.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import random
import re
POPULATION_SIZE = 100
INDIVIDUAL_SIZE = 8
CANDIDATE_AMMOUNT = 5
MAX_CROSS_ITERATIONS = 10
def arrayToString(array):
finalstring = ''
for item in array:
finalstring += format(item,'b').zfill(3)
return finalstring
def binaryToInt(string):
return int(string, 2)
def stringToArray(string):
array = re.findall('...', string)
array = list(map(binaryToInt, array))
return array
def makeIndividual():
individual = ''
for x in range(INDIVIDUAL_SIZE*3):
if random.random() > 0.5:
individual += '1'
else:
individual += '0'
return individual
def initPopulation(pop_size):
popList = []
for x in range(0,pop_size):
ind = makeIndividual()
popList.append({
'genotype': ind,
'fitness': 0
})
return popList
def calculateColisions(gen):
genAsArray = stringToArray(gen)
colisionCount = 0
for index in range(len(genAsArray)):
for searchIndex in range(1, len(genAsArray)):
posIndex = index + searchIndex
negIndex = index - searchIndex
if negIndex >= 0:
if abs(genAsArray[index] - genAsArray[negIndex]) == searchIndex:
colisionCount += 1
if(abs(genAsArray[index] - genAsArray[negIndex]) == 0):
colisionCount += 1
if posIndex <= 7:
if abs(genAsArray[index] - genAsArray[posIndex]) == searchIndex:
colisionCount += 1
if abs(genAsArray[index] - genAsArray[posIndex]) == 0:
colisionCount += 1
return colisionCount/2
def calculateIndividualFitness(ind):
genColisions = calculateColisions(ind['genotype'])
ind['fitness'] = 1/(1+genColisions)
return ind
def calculatePopulationFitness(pop):
for ind_index in range(0,len(pop)):
pop[ind_index] = calculateIndividualFitness(pop[ind_index])
return pop
def validatePopulation(pop):
maxFitness = 0
for individual in pop:
if individual['fitness'] > maxFitness:
maxFitness = individual['fitness']
return maxFitness
def getIndividualFitness(individual):
return individual['fitness']
def selectParents(pop):
parents = []
popLength = len(pop)
for iterator in range(MAX_CROSS_ITERATIONS):
parentsCandidates = []
for index in range(CANDIDATE_AMMOUNT):
random.seed()
randIndex = random.randrange(popLength);
parentsCandidates.append(pop[randIndex])
parentsCandidates.sort(key = getIndividualFitness)
parent_1 = parentsCandidates.pop()
parent_2 = parentsCandidates.pop()
parents.append({
"firstParent": parent_1,
"secondParent": parent_2
})
return parents
def cutAndCross(child_1, child_2):
gen_1 = stringToArray(child_1['genotype'])
gen_2 = stringToArray(child_2['genotype'])
genLen = int(len(gen_1)/2)
crossGen = gen_1[:genLen] + gen_2[genLen:]
return {
'genotype': arrayToString(crossGen),
'fitness': 0,
}
def generateChildren(parents):
generatedChildren = []
for pair in parents:
random.seed()
shuffleChance = random.random()
if(shuffleChance < 0.9):
#If genetic shuffle occour
child_1 = cutAndCross(pair['firstParent'], pair['secondParent'])
child_2 = cutAndCross(pair['secondParent'], pair['firstParent'])
else:
#If genetic shuffle not occour
child_1 = pair['firstParent']
child_2 = pair['secondParent']
generatedChildren.append(child_1);
generatedChildren.append(child_2);
return generatedChildren
def mutate(individual):
genAsArray = individual['genotype']
new = ''
for index in range(len(genAsArray)):
if random.random() <= 0.5:
new += str(random.getrandbits(1))
else:
new += genAsArray[index]
individual['genotype'] = new
return individual
def procreatePopulation(pop):
parentsPairs = selectParents(pop)
children = generateChildren(parentsPairs)
childrenAmmount = len(children)
for index in range(0,childrenAmmount):
random.seed()
roll = random.randrange(0,10)
if roll < 4:
children[index] = mutate(children[index])
return pop + children
def filterPopulation(pop):
newPopulation = calculatePopulationFitness(pop)
newPopulation.sort(key = getIndividualFitness, reverse = True);
populationOverflow = len(newPopulation) - 100
while populationOverflow > 0:
newPopulation.pop()
populationOverflow -= 1
return newPopulation
def printIndividuals(individuals):
indLength = len(individuals)
iterator = 1;
for index in range(0, indLength):
iterator += 1
def runByIterations(iterationMax):
population = initPopulation(POPULATION_SIZE)
population = calculatePopulationFitness(population)
iterations = 0
while(iterations <= iterationMax):
population = procreatePopulation(population)
population = filterPopulation(population)
iterations += 1
return population
def runByFitness():
population = initPopulation(100)
population = calculatePopulationFitness(population)
iterator = 0
while(validatePopulation(population) < 1 and iterator < 10000):
population = procreatePopulation(population)
population = filterPopulation(population)
iterator += 1
return {
'population': population,
'iterationCount': iterator
}