dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Solution.h
1 //
2 // Created by amarrero on 2/12/20.
3 //
4 
5 #ifndef DIGNEA_SOLUTION_H
6 #define DIGNEA_SOLUTION_H
7 
8 #include <dignea/utilities/exceptions/OutOfRange.h>
9 
10 #include <iostream>
11 #include <memory>
12 #include <nlohmann/json.hpp>
13 #include <vector>
14 
15 using namespace std;
16 using json = nlohmann::json;
17 
24 template <typename V, typename O>
25 class Solution {
26  public:
27  Solution();
28 
29  virtual ~Solution();
30 
31  Solution(const int &nVars, const int &nObjs);
32 
33  Solution(const int &nVars, const int &nObjs, const int &nCons);
34 
35  Solution(const Solution<V, O> &);
36 
37  Solution(const Solution<V, O> *);
38 
39  Solution &operator=(const Solution &copy);
40 
41  bool operator==(const Solution &other) const;
42 
48  virtual const vector<O> &getObjectives() const { return objectives; }
49 
50  virtual void setObjectives(const vector<O> &objs);
51 
57  virtual const vector<V> &getVariables() const { return variables; }
58 
59  virtual void setVariables(const vector<V> &vars);
60 
66  virtual const vector<O> &getConstraints() const { return constraints; }
67 
68  virtual void setConstraints(const vector<O> &constr);
74  virtual int getNumberOfVars() const { return nVars; }
75 
81  virtual void setNumberOfVars(const int &numberOfVars) {
82  this->nVars = numberOfVars;
83  }
84 
90  virtual int getNumberOfObjs() const { return nObjs; }
91 
97  virtual void setNumberOfbjs(const int &numberOfObjs) {
98  this->nObjs = numberOfObjs;
99  }
100 
106  virtual int getNumberOfCons() const { return nCons; }
107 
113  virtual void setNumberOfCons(const int &nConstraints) {
114  this->nCons = nConstraints;
115  }
116 
123  virtual void setRank(int id) { this->rank = id; }
124 
131  virtual int getRank() const { return this->rank; }
132 
138  virtual void setCrowDistance(float d) { this->crowDistance = d; }
139 
145  virtual float getCrowDistance() const { return this->crowDistance; }
146 
147  virtual void setVarAt(const int &, const V &v);
148 
149  virtual void setObjAt(const int &, const O &obj);
150 
151  virtual void setConstAt(const int &, const O &cons);
152 
153  virtual V getVarAt(const int &index) const;
154 
155  virtual O getObjAt(const int &index) const;
156 
157  virtual O getConstAt(const int &index) const;
158 
164  virtual float getFitness() const { return fitness; };
165 
171  virtual void setFitness(const float &f) { fitness = f; };
172 
180  float getConstraintCoeff() const { return constraintCoeff; }
181 
187  void setConstraintCoeff(float constraintCoeff) {
188  Solution::constraintCoeff = constraintCoeff;
189  }
190 
191  virtual json to_json() const;
192 
193  protected:
194  int nVars;
195  int nObjs;
196  int nCons;
197  float fitness;
200  vector<O> objectives;
201  vector<V> variables;
202  vector<O> constraints;
204  private:
205  int rank;
206  float
207  crowDistance;
208 };
209 
216 template <typename V, typename O>
218  : nVars(0),
219  nObjs(0),
220  nCons(0),
221  fitness(0.0f),
222  constraintCoeff(0.0f),
223  objectives({}),
224  variables({}),
225  constraints({}),
226  rank(-1),
227  crowDistance(-1.0) {}
228 
238 template <typename V, typename O>
239 Solution<V, O>::Solution(const int &nVars, const int &nObjs)
240  : nVars(nVars),
241  nObjs(nObjs),
242  nCons(0),
243  fitness(0.0f),
244  constraintCoeff(0.0f),
245  objectives{},
246  variables{},
247  constraints({}),
248  rank(-1),
249  crowDistance(-1.0) {
250  variables.resize(nVars, (V)0);
251  objectives.resize(nObjs, (O)0.0);
252 }
253 
264 template <typename V, typename O>
265 Solution<V, O>::Solution(const int &nVars, const int &nObjs, const int &nCons)
266  : nVars(nVars),
267  nObjs(nObjs),
268  nCons(nCons),
269  fitness(0.0f),
270  constraintCoeff(0.0f),
271  objectives{},
272  variables{},
273  constraints{},
274  rank(-1),
275  crowDistance(-1.0) {
276  variables.resize(nVars, (V)0);
277  objectives.resize(nObjs, (O)0);
278  constraints.resize(nCons, (O)0);
279 }
280 
288 template <typename V, typename O>
290  : nVars(copy.nVars),
291  nObjs(copy.nObjs),
292  nCons(copy.nCons),
293  fitness(copy.fitness),
294  constraintCoeff(copy.constraintCoeff),
295  objectives{},
296  variables{},
297  constraints{},
298  rank(copy.rank),
299  crowDistance(copy.crowDistance) {
300  variables.resize(nVars);
301  objectives.resize(nObjs);
302  constraints.resize(nCons);
303  for (int i = 0; i < nVars; i++) {
304  variables[i] = copy.variables[i];
305  }
306  for (int i = 0; i < nObjs; i++) {
307  objectives[i] = copy.objectives[i];
308  }
309  for (int i = 0; i < nCons; i++) {
310  constraints[i] = copy.constraints[i];
311  }
312 }
313 
322 template <typename V, typename O>
324  if (copy == nullptr) {
325  throw(runtime_error(
326  "Copy Solution is nullptr in Solution(Solution* copy)"));
327  }
328  this->nVars = copy->nVars;
329  this->nObjs = copy->nObjs;
330  this->nCons = copy->nCons;
331  this->fitness = copy->fitness;
332  this->constraintCoeff = copy->constraintCoeff;
333  this->variables.resize(this->nVars);
334  this->objectives.resize(this->nObjs);
335  this->constraints.resize(this->nCons);
336  this->rank = copy->rank;
337  this->crowDistance = copy->crowDistance;
338  for (int i = 0; i < this->nVars; i++) {
339  this->variables[i] = copy->variables[i];
340  }
341  for (int i = 0; i < this->nObjs; i++) {
342  this->objectives[i] = copy->objectives[i];
343  }
344  for (int i = 0; i < this->nCons; i++) {
345  this->constraints[i] = copy->constraints[i];
346  }
347 }
348 
349 template <typename V, typename O>
351  objectives.clear();
352  variables.clear();
353  constraints.clear();
354  objectives.shrink_to_fit();
355  variables.shrink_to_fit();
356  constraints.shrink_to_fit();
357 }
358 
367 template <typename V, typename O>
368 void Solution<V, O>::setVarAt(const int &index, const V &v) {
369  if (index < 0 || index >= this->nVars) {
370  std::string where = "setVarAt with index = " + to_string(index);
371  throw(OutOfRange(where));
372  }
373  this->variables[index] = v;
374 }
375 
384 template <typename V, typename O>
385 void Solution<V, O>::setObjAt(const int &index, const O &obj) {
386  if (index < 0 || index >= this->nObjs) {
387  std::string where = "setObjAt with index = " + to_string(index);
388  throw(OutOfRange(where));
389  }
390  this->objectives[index] = obj;
391 }
392 
401 template <typename V, typename O>
402 V Solution<V, O>::getVarAt(const int &index) const {
403  if (index < 0 || index >= this->nVars) {
404  std::string where = "getVarAt with index = " + to_string(index);
405  throw(OutOfRange(where));
406  }
407  return this->variables[index];
408 }
409 
418 template <typename V, typename O>
419 O Solution<V, O>::getObjAt(const int &index) const {
420  if (index < 0 || index >= this->nObjs) {
421  std::string where = "getObjAt with index = " + to_string(index);
422  throw(OutOfRange(where));
423  }
424  return this->objectives[index];
425 }
426 
435 template <typename V, typename O>
436 void Solution<V, O>::setConstAt(const int &index, const O &value) {
437  if (index < 0 || index >= this->nCons) {
438  std::string where = "setConstAt with index = " + to_string(index);
439  throw(OutOfRange(where));
440  }
441  this->constraints[index] = value;
442 }
443 
452 template <typename V, typename O>
453 O Solution<V, O>::getConstAt(const int &index) const {
454  if (index < 0 || index >= this->nCons) {
455  std::string where = "getConstAt with index = " + to_string(index);
456  throw(OutOfRange(where));
457  }
458  return this->constraints[index];
459 }
460 
468 template <typename V, typename O>
469 void Solution<V, O>::setObjectives(const vector<O> &objs) {
470  if (objs.size() != (unsigned int)this->nObjs) {
471  std::string where = "objs.size() != nObjs in Solution::setObjectives";
472  throw(OutOfRange(where));
473  }
474  this->objectives = objs;
475 }
476 
484 template <typename V, typename O>
485 void Solution<V, O>::setVariables(const vector<V> &vars) {
486  if (vars.size() != (unsigned int)this->nVars) {
487  auto vsize = vars.size();
488  std::string where = "vars.size() = " + to_string(vsize) +
489  " != nVars = " + to_string(this->nVars) +
490  " in Solution::setVariables";
491  throw(OutOfRange(where));
492  }
493  this->variables = vars;
494 }
495 
503 template <typename V, typename O>
504 void Solution<V, O>::setConstraints(const vector<O> &constr) {
505  if (constr.size() != (unsigned int)this->nCons) {
506  std::string where =
507  "constr.size() != nConst in Solution::setConstraints";
508  throw(OutOfRange(where));
509  }
510  this->constraints = constr;
511 }
512 
521 template <typename V, typename O>
523  if (this == &copy) {
524  return *this;
525  }
526  nVars = copy.nVars;
527  nObjs = copy.nObjs;
528  nCons = copy.nCons;
529  fitness = copy.fitness;
530  constraintCoeff = copy.constraintCoeff;
531  variables = copy.variables;
532  objectives = copy.objectives;
533  constraints = copy.constraints;
534  rank = copy.rank;
535  crowDistance = copy.crowDistance;
536  return *this;
537 }
538 
548 template <typename V, typename O>
549 bool Solution<V, O>::operator==(const Solution &other) const {
550  if (other.fitness != this->fitness) {
551  return false;
552  }
553  if ((this->nVars != other.nVars) || (this->nObjs != other.nObjs) ||
554  (this->nCons != other.nCons)) {
555  return false;
556  }
557  for (int i = 0; i < other.nVars; i++) {
558  if (other.variables[i] != this->variables[i]) {
559  return false;
560  }
561  }
562  for (int i = 0; i < other.nObjs; i++) {
563  if (other.objectives[i] != this->objectives[i]) {
564  return false;
565  }
566  }
567  for (int i = 0; i < other.nCons; i++) {
568  if (other.constraints[i] != this->constraints[i]) {
569  return false;
570  }
571  }
572  if (other.constraintCoeff != this->constraintCoeff) {
573  return false;
574  }
575  if (other.rank != this->rank) {
576  return false;
577  }
578  if (other.crowDistance != this->crowDistance) {
579  return false;
580  }
581  return true;
582 }
583 
590 template <typename V, typename O>
592  json data;
593  data["num_vars"] = this->nVars;
594  data["num_objs"] = this->nObjs;
595  data["num_cons"] = this->nCons;
596  data["fitness"] = this->fitness;
597  data["const_coeff"] = this->constraintCoeff;
598  data["vars"] = this->variables;
599  data["objs"] = this->objectives;
600  data["cons"] = this->constraints;
601  data["rank"] = this->rank;
602  data["crow_distance"] = this->crowDistance;
603  return data;
604 }
605 
606 #endif // DIGNEA_SOLUTION_H
nlohmann::json json
Definition: MinKnap.h:85
Class to represent a solution to the optimization problems in the tool.
Definition: Solution.h:25
virtual float getFitness() const
Get the fitness of the solution.
Definition: Solution.h:164
int nVars
Definition: Solution.h:194
virtual const vector< V > & getVariables() const
Get the variables of the solution.
Definition: Solution.h:57
virtual O getConstAt(const int &index) const
Returns the ith constraint value if exists.
Definition: Solution.h:453
vector< V > variables
Definition: Solution.h:201
int nCons
Definition: Solution.h:196
int nObjs
Definition: Solution.h:195
virtual void setObjectives(const vector< O > &objs)
Updates the objective values of the solution.
Definition: Solution.h:469
virtual void setCrowDistance(float d)
Set the Crow Distance object.
Definition: Solution.h:138
virtual int getNumberOfCons() const
Get the number of constraint in the solution.
Definition: Solution.h:106
void setConstraintCoeff(float constraintCoeff)
Set the constraint coefficient value.
Definition: Solution.h:187
virtual void setFitness(const float &f)
Set the fitness of the solution.
Definition: Solution.h:171
Solution & operator=(const Solution &copy)
Copies a solution using the assignment operator.
Definition: Solution.h:522
virtual json to_json() const
Creates and returns a JSON representation of the solution.
Definition: Solution.h:591
virtual void setNumberOfCons(const int &nConstraints)
Set the number of constraints of the solution.
Definition: Solution.h:113
virtual void setVarAt(const int &, const V &v)
Updates the ith variable of the solution with the value v.
Definition: Solution.h:368
virtual O getObjAt(const int &index) const
Returns the ith objective value if exists.
Definition: Solution.h:419
virtual void setNumberOfVars(const int &numberOfVars)
Set the number of variables (dimension) of the solution.
Definition: Solution.h:81
virtual void setObjAt(const int &, const O &obj)
Updates the ith objective with the value obj.
Definition: Solution.h:385
virtual float getCrowDistance() const
Get the Crow Distance object.
Definition: Solution.h:145
float fitness
Definition: Solution.h:197
virtual const vector< O > & getConstraints() const
Get the constraint values of the solution.
Definition: Solution.h:66
virtual void setConstAt(const int &, const O &cons)
Updates the ith constraint value with the value.
Definition: Solution.h:436
vector< O > objectives
Definition: Solution.h:200
vector< O > constraints
Definition: Solution.h:202
float constraintCoeff
Definition: Solution.h:198
virtual int getRank() const
Get the identifier of the solution in the population. Only used in the Non-Dominated Sorting operator...
Definition: Solution.h:131
bool operator==(const Solution &other) const
Compares whether two solutions are equal.
Definition: Solution.h:549
virtual void setRank(int id)
Set the identifier of the solution in the population. Only used in the Non-Dominated Sorting operator...
Definition: Solution.h:123
virtual void setConstraints(const vector< O > &constr)
Updates the constraints of the solution.
Definition: Solution.h:504
float getConstraintCoeff() const
Get the constraint coefficient of the solutions. This value means different for every problem....
Definition: Solution.h:180
Solution()
Creates a Solution with all parameters set to zero.
Definition: Solution.h:217
virtual int getNumberOfObjs() const
Get the number of objectives of the solution.
Definition: Solution.h:90
virtual void setNumberOfbjs(const int &numberOfObjs)
Set the number of objectives of the solution.
Definition: Solution.h:97
virtual const vector< O > & getObjectives() const
Returns the objective values of the solution.
Definition: Solution.h:48
virtual int getNumberOfVars() const
Get the number of variables (dimension) of the solution.
Definition: Solution.h:74
virtual V getVarAt(const int &index) const
Returns the ith variable if its in the valid range.
Definition: Solution.h:402
virtual void setVariables(const vector< V > &vars)
Updates the variable values (dimension) of the solution.
Definition: Solution.h:485