Passed
Push — master ( 4ef66c...afb360 )
by Simon
03:52
created

init_grid_search()   A

Complexity

Conditions 3

Size

Total Lines 22
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 15
nop 2
dl 0
loc 22
rs 9.65
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 .conv import values2positions
9
10
11
class Initializer:
12
    def __init__(self, search_space):
13
        self.search_space = search_space
14
        self.dim_sizes = np.array([array.size - 1 for array in search_space])
15
16
    def set_pos(self, initialize):
17
        init_positions_list = []
18
19
        if "random" in initialize:
20
            positions = self._init_random_search(initialize["random"])
21
            init_positions_list.append(positions)
22
        if "grid" in initialize:
23
            positions = self._init_grid_search(initialize["grid"])
24
            init_positions_list.append(positions)
25
        if "vertices" in initialize:
26
            positions = self._init_vertices(initialize["vertices"])
27
            init_positions_list.append(positions)
28
        if "warm_start" in initialize:
29
            positions = values2positions(self.search_space, initialize["warm_start"])
30
            init_positions_list.append(positions)
31
32
        return [item for sublist in init_positions_list for item in sublist]
33
34
    def _init_random_search(self, n_pos):
35
        positions = []
36
37
        if n_pos == 0:
38
            return positions
39
40
        for nth_pos in range(n_pos):
41
            pos = np.random.randint(self.dim_sizes, size=self.dim_sizes.shape)
42
            positions.append(pos)
43
44
        return positions
45
46
    def _fill_rest_random(self, n_pos, positions):
47
        diff_pos = n_pos - len(positions)
48
        if diff_pos > 0:
49
            pos_rnd = self._init_random_search(n_pos=diff_pos)
50
51
            return positions + pos_rnd
52
        else:
53
            return positions
54
55
    def _init_grid_search(self, n_pos):
56
        positions = []
57
58
        if n_pos == 0:
59
            return positions
60
61
        n_dim = len(self.dim_sizes)
62
        p_per_dim = int(np.power(n_pos, 1 / n_dim))
63
64
        for dim in self.dim_sizes:
65
            dim_dist = int(dim / (p_per_dim + 1))
66
            n_points = [n * dim_dist for n in range(1, p_per_dim + 1)]
67
68
            positions.append(n_points)
69
70
        pos_mesh = np.array(np.meshgrid(*positions))
71
        positions = list(pos_mesh.T.reshape(-1, n_dim))
72
73
        positions = self._fill_rest_random(n_pos, positions)
74
75
        return positions
76
77
    def _init_vertices(self, n_pos):
78
        positions = []
79
80
        if n_pos == 0:
81
            return positions
82
83
        zero_array = np.zeros(self.dim_sizes.shape)
84
        sub_arrays = []
85
86
        for dim in self.dim_sizes:
87
            sub_array = np.array([0, dim])
88
            sub_arrays.append(sub_array)
89
90
        n_dims = len(self.dim_sizes)
91
        pos_comb_np = list(np.array(np.meshgrid(*sub_arrays)).T.reshape(-1, n_dims))
92
        k = min(len(pos_comb_np), n_pos)
93
94
        positions = random.sample(pos_comb_np, k)
95
96
        positions = self._fill_rest_random(n_pos, positions)
97
98
        return positions
99
100