PGROUTING  3.2
pickDeliverEuclidean_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 #include <map>
39 
40 #include "vrp/pgr_pickDeliver.h"
41 
42 #include "cpp_common/pgr_assert.h"
43 #include "cpp_common/pgr_alloc.hpp"
44 
45 /************************************************************
46  customers_sql TEXT,
47  max_vehicles INTEGER,
48  factor FLOAT,
49  capacity FLOAT,
50  max_cycles INTEGER,
51  ***********************************************************/
52 void
54  PickDeliveryOrders_t *customers_arr,
55  size_t total_customers,
56 
57  Vehicle_t *vehicles_arr,
58  size_t total_vehicles,
59 
60  double factor,
61  int max_cycles,
62  int initial_solution_id,
63 
64  General_vehicle_orders_t **return_tuples,
65  size_t *return_count,
66 
67  char **log_msg,
68  char **notice_msg,
69  char **err_msg) {
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 }
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
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::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
Identifiers::size
size_t size() const
Definition: identifiers.hpp:78
pgr_alloc.hpp
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: pickDeliverEuclidean_driver.cpp:53
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
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
General_vehicle_orders_t
Definition: general_vehicle_orders_t.h:49
pgrouting::vrp::Pgr_pickDeliver::solve
void solve()
Definition: pgr_pickDeliver.cpp:51
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
pickDeliverEuclidean_driver.h