vrpRouting  0.3
vroom_driver.h File Reference
#include "c_types/typedefs.h"
Include dependency graph for vroom_driver.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Functions

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_cells_arr, size_t total_cells, 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. More...
 

Function Documentation

◆ 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.

Precondition
log_msg is empty
notice_msg is empty
err_msg is empty
return_tuples is empty
return_count is 0

It converts the C types to the C++ types, and passes these variables to the function vrp_vroom which calls the main function defined in the C++ Header file. It also does exception handling.

Parameters
jobsPointer to the array of jobs
total_jobsThe total number of jobs
jobs_twsPointer to the array of jobs time windows
total_jobs_twsThe total number of jobs time windows
shipmentsPointer to the array of shipments
total_shipmentsThe total number of shipments
shipments_twsPointer to the array of shipments time windows
total_shipments_twsThe total number of shipments time windows
vehiclesPointer to the array of vehicles
total_vehiclesThe total number of total vehicles
breaksPointer to the array of breaks
total_breaksThe total number of total breaks
matrix_rowsPointer to the array of matrix rows
total_matrix_rowsThe total number of matrix rows
exploration_levelExploration level to use while solving.
timeoutTimeout value to stop the solving process (seconds).
loading_timeoutAdditional time spent in loading the data from SQL Query (seconds).
result_tuplesThe rows in the result
result_countThe count of rows in the result
log_msgStores the log message
notice_msgStores the notice message
err_msgStores the error message
Returns
void

Definition at line 84 of file vroom_driver.cpp.

103  {
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 }

References vrprouting::Vrp_vroom_problem::add_jobs(), vrprouting::Vrp_vroom_problem::add_matrix(), vrprouting::Vrp_vroom_problem::add_shipments(), vrprouting::Vrp_vroom_problem::add_vehicles(), Vroom_shipment_t::d_location_id, Vroom_vehicle_t::end_id, vrprouting::base::Base_Matrix::has_no_infinity(), Vroom_job_t::location_id, Vroom_shipment_t::p_location_id, pgassert, pgr_alloc(), pgr_free(), pgr_msg(), vrprouting::base::Base_Matrix::size(), vrprouting::Vrp_vroom_problem::solve(), Vroom_vehicle_t::speed_factor, Vroom_vehicle_t::start_id, and AssertFailedException::what().

Referenced by process().

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
vrprouting::Vrp_vroom_problem
Definition: vrp_vroom_problem.hpp:54
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
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
Vroom_job_t::location_id
MatrixIndex location_id
The job's identifier.
Definition: vroom_job_t.h:56
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::start_id
MatrixIndex start_id
The vehicle's identifier.
Definition: vroom_vehicle_t.h:56
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