52 int initial_solution_id,
55 size_t *result_count) {
58 (errcode(ERRCODE_INTERNAL_ERROR),
59 errmsg(
"Illegal value in parameter: factor"),
60 errhint(
"Value found: %f <= 0", factor)));
62 (*result_tuples) = NULL;
67 elog(ERROR,
"Illegal value in parameter: max_cycles");
69 (*result_tuples) = NULL;
73 if (initial_solution_id < 0 || initial_solution_id > 7) {
74 elog(ERROR,
"Illegal value in parameter: initial");
76 (*result_tuples) = NULL;
84 size_t total_pd_orders = 0;
86 &pd_orders_arr, &total_pd_orders);
90 size_t total_vehicles = 0;
92 &vehicles_arr, &total_vehicles);
93 PGR_DBG(
"total vehicles %ld", total_vehicles);
97 for (
size_t i = 0; i < total_pd_orders; i++) {
98 PGR_DBG(
"%ld %f pick %f %f %ld - "
99 "%f %f %f deliver %f %f %ld - %f %f %f ",
101 pd_orders_arr[i].demand,
103 pd_orders_arr[i].pick_x,
104 pd_orders_arr[i].pick_y,
105 pd_orders_arr[i].pick_node_id,
107 pd_orders_arr[i].pick_open_t,
108 pd_orders_arr[i].pick_close_t,
109 pd_orders_arr[i].pick_service_t,
111 pd_orders_arr[i].deliver_x,
112 pd_orders_arr[i].deliver_y,
113 pd_orders_arr[i].deliver_node_id,
115 pd_orders_arr[i].deliver_open_t,
116 pd_orders_arr[i].deliver_close_t,
117 pd_orders_arr[i].deliver_service_t);
120 for (
size_t i = 0; i < total_vehicles; i++) {
121 PGR_DBG(
"%ld %f %f / %ld %f %f %f %f %f / %ld %f %f %f %f %f / %ld ",
123 vehicles_arr[i].capacity,
124 vehicles_arr[i].speed,
126 vehicles_arr[i].start_node_id,
127 vehicles_arr[i].start_x,
128 vehicles_arr[i].start_y,
129 vehicles_arr[i].start_open_t,
130 vehicles_arr[i].start_close_t,
131 vehicles_arr[i].start_service_t,
133 vehicles_arr[i].end_node_id,
134 vehicles_arr[i].end_x,
135 vehicles_arr[i].end_y,
136 vehicles_arr[i].end_open_t,
137 vehicles_arr[i].end_close_t,
138 vehicles_arr[i].end_service_t,
140 vehicles_arr[i].cant_v);
145 size_t total_cells = 0;
149 if (total_pd_orders == 0 || total_vehicles == 0 || total_cells == 0) {
151 (*result_tuples) = NULL;
155 PGR_DBG(
"Total %ld orders in query:", total_pd_orders);
156 PGR_DBG(
"Total %ld vehicles in query:", total_vehicles);
157 PGR_DBG(
"Total %ld matrix cells in query:", total_cells);
160 PGR_DBG(
"Starting processing");
161 clock_t start_t = clock();
162 char *log_msg = NULL;
163 char *notice_msg = NULL;
164 char *err_msg = NULL;
167 pd_orders_arr, total_pd_orders,
168 vehicles_arr, total_vehicles,
169 matrix_cells_arr, total_cells,
182 time_msg(
"pgr_pickDeliver", start_t, clock());
184 if (err_msg && (*result_tuples)) {
185 pfree(*result_tuples);
187 (*result_tuples) = NULL;
195 if (log_msg) pfree(log_msg);
196 if (notice_msg) pfree(notice_msg);
197 if (err_msg) pfree(err_msg);
198 if (pd_orders_arr) pfree(pd_orders_arr);
199 if (vehicles_arr) pfree(vehicles_arr);
200 if (matrix_cells_arr) pfree(matrix_cells_arr);
212 FuncCallContext *funcctx;
213 TupleDesc tuple_desc;
217 size_t result_count = 0;
220 if (SRF_IS_FIRSTCALL()) {
221 MemoryContext oldcontext;
222 funcctx = SRF_FIRSTCALL_INIT();
223 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
235 text_to_cstring(PG_GETARG_TEXT_P(0)),
236 text_to_cstring(PG_GETARG_TEXT_P(1)),
237 text_to_cstring(PG_GETARG_TEXT_P(2)),
246 #if PGSQL_VERSION > 95
247 funcctx->max_calls = result_count;
249 funcctx->max_calls = (uint32_t)result_count;
251 funcctx->user_fctx = result_tuples;
252 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
253 != TYPEFUNC_COMPOSITE) {
255 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
256 errmsg(
"function returning record called in context "
257 "that cannot accept type record")));
260 funcctx->tuple_desc = tuple_desc;
261 MemoryContextSwitchTo(oldcontext);
264 funcctx = SRF_PERCALL_SETUP();
265 tuple_desc = funcctx->tuple_desc;
268 if (funcctx->call_cntr < funcctx->max_calls) {
273 size_t call_cntr = funcctx->call_cntr;
294 values = palloc(numb *
sizeof(Datum));
295 nulls = palloc(numb *
sizeof(
bool));
298 for (i = 0; i < numb; ++i) {
303 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
304 values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq);
305 values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id);
306 values[3] = Int32GetDatum(result_tuples[call_cntr].stop_seq);
307 values[4] = Int32GetDatum(result_tuples[call_cntr].stop_type + 1);
308 values[5] = Int64GetDatum(result_tuples[call_cntr].stop_id);
309 values[6] = Int64GetDatum(result_tuples[call_cntr].order_id);
310 values[7] = Float8GetDatum(result_tuples[call_cntr].cargo);
311 values[8] = Float8GetDatum(result_tuples[call_cntr].travelTime);
312 values[9] = Float8GetDatum(result_tuples[call_cntr].arrivalTime);
313 values[10] = Float8GetDatum(result_tuples[call_cntr].waitTime);
314 values[11] = Float8GetDatum(result_tuples[call_cntr].serviceTime);
315 values[12] = Float8GetDatum(result_tuples[call_cntr].departureTime);
319 tuple = heap_form_tuple(tuple_desc, values, nulls);
320 result = HeapTupleGetDatum(tuple);
321 SRF_RETURN_NEXT(funcctx, result);
323 SRF_RETURN_DONE(funcctx);