Passed
Push — master ( 13200e...4b50d2 )
by Simon
04:24
created

create_performance_data.create_performance_data()   B

Complexity

Conditions 3

Size

Total Lines 61
Code Lines 46

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 46
nop 4
dl 0
loc 61
rs 8.7672
c 0
b 0
f 0

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
import time
2
from tqdm import tqdm
3
import numpy as np
4
import pandas as pd
5
6
from gradient_free_optimizers import (
7
    HillClimbingOptimizer,
8
    StochasticHillClimbingOptimizer,
9
    TabuOptimizer,
10
    RandomSearchOptimizer,
11
    RandomRestartHillClimbingOptimizer,
12
    RandomAnnealingOptimizer,
13
    SimulatedAnnealingOptimizer,
14
    ParallelTemperingOptimizer,
15
    ParticleSwarmOptimizer,
16
    EvolutionStrategyOptimizer,
17
    BayesianOptimizer,
18
    TreeStructuredParzenEstimators,
19
    DecisionTreeOptimizer,
20
    EnsembleOptimizer,
21
)
22
23
24
n_inits = 4
25
26
optimizer_dict = {
27
    "Hill climbing": HillClimbingOptimizer,
28
    "Stochastic hill climbing": StochasticHillClimbingOptimizer,
29
    "Tabu search": TabuOptimizer,
30
    "Random search": RandomSearchOptimizer,
31
    "Random restart hill climbing": RandomRestartHillClimbingOptimizer,
32
    "Random annealing": RandomAnnealingOptimizer,
33
    "Simulated annealing": SimulatedAnnealingOptimizer,
34
    "Parallel tempering": ParallelTemperingOptimizer,
35
    "Particle swarm optimizer": ParticleSwarmOptimizer,
36
    "Evolution strategy": EvolutionStrategyOptimizer,
37
    # "Bayesian optimizer": BayesianOptimizer,
38
    # "Tree structured parzen estimators": TreeStructuredParzenEstimators,
39
    # "Decision tree optimizer": DecisionTreeOptimizer,
40
    # "Ensemble optimizer": EnsembleOptimizer,
41
}
42
43
44
def objective_function(pos_new):
45
    score = -(pos_new["x1"] * pos_new["x1"] + pos_new["x2"] * pos_new["x2"])
46
    return score
47
48
49
search_space = {"x1": np.arange(-10, 11, 0.1), "x2": np.arange(-10, 11, 0.1)}
50
51
runs = 30
52
53
54
def create_performance_data(
55
    study_name, objective_function, search_space, n_iter
56
):
57
    results = []
58
59
    for opt_name in tqdm(optimizer_dict.keys()):
60
        total_time_list = []
61
        eval_time_list = []
62
        iter_time_list = []
63
64
        for random_state in tqdm(range(runs)):
65
66
            c_time = time.time()
67
            opt = optimizer_dict[opt_name](search_space)
68
            opt.search(
69
                objective_function,
70
                n_iter=n_iter,
71
                verbosity=False,
72
                random_state=random_state,
73
            )
74
75
            total_time = time.time() - c_time
76
            eval_time = np.array(opt.eval_times).sum()
77
            iter_time = np.array(opt.iter_times).sum()
78
79
            total_time_list.append(total_time)
80
            eval_time_list.append(eval_time)
81
            iter_time_list.append(iter_time)
82
83
        total_time_mean = np.array(total_time_list).mean()
84
        eval_time_mean = np.array(eval_time_list).mean()
85
        iter_time_mean = np.array(iter_time_list).mean()
86
87
        total_time_std = np.array(total_time_list).std()
88
        eval_time_std = np.array(eval_time_list).std()
89
        iter_time_std = np.array(iter_time_list).std()
90
91
        results.append(
92
            [
93
                total_time_mean,
94
                total_time_std,
95
                eval_time_mean,
96
                eval_time_std,
97
                iter_time_mean,
98
                iter_time_std,
99
            ]
100
        )
101
102
    index = [
103
        "total_time_mean",
104
        "total_time_std",
105
        "eval_time_mean",
106
        "eval_time_std",
107
        "iter_time_mean",
108
        "iter_time_std",
109
    ]
110
    columns = list(optimizer_dict.keys())
111
112
    results = np.array(results).T
113
    results = pd.DataFrame(results, columns=columns, index=index)
114
    results.to_csv("./_data/" + study_name + ".csv")
115
116
117
create_performance_data(
118
    "simple function", objective_function, search_space, n_iter=50
119
)
120
121