PGROUTING  3.2
pickDeliver_driver.cpp File Reference
#include "drivers/pickDeliver/pickDeliver_driver.h"
#include <string.h>
#include <sstream>
#include <string>
#include <deque>
#include <vector>
#include "vrp/pgr_pickDeliver.h"
#include "vrp/initials_code.h"
#include "cpp_common/Dmatrix.h"
#include "cpp_common/pgr_assert.h"
#include "cpp_common/pgr_alloc.hpp"
Include dependency graph for pickDeliver_driver.cpp:

Go to the source code of this file.

Functions

void do_pgr_pickDeliver (PickDeliveryOrders_t customers_arr[], size_t total_customers, Vehicle_t *vehicles_arr, size_t total_vehicles, Matrix_cell_t *matrix_cells_arr, size_t total_cells, double factor, int max_cycles, int initial_solution_id, General_vehicle_orders_t **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
 

Function Documentation

◆ do_pgr_pickDeliver()

void do_pgr_pickDeliver ( PickDeliveryOrders_t  customers_arr[],
size_t  total_customers,
Vehicle_t vehicles_arr,
size_t  total_vehicles,
Matrix_cell_t matrix_cells_arr,
size_t  total_cells,
double  factor,
int  max_cycles,
int  initial_solution_id,
General_vehicle_orders_t **  return_tuples,
size_t *  return_count,
char **  log_msg,
char **  notice_msg,
char **  err_msg 
)

Definition at line 47 of file pickDeliver_driver.cpp.

66  {
67  std::ostringstream log;
68  std::ostringstream notice;
69  std::ostringstream err;
70  try {
71  pgassert(!(*log_msg));
72  pgassert(!(*notice_msg));
73  pgassert(!(*err_msg));
74  pgassert(total_customers);
75  pgassert(total_vehicles);
76  pgassert(total_vehicles);
77  pgassert(*return_count == 0);
78  pgassert(!(*return_tuples));
79  log << "do_pgr_pickDeliver\n";
80 
81 
82  /*
83  * transform to C++ containers
84  */
85  std::vector<PickDeliveryOrders_t> orders(
86  customers_arr, customers_arr + total_customers);
87 
88  std::vector<Vehicle_t> vehicles(
89  vehicles_arr, vehicles_arr + total_vehicles);
90 
91  std::vector <Matrix_cell_t> data_costs(
92  matrix_cells_arr,
93  matrix_cells_arr + total_cells);
94 
95  pgrouting::tsp::Dmatrix cost_matrix(data_costs);
96 
97  auto depot_node = vehicles[0].start_node_id;
98 
99  /*
100  * This applies to the one depot problem
101  */
102  if ((pgrouting::vrp::Initials_code)(initial_solution_id) == pgrouting::vrp::OneDepot) {
103  /*
104  * All Vehicles must depart from same location
105  */
106  for (const auto &v : vehicles) {
107  if (v.start_node_id != depot_node && v.end_node_id != depot_node) {
108  err << "All vehicles must depart & arrive to same node";
109  *err_msg = pgr_msg(err.str().c_str());
110  return;
111  }
112  }
113 
114  /*
115  * All Orders must depart from depot
116  */
117  for (const auto &o : orders) {
118  if (o.pick_node_id != depot_node) {
119  err << "All orders must be picked at depot";
120  *err_msg = pgr_msg(err.str().c_str());
121  return;
122  }
123  }
124  }
125 
126  if (!cost_matrix.has_no_infinity()) {
127  err << "An Infinity value was found on the Matrix";
128  *err_msg = pgr_msg(err.str().c_str());
129  return;
130  }
131 
132  log << "Initialize problem\n";
134  orders,
135  vehicles,
136  cost_matrix,
137  factor,
138  static_cast<size_t>(max_cycles),
139  initial_solution_id);
140 
141  err << pd_problem.msg.get_error();
142  if (!err.str().empty()) {
143  log << pd_problem.msg.get_log();
144  *log_msg = pgr_msg(log.str().c_str());
145  *err_msg = pgr_msg(err.str().c_str());
146  return;
147  }
148  log << pd_problem.msg.get_log();
149  log << "Finish Reading data\n";
150  pd_problem.msg.clear();
151 
152 
153  try {
154  pd_problem.solve();
155  } catch (AssertFailedException &except) {
156  log << pd_problem.msg.get_log();
157  pd_problem.msg.clear();
158  throw;
159  } catch(...) {
160  log << "Caught unknown exception!";
161  throw;
162  }
163 
164 
165  log << pd_problem.msg.get_log();
166  log << "Finish solve\n";
167  pd_problem.msg.clear();
168 
169  auto solution = pd_problem.get_postgres_result();
170  log << pd_problem.msg.get_log();
171  pd_problem.msg.clear();
172  log << "solution size: " << solution.size() << "\n";
173 
174 
175  if (!solution.empty()) {
176  (*return_tuples) = pgr_alloc(solution.size(), (*return_tuples));
177  int seq = 0;
178  for (const auto &row : solution) {
179  (*return_tuples)[seq] = row;
180  ++seq;
181  }
182  }
183  (*return_count) = solution.size();
184 
185  pgassert(*err_msg == NULL);
186  *log_msg = log.str().empty()?
187  nullptr :
188  pgr_msg(log.str().c_str());
189  *notice_msg = notice.str().empty()?
190  nullptr :
191  pgr_msg(notice.str().c_str());
192  } catch (AssertFailedException &except) {
193  if (*return_tuples) free(*return_tuples);
194  (*return_count) = 0;
195  err << except.what();
196  *err_msg = pgr_msg(err.str().c_str());
197  *log_msg = pgr_msg(log.str().c_str());
198  } catch (std::exception& except) {
199  if (*return_tuples) free(*return_tuples);
200  (*return_count) = 0;
201  err << except.what();
202  *err_msg = pgr_msg(err.str().c_str());
203  *log_msg = pgr_msg(log.str().c_str());
204  } catch (const std::pair<std::string, std::string>& ex) {
205  (*return_count) = 0;
206  err << ex.first;
207  log.str("");
208  log.clear();
209  log << ex.second;
210  *err_msg = pgr_msg(err.str().c_str());
211  *log_msg = pgr_msg(log.str().c_str());
212  } catch (const std::pair<std::string, int64_t>& ex) {
213  (*return_count) = 0;
214  err << ex.first;
215  log.str("");
216  log.clear();
217  log << "Node missing on matrix: id = " << ex.second;
218  *err_msg = pgr_msg(err.str().c_str());
219  *log_msg = pgr_msg(log.str().c_str());
220  } catch(...) {
221  if (*return_tuples) free(*return_tuples);
222  (*return_count) = 0;
223  err << "Caught unknown exception!";
224  *err_msg = pgr_msg(err.str().c_str());
225  *log_msg = pgr_msg(log.str().c_str());
226  }
227 }

References pgrouting::Pgr_messages::clear(), pgrouting::Pgr_messages::get_error(), pgrouting::Pgr_messages::get_log(), pgrouting::vrp::Pgr_pickDeliver::get_postgres_result(), pgrouting::tsp::Dmatrix::has_no_infinity(), pgrouting::vrp::Pgr_pickDeliver::msg, pgrouting::vrp::OneDepot, pgassert, pgr_alloc(), pgr_msg(), pgrouting::vrp::Pgr_pickDeliver::solve(), and AssertFailedException::what().

Referenced by process().

pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pgr_msg
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
AssertFailedException::what
virtual const char * what() const
Definition: pgr_assert.cpp:67
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgrouting::vrp::OneDepot
@ OneDepot
Push front order that allows more orders to be inserted at the front.
Definition: initials_code.h:44
pgrouting::vrp::Pgr_pickDeliver
Definition: pgr_pickDeliver.h:57
pgrouting::tsp::Dmatrix
Definition: Dmatrix.h:43
pgrouting::vrp::Initials_code
Initials_code
Different kinds to insert an order into the vehicle.
Definition: initials_code.h:36
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139