vrpRouting  0.3
vrprouting::base::Base_Matrix Class Reference

N x N matrix. More...

#include "base_matrix.h"

Inheritance diagram for vrprouting::base::Base_Matrix:
Collaboration diagram for vrprouting::base::Base_Matrix:

Public Member Functions

 Base_Matrix ()=default
 Constructs an emtpy matrix. More...
 
 Base_Matrix (const std::map< std::pair< Coordinate, Coordinate >, Id > &, Multiplier)
 
 Base_Matrix (Matrix_cell_t *, size_t, const Identifiers< Id > &, Multiplier)
 Constructs a matrix for only specific identifiers. More...
 
 Base_Matrix (Vroom_matrix_t *, size_t, const Identifiers< Id > &, double)
 Constructor for VROOM matrix input. More...
 
TInterval at (Idx i, Idx j) const
 
Idx get_index (Id) const
 original id -> idx More...
 
Id get_original_id (Idx) const
 original id -> idx More...
 
bool has_id (Id) const
 has identifier More...
 

Private Member Functions

void set_ids (const std::vector< Matrix_cell_t > &)
 Traverses the matrix information to set the ids of the nodes. More...
 

Private Attributes

std::vector< std::vector< TravelCost > > m_cost_matrix
 the cost matrix for vroom More...
 
std::vector< Idm_ids
 DATA. More...
 
std::vector< std::vector< TInterval > > m_time_matrix
 the actual time matrix More...
 

Friends

std::ostream & operator<< (std::ostream &log, const Base_Matrix &matrix)
 print matrix (row per cell) More...
 

Detailed Description

N x N matrix.

  • The internal data interpretation is done by the user of this class
  • Once created can not be modified
dot_inline_dotgraph_7.png

Definition at line 61 of file base_matrix.h.

Constructor & Destructor Documentation

◆ Base_Matrix() [1/4]

vrprouting::base::Base_Matrix::Base_Matrix ( )
default

Constructs an emtpy matrix.

◆ Base_Matrix() [2/4]

vrprouting::base::Base_Matrix::Base_Matrix ( Matrix_cell_t data_costs,
size_t  size_matrix,
const Identifiers< Id > &  node_ids,
Multiplier  multiplier 
)

Constructs a matrix for only specific identifiers.

Parameters
[in]data_costsThe set of costs
[in]size_matrixThe size of the set of costs
[in]node_idsThe selected node identifiers to be added
[in]multiplierAll times are multiplied by this value
Precondition
data_costs is not empty
Postcondition
ids has all the ids of node_ids
data_costs[from_vid, to_vid] is ignored when from_vid is not in node_ids or to_vid is not in node_ids
costs[from_vid, to_vid] is not has the cell cost when from_vid, to_vid are in node_ids
costs[from_vid, to_vid] = inf when cell from_vid, to_vid does not exist
costs[from_vid, to_vid] = 0 when from_vid = to_vid

Definition at line 235 of file base_matrix.cpp.

238  {
239  /*
240  * Sets the selected nodes identifiers
241  */
242  m_ids.insert(m_ids.begin(), node_ids.begin(), node_ids.end());
243 
244  /*
245  * Create matrix
246  */
247  m_time_matrix.resize(
248  m_ids.size(),
249  std::vector<TInterval>(
250  m_ids.size(),
251  /*
252  * Set initial values to infinity
253  */
254  (std::numeric_limits<TInterval>::max)()));
255 
256  Identifiers<Idx> inserted;
257  /*
258  * Cycle the matrix data
259  */
260  for (size_t i = 0; i < size_matrix; ++i) {
261  auto data = data_costs[i];
262  /*
263  * skip if row is not from selected nodes
264  */
265  if (!(has_id(data.from_vid) && has_id(data.to_vid))) continue;
266 
267  /*
268  * Save the information
269  */
270  m_time_matrix[get_index(data.from_vid)][get_index(data.to_vid)] =
271  static_cast<TInterval>(static_cast<Multiplier>(data.cost) * multiplier);
272 
273  /*
274  * If the opposite direction is infinity insert the same cost
275  */
276  if (m_time_matrix[get_index(data.to_vid)][get_index(data.from_vid)] == (std::numeric_limits<TInterval>::max)()) {
277  m_time_matrix[get_index(data.to_vid)][get_index(data.from_vid)] =
278  m_time_matrix[get_index(data.from_vid)][get_index(data.to_vid)];
279  }
280  }
281 
282  /*
283  * Set the diagonal values to 0
284  */
285  for (size_t i = 0; i < m_time_matrix.size(); ++i) {
286  m_time_matrix[i][i] = 0;
287  }
288 }

References Identifiers< T >::begin(), Identifiers< T >::end(), get_index(), has_id(), m_ids, and m_time_matrix.

◆ Base_Matrix() [3/4]

vrprouting::base::Base_Matrix::Base_Matrix ( Vroom_matrix_t matrix_rows,
size_t  total_matrix_rows,
const Identifiers< Id > &  location_ids,
double  scaling_factor 
)

Constructor for VROOM matrix input.

Parameters
[in]data_costsThe set of costs
[in]size_matrixThe size of the set of costs
[in]location_idsThe location identifiers
Precondition
data_costs is not empty
Postcondition
ids has all the ids of node_ids
data_costs[from_vid, to_vid] is ignored when from_vid is not in node_ids or to_vid is not in node_ids
costs[from_vid, to_vid] is not has the cell cost when from_vid, to_vid are in node_ids
costs[from_vid, to_vid] = inf when cell from_vid, to_vid does not exist
costs[from_vid, to_vid] = 0 when from_vid = to_vid

Definition at line 305 of file base_matrix.cpp.

306  {
307  /*
308  * Sets the selected nodes identifiers
309  */
310  m_ids.insert(m_ids.begin(), location_ids.begin(), location_ids.end());
311 
312  /*
313  * Create matrix
314  */
315  m_time_matrix.resize(
316  m_ids.size(),
317  std::vector<TInterval>(m_ids.size(),
318  /*
319  * Set initial values to infinity
320  */
321  (std::numeric_limits<TInterval>::max)()));
322  m_cost_matrix.resize(
323  m_ids.size(),
324  std::vector<TravelCost>(m_ids.size(), (std::numeric_limits<TravelCost>::max)()));
325 
326  Identifiers<Idx> inserted;
327  /*
328  * Cycle the matrix data
329  */
330  for (size_t i = 0; i < total_matrix_rows; ++i) {
331  auto cell = matrix_rows[i];
332  /*
333  * skip if row is not from selected nodes
334  */
335  if (!(has_id(cell.start_id) && has_id(cell.end_id))) continue;
336 
337  /*
338  * Save the information. Scale the time matrix according to scaling_factor
339  */
340  m_time_matrix[get_index(cell.start_id)][get_index(cell.end_id)] =
341  static_cast<Duration>(std::round(cell.duration / scaling_factor));
342  m_cost_matrix[get_index(cell.start_id)][get_index(cell.end_id)] =
343  static_cast<Duration>(cell.cost);
344 
345  /*
346  * If the opposite direction is infinity insert the same cost
347  */
348  if (m_time_matrix[get_index(cell.end_id)][get_index(cell.start_id)] ==
349  (std::numeric_limits<TInterval>::max)()) {
350  m_time_matrix[get_index(cell.end_id)][get_index(cell.start_id)] =
351  m_time_matrix[get_index(cell.start_id)][get_index(cell.end_id)];
352  }
353  if (m_cost_matrix[get_index(cell.end_id)][get_index(cell.start_id)] ==
354  (std::numeric_limits<TravelCost>::max)()) {
355  m_cost_matrix[get_index(cell.end_id)][get_index(cell.start_id)] =
356  m_cost_matrix[get_index(cell.start_id)][get_index(cell.end_id)];
357  }
358  }
359 
360  /*
361  * Set the diagonal values to 0
362  */
363  for (size_t i = 0; i < m_time_matrix.size(); ++i) {
364  m_time_matrix[i][i] = 0;
365  m_cost_matrix[i][i] = 0;
366  }
367 }

References Identifiers< T >::begin(), Identifiers< T >::end(), get_index(), has_id(), m_cost_matrix, m_ids, and m_time_matrix.

◆ Base_Matrix() [4/4]

vrprouting::base::Base_Matrix::Base_Matrix ( const std::map< std::pair< Coordinate, Coordinate >, Id > &  euclidean_data,
Multiplier  multiplier 
)
explicit

Definition at line 372 of file base_matrix.cpp.

372  {
373  m_ids.reserve(euclidean_data.size());
374  for (const auto &e : euclidean_data) {
375  m_ids.push_back(e.second);
376  }
377  m_time_matrix.resize(
378  m_ids.size(),
379  std::vector<TInterval>(
380  m_ids.size(),
381  (std::numeric_limits<TInterval>::max)()));
382 
383  for (const auto &from : euclidean_data) {
384  for (const auto &to : euclidean_data) {
385  auto from_id = get_index(from.second);
386  auto to_id = get_index(to.second);
387  m_time_matrix[from_id][to_id] =
388  static_cast<TInterval>(static_cast<Multiplier>(detail::get_distance(from.first, to.first)) * multiplier);
389  m_time_matrix[to_id][from_id] = m_time_matrix[from_id][to_id];
390  }
391  }
392 
393  for (size_t i = 0; i < m_time_matrix.size(); ++i) {
394  m_time_matrix[i][i] = 0;
395  }
396 }

References vrprouting::base::detail::get_distance(), get_index(), m_ids, and m_time_matrix.

Member Function Documentation

◆ at()

TInterval vrprouting::base::Base_Matrix::at ( Idx  i,
Idx  j 
) const
inline

Definition at line 97 of file base_matrix.h.

97 {return m_time_matrix[i][j];}

References m_time_matrix.

Referenced by vrprouting::problem::Matrix::travel_time().

◆ empty()

bool vrprouting::base::Base_Matrix::empty ( ) const
inline

is the matrix empty?

Definition at line 84 of file base_matrix.h.

84 {return m_ids.empty();}

References m_ids.

◆ fix_triangle_inequality()

size_t vrprouting::base::Base_Matrix::fix_triangle_inequality ( size_t  depth = 0)

Fix 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 508 of file base_matrix.cpp.

508  {
509  if (depth > m_time_matrix.size()) return depth;
510  for (auto & i : m_time_matrix) {
511  for (size_t j = 0; j < m_time_matrix.size(); ++j) {
512  for (size_t k = 0; k < m_time_matrix.size(); ++k) {
513  if (i[k] > (i[j] + m_time_matrix[j][k])) {
514  i[k] = i[j] + m_time_matrix[j][k];
515  return fix_triangle_inequality(++depth);
516  }
517  }
518  }
519  }
520  return depth;
521 }

References m_time_matrix.

Referenced by do_compatibleVehicles(), do_optimize(), and do_pickDeliver().

◆ get_index()

Idx vrprouting::base::Base_Matrix::get_index ( Id  id) const

original id -> idx

Given an original node identifier returns the internal index.

Parameters
[in]id
Returns
the position of the identifier
dot_inline_dotgraph_8.png

Definition at line 161 of file base_matrix.cpp.

161  {
162  /*
163  * binary search the stored identifiers
164  */
165  auto pos = std::lower_bound(m_ids.begin(), m_ids.end(), id);
166  if (pos == m_ids.end()) {
167  std::ostringstream msg;
168  msg << *this << "\nNot found" << id;
169  pgassertwm(false, msg.str());
170  throw std::make_pair(std::string("(INTERNAL) Base_Matrix: Unable to find node on matrix"), msg.str());
171  }
172  pgassert(pos != m_ids.end());
173 
174  /*
175  * return the index found
176  */
177  return static_cast<Idx>(pos - m_ids.begin());
178 }

References m_ids, pgassert, and pgassertwm.

Referenced by Base_Matrix(), vrprouting::Vrp_vroom_problem::get_vroom_job(), vrprouting::Vrp_vroom_problem::get_vroom_shipment(), vrprouting::Vrp_vroom_problem::get_vroom_vehicle(), and vrprouting::problem::Matrix::travel_time().

◆ get_original_id()

Id vrprouting::base::Base_Matrix::get_original_id ( Idx  index) const

original id -> idx

Given the internal index, returns the original node identifier.

Parameters
[in]id
Returns
the original node identifier
dot_inline_dotgraph_9.png

Definition at line 202 of file base_matrix.cpp.

202  {
203  /*
204  * Go to the index in the identifiers vector
205  */
206 
207  if (index >= m_ids.size()) {
208  std::ostringstream msg;
209  msg << *this << "\nOut of range" << index;
210  pgassertwm(false, msg.str());
211  throw std::make_pair(std::string("(INTERNAL) Base_Matrix: The given index is out of range"), msg.str());
212  }
213  pgassert(index < m_ids.size());
214 
215  /*
216  * return the original id found
217  */
218  return static_cast<Id>(m_ids[index]);
219 }

References m_ids, pgassert, and pgassertwm.

Referenced by vrprouting::Vrp_vroom_problem::get_results().

◆ get_vroom_cost_matrix()

vroom::Matrix< vroom::Cost > vrprouting::base::Base_Matrix::get_vroom_cost_matrix ( ) const

Get VROOM cost matrix from vrprouting Base Matrix.

Returns
vroom::Matrix<vroom::Cost> The vroom cost matrix

Definition at line 421 of file base_matrix.cpp.

421  {
422  size_t matrix_size = m_ids.size();
423  vroom::Matrix<vroom::Cost> vroom_matrix(matrix_size);
424  for (size_t i = 0; i < matrix_size; i++) {
425  for (size_t j = 0; j < matrix_size; j++) {
426  vroom_matrix[i][j] = static_cast<vroom::Cost>(m_cost_matrix[i][j]);
427  }
428  }
429  return vroom_matrix;
430 }

References m_cost_matrix, and m_ids.

Referenced by vrprouting::Vrp_vroom_problem::solve().

◆ get_vroom_duration_matrix()

vroom::Matrix< vroom::Duration > vrprouting::base::Base_Matrix::get_vroom_duration_matrix ( ) const

Get VROOM duration matrix from vrprouting Base Matrix.

Returns
vroom::Matrix<vroom::Duration> The vroom cost matrix

Definition at line 404 of file base_matrix.cpp.

404  {
405  size_t matrix_size = m_ids.size();
406  vroom::Matrix<vroom::Cost> vroom_matrix(matrix_size);
407  for (size_t i = 0; i < matrix_size; i++) {
408  for (size_t j = 0; j < matrix_size; j++) {
409  vroom_matrix[i][j] = static_cast<vroom::Duration>(m_time_matrix[i][j]);
410  }
411  }
412  return vroom_matrix;
413 }

References m_ids, and m_time_matrix.

Referenced by vrprouting::Vrp_vroom_problem::solve().

◆ has_id()

bool vrprouting::base::Base_Matrix::has_id ( Id  id) const

has identifier

Parameters
[in]idoriginal identifier
Returns
true when it exists on the saved ids
dot_inline_dotgraph_10.png

Definition at line 126 of file base_matrix.cpp.

126  {
127  /*
128  * binary search the stored identifiers
129  */
130  auto pos = std::lower_bound(m_ids.cbegin(), m_ids.cend(), id);
131 
132  /*
133  * Return search results
134  */
135  return pos != m_ids.end() && *pos == id;
136 }

References m_ids.

Referenced by Base_Matrix(), and vrprouting::problem::Matrix::travel_time().

◆ has_no_infinity()

bool vrprouting::base::Base_Matrix::has_no_infinity ( ) const

does the matrix values not given by the user?

Returns
false at the moment it finds an infinity value
true otherwise
dot_inline_dotgraph_11.png

Definition at line 459 of file base_matrix.cpp.

459  {
460  /*
461  * Cycle the matrix
462  */
463  for (const auto &row : m_time_matrix) {
464  for (const auto &val : row) {
465  /*
466  * found infinity?
467  *
468  * yes -> return false
469  */
470  if (val == (std::numeric_limits<TInterval>::max)()) return false;
471  }
472  }
473  /*
474  * return true
475  */
476  return true;
477 }

References m_time_matrix.

Referenced by do_compatibleVehicles(), do_optimize(), do_pgr_pickDeliver(), do_pickDeliver(), and do_vrp_vroom().

◆ is_symmetric()

bool vrprouting::base::Base_Matrix::is_symmetric ( ) const

is the matrix symetric?

Definition at line 524 of file base_matrix.cpp.

524  {
525  for (size_t i = 0; i < m_time_matrix.size(); ++i) {
526  for (size_t j = 0; j < m_time_matrix.size(); ++j) {
527  if (0.000001 < std::fabs(m_time_matrix[i][j] - m_time_matrix[j][i])) {
528  std::ostringstream log;
529  log << "i \t" << i
530  << "j \t" << j
531  << "m_time_matrix[i][j] \t" << m_time_matrix[i][j]
532  << "m_time_matrix[j][i] \t" << m_time_matrix[j][i]
533  << "\n";
534  log << (*this);
535  pgassertwm(false, log.str());
536  return false;
537  }
538  }
539  }
540  return true;
541 }

References m_time_matrix, and pgassertwm.

◆ obeys_triangle_inequality()

bool vrprouting::base::Base_Matrix::obeys_triangle_inequality ( ) const

does the matrix obeys the triangle inequality?

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 m_time_matrix[i][k] != inf m_time_matrix[i][k] <= m_time_matrix[i][j] + m_time_matrix[j][k]

Definition at line 487 of file base_matrix.cpp.

487  {
488  for (size_t i = 0; i < m_time_matrix.size(); ++i) {
489  for (size_t j = 0; j < m_time_matrix.size(); ++j) {
490  for (size_t k = 0; k < m_time_matrix.size(); ++k) {
491  if (m_time_matrix[i][k] > (m_time_matrix[i][j] + m_time_matrix[j][k])) {
492  return false;
493  }
494  }
495  }
496  }
497  return true;
498 }

References m_time_matrix.

Referenced by do_compatibleVehicles(), do_optimize(), and do_pickDeliver().

◆ set_ids()

void vrprouting::base::Base_Matrix::set_ids ( const std::vector< Matrix_cell_t > &  data)
private

Traverses the matrix information to set the ids of the nodes.

Traverses the matrix information to get the ids of the nodes.

Parameters
[in]dataBase_Matrix information
Postcondition
m_ids contains all the nodes original ids
dot_inline_dotgraph_12.png

Definition at line 85 of file base_matrix.cpp.

85  {
86  pgassert(m_ids.empty());
87  Identifiers<Id> node_ids;
88  /*
89  * Cycle the information
90  */
91  for (const auto &cost : data) {
92  /*
93  * extract the original identifiers
94  */
95  node_ids += cost.from_vid;
96  node_ids += cost.to_vid;
97  }
98  /*
99  * Save the extracted information
100  */
101  m_ids.insert(m_ids.begin(), node_ids.begin(), node_ids.end());
102 }

References Identifiers< T >::begin(), Identifiers< T >::end(), m_ids, and pgassert.

◆ size()

size_t vrprouting::base::Base_Matrix::size ( ) const
inline

|idx|

Returns
the size of the matrix

Definition at line 90 of file base_matrix.h.

90 {return m_ids.size();}

References m_ids.

Referenced by do_vrp_vroom().

Friends And Related Function Documentation

◆ operator<<

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

print matrix (row per cell)

Parameters
[in,out]logstream variable where to print
[in]matrixThe matrix
dot_inline_dotgraph_13.png

Definition at line 567 of file base_matrix.cpp.

567  {
568  /*
569  * print the identifiers
570  */
571  for (const auto id : matrix.m_ids) {
572  log << "\t" << id;
573  }
574  log << "\n";
575  size_t i = 0;
576 
577  /*
578  * Cycle the cells
579  */
580  for (const auto &row : matrix.m_time_matrix) {
581  size_t j = 0;
582  for (const auto cost : row) {
583  /*
584  * print the information
585  */
586  log << "Internal(" << i << "," << j << ")"
587  << "\tOriginal(" << matrix.m_ids[i] << "," << matrix.m_ids[j] << ")"
588  << "\t = " << cost
589  << "\n";
590  ++j;
591  }
592  ++i;
593  }
594  return log;
595 }

Member Data Documentation

◆ m_cost_matrix

std::vector<std::vector<TravelCost> > vrprouting::base::Base_Matrix::m_cost_matrix
private

the cost matrix for vroom

m_cost_matrix[i][j] i and j are index from the ids

Definition at line 132 of file base_matrix.h.

Referenced by Base_Matrix(), and get_vroom_cost_matrix().

◆ m_ids

std::vector<Id> vrprouting::base::Base_Matrix::m_ids
private

◆ m_time_matrix

std::vector<std::vector<TInterval> > vrprouting::base::Base_Matrix::m_time_matrix
private

the actual time matrix

m_time_matrix[i][j] i and j are index from the ids

Definition at line 126 of file base_matrix.h.

Referenced by at(), Base_Matrix(), fix_triangle_inequality(), get_vroom_duration_matrix(), has_no_infinity(), is_symmetric(), obeys_triangle_inequality(), and vrprouting::base::operator<<().


The documentation for this class was generated from the following files:
vrprouting::base::detail::get_distance
double get_distance(std::pair< Coordinate, Coordinate > p1, std::pair< Coordinate, Coordinate > p2)
Definition: base_matrix.cpp:51
vrprouting::base::Base_Matrix::get_index
Idx get_index(Id) const
original id -> idx
Definition: base_matrix.cpp:161
vrprouting::base::Base_Matrix::m_cost_matrix
std::vector< std::vector< TravelCost > > m_cost_matrix
the cost matrix for vroom
Definition: base_matrix.h:132
pgassertwm
#define pgassertwm(expr, msg)
Adds a message to the assertion.
Definition: pgr_assert.h:118
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
Id
int64_t Id
Definition: typedefs.h:78
Identifiers::begin
const_iterator begin() const
Definition: identifiers.hpp:81
vrprouting::base::Base_Matrix::m_ids
std::vector< Id > m_ids
DATA.
Definition: base_matrix.h:120
vrprouting::base::Base_Matrix::fix_triangle_inequality
size_t fix_triangle_inequality(size_t depth=0)
Fix Triangle Inequality Theorem.
Definition: base_matrix.cpp:508
Idx
uint64_t Idx
Definition: typedefs.h:79
vrprouting::base::Base_Matrix::m_time_matrix
std::vector< std::vector< TInterval > > m_time_matrix
the actual time matrix
Definition: base_matrix.h:126
Identifiers::end
const_iterator end() const
Definition: identifiers.hpp:82
Duration
uint32_t Duration
Definition: typedefs.h:81
vrprouting::base::Base_Matrix::has_id
bool has_id(Id) const
has identifier
Definition: base_matrix.cpp:126
TInterval
int64_t TInterval
Definition: typedefs.h:72
Multiplier
double Multiplier
Definition: typedefs.h:77
Identifiers
Definition: identifiers.hpp:51