1 /* 2 This file is part of solidity. 3 4 solidity is free software: you can redistribute it and/or modify 5 it under the terms of the GNU General Public License as published by 6 the Free Software Foundation, either version 3 of the License, or 7 (at your option) any later version. 8 9 solidity is distributed in the hope that it will be useful, 10 but WITHOUT ANY WARRANTY; without even the implied warranty of 11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 GNU General Public License for more details. 13 14 You should have received a copy of the GNU General Public License 15 along with solidity. If not, see <http://www.gnu.org/licenses/>. 16 */ 17 // SPDX-License-Identifier: GPL-3.0 18 19 #include <tools/yulPhaser/Chromosome.h> 20 21 #include <tools/yulPhaser/SimulationRNG.h> 22 23 #include <libyul/optimiser/Suite.h> 24 #include <libsolutil/CommonData.h> 25 26 #include <sstream> 27 28 using namespace std; 29 using namespace solidity; 30 using namespace solidity::yul; 31 using namespace solidity::phaser; 32 33 namespace solidity::phaser 34 { 35 36 ostream& operator<<(ostream& _stream, Chromosome const& _chromosome); 37 38 } 39 makeRandom(size_t _length)40Chromosome Chromosome::makeRandom(size_t _length) 41 { 42 vector<string> steps; 43 for (size_t i = 0; i < _length; ++i) 44 steps.push_back(randomOptimisationStep()); 45 46 return Chromosome(move(steps)); 47 } 48 operator <<(ostream & _stream,Chromosome const & _chromosome)49ostream& phaser::operator<<(ostream& _stream, Chromosome const& _chromosome) 50 { 51 return _stream << _chromosome.m_genes; 52 } 53 allStepNames()54vector<string> Chromosome::allStepNames() 55 { 56 vector<string> stepNames; 57 for (auto const& step: OptimiserSuite::allSteps()) 58 stepNames.push_back(step.first); 59 60 return stepNames; 61 } 62 randomOptimisationStep()63string const& Chromosome::randomOptimisationStep() 64 { 65 static vector<string> stepNames = allStepNames(); 66 67 return stepNames[SimulationRNG::uniformInt(0, stepNames.size() - 1)]; 68 } 69 randomGene()70char Chromosome::randomGene() 71 { 72 return OptimiserSuite::stepNameToAbbreviationMap().at(randomOptimisationStep()); 73 } 74 stepsToGenes(vector<string> const & _optimisationSteps)75string Chromosome::stepsToGenes(vector<string> const& _optimisationSteps) 76 { 77 string genes; 78 for (string const& stepName: _optimisationSteps) 79 genes.push_back(OptimiserSuite::stepNameToAbbreviationMap().at(stepName)); 80 81 return genes; 82 } 83 genesToSteps(string const & _genes)84vector<string> Chromosome::genesToSteps(string const& _genes) 85 { 86 vector<string> steps; 87 for (char abbreviation: _genes) 88 steps.push_back(OptimiserSuite::stepAbbreviationToNameMap().at(abbreviation)); 89 90 return steps; 91 } 92