mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Population: Make ordering of individuals with same fitness deterministic and prioritise shorter chromosomes
- Before this change the order of chromosomes with the same fitness in a population depended on the initial order set when the population was first created. Now it only depends on the individual. - The length comparison is not strictly necessary (lexicographical order covers that) but it makes the intention clear and the comparison slightly faster when chromosomes have different lengths.
This commit is contained in:
@@ -19,6 +19,8 @@
|
||||
|
||||
#include <tools/yulPhaser/Program.h>
|
||||
|
||||
#include <libsolutil/CommonIO.h>
|
||||
|
||||
#include <algorithm>
|
||||
#include <cassert>
|
||||
#include <numeric>
|
||||
@@ -26,6 +28,7 @@
|
||||
using namespace std;
|
||||
using namespace solidity;
|
||||
using namespace solidity::langutil;
|
||||
using namespace solidity::util;
|
||||
using namespace solidity::phaser;
|
||||
|
||||
namespace solidity::phaser
|
||||
@@ -52,7 +55,11 @@ bool phaser::isFitter(Individual const& a, Individual const& b)
|
||||
{
|
||||
assert(a.fitness.has_value() && b.fitness.has_value());
|
||||
|
||||
return a.fitness.value() < b.fitness.value();
|
||||
return (
|
||||
(a.fitness.value() < b.fitness.value()) ||
|
||||
(a.fitness.value() == b.fitness.value() && a.chromosome.length() < b.chromosome.length()) ||
|
||||
(a.fitness.value() == b.fitness.value() && a.chromosome.length() == b.chromosome.length() && toString(a.chromosome) < toString(b.chromosome))
|
||||
);
|
||||
}
|
||||
|
||||
Population::Population(Program _program, vector<Chromosome> const& _chromosomes):
|
||||
|
||||
Reference in New Issue
Block a user