-
Notifications
You must be signed in to change notification settings - Fork 0
/
sade.py
195 lines (176 loc) · 7.13 KB
/
sade.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
from deBase import DERand1Bin, DECurrentToPBest1Bin
from collections import deque
import itertools
import numpy
# Some of the following code can create numpy warnings.
# Tell numpy to raise exceptions instead so we can handle them.
numpy.seterr(all='raise')
"""
This file begins by deriving three of the four DE variants used by SaDE
(DE/rand/1/bin can be used directly.)
SaDE itself then inherits from these four variants.
Note that this is the 2009 update of SaDE, not the original 2005 version.
"""
class DECurrentToBest2Bin(DECurrentToPBest1Bin):
def mutation(self, *args, **kwargs):
kwargs['p'] = 0
kwargs['n'] = 2
return DECurrentToPBest1Bin.mutation(self, *args, **kwargs)
class DERand2Bin(DERand1Bin):
def mutation(self, *args, **kwargs):
kwargs['n'] = 2
return DERand1Bin.mutation(self, *args, **kwargs)
class DECurrentToRand1(DECurrentToPBest1Bin):
def mutation(self, *args, **kwargs):
kwargs['p'] = 1
return DECurrentToPBest1Bin.mutation(self, *args, **kwargs)
def crossover(self, parentIndex, mutant, cr):
"""
This algorithm does not implement crossover to retain rotational
invariance.
"""
return mutant
class SaDE(DECurrentToBest2Bin, DERand2Bin, DECurrentToRand1, DERand1Bin):
"""
An implementation of Qin et al.'s Self-adaptive Differential Evolution
(SaDE).
"""
def __init__(self, *args, **kwargs):
"""
Upon initialisation, create a list of dicts containing
information about SaDE's four strategies.
"""
kwargs['f'] = 0.5
kwargs['cr'] = 0.5
super(SaDE, self).__init__(*args, **kwargs)
self.lp = 50
self.strategies = [
{
'algorithm': DERand1Bin,
'probability': 0.25,
'cr': 0.5,
'crMemory': deque(maxlen=self.lp)
},
{
'algorithm': DECurrentToBest2Bin,
'probability': 0.25,
'cr': 0.5,
'crMemory': deque(maxlen=self.lp)
},
{
'algorithm': DERand2Bin,
'probability': 0.25,
'cr': 0.5,
'crMemory': deque(maxlen=self.lp)
},
{
'algorithm': DECurrentToRand1,
'probability': 0.25,
'cr': 0.5,
'crMemory': deque(maxlen=self.lp)
},
]
# Note that deques delete old contents as they overflow beyond maxlen.
self.successMemory = deque(maxlen=self.lp)
self.failureMemory = deque(maxlen=self.lp)
def _updateStrategyProbabilities(self):
"""
更新变异策略概率
Update the probability of each strategy being selected by examining the
contents of the success and failure memories.
"""
denominator = float(numpy.sum(self.successMemory) + numpy.sum(self.failureMemory))
successes = numpy.sum(self.successMemory, axis=0)
# 0.01 protects against null success rates
unscaledProbabilities = [(s / denominator) + 0.01 for s in successes]
# We want the probabilities scaled to 1.
scalingFactor = 1 / sum(unscaledProbabilities)
for i in range(len(self.strategies)):
self.strategies[i]['probability'] = unscaledProbabilities[i] * scalingFactor
def _stochasticUniversalSampleStrategies(self):
"""
Returns a randomised list of NP strategy indices, sampled using the
Stochastic Universal Sampling technique, weighted by strategy
probability.
"""
# Assemble the sampling thresholds
thresholds = []
cumulativeProbability = 0
for strategy in self.strategies:
cumulativeProbability += strategy['probability']
thresholds.append(cumulativeProbability)
# Collect the sample
sample = []
interval = (1 / float(self.population.size))
pointer = interval * numpy.random.rand()
while pointer < cumulativeProbability:
for i, t in enumerate(thresholds):
if pointer < t:
sample.append(i)
break
pointer += interval
numpy.random.shuffle(sample)
return sample
def _computeCrMedians(self):
"""
Establish the median successful cr for each strategy in the last lp
generations.
"""
for i, strategy in enumerate(self.strategies):
flattenedCr = list(itertools.chain.from_iterable(strategy['crMemory']))
if flattenedCr:
# Skip this step if there were no successes
self.strategies[i]['cr'] = numpy.median(flattenedCr)
def generateTrialMember(self, i):
"""
Override to include randomisation controls and attribute marking.
"""
# Extract the strategy from the sample
strategyIndex = self.sampledStrategies[i]
strategy = self.strategies[strategyIndex]
algorithm = strategy['algorithm']
# f is a normally distributed variable. SaDE does not truncate it.
fi = numpy.random.normal(self.f, 0.3)
# cr is a normally distributed variable, regenerated if outside [0, 1].
while True:
cri = numpy.random.normal(strategy['cr'], 0.1)
if cri >= 0 and cri <= 1:
break
mutant = strategy['algorithm'].mutation(self, i, fi)
trialMember = strategy['algorithm'].crossover(self, i, mutant, cri)
# Mark the trial member with the parameters used to create it
trialMember.strategy = strategyIndex
trialMember.cr = cri
return trialMember
def generateTrialPopulation(self, *args, **kwargs):
"""
Compute cr medians.
Update strategy selection probabilities and create a strategy sample.
Add a new row to the success, failure and cr memories (old ones are
deleted automatically by the deque).
"""
# n = number of strategies in use. Called multiple times in this method.
n = len(self.strategies)
if self.generation > self.lp:
self._computeCrMedians()
self._updateStrategyProbabilities()
self.sampledStrategies = self._stochasticUniversalSampleStrategies()
# Augment all memories
self.successMemory.append([0] * n)
self.failureMemory.append([0] * n)
for i in range(n):
self.strategies[i]['crMemory'].append([])
return super(SaDE, self).generateTrialPopulation(*args, **kwargs)
def trialMemberSuccess(self, i, trialMember):
"""
This function is extended to log successful cr and strategy.
"""
self.strategies[trialMember.strategy]['crMemory'][-1].append(trialMember.cr)
self.successMemory[-1][trialMember.strategy] += 1
super(SaDE, self).trialMemberSuccess(i, trialMember)
def trialMemberFailure(self, i, trialMember):
"""
This function is extended to log unsuccessful strategies.
"""
self.failureMemory[-1][trialMember.strategy] += 1
super(SaDE, self).trialMemberFailure(i, trialMember)