vrpRouting  0.3
vrprouting::problem::Matrix Class Reference

#include "matrix.h"

Inheritance diagram for vrprouting::problem::Matrix:
Collaboration diagram for vrprouting::problem::Matrix:

Public Member Functions

 Matrix ()=default
 
 Matrix (const std::map< std::pair< Coordinate, Coordinate >, Id > &, Multiplier=1.0)
 brief constructor for euclidean version default multipliers More...
 
 Matrix (const std::map< std::pair< Coordinate, Coordinate >, Id > &, Time_multipliers_t *, size_t, Multiplier=1.0)
 brief constructor for euclidean version with time dependant multipliers More...
 
 Matrix (Matrix_cell_t *, size_t, const Identifiers< Id > &, Multiplier=1.0)
 brief constructor for matrix version default multipliers More...
 
 Matrix (Matrix_cell_t *, size_t, Time_multipliers_t *, size_t, const Identifiers< Id > &, Multiplier=1.0)
 brief constructor for matrix version with time dependant multipliers 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...
 
std::string multipliers_str () const
 
TInterval travel_time (Id, Id) const
 retrun the travel time times from the matrix More...
 
TInterval travel_time (Id, Id, TTimestamp) const
 retrun the travel time times when using the time dependant multipliers More...
 
status of the matrix
bool has_no_infinity () const
 does the matrix values not given by the user? More...
 
vroom::Matrix< vroom::Durationget_vroom_duration_matrix () const
 Get VROOM duration matrix from vrprouting Base Matrix. More...
 
vroom::Matrix< vroom::Cost > get_vroom_cost_matrix () const
 Get VROOM cost matrix from vrprouting Base Matrix. More...
 
bool obeys_triangle_inequality () const
 does the matrix obeys the triangle inequality? More...
 
size_t fix_triangle_inequality (size_t depth=0)
 Fix Triangle Inequality Theorem. More...
 
bool empty () const
 is the matrix empty? More...
 
size_t size () const
 |idx| More...
 
bool is_symmetric () const
 is the matrix symetric? More...
 
status of the matrix
bool has_no_infinity () const
 does the matrix values not given by the user? More...
 
vroom::Matrix< vroom::Durationget_vroom_duration_matrix () const
 Get VROOM duration matrix from vrprouting Base Matrix. More...
 
vroom::Matrix< vroom::Cost > get_vroom_cost_matrix () const
 Get VROOM cost matrix from vrprouting Base Matrix. More...
 
bool obeys_triangle_inequality () const
 does the matrix obeys the triangle inequality? More...
 
size_t fix_triangle_inequality (size_t depth=0)
 Fix Triangle Inequality Theorem. More...
 
bool empty () const
 is the matrix empty? More...
 
size_t size () const
 |idx| More...
 
bool is_symmetric () const
 is the matrix symetric? 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::tuple< TTimestamp, Multiplier > > m_multipliers
 time dependant multiplier More...
 
std::vector< std::vector< TInterval > > m_time_matrix
 the actual time matrix More...
 

Detailed Description

Definition at line 46 of file matrix.h.

Constructor & Destructor Documentation

◆ Matrix() [1/5]

vrprouting::problem::Matrix::Matrix ( )
default

◆ Matrix() [2/5]

vrprouting::problem::Matrix::Matrix ( Matrix_cell_t matrix,
size_t  size_matrix,
Time_multipliers_t multipliers,
size_t  size_multipliers,
const Identifiers< Id > &  node_ids,
Multiplier  multiplier = 1.0 
)

brief constructor for matrix version with time dependant multipliers

Definition at line 167 of file matrix.cpp.

171  :
172  Base_Matrix(matrix, size_matrix, node_ids, multiplier),
173  m_multipliers(set_tdm(multipliers, size_multipliers)) { }

◆ Matrix() [3/5]

vrprouting::problem::Matrix::Matrix ( const std::map< std::pair< Coordinate, Coordinate >, Id > &  euclidean_data,
Time_multipliers_t multipliers,
size_t  size_multipliers,
Multiplier  multiplier = 1.0 
)

brief constructor for euclidean version with time dependant multipliers

Definition at line 178 of file matrix.cpp.

181  :
182  Base_Matrix(euclidean_data, multiplier),
183  m_multipliers(set_tdm(multipliers, size_multipliers)) { }

◆ Matrix() [4/5]

vrprouting::problem::Matrix::Matrix ( Matrix_cell_t matrix,
size_t  size_matrix,
const Identifiers< Id > &  node_ids,
Multiplier  multiplier = 1.0 
)

brief constructor for matrix version default multipliers

Definition at line 188 of file matrix.cpp.

191  :
192  Base_Matrix(matrix, size_matrix, node_ids, multiplier),
193  m_multipliers{{0, 1}} { }

◆ Matrix() [5/5]

vrprouting::problem::Matrix::Matrix ( const std::map< std::pair< Coordinate, Coordinate >, Id > &  euclidean_data,
Multiplier  multiplier = 1.0 
)
explicit

brief constructor for euclidean version default multipliers

Definition at line 198 of file matrix.cpp.

200  :
201  Base_Matrix(euclidean_data, multiplier),
202  m_multipliers{{0, 1}} { }

Member Function Documentation

◆ at()

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

Definition at line 97 of file base_matrix.h.

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

References vrprouting::base::Base_Matrix::m_time_matrix.

Referenced by travel_time().

◆ empty()

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

is the matrix empty?

Definition at line 84 of file base_matrix.h.

84 {return m_ids.empty();}

References vrprouting::base::Base_Matrix::m_ids.

◆ fix_triangle_inequality()

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

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 vrprouting::base::Base_Matrix::m_time_matrix.

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

◆ get_index()

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

original id -> idx

Given an original node identifier returns the internal index.

Parameters
[in]id
Returns
the position of the identifier
dot_inline_dotgraph_15.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 vrprouting::base::Base_Matrix::m_ids, pgassert, and pgassertwm.

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

◆ get_original_id()

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

original id -> idx

Given the internal index, returns the original node identifier.

Parameters
[in]id
Returns
the original node identifier
dot_inline_dotgraph_16.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 vrprouting::base::Base_Matrix::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
inherited

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 vrprouting::base::Base_Matrix::m_cost_matrix, and vrprouting::base::Base_Matrix::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
inherited

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 vrprouting::base::Base_Matrix::m_ids, and vrprouting::base::Base_Matrix::m_time_matrix.

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

◆ has_id()

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

has identifier

Parameters
[in]idoriginal identifier
Returns
true when it exists on the saved ids
dot_inline_dotgraph_17.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 vrprouting::base::Base_Matrix::m_ids.

Referenced by vrprouting::base::Base_Matrix::Base_Matrix(), and travel_time().

◆ has_no_infinity()

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

does the matrix values not given by the user?

Returns
false at the moment it finds an infinity value
true otherwise
dot_inline_dotgraph_18.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 vrprouting::base::Base_Matrix::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
inherited

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 vrprouting::base::Base_Matrix::m_time_matrix, and pgassertwm.

◆ multipliers_str()

std::string vrprouting::problem::Matrix::multipliers_str ( ) const

Definition at line 256 of file matrix.cpp.

256  {
257  std::ostringstream log;
258  log << std::fixed << std::setprecision(2);
259  for (const auto &e : m_multipliers) {
260  log << "\n" << std::get<0>(e) << "\t" << std::get<1>(e);
261  }
262  return log.str();
263 }

References m_multipliers.

◆ obeys_triangle_inequality()

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

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 vrprouting::base::Base_Matrix::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)
privateinherited

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_19.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(), vrprouting::base::Base_Matrix::m_ids, and pgassert.

◆ size()

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

|idx|

Returns
the size of the matrix

Definition at line 90 of file base_matrix.h.

90 {return m_ids.size();}

References vrprouting::base::Base_Matrix::m_ids.

Referenced by do_vrp_vroom().

◆ travel_time() [1/2]

TInterval vrprouting::problem::Matrix::travel_time ( Id  ,
Id   
) const

retrun the travel time times from the matrix

◆ travel_time() [2/2]

TInterval vrprouting::problem::Matrix::travel_time ( Id  i,
Id  j,
TTimestamp  date_time_of_departure_from_i 
) const

retrun the travel time times when using the time dependant multipliers

Parameters
[in]i,joriginal identifiers
[in]date_time_of_departure_from_itime of departure from i
Returns
infinity When the original identifiers do not exist on the matrix
the cell content
dot_inline_dotgraph_20.png

Definition at line 233 of file matrix.cpp.

233  {
234  if (m_multipliers.size() == 1) return at(get_index(i), get_index(j));
235 
236  /*
237  * are ids valid?
238  *
239  * no -> return infinity
240  */
241  if (!has_id(i) || !has_id(j)) return (std::numeric_limits<TInterval>::max)();
242 
243  /* data */
244  double tt_i_j {static_cast<double>(at(get_index(i), get_index(j)))};
245  double c1(get_tdm(m_multipliers, date_time_of_departure_from_i));
246  double c2(next_tdm(m_multipliers, date_time_of_departure_from_i));
247  auto t_change(time_change(m_multipliers, date_time_of_departure_from_i));
248  auto adjusted_value = static_cast<TInterval>(tt_i_j * c1);
249 
250  if (t_change >= (date_time_of_departure_from_i + adjusted_value) || c1 == c2) return adjusted_value;
251 
252  return static_cast<TInterval>(tt_i_j * std::min(c1, c2));
253 }

References vrprouting::base::Base_Matrix::at(), vrprouting::base::Base_Matrix::get_index(), vrprouting::problem::anonymous_namespace{matrix.cpp}::get_tdm(), vrprouting::base::Base_Matrix::has_id(), m_multipliers, vrprouting::problem::anonymous_namespace{matrix.cpp}::next_tdm(), and vrprouting::problem::anonymous_namespace{matrix.cpp}::time_change().

Referenced by vrprouting::problem::Tw_node::travel_time_to().

Member Data Documentation

◆ m_cost_matrix

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

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 vrprouting::base::Base_Matrix::Base_Matrix(), and vrprouting::base::Base_Matrix::get_vroom_cost_matrix().

◆ m_ids

◆ m_multipliers

std::vector<std::tuple<TTimestamp, Multiplier> > vrprouting::problem::Matrix::m_multipliers
private

time dependant multiplier

m_multipliers[i] ith time dependant multiplier double = time when the multipliers starts to be valid double = multiplier The multiplier ends its validity at the time of the (i+1)th value

Definition at line 78 of file matrix.h.

Referenced by multipliers_str(), and travel_time().

◆ m_time_matrix


The documentation for this class was generated from the following files:
vrprouting::base::Base_Matrix::Base_Matrix
Base_Matrix()=default
Constructs an emtpy matrix.
vrprouting::base::Base_Matrix::get_index
Idx get_index(Id) const
original id -> idx
Definition: base_matrix.cpp:161
vrprouting::problem::anonymous_namespace{matrix.cpp}::get_tdm
Multiplier get_tdm(const std::vector< std::tuple< TTimestamp, Multiplier >> &tdm, TTimestamp time)
Definition: matrix.cpp:78
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
vrprouting::problem::anonymous_namespace{matrix.cpp}::next_tdm
Multiplier next_tdm(const std::vector< std::tuple< TTimestamp, Multiplier >> &tdm, TTimestamp time)
Definition: matrix.cpp:94
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
vrprouting::problem::anonymous_namespace{matrix.cpp}::set_tdm
std::vector< std::tuple< TTimestamp, Multiplier > > set_tdm(Time_multipliers_t *p_multipliers, size_t size_multipliers)
Definition: matrix.cpp:56
Idx
uint64_t Idx
Definition: typedefs.h:79
vrprouting::problem::Matrix::m_multipliers
std::vector< std::tuple< TTimestamp, Multiplier > > m_multipliers
time dependant multiplier
Definition: matrix.h:78
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
vrprouting::base::Base_Matrix::at
TInterval at(Idx i, Idx j) const
Definition: base_matrix.h:97
Identifiers
Definition: identifiers.hpp:51
vrprouting::problem::anonymous_namespace{matrix.cpp}::time_change
TTimestamp time_change(const std::vector< std::tuple< TTimestamp, Multiplier >> &tdm, TTimestamp time)
Definition: matrix.cpp:154