-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path8Queens.py
154 lines (123 loc) · 3.89 KB
/
8Queens.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
from random import randrange
import random
import time
def initialPopulation():
pop = []
size = 200
for i in range(size):
chrome = []
for i in range(8):
chrome.append(randrange(8))
pop.append(chrome)
return pop
def fitnessFunction(list):
grade = 49
slant = 0
for i in range(0, 8):
for j in range(i + 1, 8):
slant = j - i
if list[i] == list[j]:
grade = grade - 2
else:
if list[i] + slant == list[j] or list[i] - slant == list[j]:
grade = grade - 2
return grade
def mutation(list, i):
list[i] = random.randint(0, 7)
return list
def crossOver(list1, list2):
#determine wether to do crossover
crossOver_rate = 2
randNum = random.randint(0, 9)
if (randNum < crossOver_rate):
return list1, list2
childrenList1 = []
childrenList2 = []
for j in range(0, 8):
i = random.randint(0, 1)
if (i == 1):
childrenList1.append(list1[j])
childrenList2.append(list2[j])
else:
childrenList1.append(list2[j])
childrenList2.append(list1[j])
# determine wether to do mutation
mutation_rate = 1
for i in range(0, 8):
randNum = random.randint(0, 99)
if (randNum < mutation_rate):
childrenList1 = mutation(childrenList1, i)
randNum = random.randint(0, 99)
if (randNum < mutation_rate):
childrenList2 = mutation(childrenList2, i)
return childrenList1, childrenList2
def createNextGen(currGen, grades):
nextGen = []
grades, currGen = zip(*sorted(zip(grades, currGen)))
pool = []
nextGen.append(currGen[len(currGen) - 1])
nextGen.append(currGen[len(currGen) - 2])
help = 1
for i in range(len(grades)):
count = help
while count > 0:
pool.append(currGen[i])
count = count - 1
help = help + 1
# do 100 times: select 2, crossover, mutation, add to new gen
poll = []
for i in range(len(grades)):
count = grades[i]
while count > 0:
poll.append(currGen[i])
count = count - 1
for k in range(98):
#choose parents
choose = randrange(len(poll))
parent1 = poll[choose]
choose = randrange(len(poll))
parent2 = poll[choose]
#create children
child1, child2 = crossOver(parent1, parent2)
nextGen.append(child1)
nextGen.append(child2)
return nextGen
def main():
start_time = time.time()
grades = []
currGen = initialPopulation()
#first grades
for l in currGen:
grades.append(fitnessFunction(l))
gen = 0
perfect_score = 49
f = open("q.txt", "w")
while perfect_score not in grades:
#create next gen
currGen = createNextGen(currGen, grades)
# if havnt solved yet, initiate population again
if gen % 400 == 0:
currGen = initialPopulation()
grades = []
## grade gen
for l in currGen:
grades.append(fitnessFunction(l))
gen = gen + 1
f.write(str(max(grades)))
f.write(", ")
f.write(str(sum(grades) / len(grades)))
f.write("\n")
index = grades.index(perfect_score)
print("Solution using GA found after " + str(gen) + " generations.")
print("Running time: %s " % (time.time() - start_time) + "seconds.")
print("The solution is: " + str(currGen[index]))
#print solution
for i in range(8):
for j in range(8):
if currGen[index][i] == j:
print("Q", end = " ")
else:
print("-", end = " ")
print("")
if __name__ == "__main__":
main()