Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Genetic Algorithm: Fix bug in multi-threading (TheAlgorithms#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>
  • Loading branch information
Dang-Hoang-Tung and MaximSmolskiy authored Mar 29, 2025
commit 74b540ad73bd3b1187ed6e3c89bb8f309ef543fd
6 changes: 3 additions & 3 deletions genetic_algorithm/basic_string.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.

# 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.
Expand Down