From 20184aa4338272641a2b3321f3d433889ae5d148 Mon Sep 17 00:00:00 2001 From: UTSAV SINGHAL <119779889+UTSAVS26@users.noreply.github.com> Date: Mon, 2 Dec 2024 14:22:39 +0530 Subject: [PATCH] Update genetic_algorithm_optimization.py doctest for the function select_parents --- .../genetic_algorithm_optimization.py | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/genetic_algorithm/genetic_algorithm_optimization.py b/genetic_algorithm/genetic_algorithm_optimization.py index 66227ca21..8c04b3ad2 100644 --- a/genetic_algorithm/genetic_algorithm_optimization.py +++ b/genetic_algorithm/genetic_algorithm_optimization.py @@ -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.")