-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGAlgorithm.py
More file actions
305 lines (268 loc) · 10.7 KB
/
GAlgorithm.py
File metadata and controls
305 lines (268 loc) · 10.7 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
import math
import random
import time
"""
GAlgorithm: Class that implements the Genetic Algorithm
for String Consensus problems
"""
class GAlgorithm:
"""
<__init__>
Constructor for the class
parameters:
<self> - Pointer to the current object
<dataset> - List of lists containing
same length sequences of characters
<solution> - List of lists containing same
length sequences of characters with
dataset
<mutator> - Mutator object that generates new
solution sequences based on a given
sequence
<obj_func> - ObjectiveFunction object that produces
a numeric evaluation of a sequence and a
dataset (based on Hamming distances)
<status> - Status object that contains a record of
each run of an algorithm
returns:
-None-
"""
def __init__(self, dataset, solution, mutator, obj_func, status):
self.dataset = dataset
self.results = [[]]
self.solution = solution
self.mutator = mutator
self.obj_func = obj_func
self.status = status
"""
<csp_fit_value>
Funciont of csp fit value computation of Genetic Algorithm
parameters:
<self> - Pointer to the current object
returns:
<fit_value> - List of fit value of each
sequence in csp objective
function
"""
def csp_fit_value(self):
temp1 = self.solution
data = self.dataset
fit_value = []
for i in range(len(temp1)):
fit_value_ = (len(self.solution)+1) - self.obj_func.evaluate(data,temp1[i])
fit_value.append(fit_value_)
return fit_value
"""
<ffmsp_fit_value>
Funciont of ffmsp fit value computation of
Genetic Algorithm
parameters:
<self> - Pointer to the current object
returns:
<fit_value> - List of fit value of each
sequence in ffmsp objective
function
"""
def ffmsp_fit_value(self):
temp1 = self.solution
data = self.dataset
fit_value = []
for i in range(len(temp1)):
fit_value_ = (len(self.solution)+1) - self.obj_func.evaluate(data,temp1[i])
fit_value.append(fit_value_)
return fit_value
"""
<sum>
Compute the sum of fit value
parameters:
<self> - Pointer to the current object
<fit_value> - List of fit value of each
sequence in ffmsp objective
function
returns:
<total> - Sum of fit value
"""
def sum(self,fit_value):
total = 0
for i in range(len(fit_value)):
total += fit_value[i]
return total
"""
<cumsum>
Compute the probability of fit_value
patameters:
<self> - Pointer to the current object
<fit_value> - List of fit value of each
sequence in ffmsp objective
function
returns:
-None-
"""
def cumsum(self,fit_value):
for i in range(len(fit_value)-2, -1, -1):
t = 0
j = 0
while(j <= i):
t += fit_value[j]
j += 1
fit_value[i] = t
fit_value[len(fit_value)-1] = 1
"""
mutation
Mutate the attribute solution for a number of times
equivalent to its length
parameters:
<self> - Pointer to the current object
<probability_of_mutation> - Mutation probability
of each solution
returns:
-None-
"""
def mutation(self, probability_of_mutation):
solution_length = len(self.solution)
for i in range(solution_length):
self.mutator.mutate(self.solution[i])
"""
<selection>
select which secquences are left from parents
and children population
parameters:
<self> - Pointer to the current object
<fit_value> - List of fit value of each
sequence in ffmsp objective
function
returns:
-None-
"""
def selection(self, fit_value):
newfit_value = []
total_fit = sum(fit_value)
for i in range(len(fit_value)):
newfit_value.append(fit_value[i] / total_fit)
self.cumsum(newfit_value)
ms = []
solution_len = len(self.solution)
for i in range(solution_len):
ms.append(random.random())
ms.sort()
fitin = 0
newin = 0
newsolution = self.solution
while newin < solution_len:
if(ms[newin] < newfit_value[fitin]):
newsolution[newin] = self.solution[fitin]
newin = newin + 1
else:
fitin = fitin + 1
self.solution = newsolution
"""
crossover
population of parents compulate
parameters:
<self> - Pointer to the current object
<pc> - Probability of crossover
returns:
-None-
"""
def crossover(self, pc):
solution_len = len(self.solution)
for i in range(solution_len - 1):
if(random.random() < pc):
cpoint = random.randint(0,len(self.solution[0]))
temp1 = []
temp2 = []
temp1.extend(self.solution[i][0:cpoint])
temp1.extend(self.solution[i+1][cpoint:len(self.solution[i])])
temp2.extend(self.solution[i+1][0:cpoint])
temp2.extend(self.solution[i][cpoint:len(self.solution[i])])
self.solution[i] = temp1
self.solution[i+1] = temp2
"""
<best>
find the best sequence from each loop
parameters:
<self> - Pointer to the current object
<fit_value> - List of fit value of each
sequence in ffmsp objective
function
returns:
-None-
"""
def best(self, fit_value):
return self.solution[fit_value.index(max(fit_value))], max(fit_value)
"""
<run>
give the solution in genetic algorithm wiht csp
parameters:
<self> - Pointer to the current object
<iterations> - times of loop
<probability_of_mutation> - Mutation probability
of each solution and initial is 0.1.
returns:
<status> - Status object that contains a record of
each run of an algorithm
"""
def run(self, iterations, probability_of_mutation = 0.1):
start = time.time()
fit_value = []
self.status.add_function_calls()
fit_value0 = self.csp_fit_value()
best_individual, best_fit = self.best(fit_value0)
prev_fit = 0
for i in range(iterations):
self.status.add_iteration()
fit_value = self.csp_fit_value()
best_individual, best_fit = self.best(fit_value)
self.selection(fit_value)
self.mutation(probability_of_mutation)
current_entry = [i, self.status.get_function_calls(), (len(self.solution)+1)-prev_fit, (len(self.solution)+1)-best_fit]
self.status.add_solution_record_entry(current_entry)
prev_fit = best_fit
end = time.time()
print("Result: ")
print(" Solution: ")
print(best_individual)
print(" Evaluation: ")
print((len(self.solution)+1)-best_fit)
self.status.set_best_solution(best_individual)
self.status.set_elapsed_time(end-start)
return self.status
"""
<run_ffmsp>
give the solution in genetic algorithm wiht ffmsp
parameters:
<self> - Pointer to the current object
<iterations> - times of loop
<probability_of_mutation> - Mutation probability
of each solution and initial is 0.1.
returns:
<status> - Status object that contains a record of
each run of an algorithm
"""
def run_ffmsp(self, iterations, probability_of_mutation = 0.1):
start = time.time()
fit_value = []
self.status.add_function_calls()
fit_value = self.ffmsp_fit_value()
best_individual, best_fit = self.best(fit_value)
prev_fit = 0
for i in range(iterations):
self.status.add_iteration()
fit_value = self.ffmsp_fit_value()
best_individual, best_fit = self.best(fit_value)
#self.results.append([best_fit, best_individual])
self.selection(fit_value)
#self.crossover(probability_of_crossover)
self.mutation(probability_of_mutation)
current_entry = [i, self.status.get_function_calls(), (len(self.solution)+1)-prev_fit, (len(self.solution)+1)-best_fit]
self.status.add_solution_record_entry(current_entry)
prev_fit = best_fit
end = time.time()
print("Result: ")
print(" Solution: ")
print(best_individual)
print(" Evaluation: ")
print((len(self.solution)+1)-best_fit)
self.status.set_best_solution(best_individual)
self.status.set_elapsed_time(end-start)
return self.status