|
1
|
|
|
# Author: Simon Blanke |
|
2
|
|
|
# Email: [email protected] |
|
3
|
|
|
# License: MIT License |
|
4
|
|
|
|
|
5
|
|
|
import numpy as np |
|
6
|
|
|
|
|
7
|
|
|
from hyperactive import Hyperactive |
|
8
|
|
|
|
|
9
|
|
|
X, y = np.array([0]), np.array([0]) |
|
10
|
|
|
memory = False |
|
11
|
|
|
n_iter = 25 |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
def sphere_function(para, X_train, y_train): |
|
15
|
|
|
loss = [] |
|
16
|
|
|
for key in para.keys(): |
|
17
|
|
|
if key == "iteration": |
|
18
|
|
|
continue |
|
19
|
|
|
loss.append(para[key] * para[key]) |
|
20
|
|
|
|
|
21
|
|
|
return -np.array(loss).sum() |
|
22
|
|
|
|
|
23
|
|
|
|
|
24
|
|
|
search_config = { |
|
25
|
|
|
sphere_function: {"x1": np.arange(-3, 3, 0.1), "x2": np.arange(-3, 3, 0.1)} |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
|
|
29
|
|
|
def test_n_particles(): |
|
30
|
|
|
for n_particles in [2, 100]: |
|
31
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
32
|
|
|
opt.search( |
|
33
|
|
|
search_config, |
|
34
|
|
|
n_iter=n_iter, |
|
35
|
|
|
optimizer={"ParticleSwarm": {"n_particles": n_particles}}, |
|
36
|
|
|
) |
|
37
|
|
|
|
|
38
|
|
|
|
|
39
|
|
|
def test_inertia(): |
|
40
|
|
|
for inertia in [0.1, 0.9]: |
|
41
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
42
|
|
|
opt.search( |
|
43
|
|
|
search_config, |
|
44
|
|
|
n_iter=n_iter, |
|
45
|
|
|
optimizer={"ParticleSwarm": {"inertia": inertia}}, |
|
46
|
|
|
) |
|
47
|
|
|
|
|
48
|
|
|
|
|
49
|
|
|
def test_cognitive_weight(): |
|
50
|
|
|
for cognitive_weight in [0.1, 0.9]: |
|
51
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
52
|
|
|
opt.search( |
|
53
|
|
|
search_config, |
|
54
|
|
|
n_iter=n_iter, |
|
55
|
|
|
optimizer={"ParticleSwarm": {"cognitive_weight": cognitive_weight}}, |
|
56
|
|
|
) |
|
57
|
|
|
|
|
58
|
|
|
|
|
59
|
|
|
def test_social_weight(): |
|
60
|
|
|
for social_weight in [0.1, 0.9]: |
|
61
|
|
|
opt = Hyperactive(X, y, memory=memory) |
|
62
|
|
|
opt.search( |
|
63
|
|
|
search_config, |
|
64
|
|
|
n_iter=n_iter, |
|
65
|
|
|
optimizer={"ParticleSwarm": {"social_weight": social_weight}}, |
|
66
|
|
|
) |
|
67
|
|
|
|