-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathtest_objective_functions.py
69 lines (48 loc) · 1.71 KB
/
test_objective_functions.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
import numpy as np
from sklearn.datasets import load_iris
from sklearn.neighbors import KNeighborsClassifier
from sklearn.model_selection import cross_val_score
from gradient_free_optimizers import RandomSearchOptimizer
def test_function():
def objective_function(para):
score = -para["x1"] * para["x1"]
return score
search_space = {
"x1": np.arange(-100, 101, 1),
}
opt = RandomSearchOptimizer(search_space)
opt.search(objective_function, n_iter=30)
def test_sklearn():
data = load_iris()
X, y = data.data, data.target
def model(para):
knr = KNeighborsClassifier(n_neighbors=para["n_neighbors"])
scores = cross_val_score(knr, X, y, cv=5)
score = scores.mean()
return score
search_space = {
"n_neighbors": np.arange(1, 51, 1),
}
opt = RandomSearchOptimizer(search_space)
opt.search(model, n_iter=30)
def test_obj_func_return_dictionary_0():
def objective_function(para):
score = -para["x1"] * para["x1"]
return score, {"_x1_": para["x1"]}
search_space = {
"x1": np.arange(-100, 101, 1),
}
opt = RandomSearchOptimizer(search_space)
opt.search(objective_function, n_iter=30)
assert "_x1_" in list(opt.search_data.columns)
def test_obj_func_return_dictionary_1():
def objective_function(para):
score = -para["x1"] * para["x1"]
return score, {"_x1_": para["x1"], "_x1_*2": para["x1"] * 2}
search_space = {
"x1": np.arange(-100, 101, 1),
}
opt = RandomSearchOptimizer(search_space)
opt.search(objective_function, n_iter=30)
assert "_x1_" in list(opt.search_data.columns)
assert "_x1_*2" in list(opt.search_data.columns)