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 |
|
|
|
|
10
|
|
|
parameters in differential evolution: A comparative study on numerical benchmark problems. |
|
|
|
|
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): |
|
|
|
|
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): |
|
|
|
|
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: |
|
|
|
|
80
|
|
|
newSolution.F = rnd.random() |
81
|
|
|
else: |
82
|
|
|
newSolution.F = Population[i].F |
83
|
|
|
|
84
|
|
|
if rnd.random() < self.Tao: |
|
|
|
|
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: |
|
|
|
|
96
|
|
|
newSolution.Solution[j] = Population[r[0]].Solution[j] + self.F * ( |
|
|
|
|
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
|
|
|
|