Skip to content

Commit 20184aa

Browse files
authored
Update genetic_algorithm_optimization.py
doctest for the function select_parents
1 parent cdb28e5 commit 20184aa

File tree

1 file changed

+16
-5
lines changed

1 file changed

+16
-5
lines changed

genetic_algorithm/genetic_algorithm_optimization.py

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,24 @@ def select_parents(
113113
>>> len(selected_parents)
114114
2 # Should select the two parents with the best fitness scores.
115115
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0]))
116-
# Parent 1 should be [1.0, 2.0]
117-
True
116+
True # Parent 1 should be [1.0, 2.0]
118117
>>> np.array_equal(selected_parents[1], np.array([-1.0, -2.0]))
119-
# Parent 2 should be [-1.0, -2.0]
120-
True
121-
"""
118+
True # Parent 2 should be [-1.0, -2.0]
122119
120+
>>> population_score = [
121+
... (np.array([1.0, 2.0]), 5.0),
122+
... (np.array([1.0, -2.0]), 5.0),
123+
... (np.array([0.0, 0.0]), 0.0),
124+
... (np.array([-1.0, 2.0]), 5.0),
125+
... (np.array([-1.0, -2.0]), 5.0)
126+
... ]
127+
>>> selected_parents = ga.select_parents(population_score)
128+
>>> len(selected_parents)
129+
5 # Should select the top 5 parents with the best fitness scores.
130+
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0]))
131+
True # Parent 1 should be [1.0, 2.0]
132+
"""
133+
123134
if not population_score:
124135
raise ValueError("Population score is empty, cannot select parents.")
125136

0 commit comments

Comments
 (0)