Passed
Push — master ( 626f23...be3b1e )
by Simon
01:48
created

BasePopulationOptimizer._iterations()   A

Complexity

Conditions 2

Size

Total Lines 6
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 5
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
6
import random
7
import numpy as np
8
9
from ..search_tracker import SearchTracker
10
from ...converter import Converter
11
from ...results_manager import ResultsManager
12
from ...optimizers.base_optimizer import get_n_inits
13
from ...init_positions import Initializer
14
15
16 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...
17
    """
18
    Sets the random seed separately for each thread
19
    (to avoid getting the same results in each thread)
20
    """
21
    if nth_process is None:
22
        nth_process = 0
23
24
    if random_state is None:
25
        random_state = np.random.randint(0, high=2 ** 31 - 2, dtype=np.int64)
26
27
    random.seed(random_state + nth_process)
28
    np.random.seed(random_state + nth_process)
29
30
31
class BasePopulationOptimizer:
32
    def __init__(
33
        self,
34
        search_space,
35
        initialize={"grid": 4, "random": 2, "vertices": 4},
36
        random_state=None,
37
        rand_rest_p=0,
38
        nth_process=None,
39
    ):
40
        super().__init__()
41
        self.conv = Converter(search_space)
42
        self.results_mang = ResultsManager(self.conv)
43
        self.initialize = initialize
44
        self.random_state = random_state
45
        self.rand_rest_p = rand_rest_p
46
        self.nth_process = nth_process
47
48
        self.eval_times = []
49
        self.iter_times = []
50
51
        set_random_seed(nth_process, random_state)
52
53
        # get init positions
54
        init = Initializer(self.conv)
55
        self.init_positions = init.set_pos(self.initialize)
56
57
    def _iterations(self, positioners):
58
        nth_iter = 0
59
        for p in positioners:
60
            nth_iter = nth_iter + len(p.pos_new_list)
61
62
        return nth_iter
63
64
    def _create_population(self, Optimizer):
65
        if isinstance(self.population, int):
66
            population = []
67
            for pop_ in range(self.population):
68
                population.append(
69
                    Optimizer(self.conv.search_space, rand_rest_p=self.rand_rest_p)
70
                )
71
        else:
72
            population = self.population
73
74
        n_inits = get_n_inits(self.initialize)
75
76
        if n_inits < len(population):
77
            print("\n Warning: Not enough initial positions for population size")
78
            print(" Population size is reduced to", n_inits)
79
            population = population[:n_inits]
80
81
        return population
82
83
    def finish_initialization(self):
84
        pass
85