dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
ParGABuilder.h
1 //
2 // Created by amarrero on 22/6/21.
3 //
4 
5 #ifndef DIGNEA_PARGABuilder_H
6 #define DIGNEA_PARGABuilder_H
7 
8 #include <dignea/algorithms/singleobjective/ParallelGeneticAlgorithm.h>
9 #include <dignea/core/Problem.h>
10 #include <dignea/core/Solution.h>
11 #include <dignea/factories/CXFactory.h>
12 #include <dignea/factories/MutFactory.h>
14 
15 #include <iostream>
16 
23 template <class S>
24 class ParGABuilder {
25  public:
26  static ParGABuilder<S> create();
27 
28  explicit ParGABuilder();
29 
30  virtual ~ParGABuilder() = default;
31 
32  operator unique_ptr<ParallelGeneticAlgorithm<S>>();
33 
34  ParGABuilder<S> &usingCores(const int &cores);
35 
36  ParGABuilder<S> &toSolve(unique_ptr<Problem<S>> problem);
37 
44  ParGABuilder<S> &with() { return *this; };
45 
47 
49 
50  ParGABuilder<S> &withCrossRate(const float &crossRate);
51 
53 
54  ParGABuilder<S> &withMutRate(const float &mutRate);
55 
57 
58  ParGABuilder<S> &populationOf(const int &popsize);
59 
60  ParGABuilder<S> &runDuring(const int &maxEvals);
61 
62  private:
63  void newCompSet() { compSet++; }
64 
65  private:
66  unique_ptr<ParallelGeneticAlgorithm<S>> ga;
67  int compSet;
68  bool problemSet;
69  static const int MAX_COMPONENTS;
70 };
71 
72 template <class S>
74 
75 template <class S>
76 ParGABuilder<S>::ParGABuilder() : compSet(0), problemSet(false) {
77  this->ga = make_unique<ParallelGeneticAlgorithm<S>>();
78 }
79 
86 template <class S>
88  return ParGABuilder<S>();
89 }
90 
99 template <class S>
101  this->ga->setNumberOfCores(cores);
102  this->newCompSet();
103  return *this;
104 }
105 
113 template <class S>
114 ParGABuilder<S>::operator unique_ptr<ParallelGeneticAlgorithm<S>>() {
115  if (!this->problemSet) {
116  std::cerr << "Algorithm created but no problem set. "
117  "Remember to set a problem before calling run"
118  << std::endl;
119  }
120  if (this->compSet >= ParGABuilder<S>::MAX_COMPONENTS)
121  return move(this->ga);
122  else {
123  throw runtime_error(
124  "No all the components are set. ParGA requires: problem, "
125  "mutation operator, crossover operator, selection operator, "
126  "mutation rate, crossover rate, population size, "
127  "max evaluation and number of cores");
128  }
129 }
130 
139 template <class S>
141  this->ga->setProblem(move(problem));
142  newCompSet();
143  this->problemSet = true;
144  return *this;
145 }
146 
155 template <class S>
157  CXFactory<S> factory;
158  auto crossover = factory.create(cx);
159  this->ga->setCrossover(move(crossover));
160  newCompSet();
161  return *this;
162 }
163 
172 template <class S>
174  this->ga->setCrossover(move(cx));
175  newCompSet();
176  return *this;
177 }
178 
186 template <class S>
188  this->ga->setCrossRate(crossRate);
189  newCompSet();
190  return *this;
191 }
192 
201 template <class S>
203  MutFactory<S> factory;
204  auto mut = factory.create(mutation);
205  this->ga->setMutation(move(mut));
206  newCompSet();
207  return *this;
208 }
209 
217 template <class S>
219  this->ga->setMutationRate(mutRate);
220  newCompSet();
221  return *this;
222 }
223 
232 template <class S>
234  SelFactory<S> factory;
235  auto sel = factory.create(selType);
236  this->ga->setSelection(move(sel));
237  newCompSet();
238  return *this;
239 }
240 
249 template <class S>
251  this->ga->setPopulationSize(popsize);
252  newCompSet();
253  return *this;
254 }
255 
263 template <class S>
265  this->ga->setMaxEvaluations(maxEvals);
266  newCompSet();
267  return *this;
268 }
269 
270 #endif // DIGNEA_ParGABuilder_H
CXType
Types of Crossover Operators implemented in dignea.
Definition: CrossoverTypes.h:23
MutType
Type of Mutation Operators implemented in dignea.
Definition: MutationTypes.h:22
SelType
Types of Selection Operators implemented in dignea.
Definition: SelectionTypes.h:19
Crossover factory which allows the user to create Crossover operators easily.
Definition: CXFactory.h:19
unique_ptr< Crossover< S > > create(CXType type)
Creates a unique pointer to a Crossover Operator of the given type. Variants are available at CXType.
Definition: CXFactory.h:39
Abstract Crossover interface.
Definition: Crossover.h:19
Mutation factory which allows the user to create Mutation Operators easily.
Definition: MutFactory.h:23
unique_ptr< Mutation< S > > create(MutType type)
Creates a unique pointer to a Mutation Operator of type.
Definition: MutFactory.h:45
Parallel Genetic Algorithm Builder. This class creates a unique_ptr to a Parallel Genetic Algorithm.
Definition: ParGABuilder.h:24
ParGABuilder< S > & mutation(MutType mutation)
Defines the mutation operator type to use with this Genetic Algorithm. Variants are available at MutT...
Definition: ParGABuilder.h:202
ParGABuilder< S > & with()
This method only returns *this and it is used to make the building process more natural....
Definition: ParGABuilder.h:44
static ParGABuilder< S > create()
Creates a ParGABuilder object.
Definition: ParGABuilder.h:87
ParGABuilder< S > & crossover(CXType cxType)
Defines the crossover operator type to use with this Genetic Algorithm. Variants are available at CXT...
Definition: ParGABuilder.h:156
ParGABuilder< S > & withMutRate(const float &mutRate)
Defines the mutation rate to apply in the mutation operator.
Definition: ParGABuilder.h:218
ParGABuilder< S > & runDuring(const int &maxEvals)
Sets the number of evaluations to perform.
Definition: ParGABuilder.h:264
ParGABuilder< S > & toSolve(unique_ptr< Problem< S >> problem)
Defines the problem to solve with the Genetic Algorithm. Receives a unique_ptr and takes the ownershi...
Definition: ParGABuilder.h:140
ParGABuilder< S > & usingCores(const int &cores)
Defines the number of cores that the Parallel Genetic Algorithm will be using.
Definition: ParGABuilder.h:100
ParGABuilder< S > & populationOf(const int &popsize)
Defines the number of individuals in the population of the Genetic Algorithm.
Definition: ParGABuilder.h:250
ParGABuilder< S > & withCrossRate(const float &crossRate)
Defines the crossover rate to apply the crossover operator.
Definition: ParGABuilder.h:187
ParGABuilder< S > & selection(SelType selType)
Defines the selection operator to use with this Genetic Algorithm. Variants are available at SelType.
Definition: ParGABuilder.h:233
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
Selection factory which allows the user to create Selection Operators easily.
Definition: SelFactory.h:25
unique_ptr< Selection< S > > create(SelType type)
Creates a unique pointer to a Selection Operator of type.
Definition: SelFactory.h:41