Completed
Push — master ( 602eea...026eaa )
by
unknown
04:14 queued 01:18
created

SelfAdaptiveDifferentialEvolutionAlgorithm   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 86
Duplicated Lines 20.93 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 18
loc 86
rs 10
wmc 18

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
import random as rnd
2
import copy
3
4
__all__ = ['SelfAdaptiveDifferentialEvolutionAlgorithm']
5
6
7
class SolutionjDE(object):
8
    def __init__(self, D, LB, UB):
9
        self.D = D
10
        self.LB = LB
11
        self.UB = UB
12
        self.F = 0.5
13
        self.CR = 0.9
14
        self.Solution = []
15
        self.Fitness = float('inf')
16
        self.generateSolution()
17
18
    def generateSolution(self):
19
        self.Solution = [self.LB + (self.UB - self.LB) * rnd.random()
20
                         for _i in range(self.D)]
21
22 View Code Duplication
    def evaluate(self):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
23
        self.Fitness = SolutionjDE.FuncEval(self.D, self.Solution)
24
25
    def repair(self):
26
        for i in range(self.D):
27
            if self.Solution[i] > self.UB:
28
                self.Solution[i] = self.UB
29
            if self.Solution[i] < self.LB:
30
                self.Solution[i] = self.LB
31
32
    def __eq__(self, other):
33
        return self.Solution == other.Solution and self.Fitness == other.Fitness
34
35
36
class SelfAdaptiveDifferentialEvolutionAlgorithm(object):
37
38
    # pylint: disable=too-many-instance-attributes
39
    def __init__(self, D, NP, nFES, Lower, Upper, function):
40
41
    """Self-adaptive differential evolution algorithm.
42
43
    Date: 7. 2. 2018
44
45
    Authors : Uros Mlakar
46
47
    License: MIT
48
49
    Reference paper: Brest, J., Greiner, S., Boskovic, B., Mernik, M., Zumer, V. Self-adapting control
50
    parameters in differential evolution: A comparative study on numerical benchmark problems.
51
    IEEE transactions on evolutionary computation, 10(6), 646-657, 2006.
52
53
    TODO
54
    """
0 ignored issues
show
introduced by
expected an indented block (<string>, line 54)
Loading history...
55
56
    def __init__(self, D, NP, nFES, F, CR, Lower, Upper, function):
57
58
        # TODO: check for F and CR parameters!
59
        self.D = D  # dimension of problem
60
        self.Np = NP  # population size
61
        self.nFES = nFES  # number of function evaluations
62
        self.Lower = Lower  # lower bound
63
        self.Upper = Upper  # upper bound
64
65
        SolutionjDE.FuncEval = staticmethod(function)
66
        self.Population = []
67
        self.FEs = 0
68
        self.Done = False
69
        self.bestSolution = SolutionjDE(self.D, Lower, Upper)
70
        self.Tao = None  # EDITED: check please
71
72
    def evalPopulation(self):
73
        for p in self.Population:
74
            p.evaluate()
75
            if p.Fitness < self.bestSolution.Fitness:
76
                self.bestSolution = copy.deepcopy(p)
77
78
    def initPopulation(self):
79
        for _i in range(self.Np):
80
            self.Population.append(SolutionjDE(self.D, self.Lower, self.Upper))
81
82
    def tryEval(self,v):
83
        if self.FEs <= self.nFES:
84
            v.evaluate()
85
            self.FEs+=1
86
        else:
87
            self.Done = True
88
89
    def generationStep(self, Population):
90
        newPopulation = []
91
        for i in range(self.Np):
92
            newSolution = SolutionjDE(self.D, self.Lower, self.Upper)
93
94
            if rnd.random() < self.Tao:
95
                newSolution.F = rnd.random()
96
            else:
97
                newSolution.F = Population[i].F
98
99 View Code Duplication
            if rnd.random() < self.Tao:
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
100
                newSolution.CR = rnd.random()
101
            else:
102
                newSolution.CR = Population[i].CR
103
104
            r = rnd.sample(range(0, self.Np), 3)
105
            while i in r:
106
                r = rnd.sample(range(0, self.Np), 3)
107
            jrand = int(rnd.random() * self.Np)
108
109
            for j in range(self.D):
110
                if rnd.random() < newSolution.CR or j == jrand:
111
                    newSolution.Solution[j] = Population[r[0]].Solution[j] + newSolution.F * (
112
                        Population[r[1]].Solution[j] - Population[r[2]].Solution[j])
113
                else:
114
                    newSolution.Solution[j] = Population[i].Solution[j]
115
            newSolution.repair()
116
            self.tryEval(newSolution)
117
118
            if newSolution.Fitness < self.bestSolution.Fitness:
119
                self.bestSolution = copy.deepcopy(newSolution)
120
            if newSolution.Fitness < self.Population[i].Fitness:
121
                newPopulation.append(newSolution)
122
            else:
123
                newPopulation.append(Population[i])
124
        return newPopulation
125
126
    def run(self):
127
        self.initPopulation()
128
        self.evalPopulation()
129
        self.FEs = self.Np
130
        while not self.Done:
131
            self.Population = self.generationStep(self.Population)
132
        return self.bestSolution.Fitness
133