dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
NSFeatures.h
1 //
2 // Created by amarrero on 28/6/21.
3 //
4 
5 #ifndef DIGNEA_NSFEATURES_H
6 #define DIGNEA_NSFEATURES_H
7 
8 #include <dignea/distances/Distance.h>
10 #include <dignea/searches/NoveltySearch.h>
11 #include <dignea/searches/Search.h>
12 #include <dignea/utilities/KNN.h>
15 
16 #include <iostream>
17 #include <nlohmann/json.hpp>
18 #include <vector>
19 
20 #include "NumCpp.hpp"
21 
22 using namespace std;
23 using json = nlohmann::json;
24 
32 template <typename S>
33 class NSFeatures : public NoveltySearch<S> {
34  public:
35  NSFeatures() = default;
36 
37  explicit NSFeatures(unique_ptr<Distance<float>> dist,
38  const float &threshold = 2000,
39  const float &finalThresh = 2000, const int &k = 15,
40  bool warmUp = false);
41 
42  virtual ~NSFeatures() = default;
43 
44  virtual json to_json() override;
45 
46  protected:
47  virtual vector<Descriptor> beforeRun(const vector<S> &population) override;
48 
49  virtual vector<Descriptor> beforeCmpFinals(
50  const vector<S> &population) override;
51 
52  virtual void insertFinal(const S &solution) override;
53 
54  protected:
55  bool warmed;
56 };
57 
69 template <typename S>
71  const float &threshold, const float &finalThresh,
72  const int &k, bool warmUp)
73  : NoveltySearch<S>(move(dist), threshold, finalThresh, k), warmed(warmUp) {
74  // if (warmUp) {
75  // auto seed = 42;
76  // nc::random::seed(seed);
77  // auto indexes =
78  // nc::random::randInt(nc::Shape(1, 50), (long unsigned int)0,
79  // NSWarmUp::warmUpData.size());
80 
81  // for (auto ix : indexes) {
82  // this->noveltyFeatures.push_back(NSWarmUp::warmUpData[ix]);
83  // }
84  // }
85 }
86 
95 template <typename S>
96 vector<Descriptor> NSFeatures<S>::beforeRun(const vector<S> &population) {
97  vector<Descriptor> combinedPop;
98  combinedPop.reserve(population.size() + this->noveltyArchive.size());
99  for (const S &solution : population) {
100  combinedPop.push_back(solution.getFeatures());
101  }
102  for (const S &solution : this->noveltyArchive) {
103  combinedPop.push_back(solution.getFeatures());
104  }
105  // combinedPop.insert(combinedPop.end(), this->noveltyArchive.begin(),
106  // this->noveltyArchive.end(),
107  // [](const S &s) { return s.getFeatures(); });
108  return combinedPop;
109 }
110 
111 template <typename S>
112 vector<Descriptor> NSFeatures<S>::beforeCmpFinals(const vector<S> &population) {
113  vector<Descriptor> descriptors;
114  descriptors.reserve(population.size());
115  for (const S &solution : population) {
116  descriptors.push_back(solution.getFeatures());
117  }
118  return descriptors;
119 }
120 
128 template <typename S>
129 void NSFeatures<S>::insertFinal(const S &solution) {
130  this->finalSs.push_back(solution);
131  this->finalSsDesc.push_back(solution.getFeatures());
132 }
133 
140 template <typename S>
142  json data = NoveltySearch<S>::to_json();
143  data["name"] = "NSFeatures";
144  data["warm-up"] = this->warmed;
145  return data;
146 }
147 
148 #endif // DIGNEA_NOVELTYEIG_H
nlohmann::json json
Definition: MinKnap.h:85
Class to represent the Novelty Search Algorithm This specialization is exclusively for EIG because it...
Definition: NSFeatures.h:33
virtual json to_json() override
Generates a json object with the relevant information of the class.
Definition: NSFeatures.h:141
virtual vector< Descriptor > beforeRun(const vector< S > &population) override
Performs computational work necessary for running the NS This method creates a combined population us...
Definition: NSFeatures.h:96
virtual void insertFinal(const S &solution) override
Method to insert a new individual into the noveltyArchive of novelty Ss.
Definition: NSFeatures.h:129
Class to represent the Novelty Search Algorithm.
Definition: NoveltySearch.h:50