12 #ifndef __COMPARATOR_H__
13 #define __COMPARATOR_H__
15 #include <dignea/core/Problem.h>
16 #include <dignea/core/Solution.h>
17 #include <dignea/utilities/exceptions/OutOfRange.h>
20 #include <type_traits>
22 static const int FIRST_BEST = -1;
23 static const int SECOND_BEST = 1;
24 static const int TIE = 0;
26 static const int FIRST_DOMINATES = -1;
27 static const int SECOND_DOMINATES = 1;
28 static const int NON_DOMINANCES_EQUALS = 2;
29 static const int NON_DOMINANCES_NOT_EQUALS = 3;
40 double fitness1 = ind1.getFitness();
41 double fitness2 = ind2.getFitness();
42 if (fitness1 > fitness2) {
45 if (fitness1 < fitness2) {
63 int cmpByObj(
const S &ind1,
const S &ind2,
const int &nObj,
65 if (ind1.getNumberOfObjs() < nObj || ind2.getNumberOfObjs() < nObj) {
67 "Comparator::cmpByObj() with obj = " + to_string(nObj);
68 throw(OutOfRange(where));
70 double obj1 = ind1.getObjAt(nObj);
71 double obj2 = ind2.getObjAt(nObj);
104 template <
typename S>
106 if (ind1.getNumberOfCons() != ind2.getNumberOfCons()) {
107 std::string where =
"Comparator::improves() different constraints";
108 throw(OutOfRange(where));
110 vector ind1Const = ind1.getConstraints();
111 vector ind2Const = ind2.getConstraints();
113 std::accumulate(ind1Const.begin(), ind1Const.end(), 0.0f);
115 std::accumulate(ind2Const.begin(), ind2Const.end(), 0.0f);
117 if (ind1Penalty < ind2Penalty) {
119 }
else if (ind2Penalty < ind1Penalty) {
139 template <
typename S>
141 if (ind1.getNumberOfObjs() < 2 || ind2.getNumberOfObjs() < 2) {
143 "Comparator::dominanceTest() with less than two objectices";
144 throw(std::runtime_error(where));
146 if (ind1.getNumberOfObjs() != ind2.getNumberOfObjs()) {
148 "Comparator::dominanceTest() with different number of objectives "
150 throw(std::runtime_error(where));
155 for (
int i = 0; i < ind1.getNumberOfObjs(); i++) {
157 if (ind1.getObjAt(i) > ind2.getObjAt(i))
159 else if (ind1.getObjAt(i) < ind2.getObjAt(i))
164 if (ind1.getObjAt(i) < ind2.getObjAt(i))
166 else if (ind1.getObjAt(i) > ind2.getObjAt(i))
172 if (flag == -1) dominate1 = 1;
173 if (flag == 1) dominate2 = 1;
176 if ((dominate1 == 0) && (dominate2 == 0)) {
177 return NON_DOMINANCES_EQUALS;
179 if ((dominate1 == 1) && (dominate2 == 1)) {
180 return NON_DOMINANCES_NOT_EQUALS;
182 if (dominate1 == 1) {
183 return FIRST_DOMINATES;
185 return SECOND_DOMINATES;
197 template <
typename T>
198 bool areEqual(
const vector<T> &v1,
const vector<T> &v2) {
199 const T tolerance = 0.01;
200 if (v1.size() != v2.size()) {
204 for (std::size_t i = 0; i < v1.size(); ++i) {
205 if (std::abs(v1[i] - v2[i]) > tolerance) {
223 template <
typename T>
225 const T tolerance = 0.0001f;
227 return (std::abs(a - b) < tolerance * std::max(std::abs(a), std::abs(b)));
int cmpByFitness(const S &ind1, const S &ind2)
Compares two individuals by their fitness.
Definition: Comparator.h:39
int improves(const S &ind1, const S &ind2)
Checks whether a solution is improved by a new one. A solution (with constraints) is said to improve ...
Definition: Comparator.h:105
bool areEqual(const vector< T > &v1, const vector< T > &v2)
Compare whether two vectors are equal within a tolerance.
Definition: Comparator.h:198
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
int cmpByObj(const S &ind1, const S &ind2, const int &nObj, const Problem< S > *problem)
Compares two individuals by the objective nObj. The problem to solve must be given to check whether t...
Definition: Comparator.h:63
Class to represent a Problem in the tool. It includes the basic information for a problem a few metho...
Definition: Problem.h:29
virtual int getOptimizationDirection(const int i) const =0
Returns the optimization direction for each objective in the problem. It returns Minimize or Maximize...