dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
AbstractInstance.h
Go to the documentation of this file.
1 
12 #ifndef __EIG_SOLUTION_H__
13 #define __EIG_SOLUTION_H__
14 
15 #include <dignea/core/Front.h>
16 #include <dignea/core/Solution.h>
17 
18 #include <algorithm>
19 #include <vector>
20 
21 using namespace std;
22 
29 template <typename V, typename O>
30 class AbstractInstance : public Solution<V, O> {
31  public:
33 
34  explicit AbstractInstance(const int &nVars, const int &nObjs = 1,
35  const int &nCons = 0);
36 
38 
39  explicit AbstractInstance(const AbstractInstance *solution);
40 
41  AbstractInstance(unique_ptr<AbstractInstance> &ptr) = delete;
42 
43  virtual ~AbstractInstance() = default;
44 
46 
47  bool operator==(const AbstractInstance &other) const;
48 
55  json to_json() const override { return Solution<V, O>::to_json(); }
56 
62  void setFeatures(const vector<float> &v);
63 
69  virtual vector<float> getFeatures() const { return features; }
70 
76  float getDiversity() const { return diversity; }
77 
84  void setDiversity(float diversity) {
85  AbstractInstance::diversity = diversity;
86  }
87 
96  float getBiasedFitness() const { return biasedFitness; }
97 
105  void setBiasedFitness(float bFitness) {
107  }
108 
116  void setConfigFitness(const vector<vector<float>> &fitness) {
117  this->portFitness = fitness;
118  }
119 
127  void setAvgConfigFitness(const vector<float> &fitness) {
128  this->avgPortFitness = fitness;
129  }
130 
137  vector<vector<float>> getPortFitness() const { return portFitness; }
138 
145  vector<float> getAvgPortFitness() const { return avgPortFitness; }
146 
154  virtual void to_instance(std::ostream &os) const {
155  os << "Not implemented in AbstractInstance" << std::endl;
156  };
157 
165  friend std::ostream &operator<<(std::ostream &os,
166  AbstractInstance<V, O> const &solution) {
167  solution.to_instance(os);
168  return os;
169  }
170 
171  protected:
177  vector<float> features;
181  float diversity;
191  vector<vector<float>> portFitness;
197  vector<float> avgPortFitness;
198 };
199 
206 template <typename V, typename O>
208  : Solution<V, O>(),
209  features({}),
210  diversity(0.0),
211  biasedFitness(0.0),
212  portFitness({}),
213  avgPortFitness({}) {}
214 
225 template <typename V, typename O>
226 AbstractInstance<V, O>::AbstractInstance(const int &nVars, const int &nObjs,
227  const int &nCons)
228  : Solution<V, O>(nVars, nObjs, nCons),
229  features({}),
230  diversity(0.0),
231  biasedFitness(0.0),
232  portFitness({}),
233  avgPortFitness({}) {}
234 
243 template <typename V, typename O>
245  : Solution<V, O>(solution),
246  features(solution.features),
247  diversity(solution.diversity),
248  biasedFitness(solution.biasedFitness),
249  portFitness(solution.portFitness),
250  avgPortFitness(solution.avgPortFitness) {}
251 
260 template <typename V, typename O>
262  : Solution<V, O>(solution),
263  features(solution->features),
264  diversity(solution->diversity),
265  biasedFitness(solution->biasedFitness),
266  portFitness(solution->portFitness),
267  avgPortFitness(solution->avgPortFitness) {}
268 
276 template <typename V, typename O>
277 void AbstractInstance<V, O>::setFeatures(const vector<float> &v) {
278  this->features = v;
279 }
280 
289 template <typename V, typename O>
291  const AbstractInstance &copy) {
292  if (this == &copy) {
293  return *this;
294  }
295  this->nVars = copy.nVars;
296  this->nObjs = copy.nObjs;
297  this->nCons = copy.nCons;
298  this->fitness = copy.fitness;
299  this->constraintCoeff = copy.constraintCoeff;
300  this->variables = copy.variables;
301  this->objectives = copy.objectives;
302  this->constraints = copy.constraints;
303  features = copy.features;
304  diversity = copy.diversity;
305  biasedFitness = copy.biasedFitness;
306  portFitness = copy.portFitness;
307  avgPortFitness = copy.avgPortFitness;
308 
309  return *this;
310 }
311 
321 template <typename V, typename O>
323  if (other.fitness != this->fitness) {
324  return false;
325  }
326  if (other.diversity != this->diversity) {
327  return false;
328  }
329  if (other.biasedFitness != this->biasedFitness) {
330  return false;
331  }
332  if (other.constraintCoeff != this->constraintCoeff) {
333  return false;
334  }
335  if ((this->nVars != other.nVars) || (this->nObjs != other.nObjs) ||
336  (this->nCons != other.nCons)) {
337  return false;
338  }
339  for (int i = 0; i < other.nVars; i++) {
340  if (other.variables[i] != this->variables[i]) {
341  return false;
342  }
343  }
344  for (int i = 0; i < other.nObjs; i++) {
345  if (other.objectives[i] != this->objectives[i]) {
346  return false;
347  }
348  }
349  for (int i = 0; i < other.nCons; i++) {
350  if (other.constraints[i] != this->constraints[i]) {
351  return false;
352  }
353  }
354  for (unsigned int i = 0; i < other.features.size(); i++) {
355  if (other.features[i] != this->features[i]) {
356  return false;
357  }
358  }
359  return true;
360 }
361 
362 #endif // __EIG_SOLUTION_H__
Solution for a Instance Generation Problem used in the EIG. This is a solution for AbstractDomain.
Definition: AbstractInstance.h:30
virtual vector< float > getFeatures() const
Get the features descriptor of this instance.
Definition: AbstractInstance.h:69
float diversity
Individual diversity respect to the rest of the population.
Definition: AbstractInstance.h:181
AbstractInstance(const int &nVars, const int &nObjs=1, const int &nCons=0)
Construct a new AbstractInstance<V, O>::AbstractInstance object from the data given in the arguments.
Definition: AbstractInstance.h:226
float biasedFitness
Individual's fitness regarding to the performance of the target algorithm.
Definition: AbstractInstance.h:186
vector< float > getAvgPortFitness() const
Get the Avg Config Fitness vector.
Definition: AbstractInstance.h:145
AbstractInstance & operator=(const AbstractInstance &copy)
Copies a solution using the assignment operator.
Definition: AbstractInstance.h:290
AbstractInstance()
Construct a new AbstractInstance<V, O>::AbstractInstance object.
Definition: AbstractInstance.h:207
friend std::ostream & operator<<(std::ostream &os, AbstractInstance< V, O > const &solution)
Dumps into the ostream the instance representation.
Definition: AbstractInstance.h:165
void setBiasedFitness(float bFitness)
Set the Biased Fitness value. The BiasedFitness is the fitness value compute exclusively from the dif...
Definition: AbstractInstance.h:105
vector< float > features
Vector of features of the instance. This is problem dependent and must defined in order and computed ...
Definition: AbstractInstance.h:177
AbstractInstance(const AbstractInstance *solution)
Construct a new AbstractInstance<V, O>::AbstractInstance object from the data in the given solution.
Definition: AbstractInstance.h:261
void setAvgConfigFitness(const vector< float > &fitness)
Set the average fitness of each algorithm in the porftolio over this instance.
Definition: AbstractInstance.h:127
json to_json() const override
Creates a JSON format representation of the solution. It must be override by the subclasses.
Definition: AbstractInstance.h:55
vector< vector< float > > portFitness
Best fitness founds for all the algorithms in the portfolio that has solved the instance.
Definition: AbstractInstance.h:191
void setFeatures(const vector< float > &v)
Set the features descriptor of this instance.
Definition: AbstractInstance.h:277
void setConfigFitness(const vector< vector< float >> &fitness)
Set the fitness of each algorithm in the porftolio over this instance.
Definition: AbstractInstance.h:116
bool operator==(const AbstractInstance &other) const
Compares whether two solutions are equal.
Definition: AbstractInstance.h:322
float getBiasedFitness() const
Get the Biased Fitness value. The BiasedFitness is the fitness value compute exclusively from the dif...
Definition: AbstractInstance.h:96
vector< vector< float > > getPortFitness() const
Get the Config Fitness matrix.
Definition: AbstractInstance.h:137
virtual void to_instance(std::ostream &os) const
Not implemented here. The purpose of this method is to generate a string representation of the instan...
Definition: AbstractInstance.h:154
float getDiversity() const
Get the Diversity of the solution.
Definition: AbstractInstance.h:76
vector< float > avgPortFitness
Average best fitness founds for all the algorithms in the portfolio that has solved the instance in t...
Definition: AbstractInstance.h:197
void setDiversity(float diversity)
Set the Diversity of the solution. The diversity of the instance is computed using the Novelty Search...
Definition: AbstractInstance.h:84
AbstractInstance(const AbstractInstance &solution)
Construct a new AbstractInstance<V, O>::AbstractInstance object from the data in the given solution.
Definition: AbstractInstance.h:244
Class to represent a solution to the optimization problems in the tool.
Definition: Solution.h:25
int nVars
Definition: Solution.h:194
vector< V > variables
Definition: Solution.h:201
int nCons
Definition: Solution.h:196
int nObjs
Definition: Solution.h:195
virtual json to_json() const
Creates and returns a JSON representation of the solution.
Definition: Solution.h:591
float fitness
Definition: Solution.h:197
vector< O > objectives
Definition: Solution.h:200
vector< O > constraints
Definition: Solution.h:202
float constraintCoeff
Definition: Solution.h:198