PGROUTING  2.6-dev
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
6 Mail: project@pgrouting.org
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 "cpp_common/Dmatrix.h"
41 
42 #include "cpp_common/pgr_assert.h"
43 #include "cpp_common/pgr_alloc.hpp"
44 
45 void
47  PickDeliveryOrders_t customers_arr[],
48  size_t total_customers,
49 
50  Vehicle_t *vehicles_arr,
51  size_t total_vehicles,
52 
53  Matrix_cell_t *matrix_cells_arr,
54  size_t total_cells,
55 
56  double factor,
57  int max_cycles,
58  int initial_solution_id,
59 
60  General_vehicle_orders_t **return_tuples,
61  size_t *return_count,
62 
63  char **log_msg,
64  char **notice_msg,
65  char **err_msg) {
66  std::ostringstream log;
67  std::ostringstream notice;
68  std::ostringstream err;
69  try {
70  pgassert(!(*log_msg));
71  pgassert(!(*notice_msg));
72  pgassert(!(*err_msg));
73  pgassert(total_customers);
74  pgassert(total_vehicles);
75  pgassert(total_vehicles);
76  pgassert(*return_count == 0);
77  pgassert(!(*return_tuples));
78  log << "do_pgr_pickDeliver\n";
79 
80 
81  /*
82  * transform to C++ containers
83  */
84  std::vector<PickDeliveryOrders_t> orders(
85  customers_arr, customers_arr + total_customers);
86 
87  std::vector<Vehicle_t> vehicles(
88  vehicles_arr, vehicles_arr + total_vehicles);
89 
90  std::vector <Matrix_cell_t> data_costs(
91  matrix_cells_arr,
92  matrix_cells_arr + total_cells);
93 
94  pgrouting::tsp::Dmatrix cost_matrix(data_costs);
95 
96  if (!cost_matrix.has_no_infinity()) {
97  err << "An Infinity value was found on the Matrix";
98  *err_msg = pgr_msg(err.str().c_str());
99  return;
100  }
101 
102  // TODO(vicky) wrap with a try and make a throw???
103  log << "Initialize problem\n";
105  orders,
106  vehicles,
107  cost_matrix,
108  factor,
109  max_cycles,
110  initial_solution_id);
111 
112  err << pd_problem.msg.get_error();
113  if (!err.str().empty()) {
114  log << pd_problem.msg.get_log();
115  *log_msg = pgr_msg(log.str().c_str());
116  *err_msg = pgr_msg(err.str().c_str());
117  return;
118  }
119  log << pd_problem.msg.get_log();
120  log << "Finish Reading data\n";
121  pd_problem.msg.clear();
122 
123 
124  try {
125  pd_problem.solve();
126  } catch (AssertFailedException &except) {
127  log << pd_problem.msg.get_log();
128  pd_problem.msg.clear();
129  throw except;
130  } catch(...) {
131  log << "Caught unknown exception!";
132  throw;
133  }
134 
135 
136  log << pd_problem.msg.get_log();
137  log << "Finish solve\n";
138  pd_problem.msg.clear();
139 
140  auto solution = pd_problem.get_postgres_result();
141  log << pd_problem.msg.get_log();
142  pd_problem.msg.clear();
143  log << "solution size: " << solution.size() << "\n";
144 
145 
146  if (!solution.empty()) {
147  (*return_tuples) = pgr_alloc(solution.size(), (*return_tuples));
148  int seq = 0;
149  for (const auto &row : solution) {
150  (*return_tuples)[seq] = row;
151  ++seq;
152  }
153  }
154  (*return_count) = solution.size();
155 
156  pgassert(*err_msg == NULL);
157  *log_msg = log.str().empty()?
158  nullptr :
159  pgr_msg(log.str().c_str());
160  *notice_msg = notice.str().empty()?
161  nullptr :
162  pgr_msg(notice.str().c_str());
163  } catch (AssertFailedException &except) {
164  if (*return_tuples) free(*return_tuples);
165  (*return_count) = 0;
166  err << except.what();
167  *err_msg = pgr_msg(err.str().c_str());
168  *log_msg = pgr_msg(log.str().c_str());
169  } catch (std::exception& except) {
170  if (*return_tuples) free(*return_tuples);
171  (*return_count) = 0;
172  err << except.what();
173  *err_msg = pgr_msg(err.str().c_str());
174  *log_msg = pgr_msg(log.str().c_str());
175  } catch(...) {
176  if (*return_tuples) free(*return_tuples);
177  (*return_count) = 0;
178  err << "Caught unknown exception!";
179  *err_msg = pgr_msg(err.str().c_str());
180  *log_msg = pgr_msg(log.str().c_str());
181  }
182 }
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:126
std::string get_log() const
get_log
std::string get_error() const
get_error
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)
#define pgassert(expr)
Uses the standard assert syntax.
Definition: pgr_assert.h:81
bool has_no_infinity() const
Definition: Dmatrix.cpp:121
char * pgr_msg(const std::string &msg)
Definition: pgr_alloc.cpp:30
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Assertions Handling.
static Pgr_messages msg
Definition: pd_problem.h:48
virtual const char * what() const
Definition: pgr_assert.cpp:53
std::vector< General_vehicle_orders_t > get_postgres_result() const