forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathMutations.cpp
303 lines (251 loc) · 8.37 KB
/
Mutations.cpp
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
This file is part of solidity.
solidity is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
solidity is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with solidity. If not, see <http://www.gnu.org/licenses/>.
*/
// SPDX-License-Identifier: GPL-3.0
#include <tools/yulPhaser/Mutations.h>
#include <tools/yulPhaser/SimulationRNG.h>
#include <libsolutil/CommonData.h>
#include <algorithm>
#include <cassert>
#include <cmath>
#include <string>
#include <vector>
using namespace std;
using namespace solidity;
using namespace solidity::phaser;
function<Mutation> phaser::geneRandomisation(double _chance)
{
return [=](Chromosome const& _chromosome)
{
string genes;
for (char gene: _chromosome.genes())
genes.push_back(
SimulationRNG::bernoulliTrial(_chance) ?
Chromosome::randomGene() :
gene
);
return Chromosome(std::move(genes));
};
}
function<Mutation> phaser::geneDeletion(double _chance)
{
return [=](Chromosome const& _chromosome)
{
string genes;
for (char gene: _chromosome.genes())
if (!SimulationRNG::bernoulliTrial(_chance))
genes.push_back(gene);
return Chromosome(std::move(genes));
};
}
function<Mutation> phaser::geneAddition(double _chance)
{
return [=](Chromosome const& _chromosome)
{
string genes;
if (SimulationRNG::bernoulliTrial(_chance))
genes.push_back(Chromosome::randomGene());
for (char gene: _chromosome.genes())
{
genes.push_back(gene);
if (SimulationRNG::bernoulliTrial(_chance))
genes.push_back(Chromosome::randomGene());
}
return Chromosome(std::move(genes));
};
}
function<Mutation> phaser::alternativeMutations(
double _firstMutationChance,
function<Mutation> _mutation1,
function<Mutation> _mutation2
)
{
return [=](Chromosome const& _chromosome)
{
if (SimulationRNG::bernoulliTrial(_firstMutationChance))
return _mutation1(_chromosome);
else
return _mutation2(_chromosome);
};
}
function<Mutation> phaser::mutationSequence(vector<function<Mutation>> _mutations)
{
return [=](Chromosome const& _chromosome)
{
Chromosome mutatedChromosome = _chromosome;
for (size_t i = 0; i < _mutations.size(); ++i)
mutatedChromosome = _mutations[i](std::move(mutatedChromosome));
return mutatedChromosome;
};
}
namespace
{
ChromosomePair fixedPointSwap(
Chromosome const& _chromosome1,
Chromosome const& _chromosome2,
size_t _crossoverPoint
)
{
assert(_crossoverPoint <= _chromosome1.length());
assert(_crossoverPoint <= _chromosome2.length());
return {
Chromosome(
_chromosome1.genes().substr(0, _crossoverPoint) +
_chromosome2.genes().substr(_crossoverPoint, _chromosome2.length() - _crossoverPoint)
),
Chromosome(
_chromosome2.genes().substr(0, _crossoverPoint) +
_chromosome1.genes().substr(_crossoverPoint, _chromosome1.length() - _crossoverPoint)
),
};
}
}
function<Crossover> phaser::randomPointCrossover()
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
// Don't use position 0 (because this just swaps the values) unless it's the only choice.
size_t minPoint = (minLength > 0 ? 1 : 0);
assert(minPoint <= minLength);
size_t randomPoint = SimulationRNG::uniformInt(minPoint, minLength);
return get<0>(fixedPointSwap(_chromosome1, _chromosome2, randomPoint));
};
}
function<SymmetricCrossover> phaser::symmetricRandomPointCrossover()
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
// Don't use position 0 (because this just swaps the values) unless it's the only choice.
size_t minPoint = (minLength > 0 ? 1 : 0);
assert(minPoint <= minLength);
size_t randomPoint = SimulationRNG::uniformInt(minPoint, minLength);
return fixedPointSwap(_chromosome1, _chromosome2, randomPoint);
};
}
function<Crossover> phaser::fixedPointCrossover(double _crossoverPoint)
{
assert(0.0 <= _crossoverPoint && _crossoverPoint <= 1.0);
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
size_t concretePoint = static_cast<size_t>(round(double(minLength) * _crossoverPoint));
return get<0>(fixedPointSwap(_chromosome1, _chromosome2, concretePoint));
};
}
namespace
{
ChromosomePair fixedTwoPointSwap(
Chromosome const& _chromosome1,
Chromosome const& _chromosome2,
size_t _crossoverPoint1,
size_t _crossoverPoint2
)
{
assert(_crossoverPoint1 <= _chromosome1.length());
assert(_crossoverPoint1 <= _chromosome2.length());
assert(_crossoverPoint2 <= _chromosome1.length());
assert(_crossoverPoint2 <= _chromosome2.length());
size_t lowPoint = min(_crossoverPoint1, _crossoverPoint2);
size_t highPoint = max(_crossoverPoint1, _crossoverPoint2);
return {
Chromosome(
_chromosome1.genes().substr(0, lowPoint) +
_chromosome2.genes().substr(lowPoint, highPoint - lowPoint) +
_chromosome1.genes().substr(highPoint, _chromosome1.length() - highPoint)
),
Chromosome(
_chromosome2.genes().substr(0, lowPoint) +
_chromosome1.genes().substr(lowPoint, highPoint - lowPoint) +
_chromosome2.genes().substr(highPoint, _chromosome2.length() - highPoint)
),
};
}
}
function<Crossover> phaser::randomTwoPointCrossover()
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
// Don't use position 0 (because this just swaps the values) unless it's the only choice.
size_t minPoint = (minLength > 0 ? 1 : 0);
assert(minPoint <= minLength);
size_t randomPoint1 = SimulationRNG::uniformInt(minPoint, minLength);
size_t randomPoint2 = SimulationRNG::uniformInt(randomPoint1, minLength);
return get<0>(fixedTwoPointSwap(_chromosome1, _chromosome2, randomPoint1, randomPoint2));
};
}
function<SymmetricCrossover> phaser::symmetricRandomTwoPointCrossover()
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
// Don't use position 0 (because this just swaps the values) unless it's the only choice.
size_t minPoint = (minLength > 0 ? 1 : 0);
assert(minPoint <= minLength);
size_t randomPoint1 = SimulationRNG::uniformInt(minPoint, minLength);
size_t randomPoint2 = SimulationRNG::uniformInt(randomPoint1, minLength);
return fixedTwoPointSwap(_chromosome1, _chromosome2, randomPoint1, randomPoint2);
};
}
namespace
{
ChromosomePair uniformSwap(Chromosome const& _chromosome1, Chromosome const& _chromosome2, double _swapChance)
{
string steps1;
string steps2;
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
for (size_t i = 0; i < minLength; ++i)
if (SimulationRNG::bernoulliTrial(_swapChance))
{
steps1.push_back(_chromosome2.genes()[i]);
steps2.push_back(_chromosome1.genes()[i]);
}
else
{
steps1.push_back(_chromosome1.genes()[i]);
steps2.push_back(_chromosome2.genes()[i]);
}
bool swapTail = SimulationRNG::bernoulliTrial(_swapChance);
if (_chromosome1.length() > minLength)
{
if (swapTail)
steps2 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
else
steps1 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
}
if (_chromosome2.length() > minLength)
{
if (swapTail)
steps1 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
else
steps2 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
}
return {Chromosome(steps1), Chromosome(steps2)};
}
}
function<Crossover> phaser::uniformCrossover(double _swapChance)
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
return get<0>(uniformSwap(_chromosome1, _chromosome2, _swapChance));
};
}
function<SymmetricCrossover> phaser::symmetricUniformCrossover(double _swapChance)
{
return [=](Chromosome const& _chromosome1, Chromosome const& _chromosome2)
{
return uniformSwap(_chromosome1, _chromosome2, _swapChance);
};
}