forked from blakeaw/GAlibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rungaocython.py
94 lines (81 loc) · 2.51 KB
/
test_rungaocython.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
import numpy as np
import pyximport
# Added the setup_args with include_dirs for numpy so it pyximport can build
# the code on Windows machine.
pyximport.install(language_level=3, setup_args={"include_dirs": np.get_include()})
from galibrate import run_gao_cython
from galibrate.sampled_parameter import SampledParameter
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)
locs_sphere = np.zeros(3) - 10.0
widths_sphere = np.zeros(3) + 20.0
# Set the active point population size
population_size = 20
generations = 10
additional = 10
mutation_rate_sphere = 0.2
SHARED = dict()
def test_rungaocython_run_gao():
new_population, best_pg = run_gao_cython.run_gao(
population_size,
len(parm_names),
locs_sphere,
widths_sphere,
generations,
mutation_rate_sphere,
fitness,
1,
)
assert new_population.shape == (population_size, len(parm_names))
SHARED["population"] = new_population
new_population, best_pg = run_gao_cython.run_gao(
population_size,
len(parm_names),
locs_sphere,
widths_sphere,
generations,
mutation_rate_sphere,
fitness,
2,
)
assert new_population.shape == (population_size, len(parm_names))
def test_rungaocython_continue_gao():
fitnesses = np.array([fitness(individual) for individual in SHARED["population"]])
new_population, best_pg = run_gao_cython.continue_gao(
population_size,
len(parm_names),
SHARED["population"],
fitnesses,
locs_sphere,
widths_sphere,
additional,
mutation_rate_sphere,
fitness,
1,
)
assert new_population.shape == (population_size, len(parm_names))
new_population, best_pg = run_gao_cython.continue_gao(
population_size,
len(parm_names),
SHARED["population"],
fitnesses,
locs_sphere,
widths_sphere,
additional,
mutation_rate_sphere,
fitness,
2,
)
assert new_population.shape == (population_size, len(parm_names))
if __name__ == "__main__":
test_rungaocython_run_gao()
test_rungaocython_continue_gao()