-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenetic.py
More file actions
255 lines (229 loc) · 8.9 KB
/
Copy pathgenetic.py
File metadata and controls
255 lines (229 loc) · 8.9 KB
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import random
import numpy as np
from math import factorial as fact
from queen import Queen
from numba import jit
class Genetic:
@staticmethod
@jit()
def chromosome(n):
chrom = []
for i in range(n):
chrom.append(random.randint(1, n))
return chrom
def population(self, n):
chroms = []
for i in range(50):
chroms.append(self.chromosome(n))
return chroms
@staticmethod
def make_queen(chrom):
queens = []
for i in range(1, len(chrom) + 1):
queens.append(Queen([len(chrom) - (chrom[i - 1] - 1), i], False))
return queens
def attack_count(self, chrom):
count = 0
n = len(chrom)
queens = self.make_queen(chrom)
for j in range(n):
# queen for comparison
queen1 = queens[j]
for i in range(n):
# queen to be compared with
queen2 = queens[i]
if queen1 != queen2:
for k in range(1, n + 1):
# diagonal positions
pos_diag = [[queen1.pos[0] - (queen1.pos[0] - k),
queen1.pos[1] + (queen1.pos[0] - k)],
[queen1.pos[0] - (queen1.pos[0] - k),
queen1.pos[1] - (queen1.pos[0] - k)]]
# horizontal/vertical positions
pos_hori_ver = [queen1.pos[0], queen1.pos[1]]
# diagonal check
if 1 <= int(pos_diag[0][0]) <= n and 1 <= int(pos_diag[0][1]) <= n:
if queen2.pos == pos_diag[0] and not queen2.visited:
count += 1
break
if 1 <= int(pos_diag[1][0]) <= n and 1 <= int(pos_diag[1][1]) <= n:
if queen2.pos == pos_diag[1] and not queen2.visited:
count += 1
break
# horizontal/vertical check
if queen2.pos[0] == pos_hori_ver[0] and not queen2.visited:
count += 1
break
if queen2.pos[1] == pos_hori_ver[1] and not queen2.visited:
count += 1
break
queen1.visited = True
return count
@staticmethod
def selection(chroms, fit):
for i in range(len(chroms)):
for j in range(i + 1, len(chroms)):
if fit[i] < fit[j]:
temp1 = fit[i]
temp2 = chroms[i]
fit[i] = fit[j]
chroms[i] = chroms[j]
fit[j] = temp1
chroms[j] = temp2
while not len(chroms) <= 2:
chroms.pop()
chroms.pop()
l = []
for i in range(2):
if i % 2 == 0:
l.append(chroms[1])
elif i % 2 != 0:
l.append(chroms[0])
'''elif i == 3:
l.append(chroms[-1])
'''
return l
@staticmethod
def cross_over(chroms):
option = random.randint(1, 4)
if option <= 2:
rand_ind = random.randint(0, len(chroms[0]) - 1)
for i in range(len(chroms[0])):
if i >= rand_ind:
temp = chroms[0][i]
chroms[0][i] = chroms[1][i]
chroms[1][i] = temp
'''rand_ind = random.randint(0, len(chroms[0])-1)
for i in range(len(chroms[0])):
if i >= rand_ind:
temp = chroms[2][i]
chroms[2][i] = chroms[3][i]
chroms[3][i] = temp
'''
elif option >= 2:
range1 = random.randint(0, len(chroms[0]) - 1)
range2 = random.randint(0, len(chroms[0]) - 1)
if range1 > range2:
temp = range1
range1 = range2
range2 = temp
for i in range(len(chroms[0])):
if range1 <= i <= range2:
temp = chroms[0][i]
chroms[0][i] = chroms[1][i]
chroms[1][i] = temp
'''range1 = random.randint(0, len(chroms[0]) - 1)
range2 = random.randint(0, len(chroms[0]) - 1)
if range1 > range2:
temp = range1
range1 = range2
range2 = temp
for i in range(len(chroms[0])):
if range1 <= i <= range2:
temp = chroms[2][i]
chroms[2][i] = chroms[3][i]
chroms[3][i] = temp
'''
return chroms
@staticmethod
def mutation(chroms):
nxt = True
ind_done = []
for i in range(len(chroms)):
if Genetic.dupli_check(chroms[i]):
j = Genetic.return_dupli(chroms[i])
chroms[i][j] = random.randint(1, len(chroms[i]))
ind_done.append(i)
if len(ind_done) == len(chroms):
nxt = False
if nxt:
option = random.randint(0, 3)
if option == 1:
for i in range(len(chroms)):
if i in ind_done:
continue
randind = random.randint(0, len(chroms[i]) - 1)
for j in range(len(chroms[i])):
if j == randind:
chroms[i][j] = random.randint(1, len(chroms[i]))
if option == 2:
for i in range(len(chroms)):
if i in ind_done:
continue
randind = random.randint(0, len(chroms[i]) - 1)
for j in range(len(chroms[i])):
if j == randind:
chroms[i][j] = random.randint(1, len(chroms[i]))
if j + 1 <= len(chroms[i]) - 1:
chroms[i][j + 1] = random.randint(1, len(chroms[i]))
if option == 3:
for i in range(len(chroms)):
if i in ind_done:
continue
randind = random.randint(0, len(chroms[i]) - 1)
for j in range(len(chroms[i])):
if j == randind:
chroms[i][j] = random.randint(1, len(chroms[i]))
if j + 1 <= len(chroms[i]) - 1:
chroms[i][j + 1] = random.randint(1, len(chroms[i]))
if j + 2 <= len(chroms[i]) - 1:
chroms[i][j + 2] = random.randint(1, len(chroms[i]))
return chroms
@staticmethod
def return_dupli(chrom, ind=0, setlist=None):
if setlist is None:
setlist = set()
if chrom[ind] in setlist:
return ind
else:
setlist.add(chrom[ind])
ind = ind + 1
return Genetic.return_dupli(chrom, ind, setlist)
@staticmethod
def dupli_check(chrom):
if len(chrom) != len(set(chrom)):
return True
else:
return False
@staticmethod
def max_fitness(n):
return int((fact(n)) / ((fact(2)) * (fact(n - 2))))
def fitness(self, chrom):
return self.max_fitness(len(chrom)) - self.attack_count(chrom)
@staticmethod
def chessboard(chrom):
print()
board = np.zeros((len(chrom), len(chrom)), dtype=object)
board[:] = 'x'
for i in range(len(chrom)):
board[len(chrom) - chrom[i]][i] = 'Q'
print("Chessboard:\n")
for i in range(len(chrom)):
for j in range(len(chrom)):
print(board[i][j], end=" ")
print()
print()
print("(Q --> Queen)")
def genetic(self, n):
# initial population
chroms = self.population(n)
# calculating fitness
fit = []
for i in range(len(chroms)):
fit.append(self.fitness(chroms[i]))
# genetic process
while True:
if max(fit) == self.max_fitness(n):
getind = fit.index(max(fit))
break
chroms = self.selection(chroms, fit)
chroms = self.cross_over(chroms)
chroms = self.mutation(chroms)
for i in range(len(chroms)):
fit[i] = self.fitness(chroms[i])
return chroms[getind]
obj = Genetic()
num = int(input('Enter number of queens: '))
opt = obj.genetic(num)
print("Best chromosome:-", opt)
Genetic.chessboard(opt)