forked from blakeaw/GAlibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_gao.py
140 lines (108 loc) · 3.85 KB
/
test_gao.py
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
import numpy as np
from galibrate.sampled_parameter import SampledParameter
from galibrate import GAO
from galibrate.benchmarks import sphere
# Define the fitness function to minimize the 'sphere' objective function.
# minimum is x=0 and f(x) = 0
def fitness(chromosome):
return -sphere(chromosome)
# Set up the list of sampled parameters: the range is (-10:10)
parm_names = list(["x", "y", "z"])
sampled_parameters = [
SampledParameter(name=p, loc=-10.0, width=20.0) for p in parm_names
]
min_point = np.zeros(3)
# Set the active point population size
population_size = 1000
generations = 200
additional = 10
mutation_rate = 0.2
SHARED = dict()
def test_initialization():
# Construct the Genetic Algorithm-based Optimizer.
gao = GAO(
sampled_parameters,
fitness,
population_size,
generations=generations,
mutation_rate=mutation_rate,
)
SHARED["gao"] = gao
def test_attributes():
assert SHARED["gao"].fitness_function == fitness
assert SHARED["gao"].sampled_parameters == sampled_parameters
assert SHARED["gao"].population_size == population_size
assert SHARED["gao"].generations == generations
assert np.isclose(SHARED["gao"].mutation_rate, mutation_rate)
def test_run():
best_theta, best_theta_fitness = SHARED["gao"].run()
assert np.isclose(best_theta_fitness, 0.0)
assert np.allclose(min_point, best_theta)
def test_resume():
best_theta, best_theta_fitness = SHARED["gao"].resume(generations=additional)
assert np.isclose(best_theta_fitness, 0.0)
assert np.allclose(min_point, best_theta)
def test_property_best():
best_theta, best_theta_fitness = SHARED["gao"].best
assert np.isclose(best_theta_fitness, 0.0)
assert np.allclose(min_point, best_theta)
def test_property_best_fitness_per_generation():
bests = SHARED["gao"].best_fitness_per_generation
assert bests is not None
assert len(bests) == (generations + additional + 2)
assert np.isclose(bests[-1], 0.0)
assert bests[0] < 0.0
def test_property_total_generations():
total = SHARED["gao"].total_generations
assert total == (generations + additional)
def test_property_final_population():
final = SHARED["gao"].final_population
assert final is not None
assert len(final) == population_size
assert len(final[0]) == len(sampled_parameters)
def test_property_final_population_fitness():
final = SHARED["gao"].final_population_fitness
assert final is not None
assert len(final) == population_size
assert np.isclose(np.max(final), 0.0)
def test_run_parallel():
# Construct the Genetic Algorithm-based Optimizer.
gao = GAO(
sampled_parameters,
fitness,
population_size,
generations=1,
mutation_rate=mutation_rate,
)
SHARED["gao_par"] = gao
best_theta, best_theta_fitness = SHARED["gao_par"].run(nprocs=2)
def test_resume_parallel():
best_theta, best_theta_fitness = SHARED["gao_par"].resume(generations=1, nprocs=2)
if __name__ == "__main__":
test_initialization()
test_attributes()
test_run()
test_resume()
test_property_best()
test_property_best_fitness_per_generation()
test_property_total_generations()
test_property_final_population()
test_property_final_population_fitness()
test_run_parallel()
test_resume_parallel()
# # Construct the Genetic Algorithm-based Optimizer.
# gao = GAO(
# sampled_parameters, fitness, population_size, generations=100, mutation_rate=0.1
# )
# # run it
# best_theta, best_theta_fitness = gao.run()
# # print the best theta.
# print(
# "Fittest theta {} with fitness value {} ".format(best_theta, best_theta_fitness)
# )
# try:
# import matplotlib.pyplot as plt
# plt.plot(gao.best_fitness_per_generation)
# plt.show()
# except ImportError:
# pass