GALib-Parallel
Header-only C++ library for parallel evolutionary algorithms
Loading...
Searching...
No Matches
OperatorBuilder.h
Go to the documentation of this file.
1#ifndef OPERATOR_BUILDER_H
2#define OPERATOR_BUILDER_H
3
4#pragma once
5
6#include <memory>
7#include <string>
8#include <stdexcept>
9#include <yaml-cpp/yaml.h>
10
19
24
27
32
33namespace galib::internal {
42 template <typename GeneType = double>
44 public:
50 static std::unique_ptr<Selection<GeneType>> buildSelection(const YAML::Node& node) {
51 const auto type = node["type"].as<std::string>("tournament");
52
53 if (type == "tournament") {
54 const auto size = node["tournament_size"].as<std::size_t>(3);
55 return std::make_unique<TournamentSelection<GeneType>>(size);
56 }
57
58 throw std::invalid_argument("Unknown Selection type: " + type);
59 }
60
66 static std::unique_ptr<LocalSelection<GeneType>> buildLocalSelection(const YAML::Node& node) {
67 const auto type = node["type"].as<std::string>("best_neighbor");
68
69 if (type == "best_neighbor" || type == "tournament") {
70 return std::make_unique<BestNeighborSelection<GeneType>>();
71 }
72
73 throw std::invalid_argument("Unknown LocalSelection type: " + type);
74 }
75
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");
85
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);
93 }
94
95 throw std::invalid_argument("Unknown Mutation type: " + type);
96 }
97
103 static std::unique_ptr<Crossover<GeneType>> buildCrossover(const YAML::Node& node) {
104 const auto type = node["type"].as<std::string>("single_point");
105
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>>();
112 }
113
114 throw std::invalid_argument("Unknown Crossover type: " + type);
115 }
116
122 static std::unique_ptr<DemeReplacer<GeneType>> buildDemeReplacer(const YAML::Node& node) {
123 const auto type = node["type"].as<std::string>("worst");
124
125 if (type == "worst") {
126 return std::make_unique<WorstReplacer<GeneType>>();
127 }
128
129 throw std::invalid_argument("Unknown DemeReplacer type: " + type);
130 }
131
137 static std::unique_ptr<DemeSelector<GeneType>> buildDemeSelector(const YAML::Node& node) {
138 const auto type = node["type"].as<std::string>("elitism");
139
140 if (type == "elitism") {
141 return std::make_unique<ElitismSelector<GeneType>>();
142 }
143
144 throw std::invalid_argument("Unknown DemeSelector type: " + type);
145 }
146 };
147} // namespace galib::internal
148
149#endif // OPERATOR_BUILDER_H
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.