forked from blakeaw/GAlibrate
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_rungaojulia.py
146 lines (124 loc) · 4.24 KB
/
test_rungaojulia.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
141
142
143
144
145
146
import numpy as np
from galibrate import run_gao_julia
pop_size = 8
n_params = 2
locs = np.zeros(n_params)
widths = np.ones(n_params)
def test_rungaojulia_random_population():
rand_pop = run_gao_julia.Main.random_population(pop_size, n_params, locs, widths)
assert rand_pop.shape == (pop_size, n_params)
assert (rand_pop < 1.0).all()
assert (rand_pop > 0.0).all()
def test_rungaojulia_choose_mating_pairs():
fit_idx = np.zeros((pop_size, 2))
fit_idx[:, 1] = np.arange(pop_size)
fit_idx[:, 0] = np.arange(pop_size)
survivors = fit_idx[: int(pop_size / 2)]
mating_pairs = run_gao_julia.choose_mating_pairs(survivors, pop_size)
assert mating_pairs.shape == (int(pop_size / 4), 2)
assert (mating_pairs < pop_size).all()
assert (mating_pairs > -1).all()
def test_rungaojulia_crossover():
chromosome_1 = np.zeros(n_params)
chromosome_2 = np.ones(n_params)
children = run_gao_julia.Main.crossover(chromosome_1, chromosome_2, n_params)
assert children.shape == (2, n_params)
assert (children >= 0.0).all()
assert (children <= 1.0).all()
def test_rungaojulia_mutation():
rand_pop = run_gao_julia.Main.random_population(pop_size, n_params, locs, widths)
mutation_rate = 1.1
# Shift the locs up by a small amount to make
# sure all mutants should be different than the originals.
locs_adjusted = locs + 0.1
# population = rand_pop.copy()
population = run_gao_julia.Main.mutation(
rand_pop, locs_adjusted, widths, pop_size, n_params, mutation_rate
)
assert not np.allclose(rand_pop, population)
# Now all mutants should be the same as the originals.
mutation_rate = 0.0
# population = rand_pop.copy()
population = run_gao_julia.Main.mutation(
rand_pop, locs, widths, pop_size, n_params, mutation_rate
)
assert np.allclose(rand_pop, population)
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_rungaojulia_run_gao():
new_population, best_pg = run_gao_julia.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_julia.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_rungaojulia_continue_gao():
fitnesses = np.array([fitness(individual) for individual in SHARED["population"]])
new_population, best_pg = run_gao_julia.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_julia.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_rungaojulia_random_population()
test_rungaojulia_choose_mating_pairs()
test_rungaojulia_crossover()
test_rungaojulia_mutation()
test_rungaojulia_run_gao()
test_rungaojulia_continue_gao()