dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
EIGBuilder.h
1 //
2 // Created by amarrero on 23/6/21.
3 //
4 
5 #ifndef DIGNEA_EIGBUILDER_H
6 #define DIGNEA_EIGBUILDER_H
7 
8 #include <dignea/factories/CXFactory.h>
9 #include <dignea/factories/MutFactory.h>
14 #include <dignea/generator/EIG.h>
15 // #include <dignea/generator/MOEIG.h>
17 #include <dignea/searches/NSFeatures.h>
18 #include <dignea/searches/NSPerformance.h>
19 
24 enum class GeneratorType { LinearScaled, MultiObjective, MapElites };
25 
34 template <typename IP, typename IS, typename OP, typename S>
35 class EIGBuilder {
36  using EA = unique_ptr<AbstractEA<S>>;
37 
38  public:
39  static EIGBuilder<IP, IS, OP, S> create(GeneratorType type);
40 
41  virtual ~EIGBuilder() = default;
42 
43  operator unique_ptr<EIG<IP, IS, OP, S>>();
44 
45  EIGBuilder<IP, IS, OP, S> &toSolve(unique_ptr<IP> problem);
46 
47  EIGBuilder<IP, IS, OP, S> &weights(const float &fW, const float &mW);
48 
55  EIGBuilder<IP, IS, OP, S> &with() { return *this; };
56 
57  EIGBuilder<IP, IS, OP, S> &portfolio(vector<EA> &configs);
58 
60  unique_ptr<Distance<float>> dist,
61  const float &thres,
62  const float &finalThresh, const int k,
63  bool warmUp = false);
64 
65  EIGBuilder<IP, IS, OP, S> &evalWith(unique_ptr<InstanceFitness> evalMethod);
66 
67  EIGBuilder<IP, IS, OP, S> &repeating(const int &reps);
68 
70 
71  EIGBuilder<IP, IS, OP, S> &withCrossRate(const float &crSsRate);
72 
74 
75  EIGBuilder<IP, IS, OP, S> &withMutRate(const float &mutRate);
76 
78 
79  EIGBuilder<IP, IS, OP, S> &replacement(ReplacementTypes reType);
80 
81  EIGBuilder<IP, IS, OP, S> &populationOf(const int &popsize);
82 
83  EIGBuilder<IP, IS, OP, S> &runDuring(const int &generations);
84 
86 
87  private:
88  EIGBuilder(GeneratorType t);
89 
90  void newCompSet() { compSet++; }
91 
92  private:
93  unique_ptr<EIG<IP, IS, OP, S>> generator;
94  int compSet;
95  bool problemSet;
96  GeneratorType type;
97  static int MUST_COMPS;
98 };
99 
100 template <typename IP, typename IS, typename OP, typename S>
102 
112 template <typename IP, typename IS, typename OP, typename S>
114  return EIGBuilder<IP, IS, OP, S>(t);
115 }
116 
126 template <typename IP, typename IS, typename OP, typename S>
128  : compSet(0), problemSet(false), type(t) {
129  this->type = t; // Saves the type we're building
130  switch (type) {
131  case GeneratorType::LinearScaled:
132  this->generator = make_unique<EIG<IP, IS, OP, S>>();
133  break;
134  // case GeneratorType::MultiObjective:
135  // this->generator = make_unique<MOEIG<IP, IS, OP, S>>();
136  // break;
137  case GeneratorType::MapElites:
138  MUST_COMPS = 10; // MapElites does not need NS parameters
139  this->generator = make_unique<MapElites<IP, IS, OP, S>>();
140  break;
141  default:
142  std::cerr << "Unknown type in EIGBuilder. LinearScaled algorithm "
143  "created by default"
144  << std::endl;
145  this->generator = make_unique<EIG<IP, IS, OP, S>>();
146 
147  break;
148  }
149 }
150 
161 template <typename IP, typename IS, typename OP, typename S>
162 EIGBuilder<IP, IS, OP, S>::operator unique_ptr<EIG<IP, IS, OP, S>>() {
163  if (!this->problemSet) {
164  std::cerr << "EIG created but not problem set. "
165  "Remember to set a problem before calling run"
166  << std::endl;
167  }
168  if (compSet >= MUST_COMPS) {
169  return move(this->generator);
170  } else {
171  throw runtime_error(
172  "No all the components are set. EIG requires: "
173  "configurations, evaluation method,"
174  "Novelty Search configuration, "
175  "mutation operator, crSsover operator, "
176  "selection operator, mutation rate, "
177  "crSsover rate, population size, "
178  "max generations and repetitions");
179  }
180 }
181 
192 template <typename IP, typename IS, typename OP, typename S>
194  unique_ptr<IP> problem) {
195  this->generator->setInstanceProblem(move(problem));
196  newCompSet();
197  this->problemSet = true;
198  return *this;
199 }
200 
213 template <typename IP, typename IS, typename OP, typename S>
215  const float &nW) {
216  this->generator->setWeightedFunction(fW, nW);
217  newCompSet();
218  return *this;
219 }
220 
233 template <typename IP, typename IS, typename OP, typename S>
235  vector<EA> &configs) {
236  this->generator->setPortfolio(configs);
237  newCompSet();
238  return *this;
239 }
240 
257 template <typename IP, typename IS, typename OP, typename S>
259  NSType nsType, unique_ptr<Distance<float>> dist, const float &thres,
260  const float &finalThresh, const int k, bool warmUp) {
261  NSFactory<IS> factory;
262  auto ns = factory.create(nsType, move(dist), thres, finalThresh, k, warmUp);
263  this->generator->setNoveltySearch(move(ns));
264  newCompSet();
265  return *this;
266 }
267 
279 template <typename IP, typename IS, typename OP, typename S>
281  unique_ptr<InstanceFitness> evalMethod) {
282  this->generator->setInstanceFitness(move(evalMethod));
283  newCompSet();
284  return *this;
285 }
286 
298 template <typename IP, typename IS, typename OP, typename S>
300  const int &reps) {
301  this->generator->setRepetitions(reps);
302  newCompSet();
303  return *this;
304 }
305 
317 template <typename IP, typename IS, typename OP, typename S>
319  CXFactory<IS> factory;
320  auto cx = factory.create(cxType);
321  this->generator->setCrossover(move(cx));
322  newCompSet();
323  return *this;
324 }
325 
336 template <typename IP, typename IS, typename OP, typename S>
338  const float &crSsRate) {
339  this->generator->setCrossoverRate(crSsRate);
340  newCompSet();
341  return *this;
342 }
343 
355 template <typename IP, typename IS, typename OP, typename S>
357  MutType mutation) {
358  MutFactory<IS> factory;
359  auto mut = factory.create(mutation);
360  this->generator->setMutation(move(mut));
361  newCompSet();
362  return *this;
363 }
364 
375 template <typename IP, typename IS, typename OP, typename S>
377  const float &mutRate) {
378  this->generator->setMutationRate(mutRate);
379  newCompSet();
380  return *this;
381 }
382 
394 template <typename IP, typename IS, typename OP, typename S>
396  SelType selType) {
397  SelFactory<IS> factory;
398  auto sel = factory.create(selType);
399  this->generator->setSelection(move(sel));
400  newCompSet();
401  return *this;
402 }
403 
404 template <typename IP, typename IS, typename OP, typename S>
406  ReplacementTypes repType) {
407  RepFactory<IS> factory;
408  auto rep = factory.create(repType);
409  this->generator->setReplacement(move(rep));
410  newCompSet();
411  return *this;
412 }
413 
425 template <typename IP, typename IS, typename OP, typename S>
427  const int &popsize) {
428  this->generator->setPopulationSize(popsize);
429  newCompSet();
430  return *this;
431 }
432 
445 template <typename IP, typename IS, typename OP, typename S>
447  const int &generations) {
448  this->generator->setMaxEvaluations(generations);
449  newCompSet();
450  return *this;
451 }
452 
463 template <typename IP, typename IS, typename OP, typename S>
465  const Features &f) {
466  if (this->type == GeneratorType::MapElites) {
467  static_cast<MapElites<IP, IS, OP, S> *>(this->generator.get())
468  ->setFeaturesInfo(f);
469  newCompSet();
470  return *this;
471  }
472  return *this;
473 }
474 
475 #endif // DIGNEA_EIGBUILDER_H
CXType
Types of Crossover Operators implemented in dignea.
Definition: CrossoverTypes.h:23
std::map< int, FeatureInfo > Features
Features defines the information a feature.
Definition: MapElites.h:46
MutType
Type of Mutation Operators implemented in dignea.
Definition: MutationTypes.h:22
NSType
Type of Novelty Search algorithms available in dignea at the moment.
Definition: NSTypes.h:23
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
Class which represents a Builder for the EIG. Creates a EIG step by step from all the different compo...
Definition: EIGBuilder.h:35
EIGBuilder< IP, IS, OP, S > & weights(const float &fW, const float &mW)
Sets the weights that will be used in the Weighted Fitness Function. Notice that fW + nW = 1....
Definition: EIGBuilder.h:214
EIGBuilder< IP, IS, OP, S > & selection(SelType selType)
Defines the selection operator to use. Variants are available at SelType.
Definition: EIGBuilder.h:395
EIGBuilder< IP, IS, OP, S > & withSearch(NSType nsType, unique_ptr< Distance< float >> dist, const float &thres, const float &finalThresh, const int k, bool warmUp=false)
Novelty search configuration for the EIG. Here we define the Novelty Approach (Features,...
Definition: EIGBuilder.h:258
EIGBuilder< IP, IS, OP, S > & withCrossRate(const float &crSsRate)
Defines the crossover rate to apply the crossover operator.
Definition: EIGBuilder.h:337
EIGBuilder< IP, IS, OP, S > & mutation(MutType mutation)
Defines the mutation operator type to use with this Genetic Algorithm. Variants are available at MutT...
Definition: EIGBuilder.h:356
EIGBuilder< IP, IS, OP, S > & repeating(const int &reps)
Defines how many repetitions the algorithms in the portfolio must perform over each instance in each ...
Definition: EIGBuilder.h:299
static EIGBuilder< IP, IS, OP, S > create(GeneratorType type)
Creates a EIGBuilder object.
Definition: EIGBuilder.h:113
EIGBuilder< IP, IS, OP, S > & evalWith(unique_ptr< InstanceFitness > evalMethod)
Evaluation approach for generating instances. The instances must be considered easy to solve for the ...
Definition: EIGBuilder.h:280
EIGBuilder< IP, IS, OP, S > & runDuring(const int &generations)
Defines the number of generations to perform by EIG. Notice that the EIG works by generations instead...
Definition: EIGBuilder.h:446
EIGBuilder< IP, IS, OP, S > & toSolve(unique_ptr< IP > problem)
Defines the AbstractDomain to solve.
Definition: EIGBuilder.h:193
EIGBuilder< IP, IS, OP, S > & portfolio(vector< EA > &configs)
Defines the portfolio of solvers to evaluate the instances during the evolutionary process....
Definition: EIGBuilder.h:234
EIGBuilder< IP, IS, OP, S > & populationOf(const int &popsize)
Defines the population of instances to use. This is the number of instances that the EIG will be evol...
Definition: EIGBuilder.h:426
EIGBuilder< IP, IS, OP, S > & setFeatures(const Features &f)
Defines the Feature information for MapElites.
Definition: EIGBuilder.h:464
EIGBuilder< IP, IS, OP, S > & withMutRate(const float &mutRate)
Defines the mutation rate to apply in the mutation operator.
Definition: EIGBuilder.h:376
EIGBuilder< IP, IS, OP, S > & with()
This method only returns *this and it is used to make the building process more natural....
Definition: EIGBuilder.h:55
EIGBuilder< IP, IS, OP, S > & crossover(CXType cxType)
Defines the crossover operator type to use with this Genetic Algorithm. Variants are available at CXT...
Definition: EIGBuilder.h:318
Instance Generation Algorithm. Known as Meta-Evolutionary Algorithm (MapElites). This algorithm uses ...
Definition: MapElites.h:66
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
Novelty Search factory which allows the user to create Novelty Search algorithms on the go.
Definition: NSFactory.h:29
unique_ptr< NoveltySearch< S > > create(NSType type, unique_ptr< Distance< float >> dist, const float &thres, const float &finalThresh, const int k, bool warmUp)
Creates a unique pointer to a Novelty Search algorithm of given type. Variants are available at NSTyp...
Definition: NSFactory.h:64
Replacement Factory which allows the user to easily create Replacement Operators.
Definition: RepFactory.h:29
unique_ptr< Replacement< S > > create(ReplacementTypes type)
Creates a unique pointer to a Selection Operator of type.
Definition: RepFactory.h:51
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