31 #include "utils/array.h"
53 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;
68 (errcode(ERRCODE_INTERNAL_ERROR),
69 errmsg(
"Illegal value in parameter: max_cycles"),
70 errhint(
"Negative value found: max_cycles: %d ", max_cycles)));
72 (*result_tuples) = NULL;
76 if (initial_solution_id <= 0 || initial_solution_id > 6) {
77 elog(ERROR,
"Illegal value in parameter: initial_sol");
79 (*result_tuples) = NULL;
87 size_t total_pd_orders = 0;
89 &pd_orders_arr, &total_pd_orders);
93 size_t total_vehicles = 0;
95 &vehicles_arr, &total_vehicles);
96 PGR_DBG(
"total vehicles %ld", total_vehicles);
98 for (
size_t i = 0; i < total_pd_orders; i++) {
99 PGR_DBG(
"%ld %f pick %f %f %ld - "
100 "%f %f %f deliver %f %f %ld - %f %f %f ",
102 pd_orders_arr[i].demand,
104 pd_orders_arr[i].pick_x,
105 pd_orders_arr[i].pick_y,
106 pd_orders_arr[i].pick_node_id,
108 pd_orders_arr[i].pick_open_t,
109 pd_orders_arr[i].pick_close_t,
110 pd_orders_arr[i].pick_service_t,
112 pd_orders_arr[i].deliver_x,
113 pd_orders_arr[i].deliver_y,
114 pd_orders_arr[i].deliver_node_id,
116 pd_orders_arr[i].deliver_open_t,
117 pd_orders_arr[i].deliver_close_t,
118 pd_orders_arr[i].deliver_service_t);
123 for (
size_t i = 0; i < total_vehicles; i++) {
124 PGR_DBG(
"%ld %f %f , start %f %f %f %f %f "
125 "end %f %f %f %f %f number %ld ",
127 vehicles_arr[i].capacity,
128 vehicles_arr[i].speed,
130 vehicles_arr[i].start_x,
131 vehicles_arr[i].start_y,
132 vehicles_arr[i].start_open_t,
133 vehicles_arr[i].start_close_t,
134 vehicles_arr[i].start_service_t,
136 vehicles_arr[i].end_x,
137 vehicles_arr[i].end_y,
138 vehicles_arr[i].end_open_t,
139 vehicles_arr[i].end_close_t,
140 vehicles_arr[i].end_service_t,
142 vehicles_arr[i].cant_v);
145 if (total_pd_orders == 0 || total_vehicles == 0) {
147 (*result_tuples) = NULL;
151 PGR_DBG(
"Total %ld orders in query:", total_pd_orders);
153 PGR_DBG(
"Starting processing");
154 clock_t start_t = clock();
155 char *log_msg = NULL;
156 char *notice_msg = NULL;
157 char *err_msg = NULL;
159 pd_orders_arr, total_pd_orders,
160 vehicles_arr, total_vehicles,
172 time_msg(
"_pgr_pickDeliverEuclidean", start_t, clock());
174 if (err_msg && (*result_tuples)) {
175 pfree(*result_tuples);
177 (*result_tuples) = NULL;
182 if (log_msg) pfree(log_msg);
183 if (notice_msg) pfree(notice_msg);
184 if (err_msg) pfree(err_msg);
185 if (pd_orders_arr) pfree(pd_orders_arr);
186 if (vehicles_arr) pfree(vehicles_arr);
195 FuncCallContext *funcctx;
196 TupleDesc tuple_desc;
202 size_t result_count = 0;
206 if (SRF_IS_FIRSTCALL()) {
207 MemoryContext oldcontext;
208 funcctx = SRF_FIRSTCALL_INIT();
209 oldcontext = MemoryContextSwitchTo(funcctx->multi_call_memory_ctx);
222 text_to_cstring(PG_GETARG_TEXT_P(0)),
223 text_to_cstring(PG_GETARG_TEXT_P(1)),
233 #if PGSQL_VERSION > 95
234 funcctx->max_calls = result_count;
236 funcctx->max_calls = (uint32_t)result_count;
238 funcctx->user_fctx = result_tuples;
239 if (get_call_result_type(fcinfo, NULL, &tuple_desc)
240 != TYPEFUNC_COMPOSITE) {
242 (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
243 errmsg(
"function returning record called in context "
244 "that cannot accept type record")));
247 funcctx->tuple_desc = tuple_desc;
248 MemoryContextSwitchTo(oldcontext);
251 funcctx = SRF_PERCALL_SETUP();
252 tuple_desc = funcctx->tuple_desc;
255 if (funcctx->call_cntr < funcctx->max_calls) {
260 size_t call_cntr = funcctx->call_cntr;
276 values = palloc(numb *
sizeof(Datum));
277 nulls = palloc(numb *
sizeof(
bool));
280 for (i = 0; i < numb; ++i) {
286 values[0] = Int32GetDatum(funcctx->call_cntr + 1);
287 values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq);
288 values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id);
289 values[3] = Int32GetDatum(result_tuples[call_cntr].stop_seq);
290 values[4] = Int32GetDatum(result_tuples[call_cntr].stop_type + 1);
291 values[5] = Int64GetDatum(result_tuples[call_cntr].order_id);
292 values[6] = Float8GetDatum(result_tuples[call_cntr].cargo);
293 values[7] = Float8GetDatum(result_tuples[call_cntr].travelTime);
294 values[8] = Float8GetDatum(result_tuples[call_cntr].arrivalTime);
295 values[9] = Float8GetDatum(result_tuples[call_cntr].waitTime);
296 values[10] = Float8GetDatum(result_tuples[call_cntr].serviceTime);
297 values[11] = Float8GetDatum(result_tuples[call_cntr].departureTime);
301 tuple = heap_form_tuple(tuple_desc, values, nulls);
302 result = HeapTupleGetDatum(tuple);
303 SRF_RETURN_NEXT(funcctx, result);
305 SRF_RETURN_DONE(funcctx);