[yul-phaser] main: Command-line option for algorithm selection

This commit is contained in:
Kamil Śliwak 2020-02-06 05:29:19 +01:00
parent fc4fedb214
commit 0c61f6d18f

View File

@ -39,6 +39,34 @@ using namespace solidity::util;
namespace po = boost::program_options;
enum class Algorithm
{
Random
};
istream& operator>>(istream& inputStream, Algorithm& algorithm)
{
string value;
inputStream >> value;
if (value == "random")
algorithm = Algorithm::Random;
else
inputStream.setstate(ios_base::failbit);
return inputStream;
}
ostream& operator<<(ostream& outputStream, Algorithm algorithm)
{
if (algorithm == Algorithm::Random)
outputStream << "random";
else
outputStream.setstate(ios_base::failbit);
return outputStream;
}
namespace
{
@ -69,7 +97,7 @@ CharStream loadSource(string const& _sourcePath)
return CharStream(sourceCode, _sourcePath);
}
void runAlgorithm(string const& _sourcePath)
void runAlgorithm(string const& _sourcePath, Algorithm _algorithm)
{
constexpr size_t populationSize = 20;
constexpr size_t minChromosomeLength = 12;
@ -83,15 +111,24 @@ void runAlgorithm(string const& _sourcePath)
minChromosomeLength,
maxChromosomeLength
);
RandomAlgorithm(
population,
cout,
switch (_algorithm)
{
case Algorithm::Random:
{
/* elitePoolSize = */ 1.0 / populationSize,
/* minChromosomeLength = */ minChromosomeLength,
/* maxChromosomeLength = */ maxChromosomeLength,
RandomAlgorithm(
population,
cout,
{
/* elitePoolSize = */ 1.0 / populationSize,
/* minChromosomeLength = */ minChromosomeLength,
/* maxChromosomeLength = */ maxChromosomeLength,
}
).run();
break;
}
).run();
}
}
CommandLineParsingResult parseCommandLine(int argc, char** argv)
@ -114,6 +151,11 @@ CommandLineParsingResult parseCommandLine(int argc, char** argv)
("help", "Show help message and exit.")
("input-file", po::value<string>()->required(), "Input file")
("seed", po::value<uint32_t>(), "Seed for the random number generator")
(
"algorithm",
po::value<Algorithm>()->default_value(Algorithm::Random),
"Algorithm"
)
;
po::positional_options_description positionalDescription;
@ -160,7 +202,10 @@ int main(int argc, char** argv)
try
{
runAlgorithm(parsingResult.arguments["input-file"].as<string>());
runAlgorithm(
parsingResult.arguments["input-file"].as<string>(),
parsingResult.arguments["algorithm"].as<Algorithm>()
);
}
catch (InvalidProgram const& _exception)
{