Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 4eba80b

Browse files
Reduce time of single point crossover
1 parent a846a26 commit 4eba80b

File tree

2 files changed

+8
-6
lines changed

2 files changed

+8
-6
lines changed

‎pygad/utils/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,4 @@
33
from pygad.utils import mutation
44
from pygad.utils import nsga2
55

6-
__version__ = "1.1.1"
6+
__version__ = "1.2.1"

‎pygad/utils/crossover.py

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,12 @@ def single_point_crossover(self, parents, offspring_size):
2525
else:
2626
offspring = numpy.empty(offspring_size, dtype=object)
2727

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+
crossover_points = numpy.random.randint(low=0,
30+
high=parents.shape[1],
31+
size=offspring_size[0])
2832
for k in range(offspring_size[0]):
29-
# The point at which crossover takes place between two parents. Usually, it is at the center.
30-
crossover_point = numpy.random.randint(low=0, high=parents.shape[1], size=1)[0]
31-
33+
# Check if the crossover_probability parameter is used.
3234
if not (self.crossover_probability is None):
3335
probs = numpy.random.random(size=parents.shape[0])
3436
indices = numpy.where(probs <= self.crossover_probability)[0]
@@ -51,9 +53,9 @@ def single_point_crossover(self, parents, offspring_size):
5153
parent2_idx = (k+1) % parents.shape[0]
5254

5355
# The new offspring has its first half of its genes from the first parent.
54-
offspring[k, 0:crossover_point] = parents[parent1_idx, 0:crossover_point]
56+
offspring[k, 0:crossover_points[k]] = parents[parent1_idx, 0:crossover_points[k]]
5557
# The new offspring has its second half of its genes from the second parent.
56-
offspring[k, crossover_point:] = parents[parent2_idx, crossover_point:]
58+
offspring[k, crossover_points[k]:] = parents[parent2_idx, crossover_points[k]:]
5759

5860
if self.allow_duplicate_genes == False:
5961
if self.gene_space is None:

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /