GENEIAL  0.2=/
 All Classes Pages
SteadyStateAlgorithmBuilder.hpp
1 #pragma once
2 
3 #include <geneial/algorithm/SteadyStateAlgorithm.h>
4 
5 geneial_private_namespace(geneial)
6 {
7 geneial_private_namespace(algorithm)
8 {
9 using ::geneial::algorithm::stopping_criteria::BaseStoppingCriterion;
10 using ::geneial::algorithm::stopping_criteria::MaxGenerationCriterion;
11 
12 using ::geneial::operation::selection::BaseSelectionOperation;
13 using ::geneial::operation::selection::RouletteWheelSelection;
14 using ::geneial::operation::selection::BaseSelectionSettings;
15 
16 using ::geneial::operation::coupling::BaseCouplingOperation;
17 using ::geneial::operation::coupling::CouplingSettings;
18 using ::geneial::operation::coupling::RandomCouplingOperation;
19 
20 using ::geneial::operation::crossover::BaseCrossoverOperation;
21 using ::geneial::operation::crossover::MultiValueChromosomeNPointCrossover;
22 using ::geneial::operation::crossover::MultiValueChromosomeNPointCrossoverSettings;
23 
24 using ::geneial::operation::replacement::BaseReplacementOperation;
25 using ::geneial::operation::replacement::BaseReplacementSettings;
26 using ::geneial::operation::replacement::ReplaceWorstOperation;
27 
28 using ::geneial::operation::mutation::BaseMutationOperation;
29 using ::geneial::operation::mutation::MultiValueMutationSettings;
30 using ::geneial::operation::choosing::ChooseRandom;
31 using ::geneial::operation::mutation::UniformMutationOperation;
32 
33 using ::geneial::population::chromosome::BaseChromosomeFactory;
34 using ::geneial::population::chromosome::MultiValueChromosomeFactory;
35 
36 geneial_export_namespace
37 {
38 
39 template<typename FITNESS_TYPE>
40 std::shared_ptr<BaseStoppingCriterion<FITNESS_TYPE>>SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultStoppingCriterion() const
41 {
42  return std::make_shared<MaxGenerationCriterion<FITNESS_TYPE>>(100000);
43 }
44 
45 
46 template<typename FITNESS_TYPE>
47 std::shared_ptr<BaseSelectionOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultSelectionOperation() const
48 {
49  return typename RouletteWheelSelection<FITNESS_TYPE>::Builder().create();
50 }
51 
52 
53 template<typename FITNESS_TYPE>
54 std::shared_ptr<BaseCouplingOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultCouplingOperation() const
55 {
56  return typename RandomCouplingOperation<FITNESS_TYPE>::Builder().create();
57 }
58 
59 
60 template<typename FITNESS_TYPE>
61 std::shared_ptr<BaseCrossoverOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultCrossoverOperation() const
62 {
63 
64  //In this case we need to check whether the chosen chromomsome builder is compatible with our default operation:
65  auto mvcChromosomeFactory = std::dynamic_pointer_cast<MultiValueChromosomeFactory<int, FITNESS_TYPE>>(
66  *this->_chromosomeFactory);
67 
68  if (!mvcChromosomeFactory)
69  {
70  throw new std::runtime_error("Incompatible Chromosome Factory and default crossover strategy");
71  }
72 
73  auto builder = typename MultiValueChromosomeNPointCrossover<int,FITNESS_TYPE>::Builder(mvcChromosomeFactory);
74  return builder.create();
75 }
76 
77 
78 template<typename FITNESS_TYPE>
79 std::shared_ptr<BaseReplacementOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultReplacementOperation() const
80 {
81  return typename ReplaceWorstOperation<FITNESS_TYPE>::Builder().create();
82 }
83 
84 template<typename FITNESS_TYPE>
85 std::shared_ptr<BaseMutationOperation<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::getDefaultMutationOperation() const
86 {
87 
88  //In this case we need to check whether the chosen chromomsome builder is compatible with our default operation:
89  auto mvcChromosomeFactory = std::dynamic_pointer_cast<MultiValueChromosomeFactory<int, FITNESS_TYPE>>(*this->_chromosomeFactory);
90 
91  if (!mvcChromosomeFactory)
92  {
93  throw new std::runtime_error("Incompatible Chromosome Factory and Default Crossover Strategy");
94  }
95 
96  //Build Uniform mutation with all the default values.
97  auto mutationBuilder = typename UniformMutationOperation<int,FITNESS_TYPE>::Builder(mvcChromosomeFactory);
98 
99  return mutationBuilder.create();
100 }
101 
102 
103 template<typename FITNESS_TYPE>
104 std::shared_ptr<BaseGeneticAlgorithm<FITNESS_TYPE>> SteadyStateAlgorithm<FITNESS_TYPE>::Builder::create()
105 {
106 
107  if (!this->_stoppingCriterion)
108  {
109  this->_stoppingCriterion = getDefaultStoppingCriterion();
110  }
111 
112  if (!this->_selectionOperation)
113  {
114  this->_selectionOperation = getDefaultSelectionOperation();
115  }
116 
117  if (!this->_couplingOperation)
118  {
119  this->_couplingOperation = getDefaultCouplingOperation();
120  }
121 
122  if (!this->_crossoverOperation)
123  {
124  this->_crossoverOperation = getDefaultCrossoverOperation();
125  }
126 
127  if (!this->_replacementOperation)
128  {
129  this->_replacementOperation = getDefaultReplacementOperation();
130  }
131 
132  if (!this->_mutationOperation)
133  {
134  this->_mutationOperation = getDefaultMutationOperation();
135  }
136 
137  if (!this->_chromosomeFactory)
138  {
139  throw new std::runtime_error("One must at least specify a chromosome Factory with a fitness evaluator");
140  }
141 
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);
150 }
151 
152 
153 } /* geneial_export_namespace */
154 } /* private namespace algorithm */
155 } /* private namespace geneial */