2020-02-05 13:42:38 +00:00
|
|
|
/*
|
|
|
|
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/>.
|
|
|
|
*/
|
2020-07-17 14:54:12 +00:00
|
|
|
// SPDX-License-Identifier: GPL-3.0
|
2020-02-05 13:42:38 +00:00
|
|
|
|
|
|
|
#include <tools/yulPhaser/Mutations.h>
|
|
|
|
|
|
|
|
#include <tools/yulPhaser/SimulationRNG.h>
|
|
|
|
|
2020-02-06 03:34:09 +00:00
|
|
|
#include <libsolutil/CommonData.h>
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
#include <cassert>
|
|
|
|
#include <cmath>
|
2020-02-05 13:42:38 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
using namespace std;
|
|
|
|
using namespace solidity;
|
|
|
|
using namespace solidity::phaser;
|
|
|
|
|
|
|
|
function<Mutation> phaser::geneRandomisation(double _chance)
|
|
|
|
{
|
|
|
|
return [=](Chromosome const& _chromosome)
|
|
|
|
{
|
2020-09-11 18:49:25 +00:00
|
|
|
string genes;
|
|
|
|
for (char gene: _chromosome.genes())
|
|
|
|
genes.push_back(
|
2020-02-05 13:42:38 +00:00
|
|
|
SimulationRNG::bernoulliTrial(_chance) ?
|
2020-09-11 18:49:25 +00:00
|
|
|
Chromosome::randomGene() :
|
|
|
|
gene
|
2020-02-05 13:42:38 +00:00
|
|
|
);
|
|
|
|
|
2020-09-11 18:49:25 +00:00
|
|
|
return Chromosome(move(genes));
|
2020-02-05 13:42:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function<Mutation> phaser::geneDeletion(double _chance)
|
|
|
|
{
|
|
|
|
return [=](Chromosome const& _chromosome)
|
|
|
|
{
|
2020-09-11 18:49:25 +00:00
|
|
|
string genes;
|
|
|
|
for (char gene: _chromosome.genes())
|
2020-02-05 13:42:38 +00:00
|
|
|
if (!SimulationRNG::bernoulliTrial(_chance))
|
2020-09-11 18:49:25 +00:00
|
|
|
genes.push_back(gene);
|
2020-02-05 13:42:38 +00:00
|
|
|
|
2020-09-11 18:49:25 +00:00
|
|
|
return Chromosome(move(genes));
|
2020-02-05 13:42:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
function<Mutation> phaser::geneAddition(double _chance)
|
|
|
|
{
|
|
|
|
return [=](Chromosome const& _chromosome)
|
|
|
|
{
|
2020-09-11 18:49:25 +00:00
|
|
|
string genes;
|
2020-02-05 13:42:38 +00:00
|
|
|
|
|
|
|
if (SimulationRNG::bernoulliTrial(_chance))
|
2020-09-11 18:49:25 +00:00
|
|
|
genes.push_back(Chromosome::randomGene());
|
2020-02-05 13:42:38 +00:00
|
|
|
|
2020-09-11 18:49:25 +00:00
|
|
|
for (char gene: _chromosome.genes())
|
2020-02-05 13:42:38 +00:00
|
|
|
{
|
2020-09-11 18:49:25 +00:00
|
|
|
genes.push_back(gene);
|
2020-02-05 13:42:38 +00:00
|
|
|
if (SimulationRNG::bernoulliTrial(_chance))
|
2020-09-11 18:49:25 +00:00
|
|
|
genes.push_back(Chromosome::randomGene());
|
2020-02-05 13:42:38 +00:00
|
|
|
}
|
|
|
|
|
2020-09-11 18:49:25 +00:00
|
|
|
return Chromosome(move(genes));
|
2020-02-05 13:42:38 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|
2020-02-06 03:34:09 +00:00
|
|
|
|
2020-03-11 22:45:46 +00:00
|
|
|
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](move(mutatedChromosome));
|
|
|
|
|
|
|
|
return mutatedChromosome;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-02-06 03:34:09 +00:00
|
|
|
namespace
|
|
|
|
{
|
|
|
|
|
2020-03-11 01:11:58 +00:00
|
|
|
ChromosomePair fixedPointSwap(
|
2020-02-06 03:34:09 +00:00
|
|
|
Chromosome const& _chromosome1,
|
|
|
|
Chromosome const& _chromosome2,
|
|
|
|
size_t _crossoverPoint
|
|
|
|
)
|
|
|
|
{
|
|
|
|
assert(_crossoverPoint <= _chromosome1.length());
|
|
|
|
assert(_crossoverPoint <= _chromosome2.length());
|
|
|
|
|
2020-03-11 01:11:58 +00:00
|
|
|
return {
|
|
|
|
Chromosome(
|
2020-09-11 16:53:47 +00:00
|
|
|
_chromosome1.genes().substr(0, _crossoverPoint) +
|
|
|
|
_chromosome2.genes().substr(_crossoverPoint, _chromosome2.length() - _crossoverPoint)
|
2020-03-11 01:11:58 +00:00
|
|
|
),
|
|
|
|
Chromosome(
|
2020-09-11 16:53:47 +00:00
|
|
|
_chromosome2.genes().substr(0, _crossoverPoint) +
|
|
|
|
_chromosome1.genes().substr(_crossoverPoint, _chromosome1.length() - _crossoverPoint)
|
2020-03-11 01:11:58 +00:00
|
|
|
),
|
|
|
|
};
|
2020-02-06 03:34:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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.
|
2020-04-16 09:36:05 +00:00
|
|
|
size_t minPoint = (minLength > 0 ? 1 : 0);
|
2020-02-06 03:34:09 +00:00
|
|
|
assert(minPoint <= minLength);
|
|
|
|
|
|
|
|
size_t randomPoint = SimulationRNG::uniformInt(minPoint, minLength);
|
2020-03-11 01:11:58 +00:00
|
|
|
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.
|
2020-04-16 09:36:05 +00:00
|
|
|
size_t minPoint = (minLength > 0 ? 1 : 0);
|
2020-03-11 01:11:58 +00:00
|
|
|
assert(minPoint <= minLength);
|
|
|
|
|
|
|
|
size_t randomPoint = SimulationRNG::uniformInt(minPoint, minLength);
|
|
|
|
return fixedPointSwap(_chromosome1, _chromosome2, randomPoint);
|
2020-02-06 03:34:09 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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(minLength * _crossoverPoint));
|
|
|
|
|
2020-03-11 01:11:58 +00:00
|
|
|
return get<0>(fixedPointSwap(_chromosome1, _chromosome2, concretePoint));
|
2020-02-06 03:34:09 +00:00
|
|
|
};
|
|
|
|
}
|
2020-03-11 02:49:16 +00:00
|
|
|
|
|
|
|
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());
|
|
|
|
|
2020-09-11 16:53:47 +00:00
|
|
|
size_t lowPoint = min(_crossoverPoint1, _crossoverPoint2);
|
|
|
|
size_t highPoint = max(_crossoverPoint1, _crossoverPoint2);
|
2020-03-11 02:49:16 +00:00
|
|
|
|
|
|
|
return {
|
|
|
|
Chromosome(
|
2020-09-11 16:53:47 +00:00
|
|
|
_chromosome1.genes().substr(0, lowPoint) +
|
|
|
|
_chromosome2.genes().substr(lowPoint, highPoint - lowPoint) +
|
|
|
|
_chromosome1.genes().substr(highPoint, _chromosome1.length() - highPoint)
|
2020-03-11 02:49:16 +00:00
|
|
|
),
|
|
|
|
Chromosome(
|
2020-09-11 16:53:47 +00:00
|
|
|
_chromosome2.genes().substr(0, lowPoint) +
|
|
|
|
_chromosome1.genes().substr(lowPoint, highPoint - lowPoint) +
|
|
|
|
_chromosome2.genes().substr(highPoint, _chromosome2.length() - highPoint)
|
2020-03-11 02:49:16 +00:00
|
|
|
),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2020-09-11 16:53:47 +00:00
|
|
|
string steps1;
|
|
|
|
string steps2;
|
2020-03-11 02:49:16 +00:00
|
|
|
|
|
|
|
size_t minLength = min(_chromosome1.length(), _chromosome2.length());
|
|
|
|
for (size_t i = 0; i < minLength; ++i)
|
|
|
|
if (SimulationRNG::bernoulliTrial(_swapChance))
|
|
|
|
{
|
2020-09-11 16:53:47 +00:00
|
|
|
steps1.push_back(_chromosome2.genes()[i]);
|
|
|
|
steps2.push_back(_chromosome1.genes()[i]);
|
2020-03-11 02:49:16 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-09-11 16:53:47 +00:00
|
|
|
steps1.push_back(_chromosome1.genes()[i]);
|
|
|
|
steps2.push_back(_chromosome2.genes()[i]);
|
2020-03-11 02:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool swapTail = SimulationRNG::bernoulliTrial(_swapChance);
|
|
|
|
if (_chromosome1.length() > minLength)
|
|
|
|
{
|
|
|
|
if (swapTail)
|
2020-09-11 16:53:47 +00:00
|
|
|
steps2 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
|
2020-03-11 02:49:16 +00:00
|
|
|
else
|
2020-09-11 16:53:47 +00:00
|
|
|
steps1 += _chromosome1.genes().substr(minLength, _chromosome1.length() - minLength);
|
2020-03-11 02:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (_chromosome2.length() > minLength)
|
|
|
|
{
|
|
|
|
if (swapTail)
|
2020-09-11 16:53:47 +00:00
|
|
|
steps1 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
|
2020-03-11 02:49:16 +00:00
|
|
|
else
|
2020-09-11 16:53:47 +00:00
|
|
|
steps2 += _chromosome2.genes().substr(minLength, _chromosome2.length() - minLength);
|
2020-03-11 02:49:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
};
|
|
|
|
}
|