| Total Complexity | 1 |
| Total Lines | 26 |
| Duplicated Lines | 42.31 % |
| Changes | 0 | ||
Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | import numpy as np |
||
| 2 | from gradient_free_optimizers import RandomSearchOptimizer |
||
| 3 | |||
| 4 | |||
| 5 | View Code Duplication | def ackley_function(para): |
|
|
|
|||
| 6 | x, y = para["x"], para["y"] |
||
| 7 | |||
| 8 | loss = ( |
||
| 9 | -20 * np.exp(-0.2 * np.sqrt(0.5 * (x * x + y * y))) |
||
| 10 | - np.exp(0.5 * (np.cos(2 * np.pi * x) + np.cos(2 * np.pi * y))) |
||
| 11 | + np.exp(1) |
||
| 12 | + 20 |
||
| 13 | ) |
||
| 14 | |||
| 15 | return -loss |
||
| 16 | |||
| 17 | |||
| 18 | search_space = { |
||
| 19 | "x": np.arange(-10, 10, 0.01), |
||
| 20 | "y": np.arange(-10, 10, 0.01), |
||
| 21 | } |
||
| 22 | |||
| 23 | |||
| 24 | opt = RandomSearchOptimizer(search_space) |
||
| 25 | opt.search(ackley_function, n_iter=30, verbosity=False) |
||
| 26 |