forked from ethereum/solidity
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathChromosome.h
74 lines (59 loc) · 2.51 KB
/
Chromosome.h
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
/*
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
#pragma once
#include <cstddef>
#include <map>
#include <ostream>
#include <string>
#include <vector>
namespace solidity::phaser
{
/**
* An object that represents a sequence of optimiser steps that can be applied to a program.
* Such sequences are used in our genetic algorithm to represent individual members of the
* population.
*
* To calculate the fitness of an individual one must apply its sequence to a specific program.
* This class does not provide any means to do so. It just stores information.
*
* Once created a sequence cannot be changed. The only way to mutate it is to generate a new
* chromosome based on the old one.
*/
class Chromosome
{
public:
Chromosome() = default;
explicit Chromosome(std::vector<std::string> _optimisationSteps):
m_genes(stepsToGenes(_optimisationSteps)) {}
explicit Chromosome(std::string _genes):
// NOTE: We don't validate the genes - they're only checked at the point of conversion to
// actual optimisation steps names. This is very convenient in mutation tests.
m_genes(std::move(_genes)) {}
static Chromosome makeRandom(size_t _length);
size_t length() const { return m_genes.size(); }
std::string const& genes() const { return m_genes; }
std::vector<std::string> optimisationSteps() const { return genesToSteps(m_genes); }
friend std::ostream& operator<<(std::ostream& _stream, Chromosome const& _chromosome);
bool operator==(Chromosome const& _other) const { return m_genes == _other.m_genes; }
bool operator!=(Chromosome const& _other) const { return !(*this == _other); }
static std::string const& randomOptimisationStep();
static char randomGene();
static std::string stepsToGenes(std::vector<std::string> const& _optimisationSteps);
static std::vector<std::string> genesToSteps(std::string const& _genes);
private:
static std::vector<std::string> allStepNames();
std::string m_genes;
};
}