From 2b4f0b4c462da0934ef294cfaf5a77040fce3b76 Mon Sep 17 00:00:00 2001 From: Manas Date: Mon, 15 Aug 2022 19:20:21 +0530 Subject: [PATCH 01/22] [pgtap]pgtap no-crash-test --- pgtap/or_tools/no_crash_test.sql | 72 ++++++++++++++++++++++++++++++++ 1 file changed, 72 insertions(+) create mode 100644 pgtap/or_tools/no_crash_test.sql diff --git a/pgtap/or_tools/no_crash_test.sql b/pgtap/or_tools/no_crash_test.sql new file mode 100644 index 0000000000..9e29d3e34a --- /dev/null +++ b/pgtap/or_tools/no_crash_test.sql @@ -0,0 +1,72 @@ +BEGIN; +-- SET search_path TO 'vroom', 'public'; +-- SET client_min_messages TO ERROR; + +SELECT CASE WHEN min_version('0.3.0') THEN plan (116) ELSE plan(1) END; + + +PREPARE knapsack_test AS +SELECT * FROM knapsack_test_data; + +PREPARE multiple_knapsack_test AS +SELECT * FROM multiple_knapsack_test_data; + +PREPARE bin_packing_test AS +SELECT * FROM bin_packing_test_data; + +SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); +SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); +SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); + + + +CREATE OR REPLACE FUNCTION no_crash() +RETURNS SETOF TEXT AS +$BODY$ +DECLARE +params TEXT[]; +subs TEXT[]; +BEGIN + -- vrp_knapsack + params = ARRAY[ + '$$SELECT * FROM knapsack_data$$', + '1' + ]::TEXT[]; + subs = ARRAY[ + 'NULL', + 'NULL' + ]::TEXT[]; + + RETURN query SELECT * FROM no_crash_test('vrp_knapsack', params, subs); + + --vrp_multiple_knapsack + params = ARRAY[ + '$$SELECT * FROM multiple_knapsack_data$$', + '1' + ]::TEXT[]; + subs = ARRAY[ + 'NULL', + 'NULL' + ]::TEXT[]; + + RETURN query SELECT * FROM no_crash_test('vrp_multiple_knapsack', params, subs); + + -- bin_packing + params = ARRAY[ + '$$SELECT * FROM bin_packing_data$$', + '1' + ]::TEXT[]; + subs = ARRAY[ + 'NULL', + 'NULL' + ]::TEXT[]; + + RETURN query SELECT * FROM no_crash_test('vrp_bin_packing', params, subs); +END +$BODY$ +LANGUAGE plpgsql VOLATILE; + + +SELECT * FROM no_crash(); + +ROLLBACK; From b652ae633b0ed0b08f63914134024aebbe02d6cc Mon Sep 17 00:00:00 2001 From: Manas Date: Tue, 16 Aug 2022 00:14:24 +0530 Subject: [PATCH 02/22] Working on no_crash_test pgtap --- .gitignore | 1 + pgtap/or_tools/inner_query.sql | 10 ++++++ pgtap/or_tools/no_crash_test.sql | 48 ++++++++++++++++----------- sql/or_tools/bin_packing.sql | 12 +++---- sql/or_tools/knapsack.sql | 26 +++++++-------- sql/or_tools/multiple_knapsack.sql | 46 +++++++++++++------------- tools/testers/ortoolsdata.sql | 53 ++++++++++++++++++++++++++++++ tools/testers/setup_db.sql | 1 + 8 files changed, 135 insertions(+), 62 deletions(-) create mode 100644 pgtap/or_tools/inner_query.sql create mode 100644 tools/testers/ortoolsdata.sql diff --git a/.gitignore b/.gitignore index b73bb76187..5c2069973c 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,7 @@ gpush.sh vrprun.sh error_output.txt .vscode/ +vroom-v1.12.0/ # files are ignored on gh-pages doxy diff --git a/pgtap/or_tools/inner_query.sql b/pgtap/or_tools/inner_query.sql new file mode 100644 index 0000000000..dc7816ad4a --- /dev/null +++ b/pgtap/or_tools/inner_query.sql @@ -0,0 +1,10 @@ +BEGIN; + + +CREATE OR REPLACE FUNCTION knapsack() +RETURNS INTEGER AS +$BODY$ + RETURN SELECT +$BODY$ +LANGUAGE plpgsql VOLATILE; +ROLLBACK; \ No newline at end of file diff --git a/pgtap/or_tools/no_crash_test.sql b/pgtap/or_tools/no_crash_test.sql index 9e29d3e34a..9d2eab8adf 100644 --- a/pgtap/or_tools/no_crash_test.sql +++ b/pgtap/or_tools/no_crash_test.sql @@ -1,23 +1,26 @@ BEGIN; --- SET search_path TO 'vroom', 'public'; --- SET client_min_messages TO ERROR; +SET search_path TO 'ortools', 'public'; +SET client_min_messages TO ERROR; SELECT CASE WHEN min_version('0.3.0') THEN plan (116) ELSE plan(1) END; +-- DEALLOCATE knapsack_test; +-- DEALLOCATE multiple_knapsack_test; +-- DEALLOCATE bin_packing_test; -PREPARE knapsack_test AS -SELECT * FROM knapsack_test_data; +-- PREPARE knapsack_test AS +-- SELECT * FROM ortools.knapsack_data; -PREPARE multiple_knapsack_test AS -SELECT * FROM multiple_knapsack_test_data; +-- PREPARE multiple_knapsack_test AS +-- SELECT * FROM ortools.multiple_knapsack_data; -PREPARE bin_packing_test AS -SELECT * FROM bin_packing_test_data; +-- PREPARE bin_packing_test AS +-- SELECT * FROM ortools.bin_packing_data; -SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); -SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); -SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); +-- SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); +-- SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); +-- SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); CREATE OR REPLACE FUNCTION no_crash() @@ -29,39 +32,44 @@ subs TEXT[]; BEGIN -- vrp_knapsack params = ARRAY[ - '$$SELECT * FROM knapsack_data$$', - '1' + '$$SELECT * FROM ortools.knapsack_data$$', + 'capacity => 15', + 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', 'NULL' ]::TEXT[]; - RETURN query SELECT * FROM no_crash_test('vrp_knapsack', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_knapsack', params, subs); --vrp_multiple_knapsack params = ARRAY[ - '$$SELECT * FROM multiple_knapsack_data$$', - '1' + '$$SELECT * FROM ortools.multiple_knapsack_data$$', + 'capacities => ARRAY[100,100,100,100,100]', + 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', + 'NULL', 'NULL' ]::TEXT[]; - RETURN query SELECT * FROM no_crash_test('vrp_multiple_knapsack', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_multiple_knapsack', params, subs); -- bin_packing params = ARRAY[ - '$$SELECT * FROM bin_packing_data$$', - '1' + '$$SELECT * FROM ortools.bin_packing_data$$', + 'bin_capacity => 100' + 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', + 'NULL', 'NULL' ]::TEXT[]; - RETURN query SELECT * FROM no_crash_test('vrp_bin_packing', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_bin_packing', params, subs); END $BODY$ LANGUAGE plpgsql VOLATILE; diff --git a/sql/or_tools/bin_packing.sql b/sql/or_tools/bin_packing.sql index 656fb48fce..e94f527403 100644 --- a/sql/or_tools/bin_packing.sql +++ b/sql/or_tools/bin_packing.sql @@ -25,14 +25,14 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ DROP FUNCTION IF EXISTS vrp_bin_packing CASCADE; -DROP TABLE IF EXISTS bin_packing_data CASCADE; +-- DROP TABLE IF EXISTS bin_packing_data CASCADE; -CREATE TABLE bin_packing_data( - weight INTEGER); +-- CREATE TABLE bin_packing_data( +-- weight INTEGER); -INSERT INTO bin_packing_data (weight) -VALUES -(48), (30), (19), (36), (36), (27), (42), (42), (36), (24), (30); +-- INSERT INTO bin_packing_data (weight) +-- VALUES +-- (48), (30), (19), (36), (36), (27), (42), (42), (36), (24), (30); CREATE OR REPLACE FUNCTION vrp_bin_packing( diff --git a/sql/or_tools/knapsack.sql b/sql/or_tools/knapsack.sql index 60d94db552..a4abcad22d 100644 --- a/sql/or_tools/knapsack.sql +++ b/sql/or_tools/knapsack.sql @@ -26,19 +26,19 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ DROP FUNCTION IF EXISTS vrp_knapsack; -DROP TABLE IF EXISTS knapsack_data; - -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); +-- DROP TABLE IF EXISTS knapsack_data; + +-- 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); CREATE OR REPLACE FUNCTION vrp_knapsack( inner_query TEXT, -- weights_cost SQL diff --git a/sql/or_tools/multiple_knapsack.sql b/sql/or_tools/multiple_knapsack.sql index c38c55624f..7953ec256d 100644 --- a/sql/or_tools/multiple_knapsack.sql +++ b/sql/or_tools/multiple_knapsack.sql @@ -25,29 +25,29 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. ********************************************************************PGR-GNU*/ DROP FUNCTION IF EXISTS vrp_multiple_knapsack CASCADE; -DROP TABLE IF EXISTS multiple_knapsack_data CASCADE; - -CREATE TABLE multiple_knapsack_data( - weight INTEGER, - cost INTEGER); - -INSERT INTO multiple_knapsack_data (weight, cost) -VALUES -(48, 10), -(30, 30), -(42, 25), -(36, 50), -(36, 35), -(48, 30), -(42, 15), -(42, 40), -(36, 30), -(24, 35), -(30, 45), -(30, 10), -(42, 20), -(36, 30), -(36, 25); +-- DROP TABLE IF EXISTS multiple_knapsack_data CASCADE; + +-- CREATE TABLE multiple_knapsack_data( +-- weight INTEGER, +-- cost INTEGER); + +-- INSERT INTO multiple_knapsack_data (weight, cost) +-- VALUES +-- (48, 10), +-- (30, 30), +-- (42, 25), +-- (36, 50), +-- (36, 35), +-- (48, 30), +-- (42, 15), +-- (42, 40), +-- (36, 30), +-- (24, 35), +-- (30, 45), +-- (30, 10), +-- (42, 20), +-- (36, 30), +-- (36, 25); CREATE OR REPLACE FUNCTION vrp_multiple_knapsack( diff --git a/tools/testers/ortoolsdata.sql b/tools/testers/ortoolsdata.sql new file mode 100644 index 0000000000..76079c0548 --- /dev/null +++ b/tools/testers/ortoolsdata.sql @@ -0,0 +1,53 @@ +DROP SCHEMA IF EXISTS ortools CASCADE; +CREATE SCHEMA ortools; + +DROP TABLE IF EXISTS ortools.knapsack_data CASCADE; +DROP TABLE IF EXISTS ortools.multiple_knapsack_data CASCADE; +DROP TABLE IF EXISTS ortools.bin_packing_data CASCADE; + +--Bin Packing Start +CREATE TABLE ortools.bin_packing_data( + weight INTEGER); + +INSERT INTO ortools.bin_packing_data (weight) +VALUES +(48), (30), (19), (36), (36), (27), (42), (42), (36), (24), (30); +--Bin Packing End + +-- Multiple Knapsack Start +CREATE TABLE ortools.multiple_knapsack_data( + weight INTEGER, + cost INTEGER); + +INSERT INTO ortools.multiple_knapsack_data (weight, cost) +VALUES +(48, 10), +(30, 30), +(42, 25), +(36, 50), +(36, 35), +(48, 30), +(42, 15), +(42, 40), +(36, 30), +(24, 35), +(30, 45), +(30, 10), +(42, 20), +(36, 30), +(36, 25); +--Multiple Knapsack End + +--Knapsack Start +CREATE TABLE ortools.knapsack_data( + weight INTEGER, + cost INTEGER); + +INSERT INTO ortools.knapsack_data (weight, cost) +VALUES +(12, 4), +(2, 2), +(1, 1), +(4, 10), +(1, 2); +--Knapsack End diff --git a/tools/testers/setup_db.sql b/tools/testers/setup_db.sql index f8d3478421..9c28222d4a 100644 --- a/tools/testers/setup_db.sql +++ b/tools/testers/setup_db.sql @@ -28,5 +28,6 @@ BEGIN; \i no_crash_test.sql \i general_pgtap_tests.sql \i vroomdata.sql + \i ortoolsdata.sql END; From 1679240186a7eef60810820e83c08c9f80200b06 Mon Sep 17 00:00:00 2001 From: Manas Date: Tue, 16 Aug 2022 01:29:45 +0530 Subject: [PATCH 03/22] [pgtap] not working --- pgtap/or_tools/no_crash_test.sql | 57 +++++++++++++++++--------------- 1 file changed, 30 insertions(+), 27 deletions(-) diff --git a/pgtap/or_tools/no_crash_test.sql b/pgtap/or_tools/no_crash_test.sql index 9d2eab8adf..f2df722885 100644 --- a/pgtap/or_tools/no_crash_test.sql +++ b/pgtap/or_tools/no_crash_test.sql @@ -1,27 +1,7 @@ BEGIN; SET search_path TO 'ortools', 'public'; SET client_min_messages TO ERROR; - -SELECT CASE WHEN min_version('0.3.0') THEN plan (116) ELSE plan(1) END; - --- DEALLOCATE knapsack_test; --- DEALLOCATE multiple_knapsack_test; --- DEALLOCATE bin_packing_test; - --- PREPARE knapsack_test AS --- SELECT * FROM ortools.knapsack_data; - --- PREPARE multiple_knapsack_test AS --- SELECT * FROM ortools.multiple_knapsack_data; - --- PREPARE bin_packing_test AS --- SELECT * FROM ortools.bin_packing_data; - - --- SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); --- SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); --- SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); - +SELECT plan(13); CREATE OR REPLACE FUNCTION no_crash() RETURNS SETOF TEXT AS @@ -29,7 +9,24 @@ $BODY$ DECLARE params TEXT[]; subs TEXT[]; +error_messages TEXT[]; +non_empty_args INTEGER[]; BEGIN + -- DEALLOCATE knapsack_test; + -- DEALLOCATE multiple_knapsack_test; + -- DEALLOCATE bin_packing_test; + + -- PREPARE knapsack_test AS SELECT * FROM ortools.knapsack_data; + -- PREPARE multiple_knapsack_test AS SELECT * FROM ortools.multiple_knapsack_data; + -- PREPARE bin_packing_test AS SELECT * FROM ortools.bin_packing_data; + + -- RETURN QUERY + -- SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); + -- RETURN QUERY + -- SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); + -- RETURN QUERY + -- SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); + -- vrp_knapsack params = ARRAY[ '$$SELECT * FROM ortools.knapsack_data$$', @@ -38,20 +35,27 @@ BEGIN ]::TEXT[]; subs = ARRAY[ 'NULL', + 'capacity => 15', 'NULL' ]::TEXT[]; + error_messages = ARRAY[ + '', + '', + '' + ]::TEXT[]; + non_empty_args = ARRAY[0, 1, 2]::INTEGER[]; - RETURN QUERY SELECT * FROM no_crash_test('vrp_knapsack', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_knapsack', params, subs, error_messages, non_empty_args); --vrp_multiple_knapsack params = ARRAY[ - '$$SELECT * FROM ortools.multiple_knapsack_data$$', + '$$multiple_knapsack_test$$', 'capacities => ARRAY[100,100,100,100,100]', 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', - 'NULL', + 'capacities => ARRAY[100,100,100,100,100]', 'NULL' ]::TEXT[]; @@ -59,13 +63,13 @@ BEGIN -- bin_packing params = ARRAY[ - '$$SELECT * FROM ortools.bin_packing_data$$', + '$$bin_packing_test$$', 'bin_capacity => 100' 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', - 'NULL', + 'bin_capacity => 100', 'NULL' ]::TEXT[]; @@ -74,7 +78,6 @@ END $BODY$ LANGUAGE plpgsql VOLATILE; - SELECT * FROM no_crash(); ROLLBACK; From 114785f7f80e4ed890bd2c3d670f18a0da7c64de Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:01:33 +0530 Subject: [PATCH 04/22] [pgtap] Added pgtap inner_query --- pgtap/or_tools/inner_query.sql | 100 +++++++++++++++++++++++++++++++-- 1 file changed, 95 insertions(+), 5 deletions(-) diff --git a/pgtap/or_tools/inner_query.sql b/pgtap/or_tools/inner_query.sql index dc7816ad4a..14a3216439 100644 --- a/pgtap/or_tools/inner_query.sql +++ b/pgtap/or_tools/inner_query.sql @@ -1,10 +1,100 @@ BEGIN; +SET search_path TO 'ortools', 'public'; +SELECT CASE WHEN min_version('0.3.0') THEN plan (30) ELSE plan(1) END; -CREATE OR REPLACE FUNCTION knapsack() -RETURNS INTEGER AS +CREATE OR REPLACE FUNCTION test_value(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT, accept TEXT[], reject TEXT[]) +RETURNS SETOF TEXT AS $BODY$ - RETURN SELECT +DECLARE + end_sql TEXT; + query TEXT; + p TEXT; + type_name TEXT; +BEGIN + start_sql = 'SELECT * FROM ' || fn || '(' || start_sql || '$$ SELECT '; + FOREACH p IN ARRAY params + LOOP + IF p = parameter THEN CONTINUE; + END IF; + start_sql = start_sql || p || ', '; + END LOOP; + end_sql = ' FROM ' || inner_query_table || '$$' || rest_sql; + + FOREACH type_name IN ARRAY accept + LOOP + query := start_sql || parameter || '::' || type_name || end_sql; + RETURN query SELECT lives_ok(query); + END LOOP; + + FOREACH type_name IN ARRAY reject + LOOP + query := start_sql || parameter || '::' || type_name || end_sql; + RETURN query SELECT throws_ok(query, 38000); + END LOOP; +END; +$BODY$ LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION test_anyInteger(fn TEXT, inner_query_table TEXT, start_sql TEXT, rest_sql TEXT, params TEXT[], parameter TEXT) +RETURNS SETOF TEXT AS +$BODY$ +DECLARE + accept TEXT[] := ARRAY['SMALLINT', 'INTEGER', 'BIGINT']; + reject TEXT[] := ARRAY['REAL', 'FLOAT8', 'NUMERIC']; +BEGIN + RETURN query SELECT test_value(fn, inner_query_table, start_sql, rest_sql, params, parameter, accept, reject); +END; +$BODY$ LANGUAGE plpgsql; + + +CREATE OR REPLACE FUNCTION inner_query() +RETURNS SETOF TEXT AS +$BODY$ +DECLARE + fn TEXT; + start_sql TEXT; + rest_sql TEXT; + inner_query_table TEXT; + params TEXT[]; +BEGIN + IF NOT min_version('0.3.0') THEN + RETURN QUERY + SELECT skip(1, 'Function is modified on 0.3.0'); + RETURN; + END IF; + + -- vrp_knapsack + fn := 'vrp_knapsack'; + inner_query_table := 'knapsack_data'; + start_sql := ''; + rest_sql := ', 15)'; + params := ARRAY['weight', 'cost']; + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'weight'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'cost'); + + fn := 'vrp_multiple_knapsack'; + inner_query_table := 'multiple_knapsack_data'; + start_sql := ''; + rest_sql := ', ARRAY[100,100,100,100,100])'; + params := ARRAY['weight', 'cost']; + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'weight'); + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'cost'); + + fn := 'vrp_bin_packing'; + inner_query_table := 'bin_packing_data'; + start_sql := ''; + rest_sql := ', 100)'; + params := ARRAY['weight']; + RETURN QUERY SELECT test_anyInteger(fn, inner_query_table, start_sql, rest_sql, params, 'weight'); + +END; $BODY$ -LANGUAGE plpgsql VOLATILE; -ROLLBACK; \ No newline at end of file +LANGUAGE plpgsql; + +SELECT inner_query(); + + + +SELECT * FROM finish(); +ROLLBACK; From 80208cedb72899d33570adc27376f4c3322c0b8d Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:02:20 +0530 Subject: [PATCH 05/22] [pgtap] Added pgtap types_check --- pgtap/or_tools/types_check.sql | 95 ++++++++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 pgtap/or_tools/types_check.sql diff --git a/pgtap/or_tools/types_check.sql b/pgtap/or_tools/types_check.sql new file mode 100644 index 0000000000..c2260d1b0c --- /dev/null +++ b/pgtap/or_tools/types_check.sql @@ -0,0 +1,95 @@ +BEGIN; +SET search_path TO 'ortools', 'public'; + +SELECT CASE WHEN min_version('0.3.0') THEN plan (15) ELSE plan(1) END; + +CREATE OR REPLACE FUNCTION types_check() +RETURNS SETOF TEXT AS +$BODY$ +BEGIN + + IF NOT min_version('0.3.0') THEN + RETURN QUERY + SELECT skip(1, 'Function is modified on 0.3.0'); + RETURN; + END IF; + + -- vrp_knapsack + RETURN QUERY + SELECT has_function('vrp_knapsack'); + RETURN QUERY + SELECT has_function('vrp_knapsack', ARRAY['text', 'integer', 'integer']); + RETURN QUERY + SELECT function_returns('vrp_knapsack', ARRAY['text', 'integer', 'integer'], 'text'); + + -- parameter names + RETURN QUERY + SELECT bag_has( + $$SELECT proargnames from pg_proc where proname = 'vrp_knapsack'$$, + $$SELECT '{"inner_query","capacity","max_rows"}'::TEXT[]$$ + ); + + -- parameter types + RETURN QUERY + SELECT set_eq( + $$SELECT proallargtypes from pg_proc where proname = 'vrp_knapsack'$$, + $$VALUES + (NULL::OID[]) + $$ + ); + + -- vrp_multiple_knapsack + RETURN QUERY + SELECT has_function('vrp_multiple_knapsack'); + RETURN QUERY + SELECT has_function('vrp_multiple_knapsack', ARRAY['text', 'integer[]', 'integer']); + RETURN QUERY + SELECT function_returns('vrp_multiple_knapsack', ARRAY['text', 'integer[]', 'integer'], 'text'); + + -- parameter names + RETURN QUERY + SELECT bag_has( + $$SELECT proargnames from pg_proc where proname = 'vrp_multiple_knapsack'$$, + $$SELECT '{"inner_query","capacities","max_rows"}'::TEXT[]$$ + ); + + -- parameter types + RETURN QUERY + SELECT set_eq( + $$SELECT proallargtypes from pg_proc where proname = 'vrp_multiple_knapsack'$$, + $$VALUES + (NULL::OID[]) + $$ + ); + + -- vrp_bin_packing + RETURN QUERY + SELECT has_function('vrp_bin_packing'); + RETURN QUERY + SELECT has_function('vrp_bin_packing', ARRAY['text', 'integer', 'integer']); + RETURN QUERY + SELECT function_returns('vrp_bin_packing', ARRAY['text', 'integer', 'integer'], 'text'); + + -- parameter names + RETURN QUERY + SELECT bag_has( + $$SELECT proargnames from pg_proc where proname = 'vrp_bin_packing'$$, + $$SELECT '{"inner_query","bin_capacity","max_rows"}'::TEXT[]$$ + ); + + -- parameter types + RETURN QUERY + SELECT set_eq( + $$SELECT proallargtypes from pg_proc where proname = 'vrp_bin_packing'$$, + $$VALUES + (NULL::OID[]) + $$ + ); +END; +$BODY$ +LANGUAGE plpgsql; + +SELECT types_check(); + +SELECT * FROM finish(); +ROLLBACK; From a271c49f2213a54d8e70a6535f90f652eca40fe3 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:07:18 +0530 Subject: [PATCH 06/22] [pgtap] Added pgtap no_crash_test --- pgtap/or_tools/no_crash_test.sql | 57 +++++++++++++++----------------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/pgtap/or_tools/no_crash_test.sql b/pgtap/or_tools/no_crash_test.sql index f2df722885..1408bcb821 100644 --- a/pgtap/or_tools/no_crash_test.sql +++ b/pgtap/or_tools/no_crash_test.sql @@ -1,7 +1,7 @@ BEGIN; SET search_path TO 'ortools', 'public'; SET client_min_messages TO ERROR; -SELECT plan(13); +SELECT CASE WHEN min_version('0.3.0') THEN plan (116) ELSE plan(1) END; CREATE OR REPLACE FUNCTION no_crash() RETURNS SETOF TEXT AS @@ -12,21 +12,6 @@ subs TEXT[]; error_messages TEXT[]; non_empty_args INTEGER[]; BEGIN - -- DEALLOCATE knapsack_test; - -- DEALLOCATE multiple_knapsack_test; - -- DEALLOCATE bin_packing_test; - - -- PREPARE knapsack_test AS SELECT * FROM ortools.knapsack_data; - -- PREPARE multiple_knapsack_test AS SELECT * FROM ortools.multiple_knapsack_data; - -- PREPARE bin_packing_test AS SELECT * FROM ortools.bin_packing_data; - - -- RETURN QUERY - -- SELECT isnt_empty('knapsack_test', 'Should be not empty to tests be meaningful'); - -- RETURN QUERY - -- SELECT isnt_empty('multiple_test', 'Should be not empty to tests be meaningful'); - -- RETURN QUERY - -- SELECT isnt_empty('bin_packing_test', 'Should be not empty to tests be meaningful'); - -- vrp_knapsack params = ARRAY[ '$$SELECT * FROM ortools.knapsack_data$$', @@ -35,45 +20,57 @@ BEGIN ]::TEXT[]; subs = ARRAY[ 'NULL', - 'capacity => 15', - 'NULL' + 'capacity => NULL', + 'max_rows => NULL' ]::TEXT[]; error_messages = ARRAY[ - '', - '', + 38000, + 38000, '' ]::TEXT[]; - non_empty_args = ARRAY[0, 1, 2]::INTEGER[]; + non_empty_args = ARRAY[0, 1, 2, 3]::INTEGER[]; RETURN QUERY SELECT * FROM no_crash_test('vrp_knapsack', params, subs, error_messages, non_empty_args); --vrp_multiple_knapsack params = ARRAY[ - '$$multiple_knapsack_test$$', + '$$SELECT * FROM ortools.multiple_knapsack_data$$', 'capacities => ARRAY[100,100,100,100,100]', 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', - 'capacities => ARRAY[100,100,100,100,100]', - 'NULL' + 'capacities => NULL', + 'max_rows => NULL' ]::TEXT[]; + error_messages = ARRAY[ + 38000, + 38000, + '' + ]::TEXT[]; + non_empty_args = ARRAY[0, 1, 2, 3]::INTEGER[]; - RETURN QUERY SELECT * FROM no_crash_test('vrp_multiple_knapsack', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_multiple_knapsack', params, subs, error_messages, non_empty_args); -- bin_packing params = ARRAY[ - '$$bin_packing_test$$', - 'bin_capacity => 100' + '$$SELECT * FROM ortools.bin_packing_data$$', + 'bin_capacity => 100', 'max_rows => 100000' ]::TEXT[]; subs = ARRAY[ 'NULL', - 'bin_capacity => 100', - 'NULL' + 'bin_capacity => NULL', + 'max_rows => NULL' + ]::TEXT[]; + error_messages = ARRAY[ + 38000, + 38000, + '' ]::TEXT[]; + non_empty_args = ARRAY[0, 1, 2, 3]::INTEGER[]; - RETURN QUERY SELECT * FROM no_crash_test('vrp_bin_packing', params, subs); + RETURN QUERY SELECT * FROM no_crash_test('vrp_bin_packing', params, subs, error_messages, non_empty_args); END $BODY$ LANGUAGE plpgsql VOLATILE; From 790989967bb063078fe4efb137374216d44f43f2 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:09:02 +0530 Subject: [PATCH 07/22] [sql/or_tools] Made Changes to functions to support pgtaps --- sql/or_tools/bin_packing.sql | 14 ++++++++--- sql/or_tools/knapsack.sql | 40 +++++++++++++++++------------- sql/or_tools/multiple_knapsack.sql | 16 +++++++++--- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/sql/or_tools/bin_packing.sql b/sql/or_tools/bin_packing.sql index e94f527403..9a118b779a 100644 --- a/sql/or_tools/bin_packing.sql +++ b/sql/or_tools/bin_packing.sql @@ -48,6 +48,14 @@ AS $$ plpy.error(err) return "Failed" + global max_rows + if inner_query == None: + raise Exception('Inner Query Cannot be NULL') + if bin_capacity == None: + raise Exception('Capacity Cannot be NULL') + if max_rows == None: + max_rows = 100000 + plpy.notice('Entering Bin Packing program') plpy.notice('Starting Execution of inner query') @@ -70,10 +78,10 @@ AS $$ else: plpy.error("Expected column weight, Got ", colnames) return "Failed" - if coltypes == [23]: + if all(item in [20, 21, 23] for item in coltypes): plpy.notice("SQL query returned expected column types") else: - plpy.error("Returned columns of different type. Expected Integer") + raise Exception("Returned columns of different type. Expected Integer") plpy.notice('Finished Execution of inner query') data = {} @@ -147,7 +155,7 @@ AS $$ plpy.notice('The problem does not have an optimal solution') plpy.notice('Exiting Bin Packing program') return "Success" -$$ LANGUAGE plpython3u VOLATILE STRICT; +$$ LANGUAGE plpython3u VOLATILE; -- SELECT * FROM vrp_bin_packing('SELECT * FROM bin_packing_data', 100); diff --git a/sql/or_tools/knapsack.sql b/sql/or_tools/knapsack.sql index a4abcad22d..a4b3328c08 100644 --- a/sql/or_tools/knapsack.sql +++ b/sql/or_tools/knapsack.sql @@ -25,20 +25,20 @@ 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*/ -DROP FUNCTION IF EXISTS vrp_knapsack; --- DROP TABLE IF EXISTS knapsack_data; +DROP FUNCTION IF EXISTS vrp_knapsack CASCADE; +DROP TABLE IF EXISTS knapsack_data; --- CREATE TABLE knapsack_data( --- weight INTEGER, --- cost INTEGER); +CREATE TABLE knapsack_data( + weight NUMERIC, + cost INTEGER); --- INSERT INTO knapsack_data (weight, cost) --- VALUES --- (12, 4), --- (2, 2), --- (1, 1), --- (4, 10), --- (1, 2); +INSERT INTO knapsack_data (weight, cost) +VALUES +(12, 4), +(2, 2), +(1, 1), +(4, 10), +(1, 2); CREATE OR REPLACE FUNCTION vrp_knapsack( inner_query TEXT, -- weights_cost SQL @@ -53,6 +53,13 @@ AS $$ plpy.error(err) return "Failed" + global max_rows + if inner_query == None: + raise Exception('Inner Query Cannot be NULL') + if capacity == None: + raise Exception('Capacity Cannot be NULL') + if max_rows == None: + max_rows = 100000 try: solver = pywrapknapsack_solver.KnapsackSolver( pywrapknapsack_solver.KnapsackSolver. @@ -60,8 +67,7 @@ AS $$ except: plpy.error('Unable to Initialize Knapsack Solver') return "Failed" - - + capacities = [] capacities.append(capacity) @@ -87,10 +93,10 @@ AS $$ else: plpy.error("Expected columns weight and cost, Got ", colnames) return "Failed" - if coltypes == [23, 23]: + if all(item in [20, 21, 23] for item in coltypes): plpy.notice("SQL query returned expected column types") else: - plpy.error("Returned columns of different type. Expected Integer, Integer") + raise Exception("Returned columns of different type. Expected Integer, Integer") plpy.notice('Finished Execution of inner query') @@ -127,7 +133,7 @@ AS $$ plpy.info("Packed values: ", packed_values) plpy.notice("Exiting program") return "Success" -$$ LANGUAGE plpython3u VOLATILE STRICT; +$$ LANGUAGE plpython3u VOLATILE; -- SELECT * FROM vrp_knapsack('SELECT * FROM knapsack_data' , 15); diff --git a/sql/or_tools/multiple_knapsack.sql b/sql/or_tools/multiple_knapsack.sql index 7953ec256d..030165b101 100644 --- a/sql/or_tools/multiple_knapsack.sql +++ b/sql/or_tools/multiple_knapsack.sql @@ -62,7 +62,15 @@ AS $$ except Exception as err: plpy.error(err) return "Failed" - + + global max_rows + if inner_query == None: + raise Exception('Inner Query Cannot be NULL') + if capacities == None: + raise Exception('Capacity Cannot be NULL') + if max_rows == None: + max_rows = 100000 + data = {} data['values'] = [] data['weights'] = [] @@ -89,10 +97,10 @@ AS $$ else: plpy.error("Expected columns weight and cost, Got ", colnames) return "Failed" - if coltypes == [23, 23]: + if all(item in [20, 21, 23] for item in coltypes): plpy.notice("SQL query returned expected column types") else: - plpy.error("Returned columns of different type. Expected Integer, Integer") + raise Exception("Returned columns of different type. Expected Integer, Integer") plpy.notice('Finished Execution of inner query') @@ -164,7 +172,7 @@ AS $$ plpy.notice('The problem does not have an optimal solution.') plpy.notice('Exiting Multiple Knapsack program') return "Success" -$$ LANGUAGE plpython3u VOLATILE STRICT; +$$ LANGUAGE plpython3u VOLATILE; -- SELECT * FROM vrp_multiple_knapsack('SELECT * FROM multiple_knapsack_data', ARRAY[100,100,100,100,100]); From cf8776c06c1494f62c357718949b985965a6a413 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:45:56 +0530 Subject: [PATCH 08/22] [docqueries] Created Docqueries for vrp_knapsack --- docqueries/or_tools/knapsack.result | 51 +++++++++++++++++++++++++++ docqueries/or_tools/knapsack.test.sql | 9 ++--- 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/docqueries/or_tools/knapsack.result b/docqueries/or_tools/knapsack.result index e69de29bb2..56ea13995b 100644 --- a/docqueries/or_tools/knapsack.result +++ b/docqueries/or_tools/knapsack.result @@ -0,0 +1,51 @@ +BEGIN; +BEGIN +SET client_min_messages TO NOTICE; +SET +DROP TABLE IF EXISTS knapsack_query; +NOTICE: table "knapsack_query" does not exist, skipping +DROP TABLE +CREATE TABLE knapsack_query( + weight INTEGER, + cost INTEGER); +CREATE TABLE +INSERT INTO knapsack_query(weight, cost) +VALUES +(12, 4), +(2, 2), +(1, 1), +(4, 10), +(1, 2); +INSERT 0 5 +SELECT * +FROM knapsack_query; + weight | cost +--------+------ + 12 | 4 + 2 | 2 + 1 | 1 + 4 | 10 + 1 | 2 +(5 rows) + +SELECT * +FROM vrp_knapsack($$SELECT * FROM knapsack_query$$, 15); +NOTICE: Entering Knapsack program +NOTICE: Starting Execution of inner query +INFO: ('Number of rows processed : ', 5) +NOTICE: SQL query returned expected column names +NOTICE: SQL query returned expected column types +NOTICE: Finished Execution of inner query +INFO: ('Total value =', 15) +INFO: ('Total weight:', 8) +INFO: ('Packed items: ', [1, 2, 3, 4]) +INFO: ('Packed weights: ', [2, 1, 4, 1]) +INFO: ('Packed values: ', [2, 1, 10, 2]) +NOTICE: Exiting program + vrp_knapsack +-------------- + Success +(1 row) + +ROLLBACK; +ROLLBACK \ No newline at end of file diff --git a/docqueries/or_tools/knapsack.test.sql b/docqueries/or_tools/knapsack.test.sql index e3517462b4..f0c9aae63f 100644 --- a/docqueries/or_tools/knapsack.test.sql +++ b/docqueries/or_tools/knapsack.test.sql @@ -1,8 +1,9 @@ -CREATE TABLE knapsack_data( +DROP TABLE IF EXISTS knapsack_query; +CREATE TABLE knapsack_query( weight INTEGER, cost INTEGER); -INSERT INTO knapsack_data (weight, cost) +INSERT INTO knapsack_query(weight, cost) VALUES (12, 4), (2, 2), @@ -11,7 +12,7 @@ VALUES (1, 2); SELECT * -FROM knapsack_data; +FROM knapsack_query; SELECT * -FROM vrp_knapsack($$SELECT weight, cost FROM knapsack_data$$, 3); +FROM vrp_knapsack($$SELECT * FROM knapsack_query$$, 15); From fdf1aba82181a7de42a54adce56dd2cfe813abd9 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:48:24 +0530 Subject: [PATCH 09/22] [docqueries] Configuring Cmakelists and tests.conf --- docqueries/or_tools/CMakeLists.txt | 2 ++ docqueries/or_tools/test.conf | 4 ++++ 2 files changed, 6 insertions(+) diff --git a/docqueries/or_tools/CMakeLists.txt b/docqueries/or_tools/CMakeLists.txt index 990e45f2a2..801f5874df 100644 --- a/docqueries/or_tools/CMakeLists.txt +++ b/docqueries/or_tools/CMakeLists.txt @@ -1,5 +1,7 @@ SET(LOCAL_FILES knapsack + multiple_knapsack + bin_packing ) foreach (f ${LOCAL_FILES}) diff --git a/docqueries/or_tools/test.conf b/docqueries/or_tools/test.conf index 94df081b96..2352c30c94 100644 --- a/docqueries/or_tools/test.conf +++ b/docqueries/or_tools/test.conf @@ -7,10 +7,14 @@ 'newdb' => 1, 'tests' => [qw( knapsack + multiple_knapsack + bin_packing )], 'documentation' => [qw( knapsack + multiple_knapsack + bin_packing )], 'nottesting' => [qw( From c188c17a49242ac15dd7a1bbc5062d477cdff60a Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:49:58 +0530 Subject: [PATCH 10/22] [docqueries] Created empty files for other ortools functions --- docqueries/or_tools/bin_packing.result | 0 docqueries/or_tools/bin_packing.test.sql | 0 docqueries/or_tools/multiple_knapsack.result | 0 docqueries/or_tools/multiple_knapsack.test.sql | 0 4 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 docqueries/or_tools/bin_packing.result create mode 100644 docqueries/or_tools/bin_packing.test.sql create mode 100644 docqueries/or_tools/multiple_knapsack.result create mode 100644 docqueries/or_tools/multiple_knapsack.test.sql diff --git a/docqueries/or_tools/bin_packing.result b/docqueries/or_tools/bin_packing.result new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docqueries/or_tools/bin_packing.test.sql b/docqueries/or_tools/bin_packing.test.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docqueries/or_tools/multiple_knapsack.result b/docqueries/or_tools/multiple_knapsack.result new file mode 100644 index 0000000000..e69de29bb2 diff --git a/docqueries/or_tools/multiple_knapsack.test.sql b/docqueries/or_tools/multiple_knapsack.test.sql new file mode 100644 index 0000000000..e69de29bb2 From 2d846fc80e0cb869f382359746e760f5ea204499 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:58:43 +0530 Subject: [PATCH 11/22] [docqueries] Added docqueries for multiple_knapsack --- docqueries/or_tools/bin_packing.result | 47 ++++++++++++++++++++++++ docqueries/or_tools/bin_packing.test.sql | 10 +++++ 2 files changed, 57 insertions(+) diff --git a/docqueries/or_tools/bin_packing.result b/docqueries/or_tools/bin_packing.result index e69de29bb2..d9c3b8dc2d 100644 --- a/docqueries/or_tools/bin_packing.result +++ b/docqueries/or_tools/bin_packing.result @@ -0,0 +1,47 @@ +BEGIN; +BEGIN +SET client_min_messages TO NOTICE; +SET +DROP TABLE IF EXISTS bin_packing_query CASCADE; +NOTICE: table "bin_packing_query" does not exist, skipping +DROP TABLE +CREATE TABLE bin_packing_query( + weight INTEGER); +CREATE TABLE +INSERT INTO bin_packing_query (weight) +VALUES +(48), (30), (19), (36), (36), (27), (42), (42), (36), (24), (30); +INSERT 0 11 +SELECT * FROM vrp_bin_packing('SELECT * FROM bin_packing_query', 100); +NOTICE: Entering Bin Packing program +NOTICE: Starting Execution of inner query +INFO: ('Number of rows processed : ', 11) +NOTICE: SQL query returned expected column names +NOTICE: SQL query returned expected column types +NOTICE: Finished Execution of inner query +NOTICE: SCIP solver ready! +INFO: ('Bin number', 0) +INFO: (' Items packed', [0, 1, 2]) +INFO: (' Item weights', [48, 30, 19]) +INFO: (' Total weight', 97) +INFO: ('Bin number', 1) +INFO: (' Items packed', [3, 4, 5]) +INFO: (' Item weights', [36, 36, 27]) +INFO: (' Total weight', 99) +INFO: ('Bin number', 2) +INFO: (' Items packed', [6, 7]) +INFO: (' Item weights', [42, 42]) +INFO: (' Total weight', 84) +INFO: ('Bin number', 3) +INFO: (' Items packed', [8, 9, 10]) +INFO: (' Item weights', [36, 24, 30]) +INFO: (' Total weight', 90) +INFO: ('Number of bins used', 4.0) +NOTICE: Exiting Bin Packing program + vrp_bin_packing +----------------- + Success +(1 row) + +ROLLBACK; +ROLLBACK \ No newline at end of file diff --git a/docqueries/or_tools/bin_packing.test.sql b/docqueries/or_tools/bin_packing.test.sql index e69de29bb2..de8df42ef0 100644 --- a/docqueries/or_tools/bin_packing.test.sql +++ b/docqueries/or_tools/bin_packing.test.sql @@ -0,0 +1,10 @@ +DROP TABLE IF EXISTS bin_packing_query CASCADE; + +CREATE TABLE bin_packing_query( + weight INTEGER); + +INSERT INTO bin_packing_query (weight) +VALUES +(48), (30), (19), (36), (36), (27), (42), (42), (36), (24), (30); + +SELECT * FROM vrp_bin_packing('SELECT * FROM bin_packing_query', 100); \ No newline at end of file From bc6e4e8f45b4fa9853eb266f6c51cb182902fc4a Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 00:59:13 +0530 Subject: [PATCH 12/22] [docqueries] Added docqueries for bin packing --- docqueries/or_tools/multiple_knapsack.result | 96 +++++++++++++++++++ .../or_tools/multiple_knapsack.test.sql | 29 ++++++ 2 files changed, 125 insertions(+) diff --git a/docqueries/or_tools/multiple_knapsack.result b/docqueries/or_tools/multiple_knapsack.result index e69de29bb2..b9c28ac74c 100644 --- a/docqueries/or_tools/multiple_knapsack.result +++ b/docqueries/or_tools/multiple_knapsack.result @@ -0,0 +1,96 @@ +BEGIN; +BEGIN +SET client_min_messages TO NOTICE; +SET +DROP TABLE IF EXISTS multiple_knapsack_query CASCADE; +NOTICE: table "multiple_knapsack_query" does not exist, skipping +DROP TABLE +CREATE TABLE multiple_knapsack_query( + weight INTEGER, + cost INTEGER); +CREATE TABLE +INSERT INTO multiple_knapsack_query (weight, cost) +VALUES +(48, 10), +(30, 30), +(42, 25), +(36, 50), +(36, 35), +(48, 30), +(42, 15), +(42, 40), +(36, 30), +(24, 35), +(30, 45), +(30, 10), +(42, 20), +(36, 30), +(36, 25); +INSERT 0 15 +SELECT * +FROM multiple_knapsack_query; + weight | cost +--------+------ + 48 | 10 + 30 | 30 + 42 | 25 + 36 | 50 + 36 | 35 + 48 | 30 + 42 | 15 + 42 | 40 + 36 | 30 + 24 | 35 + 30 | 45 + 30 | 10 + 42 | 20 + 36 | 30 + 36 | 25 +(15 rows) + +SELECT * +FROM vrp_multiple_knapsack('SELECT * FROM multiple_knapsack_query', ARRAY[100,100,100,100,100]); +NOTICE: Entering Mulitple Knapsack program +NOTICE: Starting Execution of inner query +INFO: ('Number of rows processed : ', 15) +NOTICE: SQL query returned expected column names +NOTICE: SQL query returned expected column types +NOTICE: Finished Execution of inner query +NOTICE: SCIP solver ready! +INFO: ('Total value =', 395.0) +INFO: ('Bin :', 0) +INFO: Item 1 - weight: 30 value: 30 +INFO: Item 3 - weight: 36 value: 50 +INFO: Item 10 - weight: 30 value: 45 +INFO: ('Packed bin weight', 96) +INFO: ('Packed bin value', 125) +INFO: ('Bin :', 1) +INFO: Item 2 - weight: 42 value: 25 +INFO: Item 14 - weight: 36 value: 25 +INFO: ('Packed bin weight', 78) +INFO: ('Packed bin value', 50) +INFO: ('Bin :', 2) +INFO: Item 4 - weight: 36 value: 35 +INFO: Item 9 - weight: 24 value: 35 +INFO: Item 13 - weight: 36 value: 30 +INFO: ('Packed bin weight', 96) +INFO: ('Packed bin value', 100) +INFO: ('Bin :', 3) +INFO: Item 8 - weight: 36 value: 30 +INFO: Item 12 - weight: 42 value: 20 +INFO: ('Packed bin weight', 78) +INFO: ('Packed bin value', 50) +INFO: ('Bin :', 4) +INFO: Item 5 - weight: 48 value: 30 +INFO: Item 7 - weight: 42 value: 40 +INFO: ('Packed bin weight', 90) +INFO: ('Packed bin value', 70) +INFO: ('Total packed weight', 438) +NOTICE: Exiting Multiple Knapsack program + vrp_multiple_knapsack +----------------------- + Success +(1 row) + +ROLLBACK; +ROLLBACK \ No newline at end of file diff --git a/docqueries/or_tools/multiple_knapsack.test.sql b/docqueries/or_tools/multiple_knapsack.test.sql index e69de29bb2..b6c1be331a 100644 --- a/docqueries/or_tools/multiple_knapsack.test.sql +++ b/docqueries/or_tools/multiple_knapsack.test.sql @@ -0,0 +1,29 @@ +DROP TABLE IF EXISTS multiple_knapsack_query CASCADE; + +CREATE TABLE multiple_knapsack_query( + weight INTEGER, + cost INTEGER); + +INSERT INTO multiple_knapsack_query (weight, cost) +VALUES +(48, 10), +(30, 30), +(42, 25), +(36, 50), +(36, 35), +(48, 30), +(42, 15), +(42, 40), +(36, 30), +(24, 35), +(30, 45), +(30, 10), +(42, 20), +(36, 30), +(36, 25); + +SELECT * +FROM multiple_knapsack_query; + +SELECT * +FROM vrp_multiple_knapsack('SELECT * FROM multiple_knapsack_query', ARRAY[100,100,100,100,100]); \ No newline at end of file From cb4efd824672adf764e1590dbd94c8e7ec5b4fe5 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 01:05:24 +0530 Subject: [PATCH 13/22] [pgtap] Corrected number of tests --- pgtap/or_tools/no_crash_test.sql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pgtap/or_tools/no_crash_test.sql b/pgtap/or_tools/no_crash_test.sql index 1408bcb821..4c41e0ca3f 100644 --- a/pgtap/or_tools/no_crash_test.sql +++ b/pgtap/or_tools/no_crash_test.sql @@ -1,7 +1,7 @@ BEGIN; SET search_path TO 'ortools', 'public'; SET client_min_messages TO ERROR; -SELECT CASE WHEN min_version('0.3.0') THEN plan (116) ELSE plan(1) END; +SELECT CASE WHEN min_version('0.3.0') THEN plan (18) ELSE plan(1) END; CREATE OR REPLACE FUNCTION no_crash() RETURNS SETOF TEXT AS From f0d2b4160b97096fc0d052700a80c9b65db8ee7c Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 12:11:53 +0530 Subject: [PATCH 14/22] [workflows] building ortools in github actions --- .github/workflows/ubuntu.yml | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index a06727be9a..c0f5053cf7 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -59,7 +59,11 @@ jobs: postgresql-${{ matrix.psql }}-pgrouting \ postgresql-server-dev-${{ matrix.psql }} \ postgresql-plpython3-${{ matrix.psql }} - + - name: Install Google OR-Tools dependencies + run: | + umask 022 + python -m pip install --upgrade --user ortools + - name: Install VROOM dependencies run: | sudo apt-get install libssl-dev libasio-dev libglpk-dev From 34a9690f115a287712520a3b3e51c1073e951f48 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 12:15:14 +0530 Subject: [PATCH 15/22] [worflows] experimenting with permission settings --- .github/workflows/ubuntu.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index c0f5053cf7..5e1f71a264 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -59,9 +59,9 @@ jobs: postgresql-${{ matrix.psql }}-pgrouting \ postgresql-server-dev-${{ matrix.psql }} \ postgresql-plpython3-${{ matrix.psql }} + - name: Install Google OR-Tools dependencies run: | - umask 022 python -m pip install --upgrade --user ortools - name: Install VROOM dependencies From c3b44b7fa61838e1a7c0d2f1066f25feb871ef11 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 12:17:40 +0530 Subject: [PATCH 16/22] [worflows] experimenting with permission settings --- .github/workflows/ubuntu.yml | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 5e1f71a264..5fb18106fc 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -62,7 +62,10 @@ jobs: - name: Install Google OR-Tools dependencies run: | - python -m pip install --upgrade --user ortools + sudo su + umask 022 + pip3 install ortools + exit - name: Install VROOM dependencies run: | From b92d8b4c56d918d31eb24ffbdc309e8fc5dbb575 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 13:55:29 +0530 Subject: [PATCH 17/22] [.github] trying ortools indist-packages folder --- .github/workflows/ubuntu.yml | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 5fb18106fc..429200c02d 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -62,10 +62,7 @@ jobs: - name: Install Google OR-Tools dependencies run: | - sudo su - umask 022 - pip3 install ortools - exit + sudo pip install --root=/ ortools - name: Install VROOM dependencies run: | From 28f6d3cf9657133af7edd490e79ea8d9477425a6 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 13:59:17 +0530 Subject: [PATCH 18/22] [.github] checking where ortools is installed in github actions --- .github/workflows/ubuntu.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 429200c02d..5bac647b2e 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -63,6 +63,7 @@ jobs: - name: Install Google OR-Tools dependencies run: | sudo pip install --root=/ ortools + sudo pip install --root=/ ortools - name: Install VROOM dependencies run: | From 2de1cfa1a1c95e3559bd7ef83b272d7f155fc84a Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 14:08:33 +0530 Subject: [PATCH 19/22] [workflows] Google OR-Tools built succesfully in github actions --- .github/workflows/ubuntu.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/ubuntu.yml b/.github/workflows/ubuntu.yml index 5bac647b2e..429200c02d 100644 --- a/.github/workflows/ubuntu.yml +++ b/.github/workflows/ubuntu.yml @@ -63,7 +63,6 @@ jobs: - name: Install Google OR-Tools dependencies run: | sudo pip install --root=/ ortools - sudo pip install --root=/ ortools - name: Install VROOM dependencies run: | From 47d504d49a313caebb1b57011aedec6ddbfa2027 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 16:15:02 +0530 Subject: [PATCH 20/22] [sql] Partial Documentation update --- sql/or_tools/bin_packing.sql | 25 +++++++++++++++++++++++++ sql/or_tools/knapsack.sql | 27 +++++++++++++++++++++++++++ sql/or_tools/multiple_knapsack.sql | 26 ++++++++++++++++++++++++++ sql/or_tools/plpython/envir.sql | 8 ++++++++ 4 files changed, 86 insertions(+) create mode 100644 sql/or_tools/plpython/envir.sql diff --git a/sql/or_tools/bin_packing.sql b/sql/or_tools/bin_packing.sql index 9a118b779a..92f6e17d19 100644 --- a/sql/or_tools/bin_packing.sql +++ b/sql/or_tools/bin_packing.sql @@ -24,6 +24,31 @@ 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*/ +/* +.. signature start + +:: + + vrp_bin_packing(weight SQL, bin_capacity Integer [max_cycles]) + RETURNS SET OF: + + +.. signature end + +.. parameters start + +================= ================== ========= ================================================= +Column Type Default Description +================= ================== ========= ================================================= +**weight SQL** ``TEXT`` `weight_cost SQL`_ query contianing the weights and cost of each item +**bin_capacity** ``INTEGER`` capacity of the Bin +**max_rows** ``INTEGER`` 100000 Maximum number of items(rows) to fetch from table. + +================= ================== ========= ================================================= + +.. parameters end + +*/ DROP FUNCTION IF EXISTS vrp_bin_packing CASCADE; -- DROP TABLE IF EXISTS bin_packing_data CASCADE; diff --git a/sql/or_tools/knapsack.sql b/sql/or_tools/knapsack.sql index a4b3328c08..6032bb08bd 100644 --- a/sql/or_tools/knapsack.sql +++ b/sql/or_tools/knapsack.sql @@ -25,6 +25,33 @@ 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*/ + +/* +.. signature start + +:: + + vrp_knapsack(weight_cost SQL, capacity Integer [max_cycles]) + RETURNS SET OF: + + +.. signature end + +.. parameters start + +================= ================== ========= ================================================= +Column Type Default Description +================= ================== ========= ================================================= +**weight_cost SQL** ``TEXT`` `weight_cost SQL`_ query contianing the weights and cost of each item +**capacity** ``INTEGER`` Capacity of the knapsack +**max_rows** ``INTEGER`` 100000 Maximum number of items(rows) to fetch from table. + +================= ================== ========= ================================================= + +.. parameters end + +*/ + DROP FUNCTION IF EXISTS vrp_knapsack CASCADE; DROP TABLE IF EXISTS knapsack_data; diff --git a/sql/or_tools/multiple_knapsack.sql b/sql/or_tools/multiple_knapsack.sql index 030165b101..3423d92398 100644 --- a/sql/or_tools/multiple_knapsack.sql +++ b/sql/or_tools/multiple_knapsack.sql @@ -24,6 +24,32 @@ 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*/ +/* +.. signature start + +:: + + vrp_multiple_knapsack(weight_cost SQL, capacities Integer[] [max_cycles]) + RETURNS SET OF: + + +.. signature end + +.. parameters start + +================= ================== ========= ================================================= +Column Type Default Description +================= ================== ========= ================================================= +**weight_cost SQL** ``TEXT`` `weight_cost SQL`_ query contianing the weights and cost of each item +**capacities** ``INTEGER[]`` Capacities of each knapsack +**max_rows** ``INTEGER`` 100000 Maximum number of items(rows) to fetch from table. + +================= ================== ========= ================================================= + +.. parameters end + +*/ + DROP FUNCTION IF EXISTS vrp_multiple_knapsack CASCADE; -- DROP TABLE IF EXISTS multiple_knapsack_data CASCADE; diff --git a/sql/or_tools/plpython/envir.sql b/sql/or_tools/plpython/envir.sql new file mode 100644 index 0000000000..3df6643f4a --- /dev/null +++ b/sql/or_tools/plpython/envir.sql @@ -0,0 +1,8 @@ +CREATE OR REPLACE FUNCTION blah(varchar) RETURNS integer AS $$ + import sys + import ortools + plpy.notice("python exec = '%s'" % sys.executable) + plpy.notice("python version = '%s'" % sys.version) + plpy.notice("python path = '%s'" % sys.path) + return 1 +$$ LANGUAGE plpython3u; \ No newline at end of file From 37bfe8514fe38eb3d0b4b3ab2f4b3032945a9657 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 16:22:34 +0530 Subject: [PATCH 21/22] [doc] Doc template for OR-Tools functions --- doc/or_tools/CMakeLists.txt | 2 + doc/or_tools/bin_packing.rst | 78 ++++++++++++++++++++++++++++++ doc/or_tools/knapsack.rst | 10 ++-- doc/or_tools/multiple_knapsack.rst | 77 +++++++++++++++++++++++++++++ 4 files changed, 164 insertions(+), 3 deletions(-) create mode 100644 doc/or_tools/bin_packing.rst create mode 100644 doc/or_tools/multiple_knapsack.rst diff --git a/doc/or_tools/CMakeLists.txt b/doc/or_tools/CMakeLists.txt index 3ed505014d..187513ed02 100644 --- a/doc/or_tools/CMakeLists.txt +++ b/doc/or_tools/CMakeLists.txt @@ -1,5 +1,7 @@ SET(LOCAL_FILES knapsack.rst + multiple_knapsack.rst + bin_packing.rst ) foreach (f ${LOCAL_FILES}) diff --git a/doc/or_tools/bin_packing.rst b/doc/or_tools/bin_packing.rst new file mode 100644 index 0000000000..33662262a9 --- /dev/null +++ b/doc/or_tools/bin_packing.rst @@ -0,0 +1,78 @@ +.. + **************************************************************************** + vrpRouting Manual + Copyright(c) vrpRouting Contributors + This documentation is licensed under a Creative Commons Attribution-Share + Alike 3.0 License: https://creativecommons.org/licenses/by-sa/3.0/ + **************************************************************************** +| + +* `Documentation `__ → `vrpRouting v0 `__ +* Supported Versions + `Latest `__ + (`v0 `__) + +vrp_bin_packing - Experimental +=============================================================================== + +.. include:: experimental.rst + :start-after: begin-warn-expr + :end-before: end-warn-expr + +.. rubric:: Availability + +Version 0.0.0 + +* New **experimental** function + + * vrp_knapsack + + + +Description +------------------------------------------------------------------------------- + +The bin packing problem is an optimization problem, in which +items of different sizes must be packed into a finite number of bins or containers, +each of a fixed given capacity, in a way that minimizes the number of bins used. +The problem has many applications, such as filling up containers, loading trucks with weight capacity constraints, +creating file backups in media and technology mapping in FPGA semiconductor chip design. + +Signatures +------------------------------------------------------------------------------- + +.. include:: ../sql/or_tools/bin_packing.sql + :start-after: signature start + :end-before: signature end + +Parameters +------------------------------------------------------------------------------- + +.. include:: ../sql/or_tools/bin_packing.sql + :start-after: parameters start + :end-before: parameters end + +Inner Queries +------------------------------------------------------------------------------- + +* TBD + +Result Columns +------------------------------------------------------------------------------- + +* TBD + +Example +------------------------------------------------------------------------------- + +* TBD + +See Also +------------------------------------------------------------------------------- + +* TBD + +.. rubric:: Indices and tables + +* :ref:`genindex` +* :ref:`search` \ No newline at end of file diff --git a/doc/or_tools/knapsack.rst b/doc/or_tools/knapsack.rst index 042e7ee127..730f421230 100644 --- a/doc/or_tools/knapsack.rst +++ b/doc/or_tools/knapsack.rst @@ -25,7 +25,7 @@ Version 0.0.0 * New **experimental** function - * pgr_knapsack + * vrp_knapsack @@ -40,12 +40,16 @@ so that the total weight is less than or equal to a given limit and the total va Signatures ------------------------------------------------------------------------------- -* TBD +.. include:: ../sql/or_tools/knapsack.sql + :start-after: signature start + :end-before: signature end Parameters ------------------------------------------------------------------------------- -* TBD +.. include:: ../sql/or_tools/knapsack.sql + :start-after: parameters start + :end-before: parameters end Inner Queries ------------------------------------------------------------------------------- diff --git a/doc/or_tools/multiple_knapsack.rst b/doc/or_tools/multiple_knapsack.rst new file mode 100644 index 0000000000..a6fce60650 --- /dev/null +++ b/doc/or_tools/multiple_knapsack.rst @@ -0,0 +1,77 @@ +.. + **************************************************************************** + vrpRouting Manual + Copyright(c) vrpRouting Contributors + This documentation is licensed under a Creative Commons Attribution-Share + Alike 3.0 License: https://creativecommons.org/licenses/by-sa/3.0/ + **************************************************************************** +| + +* `Documentation `__ → `vrpRouting v0 `__ +* Supported Versions + `Latest `__ + (`v0 `__) + +vrp_multiple_knapsack - Experimental +=============================================================================== + +.. include:: experimental.rst + :start-after: begin-warn-expr + :end-before: end-warn-expr + +.. rubric:: Availability + +Version 0.0.0 + +* New **experimental** function + + * vrp_knapsack + + + +Description +------------------------------------------------------------------------------- + +The multiple knapsack problem is a problem in combinatorial optimization: +it is a more general verison of the classic knapsack problem where instead of a +single knapsack, you will be given multiple knapsacks and your goal is maximise the total +value of packed items in all knapsacks. + +Signatures +------------------------------------------------------------------------------- + +.. include:: ../sql/or_tools/multiple_knapsack.sql + :start-after: signature start + :end-before: signature end + +Parameters +------------------------------------------------------------------------------- + +.. include:: ../sql/or_tools/multiple_knapsack.sql + :start-after: parameters start + :end-before: parameters end + +Inner Queries +------------------------------------------------------------------------------- + +* TBD + +Result Columns +------------------------------------------------------------------------------- + +* TBD + +Example +------------------------------------------------------------------------------- + +* TBD + +See Also +------------------------------------------------------------------------------- + +* TBD + +.. rubric:: Indices and tables + +* :ref:`genindex` +* :ref:`search` \ No newline at end of file From 73b3ed5d7c81d093e736c01d39b3fb2bbebef023 Mon Sep 17 00:00:00 2001 From: Manas Date: Sun, 21 Aug 2022 16:25:12 +0530 Subject: [PATCH 22/22] removed an unnecessary file --- sql/or_tools/plpython/envir.sql | 8 -------- 1 file changed, 8 deletions(-) delete mode 100644 sql/or_tools/plpython/envir.sql diff --git a/sql/or_tools/plpython/envir.sql b/sql/or_tools/plpython/envir.sql deleted file mode 100644 index 3df6643f4a..0000000000 --- a/sql/or_tools/plpython/envir.sql +++ /dev/null @@ -1,8 +0,0 @@ -CREATE OR REPLACE FUNCTION blah(varchar) RETURNS integer AS $$ - import sys - import ortools - plpy.notice("python exec = '%s'" % sys.executable) - plpy.notice("python version = '%s'" % sys.version) - plpy.notice("python path = '%s'" % sys.path) - return 1 -$$ LANGUAGE plpython3u; \ No newline at end of file