dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Front.h
1 //
2 // Created by amarrero on 24/3/21.
3 //
4 
5 #ifndef DIGNEA_FRONT_H
6 #define DIGNEA_FRONT_H
7 
8 #include <dignea/core/Problem.h>
10 
11 #include <iostream>
12 #include <memory>
13 #include <nlohmann/json.hpp>
14 #include <string>
15 #include <vector>
16 
17 using namespace std;
18 using json = nlohmann::json;
19 
25 template <class S>
26 class Front {
27  public:
28  Front(const int &nSolutions);
29 
30  Front(const vector<S> &solutions);
31 
32  Front(const Front<S> &copy);
33 
34  Front(const Front<S> *copy);
35 
36  virtual ~Front();
37 
38  void setSolutions(const vector<S> &solutions);
39 
40  vector<S> getSolutions() const;
41 
42  void addSolution(const S &solution);
43 
44  void addSolution(const S &solution, const Problem<S> *problem);
45 
51  int getNumOfSolutions() const { return numOfSolutions; }
58  bool empty() const { return solutions.empty(); }
59 
60  json to_json() const;
61 
62  private:
63  int numOfSolutions;
64  vector<S> solutions;
65 };
66 
73 template <class S>
74 Front<S>::Front(const int &nSolutions) : numOfSolutions(nSolutions) {}
75 
82 template <class S>
83 Front<S>::Front(const vector<S> &sols)
84  : numOfSolutions(sols.size()), solutions{} {
85  copy(sols.begin(), sols.end(), back_inserter(this->solutions));
86 }
87 
94 template <class S>
96  : numOfSolutions(copy.numOfSolutions), solutions(copy.solutions) {}
97 
104 template <class S>
106  : numOfSolutions(copy->numOfSolutions), solutions(copy->solutions) {}
107 
108 template <class S>
110  solutions.clear();
111 }
112 
119 template <class S>
120 void Front<S>::setSolutions(const vector<S> &solutions) {
121  this->solutions.clear();
122  this->solutions = solutions;
123  numOfSolutions = solutions.size();
124 }
125 
132 template <class S>
133 vector<S> Front<S>::getSolutions() const {
134  return this->solutions;
135 }
136 
143 template <class S>
144 void Front<S>::addSolution(const S &solution) {
145  if (solution.getNumberOfObjs() != 1) {
146  string where =
147  "Using addSolution for single-objective solutions. Please "
148  "consider using addSolution(const S&, const Problem<S>*) instead";
149  throw runtime_error(where);
150  }
151  solutions.push_back(solution);
152  numOfSolutions++;
153 }
154 
161 template <class S>
162 void Front<S>::addSolution(const S &solution, const Problem<S> *problem) {
163  if (solution.getNumberOfObjs() == 1) {
164  this->addSolution(solution);
165  std::cerr << "Using addSolution for multi-objective solutions. Please "
166  "consider using addSolution(const S& solution) instead"
167  << std::endl;
168  } else {
169  unsigned int idx = 0;
170  while (idx < solutions.size()) {
171  auto dominates = dominanceTest(solution, solutions[idx], problem);
172  if ((dominates == SECOND_DOMINATES) ||
173  (dominates == NON_DOMINANCES_EQUALS)) {
174  return;
175  } else if (dominates == FIRST_DOMINATES) {
176  // Removes all dominated solutions
177  solutions[idx] = solutions[solutions.size() - 1];
178  solutions.pop_back();
179  this->numOfSolutions--;
180  }
181  idx++;
182  }
183  solutions.push_back(solution);
184  numOfSolutions++;
185  }
186 }
187 
193 template <class S>
194 json Front<S>::to_json() const {
195  json data;
196  data["n_solutions"] = numOfSolutions;
197  int i = 0;
198  for (auto solution : this->solutions) {
199  data[to_string(i)] = solution.to_json();
200  i++;
201  }
202  return data;
203 }
204 
205 #endif // DIGNEA_FRONT_H
int dominanceTest(const S &ind1, const S &ind2, const Problem< S > *problem)
Test whether an individual dominates another. A individual dominates another if all their objectives ...
Definition: Comparator.h:140
nlohmann::json json
Definition: MinKnap.h:85
Front class which stores the final results of an EA execution.
Definition: Front.h:26
Front(const int &nSolutions)
Construct a new Front object.
Definition: Front.h:74
void setSolutions(const vector< S > &solutions)
Resets the front with the solutions in the vector solutions.
Definition: Front.h:120
bool empty() const
Checks whether the front is empty or not.
Definition: Front.h:58
int getNumOfSolutions() const
Gets the number of solutions in the front.
Definition: Front.h:51
void addSolution(const S &solution)
Includes a new solution in the front. This is only for single-objective solutions.
Definition: Front.h:144
json to_json() const
Creates a JSON object with the information of the Front.
Definition: Front.h:194
vector< S > getSolutions() const
Returns the solutions in the front.
Definition: Front.h:133
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29