gradient_free_optimizers.optimizer_search.random_annealing   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 78
Duplicated Lines 85.9 %

Importance

Changes 0
Metric Value
wmc 1
eloc 35
dl 67
loc 78
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A RandomAnnealingOptimizer.__init__() 31 31 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 RandomAnnealingOptimizer as _RandomAnnealingOptimizer
9
10
11 View Code Duplication
class RandomAnnealingOptimizer(_RandomAnnealingOptimizer, Search):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
12
    """
13
    A class implementing **random annealing** for the public API.
14
    Inheriting from the `Search`-class to get the `search`-method and from
15
    the `RandomAnnealingOptimizer`-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 search process.
34
    epsilon : float
35
        The step-size for the climbing.
36
    distribution : str
37
        The type of distribution to sample from.
38
    n_neighbours : int
39
        The number of neighbours to sample and evaluate before moving to the best
40
        of those neighbours.
41
    annealing_rate : float
42
        The annealing rate for the temperature.
43
    start_temp : float
44
        The initial temperature.
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
        annealing_rate=0.98,
64
        start_temp=10,
65
    ):
66
        super().__init__(
67
            search_space=search_space,
68
            initialize=initialize,
69
            constraints=constraints,
70
            random_state=random_state,
71
            rand_rest_p=rand_rest_p,
72
            nth_process=nth_process,
73
            epsilon=epsilon,
74
            distribution=distribution,
75
            n_neighbours=n_neighbours,
76
            annealing_rate=annealing_rate,
77
            start_temp=start_temp,
78
        )
79