Passed
Push — master ( cde1a7...d8a990 )
by Simon
04:04
created

Initializer._init_vertices()   A

Complexity

Conditions 3

Size

Total Lines 12
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 10
nop 2
dl 0
loc 12
rs 9.9
c 0
b 0
f 0
1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
import random
6
import numpy as np
7
8
from .utils import move_random
9
10
11
class Initializer:
12
    def __init__(self, conv):
13
        self.conv = conv
14
15
    def set_pos(self, initialize):
16
        init_positions_list = []
17
18
        if "random" in initialize:
19
            positions = self._init_random_search(initialize["random"])
20
            init_positions_list.append(positions)
21
        if "grid" in initialize:
22
            positions = self._init_grid_search(initialize["grid"])
23
            init_positions_list.append(positions)
24
        if "vertices" in initialize:
25
            positions = self._init_vertices(initialize["vertices"])
26
            init_positions_list.append(positions)
27
        if "warm_start" in initialize:
28
            positions = self._init_warm_start(initialize["warm_start"])
29
            init_positions_list.append(positions)
30
31
        return [item for sublist in init_positions_list for item in sublist]
32
33
    def _init_warm_start(self, value_list):
34
        positions = []
35
36
        for value_ in value_list:
37
            pos = self.conv.value2position(list(value_.values()))
38
            positions.append(pos)
39
40
        return positions
41
42
    def _init_random_search(self, n_pos):
43
        positions = []
44
45
        if n_pos == 0:
46
            return positions
47
48
        for nth_pos in range(n_pos):
49
            pos = move_random(self.conv.search_space_positions)
50
            positions.append(pos)
51
52
        return positions
53
54
    def _fill_rest_random(self, n_pos, positions):
55
        diff_pos = n_pos - len(positions)
56
        if diff_pos > 0:
57
            pos_rnd = self._init_random_search(n_pos=diff_pos)
58
59
            return positions + pos_rnd
60
        else:
61
            return positions
62
63
    def _init_grid_search(self, n_pos):
64
        positions = []
65
66
        if n_pos == 0:
67
            return positions
68
69
        n_dim = len(self.conv.max_positions)
70
        if n_dim > 30:
71
            positions = []
72
        else:
73
            p_per_dim = int(np.power(n_pos, 1 / n_dim))
74
75
            for dim in self.conv.max_positions:
76
                dim_dist = int(dim / (p_per_dim + 1))
77
                n_points = [n * dim_dist for n in range(1, p_per_dim + 1)]
78
79
                positions.append(n_points)
80
81
            pos_mesh = np.array(np.meshgrid(*positions))
82
            positions = list(pos_mesh.T.reshape(-1, n_dim))
83
84
        positions = self._fill_rest_random(n_pos, positions)
85
        return positions
86
87
    def _get_random_vertex(self):
88
        vertex = []
89
        for dim_positions in self.conv.search_space_positions:
90
            rnd = random.randint(0, 1)
91
92
            if rnd == 0:
93
                dim_pos = min(dim_positions)
94
            elif rnd == 1:
95
                dim_pos = max(dim_positions)
96
97
            vertex.append(dim_pos)
0 ignored issues
show
introduced by
The variable dim_pos does not seem to be defined for all execution paths.
Loading history...
98
        return np.array(vertex)
99
100
    def _init_vertices(self, n_pos):
101
        positions = []
102
        for _ in range(n_pos):
103
            vertex = self._get_random_vertex()
104
105
            vert_in_list = any((vertex == pos).all() for pos in positions)
106
            if not vert_in_list:
107
                positions.append(vertex)
108
            else:
109
                pos = move_random(self.conv.search_space_positions)
110
                positions.append(pos)
111
        return positions
112