PGROUTING  3.2
pgrouting::tsp::EuclideanDmatrix Class Reference

#include "euclideanDmatrix.h"

Collaboration diagram for pgrouting::tsp::EuclideanDmatrix:

Public Member Functions

 EuclideanDmatrix ()=default
 
 EuclideanDmatrix (const std::vector< Coordinate_t > &data_coordinates)
 
double comparable_distance (size_t i, size_t j) const
 
double distance (size_t i, size_t j) const
 
int64_t get_id (size_t idx) const
 idx -> original id More...
 
size_t get_index (int64_t id) const
 original id -> idx More...
 
const std::vector< double > get_row (size_t idx) const
 returns a row of distances More...
 
bool has_id (int64_t id) const
 original id -> true More...
 
bool has_no_infinity () const
 
bool is_symmetric () const
 
bool obeys_triangle_inequality () const
 
void set (size_t i, size_t j, double dist)
 sets a special value for the distance(i,j) More...
 
size_t size () const
 |idx| More...
 
double tourCost (const Tour &tour) const
 tour evaluation More...
 

Protected Member Functions

void set_ids ()
 

Protected Attributes

std::vector< int64_t > ids
 

Private Attributes

size_t column
 
std::vector< Coordinate_tcoordinates
 
size_t row
 
double special_distance
 

Friends

std::ostream & operator<< (std::ostream &log, const EuclideanDmatrix &matrix)
 

Detailed Description

Definition at line 40 of file euclideanDmatrix.h.

Constructor & Destructor Documentation

◆ EuclideanDmatrix() [1/2]

pgrouting::tsp::EuclideanDmatrix::EuclideanDmatrix ( )
default

◆ EuclideanDmatrix() [2/2]

pgrouting::tsp::EuclideanDmatrix::EuclideanDmatrix ( const std::vector< Coordinate_t > &  data_coordinates)
explicit

Definition at line 119 of file euclideanDmatrix.cpp.

121  : coordinates(data_coordinates) {
122  set_ids();
123  std::sort(coordinates.begin(), coordinates.end(),
124  [](const Coordinate_t &lhs, const Coordinate_t &rhs)
125  {return lhs.id < rhs.id;});
126  }

References coordinates, and set_ids().

Member Function Documentation

◆ comparable_distance()

double pgrouting::tsp::EuclideanDmatrix::comparable_distance ( size_t  i,
size_t  j 
) const

Definition at line 55 of file euclideanDmatrix.cpp.

55  {
56  if (special_distance >= 0 &&
57  ((row == i && column == j)
58  ||(row == j && column == i))) {
60  }
61  auto dx = coordinates[i].x - coordinates[j].x;
62  auto dy = coordinates[i].y - coordinates[j].y;
63  return dx * dx + dy * dy;
64 }

References column, coordinates, row, and special_distance.

Referenced by distance().

◆ distance()

double pgrouting::tsp::EuclideanDmatrix::distance ( size_t  i,
size_t  j 
) const

Definition at line 67 of file euclideanDmatrix.cpp.

67  {
68  if (special_distance >= 0 &&
69  ((row == i && column == j)
70  ||(row == j && column == i))) {
71  return special_distance;
72  }
73  if (i == j) return 0;
74  return std::sqrt(comparable_distance(i, j));
75 }

References column, comparable_distance(), row, and special_distance.

Referenced by do_pgr_euclideanTSP(), get_row(), and tourCost().

◆ get_id()

int64_t pgrouting::tsp::EuclideanDmatrix::get_id ( size_t  idx) const

idx -> original id

Parameters
[in]idx- index (i-th coordinate)
Returns
the original id corresponding to idx

Definition at line 112 of file euclideanDmatrix.cpp.

112  {
113  return ids[id];
114 }

References ids.

Referenced by do_pgr_euclideanTSP().

◆ get_index()

size_t pgrouting::tsp::EuclideanDmatrix::get_index ( int64_t  id) const

original id -> idx

Parameters
[in]id- original id
Returns
idx index of the id in the distance table

Definition at line 104 of file euclideanDmatrix.cpp.

104  {
105  for (size_t pos = 0; pos < ids.size(); ++pos) {
106  if (ids[pos] == id) return pos;
107  }
108  return ids.size() + 1;
109 }

References ids.

Referenced by do_pgr_euclideanTSP().

◆ get_row()

const std::vector< double > pgrouting::tsp::EuclideanDmatrix::get_row ( size_t  idx) const

returns a row of distances

Parameters
[in]idxrow index
Returns
distances from idx to all other coordinates

Definition at line 42 of file euclideanDmatrix.cpp.

42  {
43  std::vector<double> result;
44 
45  for (size_t j = 0; j < ids.size(); ++j) {
46  result.push_back(distance(i, j));
47  }
48 
49  pgassert(result.size() == ids.size());
50  return result;
51 }

References distance(), ids, and pgassert.

◆ has_id()

bool pgrouting::tsp::EuclideanDmatrix::has_id ( int64_t  id) const

original id -> true

Parameters
[in]idoriginal id
Returns
true if id is in the distance table

Definition at line 96 of file euclideanDmatrix.cpp.

96  {
97  for (const auto &i : ids) {
98  if (i == id) return true;
99  }
100  return false;
101 }

References ids.

Referenced by do_pgr_euclideanTSP().

◆ has_no_infinity()

bool pgrouting::tsp::EuclideanDmatrix::has_no_infinity ( ) const

Definition at line 143 of file euclideanDmatrix.cpp.

143  {
144  return true;
145 }

◆ is_symmetric()

bool pgrouting::tsp::EuclideanDmatrix::is_symmetric ( ) const

Definition at line 153 of file euclideanDmatrix.cpp.

153  {
154  return true;
155 }

◆ obeys_triangle_inequality()

bool pgrouting::tsp::EuclideanDmatrix::obeys_triangle_inequality ( ) const

Definition at line 148 of file euclideanDmatrix.cpp.

148  {
149  return true;
150 }

◆ set()

void pgrouting::tsp::EuclideanDmatrix::set ( size_t  i,
size_t  j,
double  dist 
)
inline

sets a special value for the distance(i,j)

Parameters
[in]i- index in matrix
[in]j- index in matrix
[in]dist- distance from i to j & from j to i

Definition at line 57 of file euclideanDmatrix.h.

57  {
58  row = i; column = j; special_distance = dist;}

References column, row, and special_distance.

Referenced by do_pgr_euclideanTSP().

◆ set_ids()

void pgrouting::tsp::EuclideanDmatrix::set_ids ( )
protected

Definition at line 129 of file euclideanDmatrix.cpp.

129  {
130  ids.reserve(coordinates.size());
131  for (const auto &data : coordinates) {
132  ids.push_back(data.id);
133  }
134  std::sort(ids.begin(), ids.end());
135 #ifndef NDEBUG
136  auto total = ids.size();
137 #endif
138  ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
139  pgassertwm(total == ids.size(), "Duplicated id found");
140 }

References coordinates, ids, and pgassertwm.

Referenced by EuclideanDmatrix().

◆ size()

size_t pgrouting::tsp::EuclideanDmatrix::size ( ) const
inline

|idx|

Returns
the total number of coordinates

Definition at line 86 of file euclideanDmatrix.h.

86 {return ids.size();}

References ids.

Referenced by do_pgr_euclideanTSP().

◆ tourCost()

double pgrouting::tsp::EuclideanDmatrix::tourCost ( const Tour tour) const

tour evaluation

Parameters
[in]tour
Returns
total cost of traversing the tour

Definition at line 78 of file euclideanDmatrix.cpp.

78  {
79  double total_cost(0);
80  if (tour.cities.empty()) return total_cost;
81 
82  auto prev_id = tour.cities.front();
83  for (const auto &id : tour.cities) {
84  if (id == tour.cities.front()) continue;
85 
86  total_cost += distance(prev_id, id);
87  prev_id = id;
88  }
89  total_cost += distance(prev_id, tour.cities.front());
90  return total_cost;
91 }

References pgrouting::tsp::Tour::cities, and distance().

Referenced by do_pgr_euclideanTSP().

Friends And Related Function Documentation

◆ operator<<

std::ostream& operator<< ( std::ostream &  log,
const EuclideanDmatrix matrix 
)
friend

Definition at line 157 of file euclideanDmatrix.cpp.

157  {
158  for (const auto id : matrix.ids) {
159  log << "\t" << id;
160  }
161  log << "\n";
162  for (const auto row : matrix.coordinates) {
163  log << row.id << "(" << row.x << "," << row.y << ")\n";
164  }
165  return log;
166 }

Member Data Documentation

◆ column

size_t pgrouting::tsp::EuclideanDmatrix::column
private

Definition at line 117 of file euclideanDmatrix.h.

Referenced by comparable_distance(), distance(), and set().

◆ coordinates

std::vector< Coordinate_t > pgrouting::tsp::EuclideanDmatrix::coordinates
private

◆ ids

std::vector<int64_t> pgrouting::tsp::EuclideanDmatrix::ids
protected

◆ row

size_t pgrouting::tsp::EuclideanDmatrix::row
private

Definition at line 116 of file euclideanDmatrix.h.

Referenced by comparable_distance(), distance(), and set().

◆ special_distance

double pgrouting::tsp::EuclideanDmatrix::special_distance
private

Definition at line 118 of file euclideanDmatrix.h.

Referenced by comparable_distance(), distance(), and set().


The documentation for this class was generated from the following files:
pgrouting::tsp::EuclideanDmatrix::ids
std::vector< int64_t > ids
Definition: euclideanDmatrix.h:112
pgrouting::tsp::EuclideanDmatrix::comparable_distance
double comparable_distance(size_t i, size_t j) const
Definition: euclideanDmatrix.cpp:55
pgrouting::tsp::EuclideanDmatrix::column
size_t column
Definition: euclideanDmatrix.h:117
pgrouting::tsp::EuclideanDmatrix::distance
double distance(size_t i, size_t j) const
Definition: euclideanDmatrix.cpp:67
pgassertwm
#define pgassertwm(expr, msg)
Adds a message to the assertion.
Definition: pgr_assert.h:117
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
Coordinate_t
Definition: coordinate_t.h:37
pgrouting::tsp::EuclideanDmatrix::coordinates
std::vector< Coordinate_t > coordinates
Definition: euclideanDmatrix.h:115
pgrouting::tsp::EuclideanDmatrix::special_distance
double special_distance
Definition: euclideanDmatrix.h:118
pgrouting::tsp::EuclideanDmatrix::row
size_t row
Definition: euclideanDmatrix.h:116
pgrouting::tsp::EuclideanDmatrix::set_ids
void set_ids()
Definition: euclideanDmatrix.cpp:129