vrpRouting  0.3
pickDeliverEuclidean_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pickDeliver_driver.cpp
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 Function's developer:
8 Copyright (c) 2015 Celia Virginia Vergara Castillo
9 
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 
28 
30 
31 #include <sstream>
32 #include <utility>
33 #include <string>
34 #include <deque>
35 #include <vector>
36 #include <map>
37 
39 #include "c_types/solution_rt.h"
40 #include "c_common/pgr_alloc.hpp"
41 #include "cpp_common/pgr_assert.h"
42 
43 #include "problem/matrix.h"
44 #include "initialsol/simple.h"
45 #include "optimizers/simple.h"
46 #include "problem/pickDeliver.h"
47 
48 namespace {
50 get_initial_solution(vrprouting::problem::PickDeliver* problem_ptr, int m_initial_id) {
51  using Solution = vrprouting::problem::Solution;
52  using Initial_solution = vrprouting::initialsol::simple::Initial_solution;
54  Solution m_solutions(problem_ptr);
55  if (m_initial_id == 0) {
56  for (int i = 1; i < 7; ++i) {
57  if (i == 1) {
58  m_solutions = Initial_solution((Initials_code)i, problem_ptr);
59  } else {
60  auto new_sol = Initial_solution((Initials_code)i, problem_ptr);
61  m_solutions = (new_sol < m_solutions)? new_sol : m_solutions;
62  }
63  }
64  } else {
65  m_solutions = Initial_solution((Initials_code)m_initial_id, problem_ptr);
66  }
67 
68  return m_solutions;
69 }
70 
71 bool
73  PickDeliveryOrders_t *customers_arr,
74  size_t total_customers,
75  std::string *err_string,
76  std::string *hint_string) {
84  for (size_t i = 0; i < total_customers; ++i) {
85  if (customers_arr[i].demand == 0) {
86  *err_string = "Unexpected zero value found on column 'demand' of shipments";
87  *hint_string = "Check shipment id #:" + std::to_string(customers_arr[i].id);
88  return false;
89  }
90 
91  if (customers_arr[i].pick_service_t < 0) {
92  *err_string = "Unexpected negative value found on column 'p_service_t' of shipments";
93  *hint_string = "Check shipment id #:" + std::to_string(customers_arr[i].id);
94  return false;
95  }
96 
97  if (customers_arr[i].deliver_service_t < 0) {
98  *err_string = "Unexpected negative value found on column 'd_service_t' of shipments";
99  *hint_string = "Check shipment id #:" + std::to_string(customers_arr[i].id);
100  return false;
101  }
102 
103  if (customers_arr[i].pick_open_t > customers_arr[i].pick_close_t) {
104  *err_string = "Unexpected pickup time windows found on shipments";
105  *hint_string = "Check shipment id #:" + std::to_string(customers_arr[i].id);
106  return false;
107  }
108 
109  if (customers_arr[i].deliver_open_t > customers_arr[i].deliver_close_t) {
110  *err_string = "Unexpected delivery time windows found on shipments";
111  *hint_string = "Check shipment id #:" + std::to_string(customers_arr[i].id);
112  return false;
113  }
114  }
115  return true;
116 }
117 
118 } // namespace
119 
120 void
122  PickDeliveryOrders_t *customers_arr,
123  size_t total_customers,
124 
125  Vehicle_t *vehicles_arr,
126  size_t total_vehicles,
127 
128  double factor,
129  int max_cycles,
130  int initial_solution_id,
131 
132  Solution_rt **return_tuples,
133  size_t *return_count,
134 
135  char **log_msg,
136  char **notice_msg,
137  char **err_msg) {
138  std::ostringstream log;
139  std::ostringstream notice;
140  std::ostringstream err;
141  try {
142  *return_tuples = nullptr;
143  *return_count = 0;
144  std::string err_string;
145  std::string hint_string;
146  if (!are_shipments_ok(customers_arr, total_customers, &err_string, &hint_string)) {
147  *err_msg = pgr_msg(err_string.c_str());
148  *log_msg = pgr_msg(hint_string.c_str());
149  return;
150  }
151 
152  /*
153  * transform to C++ containers
154  */
155  std::vector<PickDeliveryOrders_t> orders(
156  customers_arr, customers_arr + total_customers);
157  std::vector<Vehicle_t> vehicles(
158  vehicles_arr, vehicles_arr + total_vehicles);
159 
160  std::map<std::pair<Coordinate, Coordinate>, Id> matrix_data;
161 
162  for (const auto &o : orders) {
163  pgassert(o.pick_node_id == 0);
164  pgassert(o.deliver_node_id == 0);
165  matrix_data[std::pair<Coordinate, Coordinate>(o.pick_x, o.pick_y)] = 0;
166  matrix_data[std::pair<Coordinate, Coordinate>(o.deliver_x, o.deliver_y)] = 0;
167  }
168 
169  for (const auto &v : vehicles) {
170  matrix_data[std::pair<Coordinate, Coordinate>(v.start_x, v.start_y)] = 0;
171  matrix_data[std::pair<Coordinate, Coordinate>(v.end_x, v.end_y)] = 0;
172  }
173 
174  Identifiers<int64_t> unique_ids;
175  /*
176  * Data does not have ids for the locations'
177  */
178  Id id(0);
179  for (auto &e : matrix_data) {
180  e.second = id++;
181  }
182 
183  for (const auto &e : matrix_data) {
184  unique_ids += e.second;
185  log << e.second << "(" << e.first.first << "," << e.first.second << ")\n";
186  }
187 
188  for (size_t i = 0; i < total_customers; ++i) {
189  customers_arr[i].pick_node_id =
190  matrix_data[std::pair<Coordinate, Coordinate>(customers_arr[i].pick_x, customers_arr[i].pick_y)];
191 
192  customers_arr[i].deliver_node_id =
193  matrix_data[std::pair<Coordinate, Coordinate>(customers_arr[i].deliver_x, customers_arr[i].deliver_y)];
194  }
195  for (auto &v : vehicles) {
196  v.start_node_id = matrix_data[std::pair<Coordinate, Coordinate>(v.start_x, v.start_y)];
197  v.end_node_id = matrix_data[std::pair<Coordinate, Coordinate>(v.end_x, v.end_y)];
198  }
199 
200  vrprouting::problem::Matrix cost_matrix(matrix_data, static_cast<Multiplier>(factor));
201 
202  log << "Initialize problem\n";
204  customers_arr, total_customers,
205  vehicles_arr, total_vehicles,
206  cost_matrix);
207 
208  err << pd_problem.msg.get_error();
209  if (!err.str().empty()) {
210  log.str("");
211  log.clear();
212  log << pd_problem.msg.get_error();
213  log << pd_problem.msg.get_log();
214  *log_msg = pgr_msg(log.str().c_str());
215  *err_msg = pgr_msg(err.str().c_str());
216  return;
217  }
218  log << pd_problem.msg.get_log();
219  log << "Finish Reading data\n";
220 
221 #if 0
222  try {
223 #endif
224  auto sol = get_initial_solution(&pd_problem, initial_solution_id);
227  sol = Optimize(sol, static_cast<size_t>(max_cycles), (Initials_code)initial_solution_id);
228 #if 0
229  } catch (AssertFailedException &except) {
230  log << pd_problem.msg.get_log();
231  throw;
232  } catch(...) {
233  log << "Caught unknown exception!";
234  throw;
235  }
236 #endif
237  log << pd_problem.msg.get_log();
238  log << "Finish solve\n";
239 
240  auto solution = sol.get_postgres_result();
241  log << pd_problem.msg.get_log();
242  log << "solution size: " << solution.size() << "\n";
243 
244 
245  if (!solution.empty()) {
246  (*return_tuples) = pgr_alloc(solution.size(), (*return_tuples));
247  int seq = 0;
248  for (const auto &row : solution) {
249  (*return_tuples)[seq] = row;
250  ++seq;
251  }
252  }
253  (*return_count) = solution.size();
254 
255  log << pd_problem.msg.get_log();
256 
257  pgassert(*err_msg == NULL);
258  *log_msg = log.str().empty()?
259  nullptr :
260  pgr_msg(log.str().c_str());
261  *notice_msg = notice.str().empty()?
262  nullptr :
263  pgr_msg(notice.str().c_str());
264  } catch (AssertFailedException &except) {
265  if (*return_tuples) free(*return_tuples);
266  (*return_count) = 0;
267  err << except.what();
268  *err_msg = pgr_msg(err.str().c_str());
269  *log_msg = pgr_msg(log.str().c_str());
270  } catch (std::exception& except) {
271  if (*return_tuples) free(*return_tuples);
272  (*return_count) = 0;
273  err << except.what();
274  *err_msg = pgr_msg(err.str().c_str());
275  *log_msg = pgr_msg(log.str().c_str());
276  } catch (const std::pair<std::string, std::string>& ex) {
277  (*return_count) = 0;
278  err << ex.first;
279  log.str("");
280  log.clear();
281  log << ex.second;
282  *err_msg = pgr_msg(err.str().c_str());
283  *log_msg = pgr_msg(log.str().c_str());
284  } catch(...) {
285  if (*return_tuples) free(*return_tuples);
286  (*return_count) = 0;
287  err << "Caught unknown exception!";
288  *err_msg = pgr_msg(err.str().c_str());
289  *log_msg = pgr_msg(log.str().c_str());
290  }
291 }
PickDeliveryOrders_t::deliver_node_id
Id deliver_node_id
Deliver y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:72
vrprouting::problem::Solution
Definition: solution.h:50
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
pickDeliveryOrders_t.h
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::initialsol::simple::Initials_code
Initials_code
Different kinds to insert an order into the vehicle.
Definition: initialsol/initials_code.h:37
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
simple.h
vrprouting::problem::PickDeliver
the pick deliver problem
Definition: pickDeliver.h:50
PickDeliveryOrders_t::deliver_close_t
TTimestamp deliver_close_t
Deliver open time.
Definition: pickDeliveryOrders_t.h:75
Vehicle_t
vehicles's attributes
Definition: vehicle_t.h:50
pgassert
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:95
matrix.h
vrprouting::problem::PickDeliver::msg
Pgr_messages msg
message controller for all classes
Definition: pickDeliver.h:99
pgr_alloc.hpp
vrprouting::problem::Matrix
Definition: matrix.h:46
Id
int64_t Id
Definition: typedefs.h:78
PickDeliveryOrders_t::pick_node_id
Id pick_node_id
Pick y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:64
PickDeliveryOrders_t::pick_open_t
TTimestamp pick_open_t
Pickup node identifier.
Definition: pickDeliveryOrders_t.h:66
vrprouting::Pgr_messages::get_log
std::string get_log() const
gets the contents of log message
Definition: pgr_messages.cpp:36
pickDeliver.h
Solution_rt
Solution schedule when twv & cw are hard restrictions.
Definition: solution_rt.h:56
anonymous_namespace{pickDeliverEuclidean_driver.cpp}::are_shipments_ok
bool are_shipments_ok(PickDeliveryOrders_t *customers_arr, size_t total_customers, std::string *err_string, std::string *hint_string)
Definition: pickDeliverEuclidean_driver.cpp:72
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::initialsol::simple::Initial_solution
Definition: initialsol/simple.h:45
vrprouting::Pgr_messages::get_error
std::string get_error() const
gets the contents of error message
Definition: pgr_messages.cpp:53
PickDeliveryOrders_t::pick_service_t
TInterval pick_service_t
Pickup close time.
Definition: pickDeliveryOrders_t.h:68
PickDeliveryOrders_t::deliver_open_t
TTimestamp deliver_open_t
Deliver node identifier.
Definition: pickDeliveryOrders_t.h:74
PickDeliveryOrders_t::deliver_service_t
TInterval deliver_service_t
Deliver close time.
Definition: pickDeliveryOrders_t.h:76
solution_rt.h
PickDeliveryOrders_t::pick_close_t
TTimestamp pick_close_t
Pickup open time.
Definition: pickDeliveryOrders_t.h:67
simple.h
Multiplier
double Multiplier
Definition: typedefs.h:77
vrprouting::optimizers::simple::Optimize
Definition: optimizers/simple.h:40
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, Solution_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Definition: pickDeliverEuclidean_driver.cpp:121
anonymous_namespace{pickDeliverEuclidean_driver.cpp}::get_initial_solution
vrprouting::problem::Solution get_initial_solution(vrprouting::problem::PickDeliver *problem_ptr, int m_initial_id)
Definition: pickDeliverEuclidean_driver.cpp:50
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
PickDeliveryOrders_t::demand
PAmount demand
Order's identifier.
Definition: pickDeliveryOrders_t.h:60
pickDeliverEuclidean_driver.h