gradient_free_optimizers.optimizer_search.stochastic_hill_climbing   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 76
Duplicated Lines 82.89 %

Importance

Changes 0
Metric Value
wmc 1
eloc 34
dl 63
loc 76
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A StochasticHillClimbingOptimizer.__init__() 29 29 1

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
from typing import List, Dict, Literal, Union
6
7
from ..search import Search
8
from ..optimizers import (
9
    StochasticHillClimbingOptimizer as _StochasticHillClimbingOptimizer,
10
)
11
12
13 View Code Duplication
class StochasticHillClimbingOptimizer(_StochasticHillClimbingOptimizer, Search):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    """
15
    A class implementing the **stochastic hill climbing optimizer** for the public API.
16
    Inheriting from the `Search`-class to get the `search`-method and from
17
    the `StochasticHillClimbingOptimizer`-backend to get the underlying algorithm.
18
19
    Parameters
20
    ----------
21
    search_space : dict[str, list]
22
        The search space to explore. A dictionary with parameter
23
        names as keys and a numpy array as values.
24
    initialize : dict[str, int]
25
        The method to generate initial positions. A dictionary with
26
        the following key literals and the corresponding value type:
27
        {"grid": int, "vertices": int, "random": int, "warm_start": list[dict]}
28
    constraints : list[callable]
29
        A list of constraints, where each constraint is a callable.
30
        The callable returns `True` or `False` dependend on the input parameters.
31
    random_state : None, int
32
        If None, create a new random state. If int, create a new random state
33
        seeded with the value.
34
    rand_rest_p : float
35
        The probability of a random iteration during the the search process.
36
    epsilon : float
37
        The step-size for the climbing.
38
    distribution : str
39
        The type of distribution to sample from.
40
    n_neighbours : int
41
        The number of neighbours to sample and evaluate before moving to the best
42
        of those neighbours.
43
    p_accept : float, default=0.5
44
        probability to accept a worse solution
45
    """
46
47
    def __init__(
48
        self,
49
        search_space: Dict[str, list],
50
        initialize: Dict[
51
            Literal["grid", "vertices", "random", "warm_start"],
52
            Union[int, list[dict]],
53
        ] = {"grid": 4, "random": 2, "vertices": 4},
54
        constraints: List[callable] = [],
55
        random_state: int = None,
56
        rand_rest_p: float = 0,
57
        nth_process: int = None,
58
        epsilon: float = 0.03,
59
        distribution: Literal[
60
            "normal", "laplace", "gumbel", "logistic"
61
        ] = "normal",
62
        n_neighbours: int = 3,
63
        p_accept: float = 0.5,
64
    ):
65
        super().__init__(
66
            search_space=search_space,
67
            initialize=initialize,
68
            constraints=constraints,
69
            random_state=random_state,
70
            rand_rest_p=rand_rest_p,
71
            nth_process=nth_process,
72
            epsilon=epsilon,
73
            distribution=distribution,
74
            n_neighbours=n_neighbours,
75
            p_accept=p_accept,
76
        )
77