1#ifndef OPERATOR_BUILDER_H
2#define OPERATOR_BUILDER_H
9#include <yaml-cpp/yaml.h>
42 template <
typename GeneType =
double>
50 static std::unique_ptr<Selection<GeneType>>
buildSelection(
const YAML::Node& node) {
51 const auto type = node[
"type"].as<std::string>(
"tournament");
53 if (type ==
"tournament") {
54 const auto size = node[
"tournament_size"].as<std::size_t>(3);
55 return std::make_unique<TournamentSelection<GeneType>>(size);
58 throw std::invalid_argument(
"Unknown Selection type: " + type);
67 const auto type = node[
"type"].as<std::string>(
"best_neighbor");
69 if (type ==
"best_neighbor" || type ==
"tournament") {
70 return std::make_unique<BestNeighborSelection<GeneType>>();
73 throw std::invalid_argument(
"Unknown LocalSelection type: " + type);
83 static std::unique_ptr<Mutation<GeneType>>
buildMutation(
const YAML::Node& node, GeneType lb = 0, GeneType ub = 0) {
84 const auto type = node[
"type"].as<std::string>(
"gaussian");
86 if (type ==
"gaussian") {
87 auto sigma = node[
"sigma"].as<
double>(0.1);
88 return std::make_unique<GaussianMutation<GeneType>>(sigma);
89 }
else if (type ==
"uniform") {
90 return std::make_unique<UniformMutation<GeneType>>(lb, ub);
91 }
else if (type ==
"boundary") {
92 return std::make_unique<BoundaryMutation<GeneType>>(lb, ub);
95 throw std::invalid_argument(
"Unknown Mutation type: " + type);
103 static std::unique_ptr<Crossover<GeneType>>
buildCrossover(
const YAML::Node& node) {
104 const auto type = node[
"type"].as<std::string>(
"single_point");
106 if (type ==
"single_point") {
107 return std::make_unique<SinglePointCrossover<GeneType>>();
108 }
else if (type ==
"arithmetic") {
109 return std::make_unique<ArithmeticCrossover<GeneType>>();
110 }
else if (type ==
"uniform") {
111 return std::make_unique<UniformCrossover<GeneType>>();
114 throw std::invalid_argument(
"Unknown Crossover type: " + type);
123 const auto type = node[
"type"].as<std::string>(
"worst");
125 if (type ==
"worst") {
126 return std::make_unique<WorstReplacer<GeneType>>();
129 throw std::invalid_argument(
"Unknown DemeReplacer type: " + type);
138 const auto type = node[
"type"].as<std::string>(
"elitism");
140 if (type ==
"elitism") {
141 return std::make_unique<ElitismSelector<GeneType>>();
144 throw std::invalid_argument(
"Unknown DemeSelector type: " + type);
Internal factory for instantiating GA operators from YAML configurations.
static std::unique_ptr< Crossover< GeneType > > buildCrossover(const YAML::Node &node)
Builds a crossover operator.
static std::unique_ptr< Mutation< GeneType > > buildMutation(const YAML::Node &node, GeneType lb=0, GeneType ub=0)
Builds a mutation operator.
static std::unique_ptr< DemeSelector< GeneType > > buildDemeSelector(const YAML::Node &node)
Builds a deme selector policy (for Island GA).
static std::unique_ptr< LocalSelection< GeneType > > buildLocalSelection(const YAML::Node &node)
Builds a local selection operator (for Cellular GA).
static std::unique_ptr< DemeReplacer< GeneType > > buildDemeReplacer(const YAML::Node &node)
Builds a deme replacer policy (for Island GA).
static std::unique_ptr< Selection< GeneType > > buildSelection(const YAML::Node &node)
Builds a selection operator.