[yul-phaser] Make FitnessMetric::evaluate() non-const

This commit is contained in:
Kamil Śliwak 2020-02-26 19:58:14 +01:00
parent 0913fd1aac
commit 7edbbe4edd
6 changed files with 16 additions and 16 deletions

View File

@ -38,7 +38,7 @@ class DummyProgramBasedMetric: public ProgramBasedMetric
{ {
public: public:
using ProgramBasedMetric::ProgramBasedMetric; using ProgramBasedMetric::ProgramBasedMetric;
size_t evaluate(Chromosome const&) const override { return 0; } size_t evaluate(Chromosome const&) override { return 0; }
}; };
class ProgramBasedMetricFixture class ProgramBasedMetricFixture

View File

@ -51,7 +51,7 @@ class ChromosomeLengthMetric: public FitnessMetric
{ {
public: public:
using FitnessMetric::FitnessMetric; using FitnessMetric::FitnessMetric;
size_t evaluate(Chromosome const& _chromosome) const override { return _chromosome.length(); } size_t evaluate(Chromosome const& _chromosome) override { return _chromosome.length(); }
}; };
// MUTATIONS // MUTATIONS

View File

@ -29,7 +29,7 @@ Program ProgramBasedMetric::optimisedProgram(Chromosome const& _chromosome) cons
return programCopy; return programCopy;
} }
size_t ProgramSize::evaluate(Chromosome const& _chromosome) const size_t ProgramSize::evaluate(Chromosome const& _chromosome)
{ {
return optimisedProgram(_chromosome).codeSize(); return optimisedProgram(_chromosome).codeSize();
} }

View File

@ -43,7 +43,7 @@ public:
FitnessMetric& operator=(FitnessMetric const&) = delete; FitnessMetric& operator=(FitnessMetric const&) = delete;
virtual ~FitnessMetric() = default; virtual ~FitnessMetric() = default;
virtual size_t evaluate(Chromosome const& _chromosome) const = 0; virtual size_t evaluate(Chromosome const& _chromosome) = 0;
}; };
/** /**
@ -84,7 +84,7 @@ class ProgramSize: public ProgramBasedMetric
{ {
public: public:
using ProgramBasedMetric::ProgramBasedMetric; using ProgramBasedMetric::ProgramBasedMetric;
size_t evaluate(Chromosome const& _chromosome) const override; size_t evaluate(Chromosome const& _chromosome) override;
}; };
} }

View File

@ -59,7 +59,7 @@ bool phaser::isFitter(Individual const& a, Individual const& b)
} }
Population Population::makeRandom( Population Population::makeRandom(
shared_ptr<FitnessMetric const> _fitnessMetric, shared_ptr<FitnessMetric> _fitnessMetric,
size_t _size, size_t _size,
function<size_t()> _chromosomeLengthGenerator function<size_t()> _chromosomeLengthGenerator
) )
@ -72,7 +72,7 @@ Population Population::makeRandom(
} }
Population Population::makeRandom( Population Population::makeRandom(
shared_ptr<FitnessMetric const> _fitnessMetric, shared_ptr<FitnessMetric> _fitnessMetric,
size_t _size, size_t _size,
size_t _minChromosomeLength, size_t _minChromosomeLength,
size_t _maxChromosomeLength size_t _maxChromosomeLength
@ -145,7 +145,7 @@ ostream& phaser::operator<<(ostream& _stream, Population const& _population)
} }
vector<Individual> Population::chromosomesToIndividuals( vector<Individual> Population::chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric, FitnessMetric& _fitnessMetric,
vector<Chromosome> _chromosomes vector<Chromosome> _chromosomes
) )
{ {

View File

@ -55,7 +55,7 @@ struct Individual
Individual(Chromosome _chromosome, size_t _fitness): Individual(Chromosome _chromosome, size_t _fitness):
chromosome(std::move(_chromosome)), chromosome(std::move(_chromosome)),
fitness(_fitness) {} fitness(_fitness) {}
Individual(Chromosome _chromosome, FitnessMetric const& _fitnessMetric): Individual(Chromosome _chromosome, FitnessMetric& _fitnessMetric):
chromosome(std::move(_chromosome)), chromosome(std::move(_chromosome)),
fitness(_fitnessMetric.evaluate(chromosome)) {} fitness(_fitnessMetric.evaluate(chromosome)) {}
@ -85,7 +85,7 @@ class Population
{ {
public: public:
explicit Population( explicit Population(
std::shared_ptr<FitnessMetric const> _fitnessMetric, std::shared_ptr<FitnessMetric> _fitnessMetric,
std::vector<Chromosome> _chromosomes = {} std::vector<Chromosome> _chromosomes = {}
): ):
Population( Population(
@ -94,12 +94,12 @@ public:
) {} ) {}
static Population makeRandom( static Population makeRandom(
std::shared_ptr<FitnessMetric const> _fitnessMetric, std::shared_ptr<FitnessMetric> _fitnessMetric,
size_t _size, size_t _size,
std::function<size_t()> _chromosomeLengthGenerator std::function<size_t()> _chromosomeLengthGenerator
); );
static Population makeRandom( static Population makeRandom(
std::shared_ptr<FitnessMetric const> _fitnessMetric, std::shared_ptr<FitnessMetric> _fitnessMetric,
size_t _size, size_t _size,
size_t _minChromosomeLength, size_t _minChromosomeLength,
size_t _maxChromosomeLength size_t _maxChromosomeLength
@ -110,7 +110,7 @@ public:
Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const; Population crossover(PairSelection const& _selection, std::function<Crossover> _crossover) const;
friend Population (::operator+)(Population _a, Population _b); friend Population (::operator+)(Population _a, Population _b);
std::shared_ptr<FitnessMetric const> fitnessMetric() const { return m_fitnessMetric; } std::shared_ptr<FitnessMetric> fitnessMetric() { return m_fitnessMetric; }
std::vector<Individual> const& individuals() const { return m_individuals; } std::vector<Individual> const& individuals() const { return m_individuals; }
static size_t uniformChromosomeLength(size_t _min, size_t _max) { return SimulationRNG::uniformInt(_min, _max); } static size_t uniformChromosomeLength(size_t _min, size_t _max) { return SimulationRNG::uniformInt(_min, _max); }
@ -122,17 +122,17 @@ public:
friend std::ostream& operator<<(std::ostream& _stream, Population const& _population); friend std::ostream& operator<<(std::ostream& _stream, Population const& _population);
private: private:
explicit Population(std::shared_ptr<FitnessMetric const> _fitnessMetric, std::vector<Individual> _individuals): explicit Population(std::shared_ptr<FitnessMetric> _fitnessMetric, std::vector<Individual> _individuals):
m_fitnessMetric(std::move(_fitnessMetric)), m_fitnessMetric(std::move(_fitnessMetric)),
m_individuals{sortedIndividuals(std::move(_individuals))} {} m_individuals{sortedIndividuals(std::move(_individuals))} {}
static std::vector<Individual> chromosomesToIndividuals( static std::vector<Individual> chromosomesToIndividuals(
FitnessMetric const& _fitnessMetric, FitnessMetric& _fitnessMetric,
std::vector<Chromosome> _chromosomes std::vector<Chromosome> _chromosomes
); );
static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals); static std::vector<Individual> sortedIndividuals(std::vector<Individual> _individuals);
std::shared_ptr<FitnessMetric const> m_fitnessMetric; std::shared_ptr<FitnessMetric> m_fitnessMetric;
std::vector<Individual> m_individuals; std::vector<Individual> m_individuals;
}; };