gradient_free_optimizers.optimizers.pop_opt.differential_evolution   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 92
Duplicated Lines 31.52 %

Importance

Changes 0
Metric Value
wmc 8
eloc 66
dl 29
loc 92
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A DifferentialEvolutionOptimizer.mutation() 0 5 1
A DifferentialEvolutionOptimizer._constraint_loop() 0 5 3
A DifferentialEvolutionOptimizer.iterate() 0 19 1
A DifferentialEvolutionOptimizer.evaluate() 0 3 1
A DifferentialEvolutionOptimizer.init_pos() 0 6 1
A DifferentialEvolutionOptimizer.__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
import random
6
import numpy as np
7
8
from ._evolutionary_algorithm import EvolutionaryAlgorithmOptimizer
9
from ._individual import Individual
10
11
12
class DifferentialEvolutionOptimizer(EvolutionaryAlgorithmOptimizer):
13
    name = "Differential Evolution"
14
    _name_ = "differential_evolution"
15
    __name__ = "DifferentialEvolutionOptimizer"
16
17
    optimizer_type = "population"
18
    computationally_expensive = False
19
20 View Code Duplication
    def __init__(
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
21
        self,
22
        search_space,
23
        initialize={"grid": 4, "random": 2, "vertices": 4},
24
        constraints=[],
25
        random_state=None,
26
        rand_rest_p=0,
27
        nth_process=None,
28
        population=10,
29
        mutation_rate=0.9,
30
        crossover_rate=0.9,
31
    ):
32
        super().__init__(
33
            search_space=search_space,
34
            initialize=initialize,
35
            constraints=constraints,
36
            random_state=random_state,
37
            rand_rest_p=rand_rest_p,
38
            nth_process=nth_process,
39
        )
40
41
        self.population = population
42
        self.mutation_rate = mutation_rate
43
        self.crossover_rate = crossover_rate
44
45
        self.individuals = self._create_population(Individual)
46
        self.optimizers = self.individuals
47
48
        self.offspring_l = []
49
50
    def mutation(self, f=1):
51
        ind_selected = random.sample(self.individuals, 3)
52
53
        x_1, x_2, x_3 = [ind.pos_best for ind in ind_selected]
54
        return x_1 + self.mutation_rate * np.subtract(x_2, x_3)
55
56
    def _constraint_loop(self, position):
57
        while True:
58
            if self.conv.not_in_constraint(position):
59
                return position
60
            position = self.p_current.move_climb(position, epsilon_mod=0.3)
61
62
    @EvolutionaryAlgorithmOptimizer.track_new_pos
63
    def init_pos(self):
64
        nth_pop = self.nth_trial % len(self.individuals)
65
66
        self.p_current = self.individuals[nth_pop]
67
        return self.p_current.init_pos()
68
69
    @EvolutionaryAlgorithmOptimizer.track_new_pos
70
    def iterate(self):
71
        self.p_current = self.individuals[
72
            self.nth_trial % len(self.individuals)
73
        ]
74
        target_vector = self.p_current.pos_new
75
76
        mutant_vector = self.mutation()
77
78
        crossover_rates = [1 - self.crossover_rate, self.crossover_rate]
79
        pos_new = self.discrete_recombination(
80
            [target_vector, mutant_vector],
81
            crossover_rates,
82
        )
83
        pos_new = self.conv2pos(pos_new)
84
        pos_new = self._constraint_loop(pos_new)
85
86
        self.p_current.pos_new = self.conv2pos(pos_new)
87
        return self.p_current.pos_new
88
89
    @EvolutionaryAlgorithmOptimizer.track_new_score
90
    def evaluate(self, score_new):
91
        self.p_current.evaluate(score_new)  # selection
92