Skip to content

Commit 561e5a0

Browse files
authored
Update genetic_algorithm/basic_string.py
1 parent b16674e commit 561e5a0

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

genetic_algorithm/basic_string.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,17 @@ def select(
6767
"""
6868
Select the second parent and generate new population
6969
70-
>>> import random
7170
>>> random.seed(42)
7271
>>> parent_1 = ("123456", 8.0)
7372
>>> population_score = [("abcdef", 4.0), ("ghijkl", 5.0), ("mnopqr", 7.0)]
7473
>>> genes = list("ABCDEF")
75-
>>> pop = []
76-
>>> child_n = int(parent_1[1]) + 1
77-
>>> child_n = 10 if child_n >= 10 else child_n
74+
>>> child_n = int(min(parent_1[1]) + 1, 10))
75+
>>> population = []
7876
>>> for _ in range(child_n):
79-
... parent_2 = population_score[random.randint(0, len(population_score) - 1)][0]
77+
... parent_2 = population_score[random.randrange(len(population_score))][0]
8078
... child_1, child_2 = crossover(parent_1[0], parent_2)
81-
... pop.append(mutate(child_1, genes))
82-
... pop.append(mutate(child_2, genes))
83-
>>> len(pop) == (int(parent_1[1]) + 1) * 2
79+
... population.extend((mutate(child_1, genes), mutate(child_2, genes)))
80+
>>> len(population) == (int(parent_1[1]) + 1) * 2
8481
True
8582
"""
8683
pop = []

0 commit comments

Comments
 (0)