Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

pgr_TSP() argument "end_id" doesn't work on MacOS and Ubuntu for PgRouting versions 2.6.2 ... 3.0.1 #1380

Open
AlexeyPechnikov opened this issue Jul 8, 2020 · 16 comments

Comments

@AlexeyPechnikov
Copy link

AlexeyPechnikov commented Jul 8, 2020

Describe the bug
On MacOS in resulting routing sequence 1...N addresses the "end_id" node is N-1 sequential address instead of N. So we can't define the last address.

To Reproduce
Use pgr_TSP() function with defined "end_id" argument. With also defined "start_id" argument the "end_id" probably ignored.

Specifications (please complete the following information):
Use the commands:

SELECT version();
                                                      version                                                      
-------------------------------------------------------------------------------------------------------------------
 PostgreSQL 12.3 on x86_64-apple-darwin18.7.0, compiled by Apple clang version 11.0.0 (clang-1100.0.33.17), 64-bit

SELECT postgis_full_version();
                                                                       postgis_full_version                                                                        
-------------------------------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="3.0.1 ec2a9aa" [EXTENSION] PGSQL="120" GEOS="3.8.1-CAPI-1.13.3" PROJ="7.1.0" LIBXML="2.9.10" LIBJSON="0.14" LIBPROTOBUF="1.3.3" WAGYU="0.4.3 (Internal)"

SELECT pgr_version();
 pgr_version 
-------------
 3.0.1
SELECT pgr_version();
              pgr_version               
----------------------------------------
 (2.6.3,v2.6.3,b14f4d56b,master,1.72.0)
@AlexeyPechnikov AlexeyPechnikov added the bug Something isn't working label Jul 8, 2020
@AlexeyPechnikov
Copy link
Author

On Ubuntu "end_id" is N-2 address (instead of N) when "start_id" is not defined and it's probably ignored when the both "end_id" and "start_id" arguments defined.

SELECT version();
                                                                version                                                                 
----------------------------------------------------------------------------------------------------------------------------------------
 PostgreSQL 10.12 (Ubuntu 10.12-0ubuntu0.18.04.1) on x86_64-pc-linux-gnu, compiled by gcc (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0, 64-bit

SELECT postgis_full_version();
                                                                                                    postgis_full_version                                                                                                    
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 POSTGIS="2.5.2 r17328" [EXTENSION] PGSQL="100" GEOS="3.7.0-CAPI-1.11.0 673b9939" PROJ="Rel. 5.2.0, September 15th, 2018" GDAL="GDAL 2.4.0, released 2018/12/14" LIBXML="2.9.4" LIBJSON="0.12.1" LIBPROTOBUF="1.2.1" RASTER

SELECT pgr_version();
              pgr_version               
----------------------------------------
 (2.6.2,v2.6.2,b14f4d56b,master,1.65.1)

@AlexeyPechnikov AlexeyPechnikov changed the title pgr_TSP() argument "end_id" misinterpreted pgr_TSP() argument "end_id" misinterpreted on MacOS and Ubuntu for PgRouting versions 2.6.2 ... 3.0.1 Jul 8, 2020
@AlexeyPechnikov AlexeyPechnikov changed the title pgr_TSP() argument "end_id" misinterpreted on MacOS and Ubuntu for PgRouting versions 2.6.2 ... 3.0.1 pgr_TSP() argument "end_id" doesn't work on MacOS and Ubuntu for PgRouting versions 2.6.2 ... 3.0.1 Jul 8, 2020
@cvvergara
Copy link
Member

Can you give us a problem to reproduce please

@cvvergara
Copy link
Member

@mobigroup If you can give us the query using the sample data of the documentation would be better.

@cvvergara cvvergara added Bug Report TSP and removed bug Something isn't working labels Jul 21, 2020
@AlexeyPechnikov
Copy link
Author

@cvvergara That's very easy to reproduce. See the code below where we have the same output for end_id := 1 and end_id := 2:

SELECT * FROM pgr_TSP(
    $$
	SELECT * FROM pgr_dijkstraCostMatrix(
		$___$
			SELECT
				1 as id, 1 as source, 2 as target, 1 as cost, 1 as reverse_cost
			UNION
			SELECT
				2 as id, 2 as source, 1 as target, 1 as cost, 1 as reverse_cost
		$___$,
		ARRAY[1,2],
		directed := false
	)
    $$,
    end_id := 1,
    randomize := false
);
seq node cost agg_cost
1 1 1 0
2 2 1 1
3 1 0 2
SELECT * FROM pgr_TSP(
    $$
	SELECT * FROM pgr_dijkstraCostMatrix(
		$___$
			SELECT
				1 as id, 1 as source, 2 as target, 1 as cost, 1 as reverse_cost
			UNION
			SELECT
				2 as id, 2 as source, 1 as target, 1 as cost, 1 as reverse_cost
		$___$,
		ARRAY[1,2],
		directed := false
	)
    $$,
    end_id := 2,
    randomize := false
);
seq node cost agg_cost
1 1 1 0
2 2 1 1
3 1 0 2

Note: we have different output for start_id := 1 and start_id := 2.

@cvvergara
Copy link
Member

hmm, I see the same output in the two queries you posted.

@AlexeyPechnikov
Copy link
Author

hmm, I see the same output in the two queries you posted.

Yes, that’s the same but it needs to be different because end_id is different in the queries above.

@cvvergara
Copy link
Member

cvvergara commented Oct 2, 2020

with start_id 1

SELECT * FROM pgr_TSP(
    $$
 SELECT * FROM pgr_dijkstraCostMatrix(
  $___$
   SELECT
    1 as id, 1 as source, 2 as target, 1 as cost, 1 as reverse_cost
   UNION
   SELECT
    2 as id, 2 as source, 1 as target, 1 as cost, 1 as reverse_cost
  $___$,
  ARRAY[1,2],
  directed := false
 )
    $$,
    start_id := 1,
    randomize := false
);
 seq | node | cost | agg_cost 
-----+------+------+----------
   1 |    1 |    1 |        0
   2 |    2 |    1 |        1
   3 |    1 |    0 |        2
(3 rows)

with start_id = 2

SELECT * FROM pgr_TSP(
    $$
 SELECT * FROM pgr_dijkstraCostMatrix(
  $___$
   SELECT
    1 as id, 1 as source, 2 as target, 1 as cost, 1 as reverse_cost
   UNION
   SELECT
    2 as id, 2 as source, 1 as target, 1 as cost, 1 as reverse_cost
  $___$,
  ARRAY[1,2],
  directed := false
 )
    $$,
    start_id := 2,
    randomize := false
);
 seq | node | cost | agg_cost 
-----+------+------+----------
   1 |    2 |    1 |        0
   2 |    1 |    1 |        1
   3 |    2 |    0 |        2
(3 rows)

So, here is what I see:
Suppose that a solution of a 4 node problem in terms is:

v1 -- v3 -- v4 -- v2 -- v1

when you want v3 to be the start_id the solution is:

v3 -- v4 -- v2 -- v1 -- v3

when you want the end_vid to be v4 (start_vid is v1 from the original problem)

v1 -- v3 -- v2 -- v4 -- v1

which forces v4 -- v1 (when v1 is the start_vid)

This would be ok

v3 -- v4 -- v2 -- v1 -- v3

The reason it is ok, is that the route can be written like

v3 -- v1 -- v2 -- v4 -- v3

@cvvergara
Copy link
Member

I shouldnt have use ->, will change to --

@cvvergara
Copy link
Member

In general,
given N 1 ...... N and w is one of them and is the furthest one from n1
maybe the algorithm chose the n1 as the first one
n1 ..................w................... , n1
when defining that w to be the end_vid what you are telling is to force this situation, and ignore the cost from w to n1
n1 ..........................................w, n1

@cvvergara
Copy link
Member

Please tell me your thoughts

@AlexeyPechnikov
Copy link
Author

Please tell me your thoughts

Here is issue for the end_id option. Why do you write about the different one (start_id)?

@cvvergara
Copy link
Member

beacuse they are related
In general, a TSP solution looks like, (where s is chosen by the algorithm)
s, u...., v, ........................w , s

if you want the start_vid to be u for example, the the solution is just rotated to get that u as first node
u...., v, ........................w , s, u

but if you want the end_vid to be v where u...., v, ........................w , s, u is the best solution for tsp
then u............................w , s, v, u

So imagine a problem, the salesperson departs from the office, has to visit the customers and ends in his house
start_vid you would set to be the node of the office, and end_vid the node of the house

@AlexeyPechnikov
Copy link
Author

Sorry, you are wrong. No, we can’t just replace end_id by start_id and reverse the route. Let’s think about the both options start_id and end_id defined at the same time.

@cvvergara
Copy link
Member

Well I would need to read the code again but as far as my memory goes that is how it works.
(I didnt write the original signature of the function)
But
it needs a lot of unit tests:
if you can help me prepare unit tests, it would be cool:
A fixed input, what is the expected output
with very small examples like the ones you made
So instead of writing the output it currently gives, write what it is expected as result.
if you are up to this task, we can work it out on gitter chat

@cvvergara cvvergara added the MacOS label Nov 5, 2020
@AlexeyPechnikov
Copy link
Author

Hi, maybe do you have some updates? I have the same results on MacOS and Linux for PgRouting 3.1.3.

@cvvergara
Copy link
Member

@mobigroup Can you try develop branch with pgr_TSP?
we changed the code

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants