vrpRouting  0.3
matrix.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 
3 FILE: Matrix.cpp
4 
5 Copyright (c) 2015 pgRouting developers
7 
8 ------
9 
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
14 
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19 
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 
24  ********************************************************************PGR-GNU*/
25 
26 #include "problem/matrix.h"
27 
28 #include <string>
29 #include <sstream>
30 #include <algorithm>
31 #include <limits>
32 #include <vector>
33 #include <map>
34 #include <cmath>
35 #include <utility>
36 #include <iomanip>
37 #include <tuple>
38 
39 
40 #include "cpp_common/pgr_assert.h"
41 #include "c_types/matrix_cell_t.h"
43 
44 
45 namespace vrprouting {
46 namespace problem {
47 
48 namespace {
49 
55 std::vector<std::tuple<TTimestamp, Multiplier>>
56 set_tdm(Time_multipliers_t *p_multipliers, size_t size_multipliers) {
57  pgassert(size_multipliers > 1);
58  std::vector<std::tuple<TTimestamp, Multiplier>> tdm;
59  /*
60  * Sort the multipliers info
61  */
62  std::sort(p_multipliers, p_multipliers + size_multipliers,
63  [] (const Time_multipliers_t &lhs, const Time_multipliers_t &rhs) {
64  return lhs.start_time < rhs.start_time;
65  });
66  for (size_t i = 0; i < size_multipliers; ++i) {
67  tdm.emplace_back(p_multipliers[i].start_time, p_multipliers[i].multiplier);
68  }
69  return tdm;
70 }
71 
78 get_tdm(const std::vector<std::tuple<TTimestamp, Multiplier>> &tdm, TTimestamp time) {
79  pgassert(time >= 0);
80  Multiplier coeff(1);
81  for (const auto &e : tdm) {
82  if (time < std::get<0>(e)) break;
83  coeff = std::get<1>(e);
84  }
85  return coeff;
86 }
87 
94 next_tdm(const std::vector<std::tuple<TTimestamp, Multiplier>> &tdm, TTimestamp time) {
95  Multiplier coeff(1);
96  for (const auto &e : tdm) {
97  coeff = std::get<1>(e);
98  if (std::get<0>(e) >= time) break;
99  }
100  return coeff;
101 }
102 
154 time_change(const std::vector<std::tuple<TTimestamp, Multiplier>> &tdm, TTimestamp time) {
155  TTimestamp t_change(0);
156  for (const auto &e : tdm) {
157  t_change = std::get<0>(e);
158  if (time <= t_change) break;
159  }
160  pgassert((time <= t_change)
161  || (t_change == std::get<0>(tdm[tdm.size() - 1 ])));
162  return t_change;
163 }
164 
165 } // namespace
166 
168  Matrix_cell_t *matrix, size_t size_matrix,
169  Time_multipliers_t *multipliers, size_t size_multipliers,
170  const Identifiers<Id>& node_ids,
171  Multiplier multiplier) :
172  Base_Matrix(matrix, size_matrix, node_ids, multiplier),
173  m_multipliers(set_tdm(multipliers, size_multipliers)) { }
174 
175 /*
176  * constructor for euclidean with time dependant multipliers
177  */
179  const std::map<std::pair<Coordinate, Coordinate>, Id> &euclidean_data,
180  Time_multipliers_t *multipliers, size_t size_multipliers,
181  Multiplier multiplier) :
182  Base_Matrix(euclidean_data, multiplier),
183  m_multipliers(set_tdm(multipliers, size_multipliers)) { }
184 
185 /*
186  * constructor for euclidean default multipliers
187  */
189  Matrix_cell_t *matrix, size_t size_matrix,
190  const Identifiers<Id>& node_ids,
191  Multiplier multiplier) :
192  Base_Matrix(matrix, size_matrix, node_ids, multiplier),
193  m_multipliers{{0, 1}} { }
194 
195 /*
196  * constructor for euclidean default multipliers
197  */
199  const std::map<std::pair<Coordinate, Coordinate>, Id> &euclidean_data,
200  Multiplier multiplier) :
201  Base_Matrix(euclidean_data, multiplier),
202  m_multipliers{{0, 1}} { }
203 
204 
232 TInterval
233 Matrix::travel_time(Id i, Id j, TTimestamp date_time_of_departure_from_i) const {
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 }
254 
255 std::string
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 }
264 
265 } // namespace problem
266 } // namespace vrprouting
time_multipliers_t.h
vrprouting::problem::Matrix::travel_time
TInterval travel_time(Id, Id, TTimestamp) const
retrun the travel time times when using the time dependant multipliers
Definition: matrix.cpp:233
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::problem::anonymous_namespace{matrix.cpp}::next_tdm
Multiplier next_tdm(const std::vector< std::tuple< TTimestamp, Multiplier >> &tdm, TTimestamp time)
Definition: matrix.cpp:94
Time_multipliers_t
Time Dependant Multipliers.
Definition: time_multipliers_t.h:46
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
vrprouting::problem::Matrix::Matrix
Matrix()=default
matrix.h
Id
int64_t Id
Definition: typedefs.h:78
vrprouting::problem::Matrix::multipliers_str
std::string multipliers_str() const
Definition: matrix.cpp:256
pgr_assert.h
An assert functionality that uses C++ throw().
Time_multipliers_t::start_time
TTimestamp start_time
Time of day where the multiplier starts to be valid.
Definition: time_multipliers_t.h:48
TTimestamp
int64_t TTimestamp
Definition: typedefs.h:71
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
Matrix_cell_t
traveling costs
Definition: matrix_cell_t.h:41
vrprouting::problem::Matrix::m_multipliers
std::vector< std::tuple< TTimestamp, Multiplier > > m_multipliers
time dependant multiplier
Definition: matrix.h:78
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
vrprouting::base::Base_Matrix::at
TInterval at(Idx i, Idx j) const
Definition: base_matrix.h:97
matrix_cell_t.h
Identifiers
Definition: identifiers.hpp:51
vrprouting
Definition: base_matrix.cpp:46
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