| Conditions | 2 |
| Total Lines | 64 |
| Code Lines | 43 |
| 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 | import numpy as np |
||
| 60 | @pytest.mark.parametrize(*optimizers) |
||
| 61 | def test_constr_opt_2(Optimizer): |
||
| 62 | n_iter = 50 |
||
| 63 | |||
| 64 | def objective_function(para): |
||
| 65 | score = -para["x1"] * para["x1"] |
||
| 66 | return score |
||
| 67 | |||
| 68 | search_space = { |
||
| 69 | "x1": np.arange(-10, 10, 0.1), |
||
| 70 | } |
||
| 71 | |||
| 72 | def constraint_1(para): |
||
| 73 | return para["x1"] > -5 |
||
| 74 | |||
| 75 | def constraint_2(para): |
||
| 76 | return para["x1"] < 5 |
||
| 77 | |||
| 78 | constraints_list = [constraint_1, constraint_2] |
||
| 79 | |||
| 80 | opt = Optimizer(search_space, constraints=constraints_list) |
||
| 81 | opt.search(objective_function, n_iter=n_iter) |
||
| 82 | |||
| 83 | search_data = opt.search_data |
||
| 84 | x0_values = search_data["x1"].values |
||
| 85 | |||
| 86 | print("\n search_data \n", search_data, "\n") |
||
| 87 | |||
| 88 | assert np.all(x0_values > -5) |
||
| 89 | assert np.all(x0_values < 5) |
||
| 90 | |||
| 91 | n_new_positions = 0 |
||
| 92 | n_new_scores = 0 |
||
| 93 | |||
| 94 | n_current_positions = 0 |
||
| 95 | n_current_scores = 0 |
||
| 96 | |||
| 97 | n_best_positions = 0 |
||
| 98 | n_best_scores = 0 |
||
| 99 | |||
| 100 | for optimizer in opt.optimizers: |
||
| 101 | n_new_positions = n_new_positions + len(optimizer.pos_new_list) |
||
| 102 | n_new_scores = n_new_scores + len(optimizer.score_new_list) |
||
| 103 | |||
| 104 | n_current_positions = n_current_positions + len(optimizer.pos_current_list) |
||
| 105 | n_current_scores = n_current_scores + len(optimizer.score_current_list) |
||
| 106 | |||
| 107 | n_best_positions = n_best_positions + len(optimizer.pos_best_list) |
||
| 108 | n_best_scores = n_best_scores + len(optimizer.score_best_list) |
||
| 109 | |||
| 110 | print("\n optimizer", optimizer) |
||
| 111 | print(" n_new_positions", optimizer.pos_new_list) |
||
| 112 | print(" n_new_scores", optimizer.score_new_list) |
||
| 113 | |||
| 114 | assert n_new_positions == n_iter |
||
| 115 | assert n_new_scores == n_iter |
||
| 116 | |||
| 117 | assert n_current_positions == n_current_scores |
||
| 118 | assert n_current_positions <= n_new_positions |
||
| 119 | |||
| 120 | assert n_best_positions == n_best_scores |
||
| 121 | assert n_best_positions <= n_new_positions |
||
| 122 | |||
| 123 | assert n_new_positions == n_new_scores |
||
| 124 |