Passed
Pull Request — master (#24)
by Grega
01:26
created

SelfAdaptiveDifferentialEvolutionAlgorithm   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 68
rs 10
wmc 18

5 Methods

Rating   Name   Duplication   Size   Complexity  
F generationStep() 0 36 10
A initPopulation() 0 3 2
A __init__() 0 10 1
A run() 0 8 2
A evalPopulation() 0 5 3
1
"""Self-adaptive differential evolution algorithm.
2
3
Date: 7. 2. 2018
4
5
Authors : Uros Mlakar
6
7
License: MIT
8
9
Reference paper: Brest, J., Greiner, S., Boskovic, B., Mernik, M., Zumer, V. Self-adapting control 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
10
parameters in differential evolution: A comparative study on numerical benchmark problems. 
0 ignored issues
show
Coding Style introduced by
Trailing whitespace
Loading history...
11
IEEE transactions on evolutionary computation, 10(6), 646-657, 2006.
12
13
TODO
14
"""
15
16
import random as rnd
17
import copy
18
19
__all__ = ['SelfAdaptiveDifferentialEvolutionAlgorithm']
20
21
22 View Code Duplication
class SolutionjDE(object):
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
23
    def __init__(self, D, LB, UB):
24
        self.D = D
25
        self.LB = LB
26
        self.UB = UB
27
        self.F = 0.5
28
        self.Cr = 0.9
29
        self.Solution = []
30
        self.Fitness = float('inf')
31
        self.generateSolution()
32
33
    def generateSolution(self):
34
        self.Solution = [self.LB + (self.UB - self.LB) * rnd.random()
35
                         for _i in range(self.D)]
36
37
    def evaluate(self):
38
        self.Fitness = SolutionjDE.FuncEval(self.D, self.Solution)
39
40
    def repair(self):
41
        for i in range(self.D):
42
            if self.Solution[i] > self.UB:
43
                self.Solution[i] = self.UB
44
            if self.Solution[i] < self.LB:
45
                self.Solution[i] = self.LB
46
47
    def __eq__(self, other):
48
        return self.Solution == other.Solution and self.Fitness == other.Fitness
49
50
51
class SelfAdaptiveDifferentialEvolutionAlgorithm(object):
52
    # pylint: disable=too-many-instance-attributes
53
    def __init__(self, D, NP, nFES, F, Cr, Lower, Upper, function):
0 ignored issues
show
Unused Code introduced by
The argument F seems to be unused.
Loading history...
Unused Code introduced by
The argument Cr seems to be unused.
Loading history...
54
        self.D = D  # dimension of problem
55
        self.Np = NP  # population size
56
        self.nFES = nFES  # number of function evaluations
57
        self.Lower = Lower  # lower bound
58
        self.Upper = Upper  # upper bound
59
60
        SolutionjDE.FuncEval = staticmethod(function)
61
        self.Population = []
62
        self.bestSolution = SolutionjDE(self.D, Lower, Upper)
63
64
    def evalPopulation(self):
65
        for p in self.Population:
66
            p.evaluate()
67
            if p.Fitness < self.bestSolution.Fitness:
68
                self.bestSolution = copy.deepcopy(p)
69
70
    def initPopulation(self):
71
        for _i in range(self.Np):
72
            self.Population.append(SolutionjDE(self.D, self.Lower, self.Upper))
73
74
    def generationStep(self, Population):
75
        newPopulation = []
76
        for i in range(self.Np):
77
            newSolution = SolutionjDE(self.D, self.Lower, self.Upper)
78
79
            if rnd.random() < self.Tao:
0 ignored issues
show
Bug introduced by
The Instance of SelfAdaptiveDifferentialEvolutionAlgorithm does not seem to have a member named Tao.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
80
                newSolution.F = rnd.random()
81
            else:
82
                newSolution.F = Population[i].F
83
84
            if rnd.random() < self.Tao:
0 ignored issues
show
Bug introduced by
The Instance of SelfAdaptiveDifferentialEvolutionAlgorithm does not seem to have a member named Tao.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
85
                newSolution.Cr = rnd.random()
86
            else:
87
                newSolution.Cr = Population[i].Cr
88
89
            r = rnd.sample(range(0, self.Np), 3)
90
            while i in r:
91
                r = rnd.sample(range(0, self.Np), 3)
92
            jrand = int(rnd.random() * self.Np)
93
94
            for j in range(self.D):
95
                if rnd.random() < self.Cr or j == jrand:
0 ignored issues
show
Bug introduced by
The Instance of SelfAdaptiveDifferentialEvolutionAlgorithm does not seem to have a member named Cr.

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
96
                    newSolution.Solution[j] = Population[r[0]].Solution[j] + self.F * (
0 ignored issues
show
Bug introduced by
Instance of 'SelfAdaptiveDifferentialEvolutionAlgorithm' has no 'F' member; maybe 'D'?

This check looks for calls to members that are non-existent. These calls will fail.

The member could have been renamed or removed.

Loading history...
97
                        Population[r[1]].Solution[j] - Population[r[2]].Solution[j])
98
                else:
99
                    newSolution.Solution[j] = Population[i].Solution[j]
100
            newSolution.repair()
101
            newSolution.evaluate()
102
103
            if newSolution.Fitness < self.bestSolution.Fitness:
104
                self.bestSolution = copy.deepcopy(newSolution)
105
            if newSolution.Fitness < self.Population[i].Fitness:
106
                newPopulation.append(newSolution)
107
            else:
108
                newPopulation.append(Population[i])
109
        return newPopulation
110
111
    def run(self):
112
        self.initPopulation()
113
        self.evalPopulation()
114
        FEs = self.Np
115
        while FEs <= self.nFES:
116
            self.Population = self.generationStep(self.Population)
117
            FEs += self.Np
118
        return self.bestSolution.Fitness
119