mirror of
https://github.com/ethereum/solidity
synced 2023-10-03 13:03:40 +00:00
[yul-phaser] Phaser: Allow selecting the classic algorithm on the command line
This commit is contained in:
parent
f6783c60b2
commit
0efea99fa5
@ -52,6 +52,11 @@ protected:
|
||||
/* gewepDeletionVsAdditionChance = */ 0.3,
|
||||
/* gewepGenesToRandomise = */ 0.4,
|
||||
/* gewepGenesToAddOrDelete = */ 0.2,
|
||||
/* classicElitePoolSize = */ 0.0,
|
||||
/* classicCrossoverChance = */ 0.75,
|
||||
/* classicMutationChance = */ 0.2,
|
||||
/* classicDeletionChance = */ 0.2,
|
||||
/* classicAdditionChance = */ 0.2,
|
||||
};
|
||||
};
|
||||
|
||||
@ -122,6 +127,18 @@ BOOST_FIXTURE_TEST_CASE(build_should_select_the_right_algorithm_and_pass_the_opt
|
||||
BOOST_TEST(gewepAlgorithm->options().deletionVsAdditionChance == m_options.gewepDeletionVsAdditionChance);
|
||||
BOOST_TEST(gewepAlgorithm->options().percentGenesToRandomise == m_options.gewepGenesToRandomise.value());
|
||||
BOOST_TEST(gewepAlgorithm->options().percentGenesToAddOrDelete == m_options.gewepGenesToAddOrDelete.value());
|
||||
|
||||
m_options.algorithm = Algorithm::Classic;
|
||||
unique_ptr<GeneticAlgorithm> algorithm3 = GeneticAlgorithmFactory::build(m_options, 100);
|
||||
BOOST_REQUIRE(algorithm3 != nullptr);
|
||||
|
||||
auto classicAlgorithm = dynamic_cast<ClassicGeneticAlgorithm*>(algorithm3.get());
|
||||
BOOST_REQUIRE(classicAlgorithm != nullptr);
|
||||
BOOST_TEST(classicAlgorithm->options().elitePoolSize == m_options.classicElitePoolSize);
|
||||
BOOST_TEST(classicAlgorithm->options().crossoverChance == m_options.classicCrossoverChance);
|
||||
BOOST_TEST(classicAlgorithm->options().mutationChance == m_options.classicMutationChance);
|
||||
BOOST_TEST(classicAlgorithm->options().deletionChance == m_options.classicDeletionChance);
|
||||
BOOST_TEST(classicAlgorithm->options().additionChance == m_options.classicAdditionChance);
|
||||
}
|
||||
|
||||
BOOST_FIXTURE_TEST_CASE(build_should_set_random_algorithm_elite_pool_size_based_on_population_size_if_not_specified, GeneticAlgorithmFactoryFixture)
|
||||
|
@ -58,6 +58,7 @@ map<Algorithm, string> const AlgorithmToStringMap =
|
||||
{
|
||||
{Algorithm::Random, "random"},
|
||||
{Algorithm::GEWEP, "GEWEP"},
|
||||
{Algorithm::Classic, "classic"},
|
||||
};
|
||||
map<string, Algorithm> const StringToAlgorithmMap = invertMap(AlgorithmToStringMap);
|
||||
|
||||
@ -107,6 +108,11 @@ GeneticAlgorithmFactory::Options GeneticAlgorithmFactory::Options::fromCommandLi
|
||||
_arguments.count("gewep-genes-to-add-or-delete") > 0 ?
|
||||
_arguments["gewep-genes-to-add-or-delete"].as<double>() :
|
||||
optional<double>{},
|
||||
_arguments["classic-elite-pool-size"].as<double>(),
|
||||
_arguments["classic-crossover-chance"].as<double>(),
|
||||
_arguments["classic-mutation-chance"].as<double>(),
|
||||
_arguments["classic-deletion-chance"].as<double>(),
|
||||
_arguments["classic-addition-chance"].as<double>(),
|
||||
};
|
||||
}
|
||||
|
||||
@ -151,6 +157,16 @@ unique_ptr<GeneticAlgorithm> GeneticAlgorithmFactory::build(
|
||||
/* percentGenesToAddOrDelete = */ percentGenesToAddOrDelete,
|
||||
});
|
||||
}
|
||||
case Algorithm::Classic:
|
||||
{
|
||||
return make_unique<ClassicGeneticAlgorithm>(ClassicGeneticAlgorithm::Options{
|
||||
/* elitePoolSize = */ _options.classicElitePoolSize,
|
||||
/* crossoverChance = */ _options.classicCrossoverChance,
|
||||
/* mutationChance = */ _options.classicMutationChance,
|
||||
/* deletionChance = */ _options.classicDeletionChance,
|
||||
/* additionChance = */ _options.classicAdditionChance,
|
||||
});
|
||||
}
|
||||
default:
|
||||
assertThrow(false, solidity::util::Exception, "Invalid Algorithm value.");
|
||||
}
|
||||
@ -475,6 +491,36 @@ Phaser::CommandLineDescription Phaser::buildCommandLineDescription()
|
||||
;
|
||||
keywordDescription.add(gewepAlgorithmDescription);
|
||||
|
||||
po::options_description classicGeneticAlgorithmDescription("CLASSIC GENETIC ALGORITHM", lineLength, minDescriptionLength);
|
||||
classicGeneticAlgorithmDescription.add_options()
|
||||
(
|
||||
"classic-elite-pool-size",
|
||||
po::value<double>()->value_name("<FRACTION>")->default_value(0),
|
||||
"Percentage of population to regenerate using mutations in each round."
|
||||
)
|
||||
(
|
||||
"classic-crossover-chance",
|
||||
po::value<double>()->value_name("<FRACTION>")->default_value(0.75),
|
||||
"Chance of a chromosome being selected for crossover."
|
||||
)
|
||||
(
|
||||
"classic-mutation-chance",
|
||||
po::value<double>()->value_name("<FRACTION>")->default_value(0.01),
|
||||
"Chance of a gene being mutated."
|
||||
)
|
||||
(
|
||||
"classic-deletion-chance",
|
||||
po::value<double>()->value_name("<PROBABILITY>")->default_value(0.01),
|
||||
"Chance of a gene being deleted."
|
||||
)
|
||||
(
|
||||
"classic-addition-chance",
|
||||
po::value<double>()->value_name("<PROBABILITY>")->default_value(0.01),
|
||||
"Chance of a random gene being added."
|
||||
)
|
||||
;
|
||||
keywordDescription.add(classicGeneticAlgorithmDescription);
|
||||
|
||||
po::options_description randomAlgorithmDescription("RANDOM ALGORITHM", lineLength, minDescriptionLength);
|
||||
randomAlgorithmDescription.add_options()
|
||||
(
|
||||
|
@ -58,6 +58,7 @@ enum class Algorithm
|
||||
{
|
||||
Random,
|
||||
GEWEP,
|
||||
Classic,
|
||||
};
|
||||
|
||||
enum class MetricChoice
|
||||
@ -101,6 +102,11 @@ public:
|
||||
double gewepDeletionVsAdditionChance;
|
||||
std::optional<double> gewepGenesToRandomise;
|
||||
std::optional<double> gewepGenesToAddOrDelete;
|
||||
double classicElitePoolSize;
|
||||
double classicCrossoverChance;
|
||||
double classicMutationChance;
|
||||
double classicDeletionChance;
|
||||
double classicAdditionChance;
|
||||
|
||||
static Options fromCommandLine(boost::program_options::variables_map const& _arguments);
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user