dignea  1.0.0
Diverse Instance Generator with Novelty Search and Evolutionary Algorithms
Euclidean.h
1 
2 #ifndef __EUCLIDEAN_H__
3 #define __EUCLIDEAN_H__
4 
5 #include <dignea/utilities/exceptions/Mismatch.h>
6 
7 #include <algorithm>
8 #include <cmath>
9 
10 #include "Distance.h"
11 
17 template <typename T>
18 class Euclidean : public Distance<T> {
19  public:
20  Euclidean() = default;
21 
22  virtual ~Euclidean() = default;
23 
24  T compute(const vector<T>&, const vector<T>&);
25 
26  private:
27  template <typename I1, typename I2>
28  T euclidean(I1 first1, I1 last1, I2 first2);
29 };
30 
39 template <typename T>
40 T Euclidean<T>::compute(const vector<T>& ind1, const vector<T>& ind2) {
41  if (ind1.size() != ind2.size()) {
42  string where = "individuals sizes in Euclidean::compute";
43  throw(Mismatch(where));
44  }
45  return euclidean(begin(ind1), end(ind1), begin(ind2));
46 }
47 
48 template <typename T>
49 template <typename I1, typename I2>
50 T Euclidean<T>::euclidean(I1 first, I1 last, I2 first2) {
51  T distance = {};
52  while (first != last) {
53  double dist = (*first++) - (*first2++);
54  distance += dist * dist;
55  }
56  return distance > 0.0 ? sqrt(distance) : 0.0;
57 }
58 
59 #endif //__EUCLIDEAN_H__
Abstract Distance Class to create an interface which can be use in different algorithms.
Definition: Distance.h:20
Euclidean distance.
Definition: Euclidean.h:18
T compute(const vector< T > &, const vector< T > &)
Computes the Euclidean distance between the two vectors.
Definition: Euclidean.h:40