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