@@ -26,24 +26,26 @@ def single_point_crossover(self, parents, offspring_size):
26
26
offspring = numpy .empty (offspring_size , dtype = object )
27
27
28
28
# Randomly generate all the K points at which crossover takes place between each two parents. The point does not have to be always at the center of the solutions.
29
+ # This saves time by calling the numpy.random.randint() function only once.
29
30
crossover_points = numpy .random .randint (low = 0 ,
30
31
high = parents .shape [1 ],
31
32
size = offspring_size [0 ])
33
+
32
34
for k in range (offspring_size [0 ]):
33
35
# Check if the crossover_probability parameter is used.
34
36
if not (self .crossover_probability is None ):
35
37
probs = numpy .random .random (size = parents .shape [0 ])
36
- indices = numpy .where (probs <= self .crossover_probability )[0 ]
38
+ indices = list ( set ( numpy .where (probs <= self .crossover_probability )[0 ]))
37
39
38
- # If no parent satisfied the probability, no crossover is applied and a parent is selected.
40
+ # If no parent satisfied the probability, no crossover is applied and a parent is selected as is .
39
41
if len (indices ) == 0 :
40
42
offspring [k , :] = parents [k % parents .shape [0 ], :]
41
43
continue
42
44
elif len (indices ) == 1 :
43
45
parent1_idx = indices [0 ]
44
46
parent2_idx = parent1_idx
45
47
else :
46
- indices = random .sample (list ( set ( indices )) , 2 )
48
+ indices = random .sample (indices , 2 )
47
49
parent1_idx = indices [0 ]
48
50
parent2_idx = indices [1 ]
49
51
else :
@@ -88,17 +90,23 @@ def two_points_crossover(self, parents, offspring_size):
88
90
else :
89
91
offspring = numpy .empty (offspring_size , dtype = object )
90
92
93
+ # Randomly generate all the first K points at which crossover takes place between each two parents.
94
+ # This saves time by calling the numpy.random.randint() function only once.
95
+ if (parents .shape [1 ] == 1 ): # If the chromosome has only a single gene. In this case, this gene is copied from the second parent.
96
+ crossover_points_1 = numpy .zeros (offspring_size [0 ])
97
+ else :
98
+ crossover_points_1 = numpy .random .randint (low = 0 ,
99
+ high = numpy .ceil (parents .shape [1 ]/ 2 + 1 ),
100
+ size = offspring_size [0 ])
101
+
102
+ # The second point must always be greater than the first point.
103
+ crossover_points_2 = crossover_points_1 + int (parents .shape [1 ]/ 2 )
104
+
91
105
for k in range (offspring_size [0 ]):
92
- if (parents .shape [1 ] == 1 ): # If the chromosome has only a single gene. In this case, this gene is copied from the second parent.
93
- crossover_point1 = 0
94
- else :
95
- crossover_point1 = numpy .random .randint (low = 0 , high = numpy .ceil (parents .shape [1 ]/ 2 + 1 ), size = 1 )[0 ]
96
-
97
- crossover_point2 = crossover_point1 + int (parents .shape [1 ]/ 2 ) # The second point must always be greater than the first point.
98
106
99
107
if not (self .crossover_probability is None ):
100
108
probs = numpy .random .random (size = parents .shape [0 ])
101
- indices = numpy .where (probs <= self .crossover_probability )[0 ]
109
+ indices = list ( set ( numpy .where (probs <= self .crossover_probability )[0 ]))
102
110
103
111
# If no parent satisfied the probability, no crossover is applied and a parent is selected.
104
112
if len (indices ) == 0 :
@@ -108,7 +116,7 @@ def two_points_crossover(self, parents, offspring_size):
108
116
parent1_idx = indices [0 ]
109
117
parent2_idx = parent1_idx
110
118
else :
111
- indices = random .sample (list ( set ( indices )) , 2 )
119
+ indices = random .sample (indices , 2 )
112
120
parent1_idx = indices [0 ]
113
121
parent2_idx = indices [1 ]
114
122
else :
@@ -118,11 +126,11 @@ def two_points_crossover(self, parents, offspring_size):
118
126
parent2_idx = (k + 1 ) % parents .shape [0 ]
119
127
120
128
# The genes from the beginning of the chromosome up to the first point are copied from the first parent.
121
- offspring [k , 0 :crossover_point1 ] = parents [parent1_idx , 0 :crossover_point1 ]
129
+ offspring [k , 0 :crossover_points_1 [ k ]] = parents [parent1_idx , 0 :crossover_points_1 [ k ] ]
122
130
# The genes from the second point up to the end of the chromosome are copied from the first parent.
123
- offspring [k , crossover_point2 :] = parents [parent1_idx , crossover_point2 :]
131
+ offspring [k , crossover_points_2 [ k ] :] = parents [parent1_idx , crossover_points_2 [ k ] :]
124
132
# The genes between the 2 points are copied from the second parent.
125
- offspring [k , crossover_point1 : crossover_point2 ] = parents [parent2_idx , crossover_point1 : crossover_point2 ]
133
+ offspring [k , crossover_points_1 [ k ]: crossover_points_2 [ k ]] = parents [parent2_idx , crossover_points_1 [ k ]: crossover_points_2 [ k ] ]
126
134
127
135
if self .allow_duplicate_genes == False :
128
136
if self .gene_space is None :
@@ -153,10 +161,18 @@ def uniform_crossover(self, parents, offspring_size):
153
161
else :
154
162
offspring = numpy .empty (offspring_size , dtype = object )
155
163
164
+ # Randomly generate all the genes sources at which crossover takes place between each two parents.
165
+ # This saves time by calling the numpy.random.randint() function only once.
166
+ # There is a list of 0 and 1 for each offspring.
167
+ # [0, 1, 0, 0, 1, 1]: If the value is 0, then take the gene from the first parent. If 1, take it from the second parent.
168
+ genes_sources = numpy .random .randint (low = 0 ,
169
+ high = 2 ,
170
+ size = offspring_size )
171
+
156
172
for k in range (offspring_size [0 ]):
157
173
if not (self .crossover_probability is None ):
158
174
probs = numpy .random .random (size = parents .shape [0 ])
159
- indices = numpy .where (probs <= self .crossover_probability )[0 ]
175
+ indices = list ( set ( numpy .where (probs <= self .crossover_probability )[0 ]))
160
176
161
177
# If no parent satisfied the probability, no crossover is applied and a parent is selected.
162
178
if len (indices ) == 0 :
@@ -166,7 +182,7 @@ def uniform_crossover(self, parents, offspring_size):
166
182
parent1_idx = indices [0 ]
167
183
parent2_idx = parent1_idx
168
184
else :
169
- indices = random .sample (list ( set ( indices )) , 2 )
185
+ indices = random .sample (indices , 2 )
170
186
parent1_idx = indices [0 ]
171
187
parent2_idx = indices [1 ]
172
188
else :
@@ -175,12 +191,11 @@ def uniform_crossover(self, parents, offspring_size):
175
191
# Index of the second parent to mate.
176
192
parent2_idx = (k + 1 ) % parents .shape [0 ]
177
193
178
- genes_source = numpy .random .randint (low = 0 , high = 2 , size = offspring_size [1 ])
179
194
for gene_idx in range (offspring_size [1 ]):
180
- if (genes_source [ gene_idx ] == 0 ):
195
+ if (genes_sources [ k , gene_idx ] == 0 ):
181
196
# The gene will be copied from the first parent if the current gene index is 0.
182
197
offspring [k , gene_idx ] = parents [parent1_idx , gene_idx ]
183
- elif (genes_source [ gene_idx ] == 1 ):
198
+ elif (genes_sources [ k , gene_idx ] == 1 ):
184
199
# The gene will be copied from the second parent if the current gene index is 1.
185
200
offspring [k , gene_idx ] = parents [parent2_idx , gene_idx ]
186
201
@@ -214,10 +229,18 @@ def scattered_crossover(self, parents, offspring_size):
214
229
else :
215
230
offspring = numpy .empty (offspring_size , dtype = object )
216
231
232
+ # Randomly generate all the genes sources at which crossover takes place between each two parents.
233
+ # This saves time by calling the numpy.random.randint() function only once.
234
+ # There is a list of 0 and 1 for each offspring.
235
+ # [0, 1, 0, 0, 1, 1]: If the value is 0, then take the gene from the first parent. If 1, take it from the second parent.
236
+ genes_sources = numpy .random .randint (low = 0 ,
237
+ high = 2 ,
238
+ size = offspring_size )
239
+
217
240
for k in range (offspring_size [0 ]):
218
241
if not (self .crossover_probability is None ):
219
242
probs = numpy .random .random (size = parents .shape [0 ])
220
- indices = numpy .where (probs <= self .crossover_probability )[0 ]
243
+ indices = list ( set ( numpy .where (probs <= self .crossover_probability )[0 ]))
221
244
222
245
# If no parent satisfied the probability, no crossover is applied and a parent is selected.
223
246
if len (indices ) == 0 :
@@ -227,7 +250,7 @@ def scattered_crossover(self, parents, offspring_size):
227
250
parent1_idx = indices [0 ]
228
251
parent2_idx = parent1_idx
229
252
else :
230
- indices = random .sample (list ( set ( indices )) , 2 )
253
+ indices = random .sample (indices , 2 )
231
254
parent1_idx = indices [0 ]
232
255
parent2_idx = indices [1 ]
233
256
else :
@@ -236,9 +259,9 @@ def scattered_crossover(self, parents, offspring_size):
236
259
# Index of the second parent to mate.
237
260
parent2_idx = (k + 1 ) % parents .shape [0 ]
238
261
239
- # A 0/1 vector where 0 means the gene is taken from the first parent and 1 means the gene is taken from the second parent.
240
- gene_sources = numpy . random . randint ( 0 , 2 , size = self . num_genes )
241
- offspring [ k , :] = numpy . where ( gene_sources == 0 , parents [ parent1_idx , :], parents [parent2_idx , :])
262
+ offspring [ k , :] = numpy . where ( genes_sources [ k ] == 0 ,
263
+ parents [ parent1_idx , :],
264
+ parents [parent2_idx , :])
242
265
243
266
if self .allow_duplicate_genes == False :
244
267
if self .gene_space is None :
0 commit comments