vrpRouting  0.3
pickDeliverEuclidean.c
Go to the documentation of this file.
1 /*PGR-GNU*****************************************************************
2 File: pickDeliverEuclidean.c
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 
29 #include "utils/array.h"
30 
31 #include "c_common/debug_macro.h"
32 #include "c_common/e_report.h"
33 #include "c_common/time_msg.h"
34 #include "c_common/orders_input.h"
37 #include "c_types/solution_rt.h"
38 
40 
41 PGDLLEXPORT Datum _vrp_pgr_pickdelivereuclidean(PG_FUNCTION_ARGS);
43 
44 
45 static
46 void
48  char* pd_orders_sql,
49  char* vehicles_sql,
50  double factor,
51  int max_cycles,
52  int initial_solution_id,
53  Solution_rt **result_tuples,
54  size_t *result_count) {
55  if (factor <= 0) {
56  ereport(ERROR,
57  (errcode(ERRCODE_INTERNAL_ERROR),
58  errmsg("Illegal value in parameter: factor"),
59  errhint("Value found: %f <= 0", factor)));
60  (*result_count) = 0;
61  (*result_tuples) = NULL;
62  return;
63  }
64 
65  if (max_cycles < 0) {
66  ereport(ERROR,
67  (errcode(ERRCODE_INTERNAL_ERROR),
68  errmsg("Illegal value in parameter: max_cycles"),
69  errhint("Negative value found: max_cycles: %d ", max_cycles)));
70  (*result_count) = 0;
71  (*result_tuples) = NULL;
72  return;
73  }
74 
75  if (initial_solution_id <= 0 || initial_solution_id > 6) {
76  elog(ERROR, "Illegal value in parameter: initial_sol");
77  (*result_count) = 0;
78  (*result_tuples) = NULL;
79  return;
80  }
81 
83 
84  PGR_DBG("Load orders");
85  struct PickDeliveryOrders_t *pd_orders_arr = NULL;
86  size_t total_pd_orders = 0;
87  get_shipments_euclidean(pd_orders_sql,
88  &pd_orders_arr, &total_pd_orders);
89 
90  PGR_DBG("Load vehicles");
91  Vehicle_t *vehicles_arr = NULL;
92  size_t total_vehicles = 0;
93  get_vehicles_euclidean(vehicles_sql,
94  &vehicles_arr, &total_vehicles,
95  false);
96  PGR_DBG("total vehicles %ld", total_vehicles);
97 
98 #if 0
99  for (size_t i = 0; i < total_pd_orders; i++) {
100  PGR_DBG("%ld %f pick %f %f %ld - "
101  "%ld %ld %ld deliver %f %f %ld - %ld %ld %ld ",
102  pd_orders_arr[i].id,
103  pd_orders_arr[i].demand,
104 
105  pd_orders_arr[i].pick_x,
106  pd_orders_arr[i].pick_y,
107  pd_orders_arr[i].pick_node_id,
108 
109  pd_orders_arr[i].pick_open_t,
110  pd_orders_arr[i].pick_close_t,
111  pd_orders_arr[i].pick_service_t,
112 
113  pd_orders_arr[i].deliver_x,
114  pd_orders_arr[i].deliver_y,
115  pd_orders_arr[i].deliver_node_id,
116 
117  pd_orders_arr[i].deliver_open_t,
118  pd_orders_arr[i].deliver_close_t,
119  pd_orders_arr[i].deliver_service_t);
120  }
121 
122 
123 
124  for (size_t i = 0; i < total_vehicles; i++) {
125  PGR_DBG("%ld %f %f , start %f %f %ld %ld %ld "
126  "end %f %f %ld %ld %ld number %ld ",
127  vehicles_arr[i].id,
128  vehicles_arr[i].capacity,
129  vehicles_arr[i].speed,
130 
131  vehicles_arr[i].start_x,
132  vehicles_arr[i].start_y,
133  vehicles_arr[i].start_open_t,
134  vehicles_arr[i].start_close_t,
135  vehicles_arr[i].start_service_t,
136 
137  vehicles_arr[i].end_x,
138  vehicles_arr[i].end_y,
139  vehicles_arr[i].end_open_t,
140  vehicles_arr[i].end_close_t,
141  vehicles_arr[i].end_service_t,
142 
143  vehicles_arr[i].cant_v);
144  }
145 #endif
146 
147  if (total_pd_orders == 0 || total_vehicles == 0) {
148  (*result_count) = 0;
149  (*result_tuples) = NULL;
150  pgr_SPI_finish();
151  return;
152  }
153  PGR_DBG("Total %ld orders in query:", total_pd_orders);
154 
155  PGR_DBG("Starting processing");
156  clock_t start_t = clock();
157  char *log_msg = NULL;
158  char *notice_msg = NULL;
159  char *err_msg = NULL;
161  pd_orders_arr, total_pd_orders,
162  vehicles_arr, total_vehicles,
163 
164  factor,
165  max_cycles,
166  initial_solution_id,
167 
168  result_tuples,
169  result_count,
170 
171  &log_msg,
172  &notice_msg,
173  &err_msg);
174  time_msg("_pgr_pickDeliverEuclidean", start_t, clock());
175 
176  if (err_msg && (*result_tuples)) {
177  pfree(*result_tuples);
178  (*result_count) = 0;
179  (*result_tuples) = NULL;
180  }
181 
182  pgr_global_report(log_msg, notice_msg, err_msg);
183 
184  if (log_msg) pfree(log_msg);
185  if (notice_msg) pfree(notice_msg);
186  if (err_msg) pfree(err_msg);
187  if (pd_orders_arr) pfree(pd_orders_arr);
188  if (vehicles_arr) pfree(vehicles_arr);
189 
190  pgr_SPI_finish();
191 }
192 /* */
193 /******************************************************************************/
194 
195 PGDLLEXPORT Datum
196 _vrp_pgr_pickdelivereuclidean(PG_FUNCTION_ARGS) {
197  FuncCallContext *funcctx;
198  TupleDesc tuple_desc;
199 
200  /**************************************************************************/
201  /* MODIFY AS NEEDED */
202  /* */
203  Solution_rt *result_tuples = 0;
204  size_t result_count = 0;
205  /* */
206  /**************************************************************************/
207 
208  if (SRF_IS_FIRSTCALL()) {
209  MemoryContext oldcontext;
210  funcctx = SRF_FIRSTCALL_INIT();
211  oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
212 
213  /**********************************************************************/
214  /* MODIFY AS NEEDED */
215  /*
216  orders_sql TEXT,
217  vehicles_sql INTEGER,
218  max_cycles INTEGER,
219  initial_id INTEGER,
220  **********************************************************************/
221 
222  PGR_DBG("Calling process");
223  process(
224  text_to_cstring(PG_GETARG_TEXT_P(0)),
225  text_to_cstring(PG_GETARG_TEXT_P(1)),
226  PG_GETARG_FLOAT8(2),
227  PG_GETARG_INT32(3),
228  PG_GETARG_INT32(4),
229  &result_tuples,
230  &result_count);
231 
232  /* */
233  /*********************************************************************/
234 
235  funcctx->max_calls = result_count;
236  funcctx->user_fctx = result_tuples;
237  if (get_call_result_type(fcinfo, NULL, &tuple_desc)
238  != TYPEFUNC_COMPOSITE) {
239  ereport(ERROR,
240  (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
241  errmsg("function returning record called in context "
242  "that cannot accept type record")));
243  }
244 
245  funcctx->tuple_desc = tuple_desc;
246  MemoryContextSwitchTo(oldcontext);
247  }
248 
249  funcctx = SRF_PERCALL_SETUP();
250  tuple_desc = funcctx->tuple_desc;
251  result_tuples = (Solution_rt*) funcctx->user_fctx;
252 
253  if (funcctx->call_cntr < funcctx->max_calls) {
254  HeapTuple tuple;
255  Datum result;
256  Datum *values;
257  bool* nulls;
258  size_t call_cntr = funcctx->call_cntr;
259 
260  /*********************************************************************/
261  /* MODIFY!!!!! */
262  /* This has to match you output otherwise the server crashes */
263  /*
264  OUT seq INTEGER,
265  OUT vehicle_id INTEGER,
266  OUT vehicle_seq INTEGER,
267  OUT order_id BIGINT,
268  OUT cost FLOAT,
269  OUT agg_cost FLOAT
270  *********************************************************************/
271 
272 
273  size_t numb = 12;
274  values = palloc(numb * sizeof(Datum));
275  nulls = palloc(numb * sizeof(bool));
276 
277  size_t i;
278  for (i = 0; i < numb; ++i) {
279  nulls[i] = false;
280  }
281 
282 
283  // postgres starts counting from 1
284  values[0] = Int32GetDatum(funcctx->call_cntr + 1);
285  values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq);
286  values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id);
287  values[3] = Int32GetDatum(result_tuples[call_cntr].stop_seq);
288  values[4] = Int32GetDatum(result_tuples[call_cntr].stop_type + 1);
289  values[5] = Int64GetDatum(result_tuples[call_cntr].order_id);
290  values[6] = Int64GetDatum(result_tuples[call_cntr].cargo);
291  values[7] = Int64GetDatum(result_tuples[call_cntr].travelTime);
292  values[8] = Int64GetDatum(result_tuples[call_cntr].arrivalTime);
293  values[9] = Int64GetDatum(result_tuples[call_cntr].waitDuration);
294  values[10] = Int64GetDatum(result_tuples[call_cntr].serviceDuration);
295  values[11] = Int64GetDatum(result_tuples[call_cntr].departureTime);
296 
297  /*********************************************************************/
298 
299  tuple = heap_form_tuple(tuple_desc, values, nulls);
300  result = HeapTupleGetDatum(tuple);
301  SRF_RETURN_NEXT(funcctx, result);
302  } else {
303  SRF_RETURN_DONE(funcctx);
304  }
305 }
PickDeliveryOrders_t::deliver_node_id
Id deliver_node_id
Deliver y coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:72
PickDeliveryOrders_t::deliver_x
Coordinate deliver_x
Pickup service duration.
Definition: pickDeliveryOrders_t.h:70
PickDeliveryOrders_t::pick_x
Coordinate pick_x
Number of demand.
Definition: pickDeliveryOrders_t.h:62
time_msg.h
pickDeliveryOrders_t.h
postgres_connection.h
PickDeliveryOrders_t
order's attributes
Definition: pickDeliveryOrders_t.h:56
pgr_SPI_connect
void pgr_SPI_connect(void)
Definition: postgres_connection.c:79
get_vehicles_euclidean
void get_vehicles_euclidean(char *sql, Vehicle_t **rows, size_t *total_rows, bool with_stops)
Reads the vehicles information.
Definition: vehicles_input.c:569
pgr_SPI_finish
void pgr_SPI_finish(void)
Definition: postgres_connection.c:71
e_report.h
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
_vrp_pgr_pickdelivereuclidean
PGDLLEXPORT Datum _vrp_pgr_pickdelivereuclidean(PG_FUNCTION_ARGS)
Definition: pickDeliverEuclidean.c:196
orders_input.h
vehicles_input.h
debug_macro.h
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
PGR_DBG
#define PGR_DBG(...)
Definition: debug_macro.h:34
PG_FUNCTION_INFO_V1
PG_FUNCTION_INFO_V1(_vrp_pgr_pickdelivereuclidean)
Solution_rt
Solution schedule when twv & cw are hard restrictions.
Definition: solution_rt.h:56
if
if(DOXYGEN_FOUND) configure_file($
Definition: doxygen/CMakeLists.txt:13
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
time_msg
void time_msg(char *msg, clock_t start_t, clock_t end_t)
Definition: time_msg.c:32
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
PickDeliveryOrders_t::pick_y
Coordinate pick_y
Pick x coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:63
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
get_shipments_euclidean
void get_shipments_euclidean(char *sql, PickDeliveryOrders_t **rows, size_t *total_rows)
Reads the pick-Deliver shipments for euclidean information.
Definition: orders_input.c:414
PickDeliveryOrders_t::deliver_y
Coordinate deliver_y
Deliver x coordinate: used in stand alone program for benchmarks.
Definition: pickDeliveryOrders_t.h:71
process
static void process(char *pd_orders_sql, char *vehicles_sql, double factor, int max_cycles, int initial_solution_id, Solution_rt **result_tuples, size_t *result_count)
Definition: pickDeliverEuclidean.c:47
pgr_global_report
void pgr_global_report(char *log, char *notice, char *err)
notice & error
Definition: e_report.c:30
PickDeliveryOrders_t::demand
PAmount demand
Order's identifier.
Definition: pickDeliveryOrders_t.h:60
pickDeliverEuclidean_driver.h