PGROUTING  3.2
pickDeliver_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pickDeliver_driver.cpp
3 
4 Generated with Template by:
5 Copyright (c) 2015 pgRouting developers
7 
8 Function's developer:
9 Copyright (c) 2015 Celia Virginia Vergara Castillo
10 Mail:
11 
12 ------
13 
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
18 
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23 
24 You should have received a copy of the GNU General Public License
25 along with this program; if not, write to the Free Software
26 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 
28  ********************************************************************PGR-GNU*/
29 
30 
32 
33 #include <string.h>
34 #include <sstream>
35 #include <string>
36 #include <deque>
37 #include <vector>
38 
39 #include "vrp/pgr_pickDeliver.h"
40 #include "vrp/initials_code.h"
41 #include "cpp_common/Dmatrix.h"
42 
43 #include "cpp_common/pgr_assert.h"
44 #include "cpp_common/pgr_alloc.hpp"
45 
46 void
48  PickDeliveryOrders_t customers_arr[],
49  size_t total_customers,
50 
51  Vehicle_t *vehicles_arr,
52  size_t total_vehicles,
53 
54  Matrix_cell_t *matrix_cells_arr,
55  size_t total_cells,
56 
57  double factor,
58  int max_cycles,
59  int initial_solution_id,
60 
61  General_vehicle_orders_t **return_tuples,
62  size_t *return_count,
63 
64  char **log_msg,
65  char **notice_msg,
66  char **err_msg) {
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 }
pgr_pickDeliver.h
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
PickDeliveryOrders_t
Definition: pickDeliveryOrders_t.h:43
initials_code.h
pgrouting::vrp::Pgr_pickDeliver::msg
Pgr_messages msg
message controller for all classes
Definition: pgr_pickDeliver.h:99
Vehicle_t
Definition: vehicle_t.h:40
pgrouting::tsp::Dmatrix::has_no_infinity
bool has_no_infinity() const
Definition: Dmatrix.cpp:162
pgrouting::Pgr_messages::get_error
std::string get_error() const
get_error
Definition: pgr_messages.cpp:53
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:94
pgr_alloc.hpp
pgrouting::vrp::OneDepot
@ OneDepot
Push front order that allows more orders to be inserted at the front.
Definition: initials_code.h:44
pickDeliver_driver.h
pgrouting::vrp::Pgr_pickDeliver
Definition: pgr_pickDeliver.h:57
pgrouting::Pgr_messages::get_log
std::string get_log() const
get_log
Definition: pgr_messages.cpp:36
pgrouting::Pgr_messages::clear
void clear()
clear
Definition: pgr_messages.cpp:59
pgr_assert.h
An assert functionality that uses C++ throw().
pgrouting::vrp::Pgr_pickDeliver::get_postgres_result
std::vector< General_vehicle_orders_t > get_postgres_result() const
Definition: pgr_pickDeliver.cpp:94
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: pickDeliver_driver.cpp:47
General_vehicle_orders_t
Definition: general_vehicle_orders_t.h:49
Dmatrix.h
pgrouting::vrp::Pgr_pickDeliver::solve
void solve()
Definition: pgr_pickDeliver.cpp:51
matrix_cell
Definition: matrix_cell_t.h:37
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