PGROUTING  3.2
pgrouting::tsp::Dmatrix Class Reference

#include "Dmatrix.h"

Collaboration diagram for pgrouting::tsp::Dmatrix:

Public Member Functions

 Dmatrix ()=default
 
 Dmatrix (const std::map< std::pair< double, double >, int64_t > &euclidean_data)
 
 Dmatrix (const std::vector< Matrix_cell_t > &data_costs)
 
double comparable_distance (size_t i, size_t j) const
 
double distance (int64_t i, int64_t j) const
 
double distance (size_t i, size_t j) const
 
bool empty () 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
 Triangle Inequality Theorem. More...
 
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 (const std::vector< matrix_cell > &data_costs)
 

Protected Attributes

std::vector< int64_t > ids
 

Private Types

typedef std::vector< std::vector< double > > Costs
 

Private Member Functions

std::vector< double > & operator[] (size_t i)
 
const std::vector< double > & operator[] (size_t i) const
 

Private Attributes

Costs costs
 

Friends

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

Detailed Description

Definition at line 43 of file Dmatrix.h.

Member Typedef Documentation

◆ Costs

typedef std::vector< std::vector < double > > pgrouting::tsp::Dmatrix::Costs
private

Definition at line 127 of file Dmatrix.h.

Constructor & Destructor Documentation

◆ Dmatrix() [1/3]

pgrouting::tsp::Dmatrix::Dmatrix ( )
default

◆ Dmatrix() [2/3]

pgrouting::tsp::Dmatrix::Dmatrix ( const std::vector< Matrix_cell_t > &  data_costs)
explicit

Definition at line 108 of file Dmatrix.cpp.

108  {
109  set_ids(data_costs);
110  costs.resize(
111  ids.size(),
112  std::vector<double>(
113  ids.size(),
114  (std::numeric_limits<double>::max)()));
115 
116  for (const auto &data : data_costs) {
117  costs[get_index(data.from_vid)][get_index(data.to_vid)] = data.cost;
118  }
119 
120  for (size_t i = 0; i < costs.size(); ++i) {
121  costs[i][i] = 0;
122  }
123 }

References costs, get_index(), ids, and set_ids().

◆ Dmatrix() [3/3]

pgrouting::tsp::Dmatrix::Dmatrix ( const std::map< std::pair< double, double >, int64_t > &  euclidean_data)
explicit

Definition at line 136 of file Dmatrix.cpp.

136  {
137  ids.reserve(euclidean_data.size());
138  for (const auto &e: euclidean_data) {
139  ids.push_back(e.second);
140  }
141  costs.resize(
142  ids.size(),
143  std::vector<double>(
144  ids.size(),
145  (std::numeric_limits<double>::max)()));
146 
147  for (const auto &from : euclidean_data) {
148  for (const auto &to : euclidean_data) {
149  auto from_id = get_index(from.second);
150  auto to_id = get_index(to.second);
151  costs[from_id][to_id] = get_distance(from.first, to.first);
152  costs[to_id][from_id] = costs[from_id][to_id];
153  }
154  }
155 
156  for (size_t i = 0; i < costs.size(); ++i) {
157  costs[i][i] = 0;
158  }
159 }

References costs, pgrouting::tsp::get_distance(), get_index(), and ids.

Member Function Documentation

◆ comparable_distance()

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

Definition at line 105 of file Dmatrix.h.

105  {
106  return distance(i, j);}

References distance().

◆ distance() [1/2]

double pgrouting::tsp::Dmatrix::distance ( int64_t  i,
int64_t  j 
) const
inline

Definition at line 108 of file Dmatrix.h.

108  {
109  return distance(get_index(i), get_index(j));}

References get_index().

Referenced by comparable_distance(), pgrouting::vrp::Dnode::distance(), do_pgr_tsp(), and tourCost().

◆ distance() [2/2]

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

Definition at line 111 of file Dmatrix.h.

111  {
112  return costs[i][j];}

References costs.

◆ empty()

bool pgrouting::tsp::Dmatrix::empty ( ) const
inline

Definition at line 118 of file Dmatrix.h.

118  {
119  return ids.empty();
120  }

References ids.

Referenced by pgrouting::vrp::Fleet::build_fleet(), and pgrouting::vrp::Pgr_pickDeliver::Pgr_pickDeliver().

◆ get_id()

int64_t pgrouting::tsp::Dmatrix::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 101 of file Dmatrix.cpp.

101  {
102  return ids[id];
103 }

References ids.

Referenced by do_pgr_tsp().

◆ get_index()

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

original id -> idx

given a users id returns the internal index

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

in[] id returns index

Definition at line 93 of file Dmatrix.cpp.

93  {
94  for (size_t pos = 0; pos < ids.size(); ++pos) {
95  if (ids[pos] == id) return pos;
96  }
97  throw std::make_pair(std::string("(INTERNAL) Dmatrix: Unable to find node on matrix"), id);
98 }

References ids.

Referenced by pgrouting::vrp::Dnode::distance(), distance(), Dmatrix(), do_pgr_tsp(), and pgrouting::tsp::operator<<().

◆ get_row()

const std::vector<double>& pgrouting::tsp::Dmatrix::get_row ( size_t  idx) const
inline

returns a row of distances

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

Definition at line 102 of file Dmatrix.h.

102  {
103  return costs[idx];}

References costs.

◆ has_id()

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

original id -> true

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

Definition at line 79 of file Dmatrix.cpp.

79  {
80  for (const auto &i : ids) {
81  if (i == id) return true;
82  }
83  return false;
84 }

References ids.

Referenced by pgrouting::vrp::Fleet::build_fleet(), pgrouting::vrp::PD_Orders::build_orders(), and do_pgr_tsp().

◆ has_no_infinity()

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

Definition at line 162 of file Dmatrix.cpp.

162  {
163  for (const auto &row : costs) {
164  for (const auto &val : row) {
165  if (val == (std::numeric_limits<double>::infinity)()) return false;
166  if (val == (std::numeric_limits<double>::max)()) return false;
167  }
168  }
169  return true;
170 }

References costs.

Referenced by do_pgr_pickDeliver(), and do_pgr_tsp().

◆ is_symmetric()

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

Definition at line 193 of file Dmatrix.cpp.

193  {
194  for (size_t i = 0; i < costs.size(); ++i) {
195  for (size_t j = 0; j < costs.size(); ++j) {
196  if (0.000001 < std::fabs(costs[i][j] - costs[j][i])) {
197  std::ostringstream log;
198  log << "i \t" << i
199  << "j \t" << j
200  << "costs[i][j] \t" << costs[i][j]
201  << "costs[j][i] \t" << costs[j][i]
202  << "\n";
203  log << (*this);
204  pgassertwm(false, log.str());
205  return false;
206  }
207  }
208  }
209  return true;
210 }

References costs, and pgassertwm.

Referenced by do_pgr_tsp().

◆ obeys_triangle_inequality()

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

Triangle Inequality Theorem.

The sum of the lengths of any two sides of a triangle is greater than the length of the third side. NOTE: can also be equal for streets costs[i][k] != inf costs[i][k] <= costs[i][j] + costs[j][k]

Definition at line 181 of file Dmatrix.cpp.

181  {
182  for (size_t i = 0; i < costs.size(); ++i) {
183  for (size_t j = 0; j < costs.size(); ++j) {
184  for (size_t k = 0; k < costs.size(); ++k) {
185  if (!(costs[i][k] <= (costs[i][j] + costs[j][k]))) return false;
186  }
187  }
188  }
189  return true;
190 }

References costs.

◆ operator[]() [1/2]

std::vector< double >& pgrouting::tsp::Dmatrix::operator[] ( size_t  i)
inlineprivate

Definition at line 129 of file Dmatrix.h.

129 {return costs[i];}

References costs.

◆ operator[]() [2/2]

const std::vector< double >& pgrouting::tsp::Dmatrix::operator[] ( size_t  i) const
inlineprivate

Definition at line 130 of file Dmatrix.h.

130 {return costs[i];}

References costs.

◆ set()

void pgrouting::tsp::Dmatrix::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 60 of file Dmatrix.h.

60  {
61  costs[i][j] = costs[j][i] = dist;}

References costs.

Referenced by do_pgr_tsp().

◆ set_ids()

void pgrouting::tsp::Dmatrix::set_ids ( const std::vector< matrix_cell > &  data_costs)
protected

Definition at line 64 of file Dmatrix.cpp.

64  {
65  ids.reserve(data_costs.size() * 2);
66  for (const auto &cost : data_costs) {
67  ids.push_back(cost.from_vid);
68  ids.push_back(cost.to_vid);
69  }
70  std::sort(ids.begin(), ids.end());
71  ids.erase(std::unique(ids.begin(), ids.end()), ids.end());
72  /*
73  * freeing up unused space
74  */
75  ids.shrink_to_fit();
76 }

References ids.

Referenced by Dmatrix().

◆ size()

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

|idx|

Returns
the total number of coordinates

Definition at line 88 of file Dmatrix.h.

88 {return ids.size();}

References ids.

Referenced by do_pgr_tsp().

◆ tourCost()

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

tour evaluation

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

Definition at line 44 of file Dmatrix.cpp.

44  {
45  double total_cost(0);
46  if (tour.cities.empty()) return total_cost;
47 
48  auto prev_id = tour.cities.front();
49  for (const auto &id : tour.cities) {
50  if (id == tour.cities.front()) continue;
51 
52  pgassert(distance(prev_id, id) != (std::numeric_limits<double>::max)());
53 
54  total_cost += costs[prev_id][id];
55  prev_id = id;
56  }
57  total_cost += costs[prev_id][tour.cities.front()];
58  return total_cost;
59 }

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

Referenced by do_pgr_tsp().

Friends And Related Function Documentation

◆ operator<<

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

Definition at line 216 of file Dmatrix.cpp.

216  {
217  for (const auto id : matrix.ids) {
218  log << "\t" << id;
219  }
220  log << "\n";
221  size_t i = 0;
222  for (const auto &row : matrix.costs) {
223  size_t j = 0;
224  for (const auto cost : row) {
225  log << "Internal(" << i << "," << j << ")"
226  << "\tUsers(" << matrix.ids[i] << "," << matrix.ids[j] << ")"
227  << "\t = " << cost
228 #if 0
229  << "\t(" << matrix.get_index(matrix.ids[i])
230  << "," << matrix.get_index(matrix.ids[j]) << ")"
231  << "\t = " << matrix.costs[i][j]
232  << "\t = " << matrix.costs[j][i]
233  << "=inf:"
234  << (matrix.costs[i][j] ==
235  (std::numeric_limits<double>::infinity)())
236  << "=inf:"
237  << (matrix.costs[j][i] ==
238  (std::numeric_limits<double>::infinity)())
239 #endif
240  << "\n";
241  ++j;
242  }
243  ++i;
244  }
245 #if 0
246  for (size_t i = 0; i < matrix.costs.size(); ++i) {
247  for (size_t j = 0; j < matrix.costs.size(); ++j) {
248  for (size_t k = 0; k < matrix.costs.size(); ++k) {
249  log << matrix.costs[i][k] << " <= ("
250  << matrix.costs[i][j] << " + " << matrix.costs[j][k] << ")"
251  << (matrix.costs[i][k]
252  <= (matrix.costs[i][j] + matrix.costs[j][k]))
253  << "\n";
254  }
255  }
256  }
257 #endif
258  return log;
259 }

Member Data Documentation

◆ costs

Costs pgrouting::tsp::Dmatrix::costs
private

◆ ids

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

The documentation for this class was generated from the following files:
pgrouting::tsp::Dmatrix::get_index
size_t get_index(int64_t id) const
original id -> idx
Definition: Dmatrix.cpp:93
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
pgrouting::tsp::Dmatrix::ids
std::vector< int64_t > ids
Definition: Dmatrix.h:124
pgrouting::tsp::Dmatrix::costs
Costs costs
Definition: Dmatrix.h:128
pgrouting::tsp::Dmatrix::set_ids
void set_ids(const std::vector< matrix_cell > &data_costs)
Definition: Dmatrix.cpp:64
pgrouting::tsp::Dmatrix::distance
double distance(int64_t i, int64_t j) const
Definition: Dmatrix.h:108
pgrouting::tsp::get_distance
double get_distance(std::pair< double, double > p1, std::pair< double, double > p2)
Definition: Dmatrix.cpp:127