vrpRouting  0.3
vroom_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: vroom_driver.cpp
3 
4 Copyright (c) 2021 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2021 Ashish Kumar
10 ------
11 
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
16 
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
21 
22 You should have received a copy of the GNU General Public License
23 along with this program; if not, write to the Free Software
24 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 
26  ********************************************************************PGR-GNU*/
27 
29 
30 #include <sstream>
31 #include <vector>
32 #include <algorithm>
33 #include <string>
34 #include <limits>
35 #include <cmath>
36 
37 #include "c_common/pgr_alloc.hpp"
38 #include "cpp_common/pgr_assert.h"
40 
83 void
85  Vroom_job_t *jobs, size_t total_jobs,
86  Vroom_time_window_t *jobs_tws, size_t total_jobs_tws,
87  Vroom_shipment_t *shipments, size_t total_shipments,
88  Vroom_time_window_t *shipments_tws, size_t total_shipments_tws,
89  Vroom_vehicle_t *vehicles, size_t total_vehicles,
90  Vroom_break_t *breaks, size_t total_breaks,
91  Vroom_time_window_t *breaks_tws, size_t total_breaks_tws,
92  Vroom_matrix_t *matrix_rows, size_t total_matrix_rows,
93 
94  int32_t exploration_level,
95  int32_t timeout,
96  int32_t loading_time,
97 
98  Vroom_rt **return_tuples,
99  size_t *return_count,
100 
101  char ** log_msg,
102  char ** notice_msg,
103  char ** err_msg) {
104  std::ostringstream log;
105  std::ostringstream err;
106  std::ostringstream notice;
107  try {
108  pgassert(!(*log_msg));
109  pgassert(!(*notice_msg));
110  pgassert(!(*err_msg));
111  pgassert(!(*return_tuples));
112  pgassert(!(*return_count));
113  pgassert(jobs || shipments);
114  pgassert(vehicles);
115  pgassert(matrix_rows);
116  pgassert(total_jobs || total_shipments);
117  pgassert(total_vehicles);
118  pgassert(total_matrix_rows);
119 
120  auto start_time = std::chrono::high_resolution_clock::now();
121 
122  Identifiers<Id> location_ids;
123 
124  for (size_t i = 0; i < total_jobs; ++i) {
125  location_ids += jobs[i].location_id;
126  }
127 
128  for (size_t i = 0; i < total_shipments; ++i) {
129  location_ids += shipments[i].p_location_id;
130  location_ids += shipments[i].d_location_id;
131  }
132 
133  double min_speed_factor, max_speed_factor;
134  min_speed_factor = max_speed_factor = vehicles[0].speed_factor;
135 
136  for (size_t i = 0; i < total_vehicles; ++i) {
137  min_speed_factor = std::min(min_speed_factor, vehicles[i].speed_factor);
138  max_speed_factor = std::max(max_speed_factor, vehicles[i].speed_factor);
139  if (vehicles[i].start_id != -1) {
140  location_ids += vehicles[i].start_id;
141  }
142  if (vehicles[i].end_id != -1) {
143  location_ids += vehicles[i].end_id;
144  }
145  }
146 
147  /*
148  * Verify that max value of speed factor is not greater
149  * than 5 times the speed factor of any other vehicle.
150  */
151  if (max_speed_factor > 5 * min_speed_factor) {
152  (*return_tuples) = NULL;
153  (*return_count) = 0;
154  err << "The speed_factor " << max_speed_factor << " is more than five times "
155  "the speed factor " << min_speed_factor;
156  *err_msg = pgr_msg(err.str());
157  return;
158  }
159 
160  /*
161  * Scale the vehicles speed factors according to the minimum speed factor
162  */
163  for (size_t i = 0; i < total_vehicles; ++i) {
164  vehicles[i].speed_factor = std::round(vehicles[i].speed_factor / min_speed_factor);
165  }
166 
167  /*
168  * Create the matrix. Also, scale the time matrix according to min_speed_factor
169  */
170  vrprouting::base::Base_Matrix matrix(matrix_rows, total_matrix_rows,
171  location_ids, min_speed_factor);
172 
173  /*
174  * Verify matrix cells preconditions
175  */
176  if (!matrix.has_no_infinity()) {
177  (*return_tuples) = NULL;
178  (*return_count) = 0;
179  err << "An Infinity value was found on the Matrix. Might be missing information of a node";
180  *err_msg = pgr_msg(err.str());
181  return;
182  }
183 
184  /*
185  * Verify size of matrix cell lies in the limit
186  */
187  if (matrix.size() > (std::numeric_limits<vroom::Index>::max)()) {
188  (*return_tuples) = NULL;
189  (*return_count) = 0;
190  err << "The size of time matrix exceeds the limit";
191  *err_msg = pgr_msg(err.str());
192  return;
193  }
194 
196  problem.add_matrix(matrix);
197  problem.add_vehicles(vehicles, total_vehicles,
198  breaks, total_breaks,
199  breaks_tws, total_breaks_tws);
200  problem.add_jobs(jobs, total_jobs,
201  jobs_tws, total_jobs_tws);
202  problem.add_shipments(shipments, total_shipments,
203  shipments_tws, total_shipments_tws);
204 
205  auto end_time = std::chrono::high_resolution_clock::now();
206  loading_time += static_cast<int32_t>(
207  std::chrono::duration_cast<std::chrono::milliseconds>(end_time - start_time)
208  .count());
209 
210  std::vector < Vroom_rt > results = problem.solve(exploration_level, timeout, loading_time);
211 
212  auto count = results.size();
213  if (count == 0) {
214  (*return_tuples) = NULL;
215  (*return_count) = 0;
216  notice << "No results found";
217  *notice_msg = notice.str().empty()?
218  *notice_msg :
219  pgr_msg(notice.str().c_str());
220  return;
221  }
222 
223  (*return_tuples) = pgr_alloc(count, (*return_tuples));
224  for (size_t i = 0; i < count; i++) {
225  *((*return_tuples) + i) = results[i];
226  }
227 
228  (*return_count) = count;
229 
230  pgassert(*err_msg == NULL);
231  *log_msg = log.str().empty()?
232  *log_msg :
233  pgr_msg(log.str().c_str());
234  *notice_msg = notice.str().empty()?
235  *notice_msg :
236  pgr_msg(notice.str().c_str());
237  } catch (AssertFailedException &except) {
238  (*return_tuples) = pgr_free(*return_tuples);
239  (*return_count) = 0;
240  err << except.what();
241  *err_msg = pgr_msg(err.str().c_str());
242  *log_msg = pgr_msg(log.str().c_str());
243  } catch (const vroom::Exception &except) {
244  (*return_tuples) = pgr_free(*return_tuples);
245  (*return_count) = 0;
246  err << except.message;
247  *err_msg = pgr_msg(err.str().c_str());
248  *log_msg = pgr_msg(log.str().c_str());
249  } catch (std::exception &except) {
250  (*return_tuples) = pgr_free(*return_tuples);
251  (*return_count) = 0;
252  err << except.what();
253  *err_msg = pgr_msg(err.str().c_str());
254  *log_msg = pgr_msg(log.str().c_str());
255  } catch(...) {
256  (*return_tuples) = pgr_free(*return_tuples);
257  (*return_count) = 0;
258  err << "Caught unknown exception!";
259  *err_msg = pgr_msg(err.str().c_str());
260  *log_msg = pgr_msg(log.str().c_str());
261  }
262 }
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Vroom_vehicle_t::end_id
MatrixIndex end_id
Start location index in matrix.
Definition: vroom_vehicle_t.h:57
vrprouting::Vrp_vroom_problem::add_vehicles
void add_vehicles(const std::vector< Vroom_vehicle_t > &vehicles, const std::vector< Vroom_break_t > &breaks, const std::vector< Vroom_time_window_t > &breaks_tws)
Definition: vrp_vroom_problem.hpp:382
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:33
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
vroom_driver.h
vrprouting::Vrp_vroom_problem
Definition: vrp_vroom_problem.hpp:54
Vroom_time_window_t
Time window's attributes.
Definition: vroom_time_window_t.h:46
Vroom_vehicle_t::speed_factor
double speed_factor
Time window end time.
Definition: vroom_vehicle_t.h:68
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
Vroom_shipment_t::p_location_id
MatrixIndex p_location_id
Shipment identifier.
Definition: vroom_shipment_t.h:60
vrprouting::Vrp_vroom_problem::add_matrix
void add_matrix(vrprouting::base::Base_Matrix matrix)
Definition: vrp_vroom_problem.hpp:424
pgr_alloc.hpp
vrp_vroom_problem.hpp
vrprouting::Vrp_vroom_problem::add_jobs
void add_jobs(const std::vector< Vroom_job_t > &jobs, const std::vector< Vroom_time_window_t > &jobs_tws)
Definition: vrp_vroom_problem.hpp:190
vrprouting::base::Base_Matrix::size
size_t size() const
|idx|
Definition: base_matrix.h:90
Vroom_shipment_t
Vehicles's attributes.
Definition: vroom_shipment_t.h:56
pgr_assert.h
An assert functionality that uses C++ throw().
Vroom_job_t::location_id
MatrixIndex location_id
The job's identifier.
Definition: vroom_job_t.h:56
vrprouting::base::Base_Matrix::has_no_infinity
bool has_no_infinity() const
does the matrix values not given by the user?
Definition: base_matrix.cpp:459
Vroom_break_t
Vehicle's break attributes.
Definition: vroom_break_t.h:46
Vroom_matrix_t
Matrix's attributes.
Definition: vroom_matrix_t.h:46
Vroom_job_t
Job's attributes.
Definition: vroom_job_t.h:54
vrprouting::Vrp_vroom_problem::solve
std::vector< Vroom_rt > solve(int32_t exploration_level, int32_t timeout, int32_t loading_time)
Definition: vrp_vroom_problem.hpp:593
vrprouting::Vrp_vroom_problem::add_shipments
void add_shipments(const std::vector< Vroom_shipment_t > &shipments, const std::vector< Vroom_time_window_t > &shipments_tws)
Definition: vrp_vroom_problem.hpp:262
Vroom_shipment_t::d_location_id
MatrixIndex d_location_id
Pickup service time.
Definition: vroom_shipment_t.h:65
pgr_free
T * pgr_free(T *ptr)
Definition: pgr_alloc.hpp:77
vrprouting::base::Base_Matrix
N x N matrix.
Definition: base_matrix.h:61
Vroom_vehicle_t
Vehicles's attributes.
Definition: vroom_vehicle_t.h:54
Vroom_vehicle_t::start_id
MatrixIndex start_id
The vehicle's identifier.
Definition: vroom_vehicle_t.h:56
Vroom_rt
Solution's attributes.
Definition: vroom_rt.h:57
do_vrp_vroom
void do_vrp_vroom(Vroom_job_t *jobs, size_t total_jobs, Vroom_time_window_t *jobs_tws, size_t total_jobs_tws, Vroom_shipment_t *shipments, size_t total_shipments, Vroom_time_window_t *shipments_tws, size_t total_shipments_tws, Vroom_vehicle_t *vehicles, size_t total_vehicles, Vroom_break_t *breaks, size_t total_breaks, Vroom_time_window_t *breaks_tws, size_t total_breaks_tws, Vroom_matrix_t *matrix_rows, size_t total_matrix_rows, int32_t exploration_level, int32_t timeout, int32_t loading_time, Vroom_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Performs exception handling and converts the results to postgres.
Definition: vroom_driver.cpp:84
Identifiers
Definition: identifiers.hpp:51
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:140