-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenetic_sequence_algorithm
164 lines (127 loc) · 4.04 KB
/
Genetic_sequence_algorithm
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
# coding: utf-8
# In[10]:
import numpy as np
import sys
num_queen = 8
terminatep= 28
mutating= 0.000001
generate_mutate_flag= True
high_iter = 100000
POPULATION = None
class QueenPosition:
def __init__(self):
self.sequence = None
self.fit_score = None
self.survival = None
def setSequence(self, val):
self.sequence = val
def setfit_score(self, fit_score):
self.fit_score = fit_score
def setSurvival(self, val):
self.survival = val
def getAttr(self):
return {'sequence':sequence, 'fit_score':fit_score, 'survival':survival}
def fit_score(chromosome = None):
clashes = 0;
row_col_clashes = abs(len(chromosome) - len(np.unique(chromosome)))
clashes += row_col_clashes
# calculate diagonal clashes
for i in range(len(chromosome)):
for j in range(len(chromosome)):
if ( i != j):
dx = abs(i-j)
dy = abs(chromosome[i] - chromosome[j])
if(dx == dy):
clashes += 1
return 28 - clashes
def getSequence():
# randomly generates a sequence of board states.
global num_queen
init_distribution = np.arange(num_queen)
np.random.shuffle(init_distribution)
return init_distribution
def getPopulation(population_size = 100):
global POPULATION
POPULATION = population_size
population = [QueenPosition() for i in range(population_size)]
for i in range(population_size):
population[i].setSequence(getSequence())
population[i].setfit_score(fit_score(population[i].sequence))
return population
def retriveParent():
globals()
parent1, parent2 = None, None
summation_fit_score = np.sum([x.fit_score for x in population])
for each in population:
each.survival = each.fit_score/(summation_fit_score*1.0)
while True:
parent1_random = np.random.rand()
parent1_rn = [x for x in population if x.survival <= parent1_random]
try:
parent1 = parent1_rn[0]
break
except:
pass
while True:
parent2_random = np.random.rand()
parent2_rn = [x for x in population if x.survival <= parent2_random]
try:
t = np.random.randint(len(parent2_rn))
parent2 = parent2_rn[t]
if parent2 != parent1:
break
else:
print("equal parents")
continue
except:
print("exception")
continue
if parent1 is not None and parent2 is not None:
return parent1, parent2
else:
sys.exit(-1)
def generate_cross(parent1, parent2):
globals()
n = len(parent1.sequence)
c = np.random.randint(n, size=1)
child = QueenPosition()
child.sequence = []
child.sequence.extend(parent1.sequence[0:c])
child.sequence.extend(parent2.sequence[c:])
child.setfit_score(fit_score(child.sequence))
return child
def generate_mutate(child):
if child.survival < generate_mutate:
c = np.random.randint(8)
child.sequence[c] = np.random.randint(8)
return child
def geneticAlgo(iteration):
print(" #"*10 ,"Executing Genetic generation : "), iteration , " #"*10
globals()
newpopulation = []
for i in range(len(population)):
parent1, parent2 = retriveParent()
# print("Parents generated : "), parent1, parent2
child = generate_cross(parent1, parent2)
if(generate_mutate_FLAG):
child = generate_mutate(child)
newpopulation.append(child)
return newpopulation
def terminate():
globals()
fit_scorevals = [pos.fit_score for pos in population]
if terminatep in fit_scorevals:
return True
if high_iter == iteration:
return True
return False
population = getPopulation(1000)
print("POPULATION size : "), population
iteration = 0;
while not terminate():
population = geneticAlgo(iteration)
iteration +=1
print("Iteration number : "), iteration
for each in population:
if each.fit_score == 28:
print(each.sequence)