Genetic Algorithm: Fix bug in multi-threading (#12644)

* 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.

* Update basic_string.py

---------

Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
Tony Dang 2025-03-29 08:13:47 +00:00 committed by GitHub
parent e3773dbec1
commit 74b540ad73
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -144,18 +144,18 @@ def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int,
# Random population created. Now it's time to evaluate. # 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 # import concurrent.futures
# population_score: list[tuple[str, float]] = [] # population_score: list[tuple[str, float]] = []
# with concurrent.futures.ThreadPoolExecutor( # with concurrent.futures.ThreadPoolExecutor(
# max_workers=NUM_WORKERS) as executor: # 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) # concurrent.futures.wait(futures)
# population_score = [item.result() for item in futures] # population_score = [item.result() for item in futures]
# #
# but with a simple algorithm like this, it will probably be slower. # 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] population_score = [evaluate(item, target) for item in population]
# Check if there is a matching evolution. # Check if there is a matching evolution.