diff --git a/.gitignore b/.gitignore index 93ee6f1e23..2735dea235 100644 --- a/.gitignore +++ b/.gitignore @@ -6,6 +6,9 @@ src/version/_version.h v[0-9]* ortools build1 +bin_packing.cc +gpush.sh +vroom-v1.12.0 # files are ignored on gh-pages doxy diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000000..9cb0100533 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,5 @@ +{ + "files.associations": { + "knapsack_driver.h": "c" + } +} \ No newline at end of file diff --git a/CMakeLists.txt b/CMakeLists.txt index 08c6cf93ca..5711b99fa3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -332,14 +332,14 @@ endforeach() #---------------------- # set(CMAKE_PREFIX_PATH "/home/manas/Codes/GSOC-2022/MyFork/ortools/build") -add_subdirectory(ortools) +# add_subdirectory(ortools) # set(ortools_DIR "/home/manas/Codes/GSOC-2022/MyFork/ortools/build") # set(absl_DIR "/home/manas/Codes/GSOC-2022/MyFork/ortools/build/_deps/absl-build") # set(re2_DIR "/home/manas/Codes/GSOC-2022/MyFork/ortools/build/_deps/re2-build/CMakeFiles/Export/lib/cmake/re2") # set(Protobuf_DIR "/home/manas/Codes/GSOC-2022/MyFork/ortools/build/_deps/protobuf-build/lib/cmake/protobuf") # find_package(ortools CONFIG REQUIRED) -add_executable(myapp bin_packing.cc) -target_link_libraries(myapp ortools::ortools) +# add_executable(myapp bin_packing.cc) +# target_link_libraries(myapp ortools::ortools) #----------------------------------------------------------------------------- diff --git a/configuration.conf b/configuration.conf index 05b0af88a7..cb1f091169 100644 --- a/configuration.conf +++ b/configuration.conf @@ -28,4 +28,4 @@ simulation | N | Y | N optimizers | Y | N | N initialsol | Y | N | N vroom | Y | Y | Y -or_tools | N | N | N +or_tools | Y | Y | N diff --git a/docqueries/or_tools/CMakeLists.txt b/docqueries/or_tools/CMakeLists.txt index 85ace15888..990e45f2a2 100644 --- a/docqueries/or_tools/CMakeLists.txt +++ b/docqueries/or_tools/CMakeLists.txt @@ -1,4 +1,5 @@ SET(LOCAL_FILES + knapsack ) foreach (f ${LOCAL_FILES}) diff --git a/docqueries/or_tools/knapsack.result b/docqueries/or_tools/knapsack.result new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docqueries/or_tools/knapsack.test.sql b/docqueries/or_tools/knapsack.test.sql index af360e1854..e3517462b4 100644 --- a/docqueries/or_tools/knapsack.test.sql +++ b/docqueries/or_tools/knapsack.test.sql @@ -1 +1,17 @@ --- TODO make the single tests. \ No newline at end of file +CREATE TABLE knapsack_data( + weight INTEGER, + cost INTEGER); + +INSERT INTO knapsack_data (weight, cost) +VALUES +(12, 4), +(2, 2), +(1, 1), +(4, 10), +(1, 2); + +SELECT * +FROM knapsack_data; + +SELECT * +FROM vrp_knapsack($$SELECT weight, cost FROM knapsack_data$$, 3); diff --git a/docqueries/or_tools/test.conf b/docqueries/or_tools/test.conf new file mode 100644 index 0000000000..94df081b96 --- /dev/null +++ b/docqueries/or_tools/test.conf @@ -0,0 +1,21 @@ +#!/usr/bin/perl -w + +%main::tests = ( + 'any' => { + 'comment' => 'test for or-tools', + 'data' => [], + 'newdb' => 1, + 'tests' => [qw( + knapsack + )], + + 'documentation' => [qw( + knapsack + )], + + 'nottesting' => [qw( + )] + }, +); + +1; \ No newline at end of file diff --git a/include/c_common/weights_costs_input.h b/include/c_common/weights_costs_input.h new file mode 100644 index 0000000000..668aeeb0d7 --- /dev/null +++ b/include/c_common/weights_costs_input.h @@ -0,0 +1,33 @@ +/*PGR-GNU***************************************************************** + * TODO fix licence +File: matrixRows_input.h +Copyright (c) 2022 Manas Sivakumar +manas23601@gmail.com +------ +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + ********************************************************************PGR-GNU*/ + +#ifndef INCLUDE_C_COMMON_WEIGHTS_COSTS_INPUT_H_ +#define INCLUDE_C_COMMON_WEIGHTS_COSTS_INPUT_H_ +#pragma once +#include + +typedef struct Knapsack_rt Knapsack_rt; + +/** @brief Get the weights and cost for each item */ +void get_weights_costs( + char *sql, + Knapsack_rt **rows, + size_t *total_rows); + +#endif // INCLUDE_C_COMMON_WEIGHTS_COSTS_INPUT_H_ diff --git a/include/c_types/knapsack_rt.h b/include/c_types/knapsack_rt.h new file mode 100644 index 0000000000..ee99c40c21 --- /dev/null +++ b/include/c_types/knapsack_rt.h @@ -0,0 +1,54 @@ +/*PGR-GNU***************************************************************** +File: knapsack_rt.h + +Copyright (c) 2022 Manas Sivakumar +Mail: vicky_vergara@hotmail.com + +------ + +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + + ********************************************************************PGR-GNU*/ +/*! @file */ + +#ifndef INCLUDE_C_TYPES_KNAPSACK_RT_H_ +#define INCLUDE_C_TYPES_KNAPSACK_RT_H_ +#pragma once + +/* for int64_t */ +#ifdef __cplusplus +# include +#else +# include +#endif + +/** @brief + +@note C/C++/postgreSQL connecting structure for output +name | description +:----- | :------- +Index | Position of the item in the order in which the input was given +item_weight | Weight of the item +item_cost | Value of the item +*/ + +struct Knapsack_rt { + int64_t item_weight; + int64_t item_cost; +}; + +/*************************************************************************/ + +#endif // INCLUDE_C_TYPES_KNAPSACK_RT_H_ diff --git a/include/c_types/typedefs.h b/include/c_types/typedefs.h index 84bb26cb20..03c41a6211 100644 --- a/include/c_types/typedefs.h +++ b/include/c_types/typedefs.h @@ -60,6 +60,7 @@ using Vroom_matrix_t = struct Vroom_matrix_t; using Vroom_break_t = struct Vroom_break_t; using Vroom_time_window_t = struct Vroom_time_window_t; using Vroom_rt = struct Vroom_rt; +using Knapsack_rt = struct Knapsack_rt; /* * Index on a container */ @@ -97,6 +98,7 @@ typedef struct Vroom_matrix_t Vroom_matrix_t; typedef struct Vroom_break_t Vroom_break_t; typedef struct Vroom_time_window_t Vroom_time_window_t; typedef struct Vroom_rt Vroom_rt; +typedef struct Knapsack_rt Knapsack_rt; #endif /*************************************************************************/ diff --git a/include/drivers/or_tools/knapsack_driver.h b/include/drivers/or_tools/knapsack_driver.h index 9873fa2330..d18e96712a 100644 --- a/include/drivers/or_tools/knapsack_driver.h +++ b/include/drivers/or_tools/knapsack_driver.h @@ -39,32 +39,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. # include #endif -#include "c_types/vehicle_t.h" -#include "c_types/matrix_cell_t.h" -typedef struct PickDeliveryOrders_t PickDeliveryOrders_t; -typedef struct Solution_rt Solution_rt; +typedef struct Knapsack_rt Knapsack_rt; #ifdef __cplusplus extern "C" { #endif /********************************************************* - weights_sql TEXT, - values_sql TEXT, - bin_capacity INTEGER, - max_cycles INTEGER, + weights_cost_sql TEXT, + capacity INTEGER, ********************************************************/ - void do_pgr_pickDeliver( - PickDeliveryOrders_t *pd_orders_arr, size_t total_pd_orders, - Vehicle_t *vehicles_arr, size_t total_vehicles, - Matrix_cell_t *matrix_cells_arr, size_t total_cells, + void do_knapsack( + Knapsack_rt *knapsack_items, size_t total_knapsack_items, - double factor, - int max_cycles, - int initial_solution_id, + int capacity, - Solution_rt **return_tuples, + Knapsack_rt **return_tuples, size_t *return_count, char **log_msg, diff --git a/sql/or_tools/CMakeLists.txt b/sql/or_tools/CMakeLists.txt index 44586ef4d7..9d76c13cc9 100644 --- a/sql/or_tools/CMakeLists.txt +++ b/sql/or_tools/CMakeLists.txt @@ -1,6 +1,6 @@ SET(LOCAL_FILES + _knapsack.sql knapsack.sql - _knapsack.sql ) foreach (f ${LOCAL_FILES}) diff --git a/sql/or_tools/_knapsack.sql b/sql/or_tools/_knapsack.sql index 7f64734712..7b49f8c730 100644 --- a/sql/or_tools/_knapsack.sql +++ b/sql/or_tools/_knapsack.sql @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: _knapsack_0_1.sql +File: _knapsack.sql Copyright (c) 2021 pgRouting developers Mail: project@pgrouting.org @@ -27,14 +27,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. CREATE OR REPLACE FUNCTION _vrp_knapsack( - TEXT, -- weights SQL - TEXT, -- values SQL + TEXT, -- weights_cost SQL - INTEGER, -- bin capacity + INTEGER, -- capacity - OUT total_value INTEGER, + OUT total_cost INTEGER, OUT total_weight INTEGER, - OUT packed_items INTEGER[], + OUT packed_costs INTEGER[], OUT packed_weights INTEGER[] ) RETURNS SETOF RECORD AS @@ -43,5 +42,5 @@ LANGUAGE c VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION _vrp_knapsack(TEXT, TEXT, INTEGER) +COMMENT ON FUNCTION _vrp_knapsack(TEXT, INTEGER) IS 'vrprouting internal function'; \ No newline at end of file diff --git a/sql/or_tools/knapsack.sql b/sql/or_tools/knapsack.sql index fa0db2ea8c..fe12d261e3 100644 --- a/sql/or_tools/knapsack.sql +++ b/sql/or_tools/knapsack.sql @@ -1,5 +1,5 @@ /*PGR-GNU***************************************************************** -File: knapsack_0-1.sql +File: knapsack_.sql Copyright (c) 2021 pgRouting developers Mail: project@pgrouting.org @@ -28,24 +28,23 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. CREATE OR REPLACE FUNCTION vrp_knapsack( - TEXT, -- weights SQL - TEXT, -- values SQL + TEXT, -- weights_cost SQL - INTEGER, -- bin capacity + INTEGER, -- capacity - OUT total_value INTEGER, + OUT total_cost INTEGER, OUT total_weight INTEGER, - OUT packed_items INTEGER[], + OUT packed_cost INTEGER[], OUT packed_weights INTEGER[] ) RETURNS SETOF RECORD AS $BODY$ SELECT * - FROM vrp_knapsack(_pgr_get_statement($1), _pgr_get_statement($2), $3); + FROM _vrp_knapsack(_pgr_get_statement($1), $2); $BODY$ LANGUAGE SQL VOLATILE STRICT; -- COMMENTS -COMMENT ON FUNCTION vrp_knapsack(TEXT, TEXT, INTEGER) +COMMENT ON FUNCTION vrp_knapsack(TEXT, INTEGER) IS 'vrp_knapsack'; \ No newline at end of file diff --git a/src/common/CMakeLists.txt b/src/common/CMakeLists.txt index 268e4c29c2..513575f711 100644 --- a/src/common/CMakeLists.txt +++ b/src/common/CMakeLists.txt @@ -7,6 +7,7 @@ ADD_LIBRARY(common OBJECT orders_input.c vehicles_input.c time_multipliers_input.c + weights_costs_input.c time_msg.c e_report.c diff --git a/src/common/weights_costs_input.c b/src/common/weights_costs_input.c new file mode 100644 index 0000000000..a45cd8fdac --- /dev/null +++ b/src/common/weights_costs_input.c @@ -0,0 +1,140 @@ +/*PGR-GNU***************************************************************** +File: weights_costs_input.c +Copyright (c) 2022 pgRouting developers +Mail: project@pgrouting.org +Developer: +Copyright (c) 2022 Manas Sivakumar +------ +This program is free software; you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation; either version 2 of the License, or +(at your option) any later version. +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + ********************************************************************PGR-GNU*/ + +#include "c_common/weights_costs_input.h" + +#include "c_types/column_info_t.h" +#include "c_types/knapsack_rt.h" + +#include "c_common/get_check_data.h" + +#ifdef PROFILE +#include "c_common/time_msg.h" +#include "c_common/debug_macro.h" +#endif + +static +void fetch_weights( + HeapTuple *tuple, + TupleDesc *tupdesc, + Column_info_t info[2], + Knapsack_rt *row) { + row->item_weight = get_Id(tuple, tupdesc, info[0], -1); + row->item_cost = get_Id(tuple, tupdesc, info[1], -1); +} + +static +void +get_weights_general( + char *sql, + Column_info_t *info, + Knapsack_rt **rows, + size_t *total_rows) { +#if 0 + clock_t start_t = clock(); + PGR_DBG("%s", sql); +#endif + + const int tuple_limit = 1000000; + size_t total_tuples = 0; + const int column_count = 2; + + void *SPIplan; + SPIplan = pgr_SPI_prepare(sql); + + Portal SPIportal; + SPIportal = pgr_SPI_cursor_open(SPIplan); + + + bool moredata = true; + (*total_rows) = total_tuples; + + while (moredata == true) { + SPI_cursor_fetch(SPIportal, true, tuple_limit); + if (total_tuples == 0) + pgr_fetch_column_info(info, column_count); + + size_t ntuples = SPI_processed; + total_tuples += ntuples; + + if (ntuples > 0) { + if ((*rows) == NULL) + (*rows) = (Knapsack_rt *)palloc0( + total_tuples * sizeof(Knapsack_rt)); + else + (*rows) = (Knapsack_rt *)repalloc( + (*rows), total_tuples * sizeof(Knapsack_rt)); + + if ((*rows) == NULL) { + elog(ERROR, "Out of memory"); + } + + SPITupleTable *tuptable = SPI_tuptable; + TupleDesc tupdesc = SPI_tuptable->tupdesc; + + size_t t; + for (t = 0; t < ntuples; t++) { + HeapTuple tuple = tuptable->vals[t]; + fetch_weights(&tuple, &tupdesc, info, + &(*rows)[total_tuples - ntuples + t]); + } + SPI_freetuptable(tuptable); + } else { + moredata = false; + } + } + + SPI_cursor_close(SPIportal); + + + if (total_tuples == 0) { + (*total_rows) = 0; + return; + } + + (*total_rows) = total_tuples; +#ifdef PROFILE + time_msg(" reading weights", start_t, clock()); +#endif +} + +/** + * @param [in] sql SQL query that has the following columns: start_vid, end_vid, agg_cost + * @param [out] rows C Container that holds all the matrix rows + * @param [out] total_rows Total rows recieved + */ +void +get_weights_costs( + char *sql, + Knapsack_rt **rows, + size_t *total_rows) { + Column_info_t info[2]; + + int i; + for (i = 0; i < 2; ++i) { + info[i].colNumber = -1; + info[i].type = 0; + info[i].strict = true; + info[i].eType = ANY_INTEGER; + } + info[0].name = "weight"; + info[1].name = "cost"; + get_weights_general(sql, info, rows, total_rows); +} diff --git a/src/or_tools/knapsack.c b/src/or_tools/knapsack.c index feec208e79..035d203fbe 100644 --- a/src/or_tools/knapsack.c +++ b/src/or_tools/knapsack.c @@ -23,8 +23,8 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. #include "c_common/debug_macro.h" #include "c_common/e_report.h" #include "c_common/time_msg.h" - - +#include "c_common/weights_costs_input.h" +#include "c_types/knapsack_rt.h" #include "drivers/or_tools/knapsack_driver.h" PGDLLEXPORT Datum @@ -35,60 +35,40 @@ PG_FUNCTION_INFO_V1(_vrp_knapsack); static void process( - char* weights_sql, - char* values_sql, - char* matrix_sql, - int bin_capacity, - double factor, - int max_cycles, - int initial_solution_id, + char* weights_costs_sql, + int capacity, - Solution_rt **result_tuples, + Knapsack_rt **result_tuples, size_t *result_count) { -#if 0 - if (factor <= 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: factor"), - errhint("Value found: %f <= 0", factor))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (max_cycles < 0) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: max_cycles"), - errhint("Value found: %d <= 0", max_cycles))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - - if (initial_solution_id < 0 || initial_solution_id > 7) { - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("Illegal value in parameter: initial"), - errhint("Value found: %d <= 0", initial_solution_id))); - (*result_count) = 0; - (*result_tuples) = NULL; - return; - } - pgr_SPI_connect(); + weights_costs_sql = weights_costs_sql; + PGR_DBG("%d", capacity); + result_tuples = NULL; + result_count = 0; + if (result_tuples) {pfree(result_tuples); result_tuples = NULL;} + result_count = result_count + 1 -1; + elog(WARNING, "Stopping execution"); + + Knapsack_rt *weights_arr = NULL; + size_t total_weights = 0; +#if 0 + if (weights_arr) {pfree(weights_arr); weights_arr = NULL;} + total_weights = total_weights + 1 -1; +#endif + get_weights_costs(weights_costs_sql, + &weights_arr, &total_weights); +#if 0 + Knapsack_rt *knapsack_items = NULL; + size_t total_knapsack_items = 0; + get_weights_costs(weights_costs_sql, + &knapsack_items, &total_knapsack_items); - PickDeliveryOrders_t *pd_orders_arr = NULL; - size_t total_pd_orders = 0; - get_shipments_raw(pd_orders_sql, - &pd_orders_arr, &total_pd_orders); - - if (total_pd_orders == 0) { + if (total_knapsack_items == 0) { (*result_count) = 0; (*result_tuples) = NULL; /* freeing memory before return */ - if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;} + if (knapsack_items) {pfree(knapsack_items); knapsack_items = NULL;} pgr_SPI_finish(); ereport(ERROR, @@ -97,119 +77,15 @@ process( return; } - - Vehicle_t *vehicles_arr = NULL; - size_t total_vehicles = 0; - get_vehicles_raw(vehicles_sql, - &vehicles_arr, &total_vehicles, - false); - - if (total_vehicles == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;} - if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;} - - pgr_SPI_finish(); - ereport(ERROR, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No vehicles found"))); - return; - } - - PGR_DBG("total orders %ld", total_pd_orders); - for (size_t i = 0; i < total_pd_orders; i++) { - PGR_DBG("%ld %f pick %ld - %ld %ld %ld" - "deliver %ld - %ld %ld %ld ", - pd_orders_arr[i].id, - pd_orders_arr[i].demand, - - pd_orders_arr[i].pick_node_id, - - pd_orders_arr[i].pick_open_t, - pd_orders_arr[i].pick_close_t, - pd_orders_arr[i].pick_service_t, - - pd_orders_arr[i].deliver_node_id, - - pd_orders_arr[i].deliver_open_t, - pd_orders_arr[i].deliver_close_t, - pd_orders_arr[i].deliver_service_t); - } - - - - PGR_DBG("total vehicles %ld", total_vehicles); - for (size_t i = 0; i < total_vehicles; i++) { - PGR_DBG("%ld %f %f , start %ld %ld %ld %ld " - "end %ld %ld %ld %ld number %ld ", - vehicles_arr[i].id, - vehicles_arr[i].capacity, - vehicles_arr[i].speed, - - vehicles_arr[i].start_node_id, - vehicles_arr[i].start_open_t, - vehicles_arr[i].start_close_t, - vehicles_arr[i].start_service_t, - - vehicles_arr[i].end_node_id, - vehicles_arr[i].end_open_t, - vehicles_arr[i].end_close_t, - vehicles_arr[i].end_service_t, - - vehicles_arr[i].cant_v); - } - - Matrix_cell_t *matrix_cells_arr = NULL; - size_t total_cells = 0; - get_matrixRows_plain(matrix_sql, &matrix_cells_arr, &total_cells); - - PGR_DBG("total matrix rows %ld", total_cells); - for (size_t i = 0; i < total_cells; i++) { - PGR_DBG("%ld %ld %f", - matrix_cells_arr[i].from_vid, - matrix_cells_arr[i].to_vid, - matrix_cells_arr[i].cost); - } - - if (total_cells == 0) { - (*result_count) = 0; - (*result_tuples) = NULL; - - /* freeing memory before return */ - if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;} - if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;} - if (matrix_cells_arr) {pfree(matrix_cells_arr); matrix_cells_arr = NULL;} - - ereport(WARNING, - (errcode(ERRCODE_INTERNAL_ERROR), - errmsg("No matrix found"))); - pgr_SPI_finish(); - return; - } - - - PGR_DBG("Total %ld orders in query:", total_pd_orders); - PGR_DBG("Total %ld vehicles in query:", total_vehicles); - PGR_DBG("Total %ld matrix cells in query:", total_cells); - - - PGR_DBG("Starting processing"); clock_t start_t = clock(); char *log_msg = NULL; char *notice_msg = NULL; char *err_msg = NULL; - do_pgr_pickDeliver( - pd_orders_arr, total_pd_orders, - vehicles_arr, total_vehicles, - matrix_cells_arr, total_cells, + do_knapsack( + knapsack_items, total_knapsack_items, - factor, - max_cycles, - initial_solution_id, + capacity, result_tuples, result_count, @@ -226,23 +102,14 @@ process( (*result_tuples) = NULL; } pgr_global_report(log_msg, notice_msg, err_msg); -/* TODO(dev) remove unused includes */ -#include "c_common/orders_input.h" -#include "c_common/vehicles_input.h" -#include "c_common/matrixRows_input.h" -#include "c_types/pickDeliveryOrders_t.h" -#include "drivers/pgr_pickDeliver/pickDeliver_driver.h" -#include "c_types/solution_rt.h" + /* freeing memory before return */ if (log_msg) {pfree(log_msg); log_msg = NULL;} if (notice_msg) {pfree(notice_msg); notice_msg = NULL;} if (err_msg) {pfree(err_msg); err_msg = NULL;} - if (pd_orders_arr) {pfree(pd_orders_arr); pd_orders_arr = NULL;} - if (vehicles_arr) {pfree(vehicles_arr); vehicles_arr = NULL;} - if (matrix_cells_arr) {pfree(matrix_cells_arr); matrix_cells_arr = NULL;} - - pgr_SPI_finish(); + if (knapsack_items) {pfree(knapsack_items); knapsack_items = NULL;} #endif + pgr_SPI_finish(); } @@ -252,7 +119,7 @@ _vrp_knapsack(PG_FUNCTION_ARGS) { FuncCallContext *funcctx; TupleDesc tuple_desc; - Solution_rt *result_tuples = 0; + Knapsack_rt *result_tuples = NULL; size_t result_count = 0; if (SRF_IS_FIRSTCALL()) { @@ -262,11 +129,7 @@ _vrp_knapsack(PG_FUNCTION_ARGS) { process( text_to_cstring(PG_GETARG_TEXT_P(0)), - text_to_cstring(PG_GETARG_TEXT_P(1)), - text_to_cstring(PG_GETARG_TEXT_P(2)), - PG_GETARG_FLOAT8(3), - PG_GETARG_INT32(4), - PG_GETARG_INT32(5), + PG_GETARG_INT32(1), &result_tuples, &result_count); @@ -286,7 +149,7 @@ _vrp_knapsack(PG_FUNCTION_ARGS) { funcctx = SRF_PERCALL_SETUP(); tuple_desc = funcctx->tuple_desc; - result_tuples = (Solution_rt*) funcctx->user_fctx; + result_tuples = (Knapsack_rt*) funcctx->user_fctx; if (funcctx->call_cntr < funcctx->max_calls) { HeapTuple tuple; @@ -305,19 +168,8 @@ _vrp_knapsack(PG_FUNCTION_ARGS) { } values[0] = Int32GetDatum(funcctx->call_cntr + 1); - values[1] = Int32GetDatum(result_tuples[call_cntr].vehicle_seq); - values[2] = Int64GetDatum(result_tuples[call_cntr].vehicle_id); - values[3] = Int32GetDatum(result_tuples[call_cntr].stop_seq); - values[4] = Int32GetDatum(result_tuples[call_cntr].stop_type + 1); - values[5] = Int64GetDatum(result_tuples[call_cntr].stop_id); - values[6] = Int64GetDatum(result_tuples[call_cntr].order_id); - values[7] = Int64GetDatum(result_tuples[call_cntr].cargo); - values[8] = Int64GetDatum(result_tuples[call_cntr].travelTime); - values[9] = Int64GetDatum(result_tuples[call_cntr].arrivalTime); - values[10] = Int64GetDatum(result_tuples[call_cntr].waitDuration); - values[11] = Int64GetDatum(result_tuples[call_cntr].serviceDuration); - values[12] = Int64GetDatum(result_tuples[call_cntr].departureTime); - + values[1] = Int32GetDatum(result_tuples[call_cntr].item_weight); + values[2] = Int64GetDatum(result_tuples[call_cntr].item_cost); tuple = heap_form_tuple(tuple_desc, values, nulls); result = HeapTupleGetDatum(tuple); diff --git a/src/or_tools/knapsack_driver.cpp b/src/or_tools/knapsack_driver.cpp index c72200e4db..2384e20e8f 100644 --- a/src/or_tools/knapsack_driver.cpp +++ b/src/or_tools/knapsack_driver.cpp @@ -66,7 +66,7 @@ get_initial_solution(vrprouting::problem::PickDeliver* problem_ptr, int m_initia } // namespace void -do_pgr_pickDeliver( +do_knapsack( struct PickDeliveryOrders_t customers_arr[], size_t total_customers,