vrpRouting  0.3
compatibleVehicles_driver.cpp
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: compatibleVehicles_driver.cpp
3 
4 Copyright (c) 2015 pgRouting developers
6 
7 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*/
31 
32 #include <cstring>
33 #include <sstream>
34 #include <string>
35 #include <deque>
36 
37 #include "problem/pickDeliver.h"
38 #include "problem/matrix.h"
41 #include "c_types/vehicle_t.h"
42 
43 #include "cpp_common/pgr_assert.h"
44 #include "c_common/pgr_alloc.hpp"
45 
97 void
99  PickDeliveryOrders_t customers_arr[],
100  size_t total_customers,
101 
102  Vehicle_t *vehicles_arr,
103  size_t total_vehicles,
104 
105  Matrix_cell_t *matrix_cells_arr,
106  size_t total_cells,
107 
108  Time_multipliers_t *multipliers_arr,
109  size_t total_multipliers,
110 
111  double factor,
112 
113  CompatibleVehicles_rt **return_tuples,
114  size_t *return_count,
115 
116  char **log_msg,
117  char **notice_msg,
118  char **err_msg) {
119  std::ostringstream log;
120  std::ostringstream notice;
121  std::ostringstream err;
122  try {
123  /*
124  * verify preconditions
125  */
126  pgassert(!(*log_msg));
127  pgassert(!(*notice_msg));
128  pgassert(!(*err_msg));
129  pgassert(total_customers);
130  pgassert(total_vehicles);
131  pgassert(total_vehicles);
132  pgassert(*return_count == 0);
133  pgassert(!(*return_tuples));
134  log << "do_compatibleVehicles\n";
135 
136 
137  Identifiers<Id> node_ids;
138 
139  for (size_t i = 0; i < total_customers; ++i) {
140  node_ids += customers_arr[i].pick_node_id;
141  node_ids += customers_arr[i].deliver_node_id;
142  }
143 
144  for (size_t i = 0; i < total_vehicles; ++i) {
145  node_ids += vehicles_arr[i].start_node_id;
146  node_ids += vehicles_arr[i].end_node_id;
147  }
148 
149  /*
150  * Verify matrix cells preconditions
151  */
152  vrprouting::problem::Matrix cost_matrix(
153  matrix_cells_arr, total_cells,
154  multipliers_arr, total_multipliers,
155  node_ids, static_cast<Multiplier>(factor));
156 #if 0
157  /*
158  * Verify matrix triangle inequality
159  */
160  if (!cost_matrix.obeys_triangle_inequality()) {
161  log << "[Compatible Vehicles] Fixing Matrix that does not obey triangle inequality ";
162  log << cost_matrix.fix_triangle_inequality() << " cycles used";
163 
164  if (!cost_matrix.obeys_triangle_inequality()) {
165  log << "[Compatible Vehicles] Matrix Still does not obey triangle inequality";
166  }
167  }
168 #endif
169  if (!cost_matrix.has_no_infinity()) {
170  err << "An Infinity value was found on the Matrix";
171  *err_msg = pgr_msg(err.str());
172  return;
173  }
174 
175  /*
176  * Construct problem
177  */
178  log << "Initialize problem\n";
180  customers_arr, total_customers,
181  vehicles_arr, total_vehicles,
182  cost_matrix);
183 
184  err << pd_problem.msg.get_error();
185  if (!err.str().empty()) {
186  log << pd_problem.msg.get_log();
187  *log_msg = pgr_msg(log.str());
188  *err_msg = pgr_msg(err.str());
189  return;
190  }
191  log << pd_problem.msg.get_log();
192  log << "Finish Reading data\n";
193  pd_problem.msg.clear();
194 
195  /*
196  * Prepare results
197  */
198  auto solution = pd_problem.get_pg_compatibleVehicles();
199  log << "solution size: " << solution.size() << "\n";
200  log << "solution empty: " << solution.empty() << "\n";
201 
202  if (!solution.empty()) {
203  log << "solution empty " << "\n";
204  (*return_tuples) = pgr_alloc(solution.size(), (*return_tuples));
205  int seq = 0;
206  for (const auto &row : solution) {
207  (*return_tuples)[seq] = row;
208  ++seq;
209  }
210  }
211  (*return_count) = solution.size();
212 
213 
214  pgassert(*err_msg == nullptr);
215  *log_msg = log.str().empty()?
216  nullptr :
217  pgr_msg(log.str());
218  *notice_msg = notice.str().empty()?
219  nullptr :
220  pgr_msg(notice.str());
221  } catch (AssertFailedException &except) {
222  if (*return_tuples) free(*return_tuples);
223  (*return_count) = 0;
224  err << except.what();
225  *err_msg = pgr_msg(err.str());
226  *log_msg = pgr_msg(log.str());
227  } catch (std::exception& except) {
228  if (*return_tuples) free(*return_tuples);
229  (*return_count) = 0;
230  err << except.what();
231  *err_msg = pgr_msg(err.str());
232  *log_msg = pgr_msg(log.str());
233  } catch(...) {
234  if (*return_tuples) free(*return_tuples);
235  (*return_count) = 0;
236  err << "Caught unknown exception!";
237  *err_msg = pgr_msg(err.str());
238  *log_msg = pgr_msg(log.str());
239  }
240 }
PickDeliveryOrders_t::deliver_node_id
Id deliver_node_id
Deliver y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:72
pgr_alloc
T * pgr_alloc(std::size_t size, T *ptr)
allocates memory
Definition: pgr_alloc.hpp:66
Vehicle_t::start_node_id
Id start_node_id
Stops size.
Definition: vehicle_t.h:58
do_compatibleVehicles
void do_compatibleVehicles(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, Time_multipliers_t *multipliers_arr, size_t total_multipliers, double factor, CompatibleVehicles_rt **return_tuples, size_t *return_count, char **log_msg, char **notice_msg, char **err_msg)
Driver for processing a "compatible vehicles" problem.
Definition: compatibleVehicles_driver.cpp:98
pickDeliveryOrders_t.h
vehicle_t.h
Vehicle_t::end_node_id
Id end_node_id
Definition: vehicle_t.h:65
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
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
Time_multipliers_t
Time Dependant Multipliers.
Definition: time_multipliers_t.h:46
vrprouting::problem::PickDeliver
the pick deliver problem
Definition: pickDeliver.h:50
vrprouting::problem::PickDeliver::get_pg_compatibleVehicles
std::vector< CompatibleVehicles_rt > get_pg_compatibleVehicles() const
get the vehicles compatibility results as C++ container
Definition: pickDeliver.h:84
Vehicle_t
vehicles's attributes
Definition: vehicle_t.h:50
compatibleVehicles_rt.h
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
PickDeliveryOrders_t::pick_node_id
Id pick_node_id
Pick y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:64
vrprouting::Pgr_messages::get_log
std::string get_log() const
gets the contents of log message
Definition: pgr_messages.cpp:36
pickDeliver.h
pgr_assert.h
An assert functionality that uses C++ throw().
vrprouting::base::Base_Matrix::has_no_infinity
bool has_no_infinity() const
does the matrix values not given by the user?
Definition: base_matrix.cpp:459
vrprouting::base::Base_Matrix::fix_triangle_inequality
size_t fix_triangle_inequality(size_t depth=0)
Fix Triangle Inequality Theorem.
Definition: base_matrix.cpp:508
vrprouting::Pgr_messages::get_error
std::string get_error() const
gets the contents of error message
Definition: pgr_messages.cpp:53
Matrix_cell_t
traveling costs
Definition: matrix_cell_t.h:41
vrprouting::Pgr_messages::clear
void clear()
Clears all the messages.
Definition: pgr_messages.cpp:59
CompatibleVehicles_rt
order-vehicle compatability relationship
Definition: compatibleVehicles_rt.h:50
Multiplier
double Multiplier
Definition: typedefs.h:77
compatibleVehicles_driver.h
Identifiers
Definition: identifiers.hpp:51
vrprouting::base::Base_Matrix::obeys_triangle_inequality
bool obeys_triangle_inequality() const
does the matrix obeys the triangle inequality?
Definition: base_matrix.cpp:487
AssertFailedException
Extends std::exception and is the exception that we throw if an assert fails.
Definition: pgr_assert.h:140