Passed
Push — master ( 0f0b8d...36e873 )
by Simon
01:29
created

gradient_free_optimizers.utils   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 31
Duplicated Lines 41.94 %

Importance

Changes 0
Metric Value
eloc 16
dl 13
loc 31
rs 10
c 0
b 0
f 0
wmc 5

2 Functions

Rating   Name   Duplication   Size   Complexity  
A move_random() 0 7 2
A set_random_seed() 13 13 3

How to fix   Duplicated Code   

Duplicated Code

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
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import random
6
import numpy as np
7
8
9 View Code Duplication
def set_random_seed(nth_process, random_state):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
10
    """
11
    Sets the random seed separately for each thread
12
    (to avoid getting the same results in each thread)
13
    """
14
    if nth_process is None:
15
        nth_process = 0
16
17
    if random_state is None:
18
        random_state = np.random.randint(0, high=2 ** 31 - 2, dtype=np.int64)
19
20
    random.seed(random_state + nth_process)
21
    np.random.seed(random_state + nth_process)
22
23
24
def move_random(ss_positions):
25
    position = []
26
    for search_space_pos in ss_positions:
27
        pos_ = random.choice(search_space_pos)
28
        position.append(pos_)
29
30
    return np.array(position)
31