|
1
|
|
|
"""Genetic algorithm - BUG is somewhere in code. |
|
2
|
|
|
|
|
3
|
|
|
Date: 12. 2. 2018 |
|
4
|
|
|
|
|
5
|
|
|
Authors : Uros Mlakar |
|
6
|
|
|
|
|
7
|
|
|
License: MIT |
|
8
|
|
|
|
|
9
|
|
|
Reference paper: TODO. |
|
10
|
|
|
|
|
11
|
|
|
""" |
|
12
|
|
|
|
|
13
|
|
|
import random as rnd |
|
14
|
|
|
import copy |
|
15
|
|
|
|
|
16
|
|
|
__all__ = ['GeneticAlgorithm'] |
|
17
|
|
|
|
|
18
|
|
View Code Duplication |
class Chromosome: |
|
|
|
|
|
|
19
|
|
|
def __init__(self, D,LB,UB): |
|
20
|
|
|
self.D = D |
|
21
|
|
|
self.LB = LB |
|
22
|
|
|
self.UB = UB |
|
23
|
|
|
|
|
24
|
|
|
self.Solution = [] |
|
25
|
|
|
self.Fitness = float('inf') |
|
26
|
|
|
self.generateSolution() |
|
27
|
|
|
|
|
28
|
|
|
def generateSolution(self): |
|
29
|
|
|
self.Solution = [self.LB + (self.UB - self.LB) * rnd.random() for i in range(self.D)] |
|
30
|
|
|
|
|
31
|
|
|
def evaluate(self): |
|
32
|
|
|
self.Fitness = Chromosome.FuncEval(self.D,self.Solution) |
|
33
|
|
|
|
|
34
|
|
|
def repair(self): |
|
35
|
|
|
for i in range(self.D): |
|
36
|
|
|
if (self.Solution[i] > self.UB): |
|
37
|
|
|
self.Solution[i] = self.UB |
|
38
|
|
|
if self.Solution[i] < self.LB: |
|
39
|
|
|
self.Solution[i] = self.LB |
|
40
|
|
|
def __eq__(self,other): |
|
41
|
|
|
return self.Solution == other.Solution and self.Fitness == other.Fitness |
|
42
|
|
|
|
|
43
|
|
|
def toString(self): |
|
44
|
|
|
print([i for i in self.Solution]) |
|
45
|
|
|
|
|
46
|
|
|
|
|
47
|
|
|
class GeneticAlgorithm: |
|
48
|
|
|
def __init__(self,D, NP, nFES, Ts, Lower, Upper, function): |
|
49
|
|
|
self.NP = NP |
|
50
|
|
|
self.D = D |
|
51
|
|
|
self.Ts = 4 |
|
52
|
|
|
self.Mr = 0.05 |
|
53
|
|
|
self.Lower = Lower |
|
54
|
|
|
self.Upper = Upper |
|
55
|
|
|
self.Population = [] |
|
56
|
|
|
self.nFES = nFES |
|
57
|
|
|
Chromosome.FuncEval = staticmethod(function) |
|
58
|
|
|
|
|
59
|
|
|
self.Best = Chromosome(self.D,Lower,Upper) |
|
60
|
|
|
|
|
61
|
|
|
def checkForBest(self,pChromosome): |
|
62
|
|
|
if pChromosome.Fitness <= self.Best.Fitness: |
|
63
|
|
|
self.Best = copy.deepcopy(pChromosome) |
|
64
|
|
|
|
|
65
|
|
|
def TournamentSelection(self): |
|
66
|
|
|
indices = range(self.NP) |
|
67
|
|
|
rnd.shuffle(indices) |
|
68
|
|
|
tPop = [] |
|
69
|
|
|
for i in range(self.Ts): |
|
70
|
|
|
tPop.append(self.Population[i]) |
|
71
|
|
|
tPop.sort(key=lambda x: x.Fitness) |
|
72
|
|
|
|
|
73
|
|
|
self.Population.remove(tPop[0]) |
|
74
|
|
|
self.Population.remove(tPop[1]) |
|
75
|
|
|
return tPop[0],tPop[1] |
|
76
|
|
|
|
|
77
|
|
|
def CrossOver(self,parent1,parent2): |
|
78
|
|
|
gamma = 0.4 |
|
79
|
|
|
alpha = [-gamma + (1+2*gamma) * rnd.random() for i in range(self.D)] |
|
80
|
|
|
child1 = Chromosome(self.D,self.Lower,self.Upper) |
|
81
|
|
|
child2 = Chromosome(self.D,self.Lower,self.Upper) |
|
82
|
|
|
child1.Solution = [alpha[i]*parent1.Solution[i]+(1-alpha[i]) * parent2.Solution[i] for i in range(self.D)] |
|
83
|
|
|
child2.Solution = [alpha[i]*parent2.Solution[i]+(1-alpha[i]) * parent1.Solution[i] for i in range(self.D)] |
|
84
|
|
|
return child1,child2 |
|
85
|
|
|
|
|
86
|
|
|
def Mutate(self,child): |
|
87
|
|
|
for i in range(self.D): |
|
88
|
|
|
if rnd.random() < self.Mr: |
|
89
|
|
|
sigma = 0.20 * float(child.UB - child.LB) |
|
90
|
|
|
child.Solution[i] = min(max(rnd.gauss(child.Solution[i], sigma), child.LB), child.UB) |
|
91
|
|
|
|
|
92
|
|
|
def init(self): |
|
93
|
|
|
for i in range(self.NP): |
|
94
|
|
|
self.Population.append(Chromosome(self.D,self.Lower,self.Upper)) |
|
95
|
|
|
self.Population[i].evaluate() |
|
96
|
|
|
self.checkForBest(self.Population[i]) |
|
97
|
|
|
|
|
98
|
|
|
def run(self): |
|
99
|
|
|
self.init() |
|
100
|
|
|
FEs = self.NP |
|
101
|
|
|
while FEs <= self.nFES: |
|
102
|
|
|
for k in range(self.NP/2): |
|
103
|
|
|
parent1,parent2 = self.TournamentSelection() |
|
104
|
|
|
child1,child2 = self.CrossOver(parent1,parent2) |
|
105
|
|
|
|
|
106
|
|
|
self.Mutate(child1) |
|
107
|
|
|
self.Mutate(child2) |
|
108
|
|
|
|
|
109
|
|
|
child1.repair() |
|
110
|
|
|
child2.repair() |
|
111
|
|
|
|
|
112
|
|
|
child1.evaluate() |
|
113
|
|
|
child2.evaluate() |
|
114
|
|
|
FEs+=2 |
|
115
|
|
|
|
|
116
|
|
|
tPop = [parent1, parent2, child1,child2] |
|
117
|
|
|
tPop.sort(key=lambda x: x.Fitness) |
|
118
|
|
|
self.Population.append(tPop[0]) |
|
119
|
|
|
self.Population.append(tPop[1]) |
|
120
|
|
|
|
|
121
|
|
|
for i in range(self.NP): |
|
|
|
|
|
|
122
|
|
|
self.checkForBest(self.Population[i]) |
|
123
|
|
|
print self.Best.toString() |
|
124
|
|
|
return self.Best |
|
125
|
|
|
|
|
126
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
|
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
|