Skip to content

Commit 74b540a

Browse files
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 <[email protected]>
1 parent e3773db commit 74b540a

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

genetic_algorithm/basic_string.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,18 @@ def basic(target: str, genes: list[str], debug: bool = True) -> tuple[int, int,
144144

145145
# Random population created. Now it's time to evaluate.
146146

147-
# Adding a bit of concurrency can make everything faster,
147+
# (Option 1) Adding a bit of concurrency can make everything faster,
148148
#
149149
# import concurrent.futures
150150
# population_score: list[tuple[str, float]] = []
151151
# with concurrent.futures.ThreadPoolExecutor(
152152
# max_workers=NUM_WORKERS) as executor:
153-
# futures = {executor.submit(evaluate, item) for item in population}
153+
# futures = {executor.submit(evaluate, item, target) for item in population}
154154
# concurrent.futures.wait(futures)
155155
# population_score = [item.result() for item in futures]
156156
#
157157
# but with a simple algorithm like this, it will probably be slower.
158-
# We just need to call evaluate for every item inside the population.
158+
# (Option 2) We just need to call evaluate for every item inside the population.
159159
population_score = [evaluate(item, target) for item in population]
160160

161161
# Check if there is a matching evolution.

0 commit comments

Comments
 (0)