-
Notifications
You must be signed in to change notification settings - Fork 87
/
Copy pathtest_max_score.py
79 lines (59 loc) · 1.72 KB
/
test_max_score.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
import time
import numpy as np
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import cross_val_score
from sklearn.tree import DecisionTreeClassifier
from gradient_free_optimizers import (
RandomSearchOptimizer,
HillClimbingOptimizer,
)
def objective_function(para):
score = -para["x1"] * para["x1"]
return score
search_space = {
"x1": np.arange(0, 100000, 0.1),
}
def test_max_score_0():
def objective_function(para):
score = -para["x1"] * para["x1"]
return score
search_space = {
"x1": np.arange(0, 100, 0.1),
}
max_score = -9999
opt = HillClimbingOptimizer(
search_space,
initialize={"warm_start": [{"x1": 99}]},
epsilon=0.01,
rand_rest_p=0,
)
opt.search(
objective_function,
n_iter=100000,
max_score=max_score,
)
print("\n Results head \n", opt.search_data.head())
print("\n Results tail \n", opt.search_data.tail())
print("\nN iter:", len(opt.search_data))
assert -100 > opt.best_score > max_score
def test_max_score_1():
def objective_function(para):
score = -para["x1"] * para["x1"]
time.sleep(0.01)
return score
search_space = {
"x1": np.arange(0, 100, 0.1),
}
max_score = -9999
c_time = time.time()
opt = HillClimbingOptimizer(search_space, initialize={"warm_start": [{"x1": 99}]})
opt.search(
objective_function,
n_iter=100000,
max_score=max_score,
)
diff_time = time.time() - c_time
print("\n Results head \n", opt.search_data.head())
print("\n Results tail \n", opt.search_data.tail())
print("\nN iter:", len(opt.search_data))
assert diff_time < 1