Conditions | 1 |
Total Lines | 69 |
Code Lines | 55 |
Lines | 69 |
Ratio | 100 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | # Author: Simon Blanke |
||
21 | View Code Duplication | def test_start_temp_0(): |
|
|
|||
22 | n_initialize = 1 |
||
23 | |||
24 | start_temp_0 = 0 |
||
25 | start_temp_1 = 0.1 |
||
26 | start_temp_10 = 1 |
||
27 | start_temp_100 = 100 |
||
28 | start_temp_inf = np.inf |
||
29 | |||
30 | epsilon = 1 / np.inf |
||
31 | |||
32 | opt = SimulatedAnnealingOptimizer( |
||
33 | search_space, |
||
34 | start_temp=start_temp_0, |
||
35 | epsilon=epsilon, |
||
36 | initialize={"random": n_initialize}, |
||
37 | ) |
||
38 | opt.search(objective_function, n_iter=n_iter) |
||
39 | n_transitions_0 = opt.n_transitions |
||
40 | |||
41 | opt = SimulatedAnnealingOptimizer( |
||
42 | search_space, |
||
43 | start_temp=start_temp_1, |
||
44 | epsilon=epsilon, |
||
45 | initialize={"random": n_initialize}, |
||
46 | ) |
||
47 | opt.search(objective_function, n_iter=n_iter) |
||
48 | n_transitions_1 = opt.n_transitions |
||
49 | |||
50 | opt = SimulatedAnnealingOptimizer( |
||
51 | search_space, |
||
52 | start_temp=start_temp_10, |
||
53 | epsilon=epsilon, |
||
54 | initialize={"random": n_initialize}, |
||
55 | ) |
||
56 | opt.search(objective_function, n_iter=n_iter) |
||
57 | n_transitions_10 = opt.n_transitions |
||
58 | |||
59 | opt = SimulatedAnnealingOptimizer( |
||
60 | search_space, |
||
61 | start_temp=start_temp_100, |
||
62 | epsilon=epsilon, |
||
63 | initialize={"random": n_initialize}, |
||
64 | ) |
||
65 | opt.search(objective_function, n_iter=n_iter) |
||
66 | n_transitions_100 = opt.n_transitions |
||
67 | |||
68 | opt = SimulatedAnnealingOptimizer( |
||
69 | search_space, |
||
70 | start_temp=start_temp_inf, |
||
71 | epsilon=epsilon, |
||
72 | initialize={"random": n_initialize}, |
||
73 | ) |
||
74 | opt.search(objective_function, n_iter=n_iter) |
||
75 | n_transitions_inf = opt.n_transitions |
||
76 | |||
77 | print("\n n_transitions_0", n_transitions_0) |
||
78 | print("\n n_transitions_1", n_transitions_1) |
||
79 | print("\n n_transitions_10", n_transitions_10) |
||
80 | print("\n n_transitions_100", n_transitions_100) |
||
81 | print("\n n_transitions_inf", n_transitions_inf) |
||
82 | |||
83 | assert n_transitions_0 == start_temp_0 |
||
84 | assert ( |
||
85 | n_transitions_1 |
||
86 | == n_transitions_10 |
||
87 | == n_transitions_100 |
||
88 | == n_transitions_inf |
||
89 | == n_iter - n_initialize |
||
90 | ) |
||
212 |