Passed
Push — master ( 7ebd63...8bd9a3 )
by Simon
06:11
created

PatternSearch.__init__()   A

Complexity

Conditions 1

Size

Total Lines 24
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 23
dl 0
loc 24
rs 9.328
c 0
b 0
f 0
cc 1
nop 10

How to fix   Many Parameters   

Many Parameters

Methods with many parameters are not only hard to understand, but their parameters also often become inconsistent when you need more, or different data.

There are several approaches to avoid long parameter lists:

1
# Author: Simon Blanke
2
# Email: [email protected]
3
# License: MIT License
4
5
from typing import List, Dict, Literal, Literal
6
7
from ..search import Search
8
from ..optimizers import PatternSearch as _PatternSearch
9
10
11
class PatternSearch(_PatternSearch, Search):
12
    """
13
    A class implementing the **pattern search** for the public API.
14
    Inheriting from the `Search`-class to get the `search`-method and from
15
    the `PatternSearch`-backend to get the underlying algorithm.
16
17
    Parameters
18
    ----------
19
    search_space : dict[str, list]
20
        The search space to explore. A dictionary with parameter
21
        names as keys and a numpy array as values.
22
    initialize : dict[str, int]
23
        The method to generate initial positions. A dictionary with
24
        the following key literals and the corresponding value type:
25
        {"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
26
    constraints : list[callable]
27
        A list of constraints, where each constraint is a callable.
28
        The callable returns `True` or `False` dependend on the input parameters.
29
    random_state : None, int
30
        If None, create a new random state. If int, create a new random state
31
        seeded with the value.
32
    rand_rest_p : float
33
        The probability of a random iteration during the the search process.
34
    n_positions : int
35
        Number of positions that the pattern consists of.
36
    pattern_size : float
37
        The initial size of the patterns in percentage of the size of the search space in the corresponding dimension.
38
    reduction : float
39
        The factor that reduces the size of the pattern if no better position is found.
40
    """
41
42
    def __init__(
43
        self,
44
        search_space: Dict[str, list],
45
        initialize: Dict[
46
            Literal["grid", "vertices", "random", "warm_start"], int | List
47
        ] = {"grid": 4, "random": 2, "vertices": 4},
48
        constraints: List[callable] = [],
49
        random_state: int = None,
50
        rand_rest_p: float = 0,
51
        nth_process: int = None,
52
        n_positions=4,
53
        pattern_size=0.25,
54
        reduction=0.9,
55
    ):
56
        super().__init__(
57
            search_space=search_space,
58
            initialize=initialize,
59
            constraints=constraints,
60
            random_state=random_state,
61
            rand_rest_p=rand_rest_p,
62
            nth_process=nth_process,
63
            n_positions=n_positions,
64
            pattern_size=pattern_size,
65
            reduction=reduction,
66
        )
67