From a722243cd6e5015b3339bce3cc49f4f147603299 Mon Sep 17 00:00:00 2001 From: Tony Dang <62843153+Dang-Hoang-Tung@users.noreply.github.com> Date: Fri, 28 Mar 2025 23:39:33 +0000 Subject: [PATCH 1/2] Fix bug in multi-threading - Multi-threading (despite being commented out) had a tiny bug: missing target argument (2nd argument). - Commented out code was also slightly hard to understand, added (Option 1/2) in comments to clarify where a user may choose between 2 implementations. --- genetic_algorithm/basic_string.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index a906ce85a779..58b5d79d98f9 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -144,18 +144,19 @@ def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int, # Random population created. Now it's time to evaluate. - # Adding a bit of concurrency can make everything faster, + # (Option 1) Adding a bit of concurrency can make everything faster, # # import concurrent.futures # population_score: list[tuple[str, float]] = [] # with concurrent.futures.ThreadPoolExecutor( # max_workers=NUM_WORKERS) as executor: - # futures = {executor.submit(evaluate, item) for item in population} + # futures = {executor.submit(evaluate, item, target) for item in population} # concurrent.futures.wait(futures) # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. - # We just need to call evaluate for every item inside the population. + + # (Option 2) We just need to call evaluate for every item inside the population. population_score = [evaluate(item, target) for item in population] # Check if there is a matching evolution. From 3085c7e94e8d721d2c1deb99fba210043c74efb9 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy <mithridatus@mail.ru> Date: Sat, 29 Mar 2025 11:08:34 +0300 Subject: [PATCH 2/2] Update basic_string.py --- genetic_algorithm/basic_string.py | 1 - 1 file changed, 1 deletion(-) diff --git a/genetic_algorithm/basic_string.py b/genetic_algorithm/basic_string.py index 58b5d79d98f9..b75491d9a949 100644 --- a/genetic_algorithm/basic_string.py +++ b/genetic_algorithm/basic_string.py @@ -155,7 +155,6 @@ def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int, # population_score = [item.result() for item in futures] # # but with a simple algorithm like this, it will probably be slower. - # (Option 2) We just need to call evaluate for every item inside the population. population_score = [evaluate(item, target) for item in population]