forked from ahmedfgad/GeneticAlgorithmPython
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_parallel.py
More file actions
73 lines (66 loc) · 2.79 KB
/
test_parallel.py
File metadata and controls
73 lines (66 loc) · 2.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
import pygad
import numpy
import time
# Global constants for testing
num_generations = 5
num_parents_mating = 4
sol_per_pop = 10
num_genes = 3
random_seed = 42
def fitness_func(ga_instance, solution, solution_idx):
# Simulate some work
# time.sleep(0.01)
return numpy.sum(solution**2)
def fitness_func_batch(ga_instance, solutions, indices):
return [numpy.sum(s**2) for s in solutions]
def test_parallel_thread():
"""Test parallel_processing with 'thread' mode."""
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
fitness_func=fitness_func,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
parallel_processing=["thread", 2],
random_seed=random_seed,
suppress_warnings=True
)
ga_instance.run()
assert ga_instance.run_completed
print("test_parallel_thread passed.")
def test_parallel_process():
"""Test parallel_processing with 'process' mode."""
# Note: 'process' mode might be tricky in some environments (e.g. Windows without if __name__ == '__main__':)
# But for a CI environment it should be tested.
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
fitness_func=fitness_func,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
parallel_processing=["process", 2],
random_seed=random_seed,
suppress_warnings=True
)
ga_instance.run()
assert ga_instance.run_completed
print("test_parallel_process passed.")
def test_parallel_thread_batch():
"""Test parallel_processing with 'thread' mode and batch fitness."""
ga_instance = pygad.GA(num_generations=num_generations,
num_parents_mating=num_parents_mating,
fitness_func=fitness_func_batch,
sol_per_pop=sol_per_pop,
num_genes=num_genes,
parallel_processing=["thread", 2],
fitness_batch_size=2,
random_seed=random_seed,
suppress_warnings=True
)
ga_instance.run()
assert ga_instance.run_completed
print("test_parallel_thread_batch passed.")
if __name__ == "__main__":
# For 'process' mode to work on Windows/macOS, we need this guard
test_parallel_thread()
test_parallel_process()
test_parallel_thread_batch()
print("\nAll parallel tests passed!")