Update genetic_algorithm_optimization.py

doctest for the function select_parents
This commit is contained in:
UTSAV SINGHAL 2024-12-02 14:22:39 +05:30 committed by GitHub
parent cdb28e53e5
commit 20184aa433
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -113,13 +113,24 @@ class GeneticAlgorithm:
>>> len(selected_parents)
2 # Should select the two parents with the best fitness scores.
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0]))
# Parent 1 should be [1.0, 2.0]
True
True # Parent 1 should be [1.0, 2.0]
>>> np.array_equal(selected_parents[1], np.array([-1.0, -2.0]))
# Parent 2 should be [-1.0, -2.0]
True
"""
True # Parent 2 should be [-1.0, -2.0]
>>> population_score = [
... (np.array([1.0, 2.0]), 5.0),
... (np.array([1.0, -2.0]), 5.0),
... (np.array([0.0, 0.0]), 0.0),
... (np.array([-1.0, 2.0]), 5.0),
... (np.array([-1.0, -2.0]), 5.0)
... ]
>>> selected_parents = ga.select_parents(population_score)
>>> len(selected_parents)
5 # Should select the top 5 parents with the best fitness scores.
>>> np.array_equal(selected_parents[0], np.array([1.0, 2.0]))
True # Parent 1 should be [1.0, 2.0]
"""
if not population_score:
raise ValueError("Population score is empty, cannot select parents.")