3 #include <geneial/algorithm/SteadyStateAlgorithm.h>
5 geneial_private_namespace(geneial)
7 geneial_private_namespace(algorithm)
9 using ::geneial::algorithm::stopping_criteria::BaseStoppingCriterion;
10 using ::geneial::algorithm::stopping_criteria::MaxGenerationCriterion;
12 using ::geneial::operation::selection::BaseSelectionOperation;
13 using ::geneial::operation::selection::RouletteWheelSelection;
14 using ::geneial::operation::selection::BaseSelectionSettings;
16 using ::geneial::operation::coupling::BaseCouplingOperation;
17 using ::geneial::operation::coupling::CouplingSettings;
18 using ::geneial::operation::coupling::RandomCouplingOperation;
20 using ::geneial::operation::crossover::BaseCrossoverOperation;
21 using ::geneial::operation::crossover::MultiValueChromosomeNPointCrossover;
22 using ::geneial::operation::crossover::MultiValueChromosomeNPointCrossoverSettings;
24 using ::geneial::operation::replacement::BaseReplacementOperation;
25 using ::geneial::operation::replacement::BaseReplacementSettings;
26 using ::geneial::operation::replacement::ReplaceWorstOperation;
28 using ::geneial::operation::mutation::BaseMutationOperation;
29 using ::geneial::operation::mutation::MultiValueMutationSettings;
30 using ::geneial::operation::choosing::ChooseRandom;
31 using ::geneial::operation::mutation::UniformMutationOperation;
33 using ::geneial::population::chromosome::BaseChromosomeFactory;
34 using ::geneial::population::chromosome::MultiValueChromosomeFactory;
36 geneial_export_namespace
39 template<
typename FITNESS_TYPE>
40 std::shared_ptr<BaseStoppingCriterion<FITNESS_TYPE>>SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultStoppingCriterion()
const
42 return std::make_shared<MaxGenerationCriterion<FITNESS_TYPE>>(100000);
46 template<
typename FITNESS_TYPE>
47 std::shared_ptr<BaseSelectionOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultSelectionOperation()
const
49 return typename RouletteWheelSelection<FITNESS_TYPE>::Builder().create();
53 template<
typename FITNESS_TYPE>
54 std::shared_ptr<BaseCouplingOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultCouplingOperation()
const
56 return typename RandomCouplingOperation<FITNESS_TYPE>::Builder().create();
60 template<
typename FITNESS_TYPE>
61 std::shared_ptr<BaseCrossoverOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultCrossoverOperation()
const
65 auto mvcChromosomeFactory = std::dynamic_pointer_cast<MultiValueChromosomeFactory<int, FITNESS_TYPE>>(
66 *this->_chromosomeFactory);
68 if (!mvcChromosomeFactory)
70 throw new std::runtime_error(
"Incompatible Chromosome Factory and default crossover strategy");
73 auto builder =
typename MultiValueChromosomeNPointCrossover<int,FITNESS_TYPE>::Builder(mvcChromosomeFactory);
74 return builder.create();
78 template<
typename FITNESS_TYPE>
79 std::shared_ptr<BaseReplacementOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultReplacementOperation()
const
81 return typename ReplaceWorstOperation<FITNESS_TYPE>::Builder().create();
84 template<
typename FITNESS_TYPE>
85 std::shared_ptr<BaseMutationOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultMutationOperation()
const
89 auto mvcChromosomeFactory = std::dynamic_pointer_cast<MultiValueChromosomeFactory<int, FITNESS_TYPE>>(*this->_chromosomeFactory);
91 if (!mvcChromosomeFactory)
93 throw new std::runtime_error(
"Incompatible Chromosome Factory and Default Crossover Strategy");
97 auto mutationBuilder =
typename UniformMutationOperation<int,FITNESS_TYPE>::Builder(mvcChromosomeFactory);
99 return mutationBuilder.create();
103 template<
typename FITNESS_TYPE>
104 std::shared_ptr<BaseGeneticAlgorithm<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::create()
107 if (!this->_stoppingCriterion)
109 this->_stoppingCriterion = getDefaultStoppingCriterion();
112 if (!this->_selectionOperation)
114 this->_selectionOperation = getDefaultSelectionOperation();
117 if (!this->_couplingOperation)
119 this->_couplingOperation = getDefaultCouplingOperation();
122 if (!this->_crossoverOperation)
124 this->_crossoverOperation = getDefaultCrossoverOperation();
127 if (!this->_replacementOperation)
129 this->_replacementOperation = getDefaultReplacementOperation();
132 if (!this->_mutationOperation)
134 this->_mutationOperation = getDefaultMutationOperation();
137 if (!this->_chromosomeFactory)
139 throw new std::runtime_error(
"One must at least specify a chromosome Factory with a fitness evaluator");
142 auto algorithm = SteadyStateAlgorithm<FITNESS_TYPE>::makeShared(*this->_stoppingCriterion,
143 *this->_selectionOperation,
144 *this->_couplingOperation,
145 *this->_crossoverOperation,
146 *this->_replacementOperation,
147 *this->_mutationOperation,
148 *this->_chromosomeFactory);
149 return std::move(algorithm);