gradient_free_optimizers.optimizer_search.parallel_tempering   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 65
Duplicated Lines 80 %

Importance

Changes 0
Metric Value
wmc 1
eloc 28
dl 52
loc 65
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A ParallelTemperingOptimizer.__init__() 23 23 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
    ParallelTemperingOptimizer as _ParallelTemperingOptimizer,
10
)
11
12
13 View Code Duplication
class ParallelTemperingOptimizer(_ParallelTemperingOptimizer, Search):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
14
    """
15
    A class implementing **parallel tempering** for the public API.
16
    Inheriting from the `Search`-class to get the `search`-method and from
17
    the `ParallelTemperingOptimizer`-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
    population : int
37
        The number of simulated annealers in the population.
38
    n_iter_swap : int
39
        The number of iterations the algorithm performs before switching temperatures of the individual optimizers in the population.
40
    """
41
42
    def __init__(
43
        self,
44
        search_space: Dict[str, list],
45
        initialize: Dict[
46
            Literal["grid", "vertices", "random", "warm_start"],
47
            Union[int, list[dict]],
48
        ] = {"grid": 4, "random": 2, "vertices": 4},
49
        constraints: List[callable] = [],
50
        random_state: int = None,
51
        rand_rest_p: float = 0,
52
        nth_process: int = None,
53
        population: int = 5,
54
        n_iter_swap: int = 5,
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
            population=population,
64
            n_iter_swap=n_iter_swap,
65
        )
66