dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
NSPerformance.h
1 //
2 // Created by amarrero on 28/6/21.
3 //
4 
5 #ifndef DIGNEA_NSPERFORMANCE_H
6 #define DIGNEA_NSPERFORMANCE_H
7 
8 #include <dignea/distances/Distance.h>
10 #include <dignea/searches/NSFeatures.h>
11 #include <dignea/utilities/KNN.h>
13 
14 #include <iostream>
15 #include <nlohmann/json.hpp>
16 #include <vector>
17 
18 using namespace std;
19 using json = nlohmann::json;
20 
29 template <typename S>
30 class NSPerformance : public NoveltySearch<S> {
31  public:
32  NSPerformance() = default;
33 
34  explicit NSPerformance(unique_ptr<Distance<float>> dist,
35  const float &threshold = 2000,
36  const float &finalThresh = 2000, const int &k = 15);
37 
38  virtual ~NSPerformance() = default;
39 
40  virtual json to_json() override;
41 
42  protected:
43  virtual vector<Descriptor> beforeRun(const vector<S> &population) override;
44 
45  virtual vector<Descriptor> beforeCmpFinals(
46  const vector<S> &population) override;
47 
48  virtual void insertFinal(const S &solution);
49 };
50 
59 template <typename S>
61  const float &threshold,
62  const float &finalThresh, const int &k)
63  : NoveltySearch<S>(move(dist), threshold, finalThresh, k) {}
64 
73 template <typename S>
74 vector<Descriptor> NSPerformance<S>::beforeRun(const vector<S> &population) {
75  vector<Descriptor> combinedPop;
76  combinedPop.reserve(population.size() + this->noveltyArchive.size());
77  for (const S &solution : population) {
78  combinedPop.push_back(solution.getAvgPortFitness());
79  }
80  for (const S &solution : this->noveltyArchive) {
81  combinedPop.push_back(solution.getAvgPortFitness());
82  }
83  // combinedPop.insert(combinedPop.end(), this->noveltyArchive.begin(),
84  // this->noveltyArchive.end(),
85  // [](const S &s) { return s.getAvgPortFitness(); });
86  return combinedPop;
87 }
88 
89 template <typename S>
90 vector<Descriptor> NSPerformance<S>::beforeCmpFinals(
91  const vector<S> &population) {
92  vector<Descriptor> descriptors;
93  descriptors.reserve(population.size());
94  for (const S &solution : population) {
95  descriptors.push_back(solution.getAvgPortFitness());
96  }
97  return descriptors;
98 }
99 
107 template <typename S>
108 void NSPerformance<S>::insertFinal(const S &solution) {
109  this->finalSs.push_back(solution);
110  this->finalSsDesc.push_back(solution.getAvgPortFitness());
111 }
112 
119 template <typename S>
121  json data = NoveltySearch<S>::to_json();
122  data["name"] = "NSPerformance";
123 
124  return data;
125 }
126 
127 #endif
nlohmann::json json
Definition: MinKnap.h:85
Class to represent the Novelty Search Algorithm This specialization is exclusively for EIG because it...
Definition: NSPerformance.h:30
virtual vector< Descriptor > beforeRun(const vector< S > &population) override
PerforS computational work necessary for running the NS This method creats a combined population usin...
Definition: NSPerformance.h:74
virtual void insertFinal(const S &solution)
Method to insert a new individual into the noveltyArchive of novelty Ss.
Definition: NSPerformance.h:108
virtual json to_json() override
Generates a json object with the relevant information of the class.
Definition: NSPerformance.h:120
Class to represent the Novelty Search Algorithm.
Definition: NoveltySearch.h:50