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

Go to the source code of this file.

Functions

void do_pgr_pickDeliverEuclidean (PickDeliveryOrders_t *customers_arr, size_t total_customers, Vehicle_t *vehicles_arr, size_t total_vehicles, 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_pickDeliverEuclidean()

void do_pgr_pickDeliverEuclidean ( PickDeliveryOrders_t customers_arr,
size_t  total_customers,
Vehicle_t vehicles_arr,
size_t  total_vehicles,
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 53 of file pickDeliverEuclidean_driver.cpp.

69  {
70  std::ostringstream log;
71  std::ostringstream notice;
72  std::ostringstream err;
73  try {
74  *return_tuples = nullptr;
75  *return_count = 0;
76 
77  /*
78  * transform to C++ containers
79  */
80  std::vector<PickDeliveryOrders_t> orders(
81  customers_arr, customers_arr + total_customers);
82  std::vector<Vehicle_t> vehicles(
83  vehicles_arr, vehicles_arr + total_vehicles);
84 
85  std::map<std::pair<double, double>, int64_t> matrix_data;
86 
87  for (const auto &o : orders) {
88  matrix_data[std::pair<double, double>(o.pick_x, o.pick_y)] = o.pick_node_id;
89  matrix_data[std::pair<double, double>(o.deliver_x, o.deliver_y)] = o.deliver_node_id;
90  }
91 
92  for (const auto &v : vehicles) {
93  matrix_data[std::pair<double, double>(v.start_x, v.start_y)] = v.start_node_id;
94  matrix_data[std::pair<double, double>(v.end_x, v.end_y)] = v.end_node_id;
95  }
96 
97  Identifiers<int64_t> unique_ids;
98  for (const auto &e: matrix_data) {
99  unique_ids += e.second;
100  }
101  if (unique_ids.size() != matrix_data.size()) {
102  // ignoring ids given by the user
103  int64_t id(0);
104  for (auto &e: matrix_data) {
105  e.second = id++;
106  }
107  }
108 
109  for (auto &o : orders) {
110  o.pick_node_id = matrix_data[std::pair<double, double>(o.pick_x, o.pick_y)];
111  o.deliver_node_id = matrix_data[std::pair<double, double>(o.deliver_x, o.deliver_y)];
112  }
113  for (auto &v : vehicles) {
114  v.start_node_id = matrix_data[std::pair<double, double>(v.start_x, v.start_y)];
115  v.end_node_id = matrix_data[std::pair<double, double>(v.end_x, v.end_y)];
116  }
117 
118  pgrouting::tsp::Dmatrix cost_matrix(matrix_data);
119 
120  log << "Initialize problem\n";
122  orders,
123  vehicles,
124  cost_matrix,
125  factor,
126  static_cast<size_t>(max_cycles),
127  initial_solution_id);
128 
129  err << pd_problem.msg.get_error();
130  if (!err.str().empty()) {
131  log.str("");
132  log.clear();
133  log << pd_problem.msg.get_error();
134  log << pd_problem.msg.get_log();
135  *log_msg = pgr_msg(log.str().c_str());
136  *err_msg = pgr_msg(err.str().c_str());
137  return;
138  }
139  log << pd_problem.msg.get_log();
140  log << "Finish Reading data\n";
141 
142  try {
143  pd_problem.solve();
144  } catch (AssertFailedException &except) {
145  log << pd_problem.msg.get_log();
146  throw;
147  } catch(...) {
148  log << "Caught unknown exception!";
149  throw;
150  }
151 
152  log << pd_problem.msg.get_log();
153  log << "Finish solve\n";
154 
155  auto solution = pd_problem.get_postgres_result();
156  log << pd_problem.msg.get_log();
157  log << "solution size: " << solution.size() << "\n";
158 
159 
160  if (!solution.empty()) {
161  (*return_tuples) = pgr_alloc(solution.size(), (*return_tuples));
162  int seq = 0;
163  for (const auto &row : solution) {
164  (*return_tuples)[seq] = row;
165  ++seq;
166  }
167  }
168  (*return_count) = solution.size();
169 
170  log << pd_problem.msg.get_log();
171 
172  pgassert(*err_msg == NULL);
173  *log_msg = log.str().empty()?
174  nullptr :
175  pgr_msg(log.str().c_str());
176  *notice_msg = notice.str().empty()?
177  nullptr :
178  pgr_msg(notice.str().c_str());
179  } catch (AssertFailedException &except) {
180  if (*return_tuples) free(*return_tuples);
181  (*return_count) = 0;
182  err << except.what();
183  *err_msg = pgr_msg(err.str().c_str());
184  *log_msg = pgr_msg(log.str().c_str());
185  } catch (std::exception& except) {
186  if (*return_tuples) free(*return_tuples);
187  (*return_count) = 0;
188  err << except.what();
189  *err_msg = pgr_msg(err.str().c_str());
190  *log_msg = pgr_msg(log.str().c_str());
191  } catch (const std::pair<std::string, std::string>& ex) {
192  (*return_count) = 0;
193  err << ex.first;
194  log.str("");
195  log.clear();
196  log << ex.second;
197  *err_msg = pgr_msg(err.str().c_str());
198  *log_msg = pgr_msg(log.str().c_str());
199  } catch(...) {
200  if (*return_tuples) free(*return_tuples);
201  (*return_count) = 0;
202  err << "Caught unknown exception!";
203  *err_msg = pgr_msg(err.str().c_str());
204  *log_msg = pgr_msg(log.str().c_str());
205  }
206 }

References pgrouting::Pgr_messages::get_error(), pgrouting::Pgr_messages::get_log(), pgrouting::vrp::Pgr_pickDeliver::get_postgres_result(), pgrouting::vrp::Pgr_pickDeliver::msg, pgassert, pgr_alloc(), pgr_msg(), Identifiers< T >::size(), 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
Identifiers::size
size_t size() const
Definition: identifiers.hpp:78
pgrouting::vrp::Pgr_pickDeliver
Definition: pgr_pickDeliver.h:57
pgrouting::tsp::Dmatrix
Definition: Dmatrix.h:43
Identifiers< int64_t >
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:139