dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Weighted.h
1 //
2 // Created by amarrero on 21/6/21.
3 //
4 
5 #ifndef DIGNEA_WEIGHTED_H
6 #define DIGNEA_WEIGHTED_H
8 #include <dignea/generator/evaluations/InstanceFitness.h>
9 #include <dignea/searches/NoveltySearch.h>
10 #include <fmt/core.h>
11 
12 #include <string>
13 #include <string_view>
14 #include <tuple>
15 
23 template <class AbstractInstance>
24 class Weighted {
25  public:
26  Weighted(const float &fRatio, const float &dRatio);
27 
28  virtual ~Weighted() = default;
29 
30  void computeFitness(vector<AbstractInstance> &solutions);
31 
38  std::tuple<float, float> getFAndNRatios() {
39  return {fitnessRatio, diversityRatio};
40  }
41 
42  private:
43  float fitnessRatio;
44  float diversityRatio;
45  unique_ptr<InstanceFitness> fitnessFunc;
46 };
47 
56 template <class AbstractInstance>
57 Weighted<AbstractInstance>::Weighted(const float &fRatio, const float &dRatio)
58  : fitnessRatio(fRatio), diversityRatio(dRatio), fitnessFunc() {}
59 
69 template <class AbstractInstance>
71  vector<AbstractInstance> &solutions) {
72  for (unsigned int i = 0; i < solutions.size(); i++) {
73  auto fullFitness = solutions[i].getBiasedFitness() * fitnessRatio +
74  solutions[i].getDiversity() * diversityRatio;
75 
76 #ifdef DEBUG
77  std::cout << fmt::format("{:>20}\t{:>20}\t{:>20}",
78  solutions[i].getBiasedFitness(),
79  solutions[i].getDiversity(), fullFitness)
80  << std::endl;
81 #endif
82 
83  solutions[i].setFitness(fullFitness);
84  }
85 }
86 
87 #endif // DIGNEA_WEIGHTED_H
Weighted Fitness Function used from evaluating the instances in EIG. This functions performs a linear...
Definition: Weighted.h:24
void computeFitness(vector< AbstractInstance > &solutions)
Computes the actual fitness of the solutions based on the fitness pre-defined in the instances and th...
Definition: Weighted.h:70
std::tuple< float, float > getFAndNRatios()
Returns a tuple containing the fitness and novelty ratio in that order!
Definition: Weighted.h:38
Weighted(const float &fRatio, const float &dRatio)
Construct a new Weighted object with the corresponding fitness ratio and diversity ratio.
Definition: Weighted.h:57