[pgrouting-users] pgrouting Dijksta Travelling Salesman Problem (TSP) with OpenStreetmap

Carsten Hogertz carsten.hogertz at gmail.com
Thu Feb 20 13:39:47 PST 2014


2014-02-20 22:20 GMT+01:00 Stephen Woodbridge <woodbri at swoodbridge.com>:

> On 2/20/2014 3:54 PM, Carsten Hogertz wrote:
>
>> Thanks Steve,
>>
>> coming home and adding your lines it returns with an error:
>>
>> NOTICE:  r2:
>> (34304,"{0,5.3602868897,4.6782573327,5.6026971537,5.2062747797,5.
>> 0497181017,5.3147719797,5.0700916462,4.3220066797,4.8593618137
>> ,4.0002255227,6.055760592,12.5155379824,9.3170226155,8.72783427270001,5.
>> 9460446587,3.0868131677,5.1839796057,5.2301666167,2.1211198847,5.
>> 7037465591,2.7282842677,8.4758437557,11.1027935904,7.9434273599,7.
>> 8361619299,7.7610621969,7.4929139614,5.8106133327,5.
>> 9579132527,5.9888473034,6.5618433959,4.5013708887,1.
>> 9218516244,5.7559805232,5.2267081177,5.0968196707,4.8949133597,5.
>> 9137964962,4.2936059427,4.6948533593,3.4377318427,3.7991617067,4.
>> 1242444467,3.8858731777,3.9596393887,4.9192685996,4.
>> 6734033563,4.7511846457,4.4544981945,4.2748693367,3.
>> 411559417,6.6851381985,6.6744741285,6.9296475385,7.
>> 0136847108,7.1371500908,7.2719520708,6.6787720528,7.
>> 3530295908,6.8599596928,7.2216891568,7.4951304208,7.
>> 4404728868,8.1332243958,6.2736028029,6.963685048,6.
>> 0483763169,5.8653351624,5.6164044005,6.54979166,5.
>> 8783224469,6.533468297,5.8553434082,5.7676662122,6.267915357,5.22607939
>>
> 03,3.745459072,6.352470342,5.4392033722,5.5562602522,5.
> 3996670482,6.171297855,5.648779598,5.5653279855,5.
> 671668653,5.3243595462,5.879320328,5.859442385,6.080574272,5.3767159992,6.
> 170852717,5.3001119472,5.687201287,5.3573110752,3.586385579,5.566591422,5.
> 5104826925,5.916268775,5.4418422465,5.4037394705,5.
> 640889612,5.1407657702,4.5523677017,5.3873769863,3.
> 601570449,2.978252449,4.8725960282,4.9817842222,5.
> 2355359365,5.1227105795,5.177856607,5.098596533,5.
> 1891181988,4.9327118245,4.4632330777,7.4879538737,4.
> 8591269625,3.1339054377,5.8472818275,4.6043306952,3.
> 3542942377,4.7660145915,6.6679095357,5.51150886,2.
> 7404344677,5.9332075344,6.2019594857,5.266709364,5.
> 4924174224,5.180577724,3.0277711677,6.2913260157,4.
> 2862670877,5.5461328154,5.5809644644,5.1132029296,5.
> 1904326724,4.8352505633,4.9641930171,5.1366240426,7.
> 0699378037,3.1274712477,5.1487646306,5.2717034197,5.
> 0200090866,3.0911490977,5.8984613527,5.1973381747,4.
> 2427530627,6.7293864957,4.7943011637,5.1130266607,5.
> 3948779287,4.7124246547,5.2
> 186371577,2.9723417287,2.3171875277,2.8159853427,4.7913513947,4.7863347867
> ,3.4531342837,3.8161332287,5.8431115757,4.4873126817,5.6577494097,5.
> 5867970832,9.4583860946,2.7784615917,5.4279953797,1.
> 9376872717,5.0813930177,5.8860618114,6.2411339874,6.
> 4536904614,6.0972641874,5.7247963284,5.9823452114,5.
> 8316140784,5.2398164977,6.2963392357,5.5864457124,6.
> 1599995178,5.2609646384,6.1012355027,6.4129483756,6.
> 0056487896,5.0106569884,5.4239402364,6.1148656496,2.
> 673828844,4.3332873457,3.2943586634,4.5834393467,7.
> 278601561,3.5870893269,3.3299191554,4.1049776549,5.
> 9256568037,6.2719334873,6.2594329923,5.5752581689,5.
> 6058592867,6.1311384893,6.4726413519,5.8973941434,6.
> 2572672069,5.9667442136,5.3965422637,7.2837623985,6.
> 9586195092,7.1137641952,6.3682104908,7.8495628418,7.
> 2245886958,6.2983788278,7.3509762248,6.9925366658,6.
> 5600944468,6.936328733,6.616665943,6.738732443}")
>
>> CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
>> PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
>> NOTICE:  r2.id1: 34304
>> CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
>> PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
>> NOTICE:  ids: <NULL>
>> CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
>> PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
>> ERROR:  cannot concatenate incompatible arrays
>> DETAIL:  Arrays with differing element dimensions are not compatible for
>> concatenation.
>> CONTEXT:  PL/pgSQL function "pgr_makedijkstramatrix" line 25 at assignment
>> PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
>> PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
>>
>> I tried to write to Dose but he didn't reply yet.
>> Does anyone have a clue?
>>
>
> Try adding the follow line after the "begin" line:
>
>    ids := '{}'::integer[];
>
> This should make sure the ids is initialized as an empty array of integers.
>
> -Steve
>


Again:
NOTICE:  r2:
(34304,"{0,5.3602868897,4.6782573327,5.6026971537,5.2062747797,5.0497181017,5.3147719797,5.0700916462,4.3220066797,4.8593618137,4.0002255227,6.055760592,12.5155379824,9.3170226155,8.72783427270001,5.9460446587,3.0868131677,5.1839796057,5.2301666167,2.1211198847,5.7037465591,2.7282842677,8.4758437557,11.1027935904,7.9434273599,7.8361619299,7.7610621969,7.4929139614,5.8106133327,5.9579132527,5.9888473034,6.5618433959,4.5013708887,1.9218516244,5.7559805232,5.2267081177,5.0968196707,4.8949133597,5.9137964962,4.2936059427,4.6948533593,3.4377318427,3.7991617067,4.1242444467,3.8858731777,3.9596393887,4.9192685996,4.6734033563,4.7511846457,4.4544981945,4.2748693367,3.411559417,6.6851381985,6.6744741285,6.9296475385,7.0136847108,7.1371500908,7.2719520708,6.6787720528,7.3530295908,6.8599596928,7.2216891568,7.4951304208,7.4404728868,8.1332243958,6.2736028029,6.963685048,6.0483763169,5.8653351624,5.6164044005,6.54979166,5.8783224469,6.533468297,5.8553434082,5.7676662122,6.267915357,5.2260793903,3.745459072,6.352470342,5.4392033722,5.5562602522,5.3996670482,6.171297855,5.648779598,5.5653279855,5.671668653,5.3243595462,5.879320328,5.859442385,6.080574272,5.3767159992,6.170852717,5.3001119472,5.687201287,5.3573110752,3.586385579,5.566591422,5.5104826925,5.916268775,5.4418422465,5.4037394705,5.640889612,5.1407657702,4.5523677017,5.3873769863,3.601570449,2.978252449,4.8725960282,4.9817842222,5.2355359365,5.1227105795,5.177856607,5.098596533,5.1891181988,4.9327118245,4.4632330777,7.4879538737,4.8591269625,3.1339054377,5.8472818275,4.6043306952,3.3542942377,4.7660145915,6.6679095357,5.51150886,2.7404344677,5.9332075344,6.2019594857,5.266709364,5.4924174224,5.180577724,3.0277711677,6.2913260157,4.2862670877,5.5461328154,5.5809644644,5.1132029296,5.1904326724,4.8352505633,4.9641930171,5.1366240426,7.0699378037,3.1274712477,5.1487646306,5.2717034197,5.0200090866,3.0911490977,5.8984613527,5.1973381747,4.2427530627,6.7293864957,4.7943011637,5.1130266607,5.3948779287,4.7124246547,5.2186371577,2.9723417287,2.3171875277,2.8159853427,4.7913513947,4.7863347867,3.4531342837,3.8161332287,5.8431115757,4.4873126817,5.6577494097,5.5867970832,9.4583860946,2.7784615917,5.4279953797,1.9376872717,5.0813930177,5.8860618114,6.2411339874,6.4536904614,6.0972641874,5.7247963284,5.9823452114,5.8316140784,5.2398164977,6.2963392357,5.5864457124,6.1599995178,5.2609646384,6.1012355027,6.4129483756,6.0056487896,5.0106569884,5.4239402364,6.1148656496,2.673828844,4.3332873457,3.2943586634,4.5834393467,7.278601561,3.5870893269,3.3299191554,4.1049776549,5.9256568037,6.2719334873,6.2594329923,5.5752581689,5.6058592867,6.1311384893,6.4726413519,5.8973941434,6.2572672069,5.9667442136,5.3965422637,7.2837623985,6.9586195092,7.1137641952,6.3682104908,7.8495628418,7.2245886958,6.2983788278,7.3509762248,6.9925366658,6.5600944468,6.936328733,6.616665943,6.738732443}")
CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
NOTICE:  r2.id1: 34304
CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
NOTICE:  ids: {}
CONTEXT:  PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY
ERROR:  cannot concatenate incompatible arrays
DETAIL:  Arrays with differing element dimensions are not compatible for
concatenation.
CONTEXT:  PL/pgSQL function "pgr_makedijkstramatrix" line 26 at assignment
PL/pgSQL function "pgr_tspdijkstra" line 8 at RETURN QUERY
PL/pgSQL function "pgr_tspdijkstralen" line 3 at RETURN QUERY

Thanks for your help. Maybe you need some other facts about my table?
The whole script I am excecuting is the following. I changed some things,
like adding your code and changing the table name 'ways' to 'cgn_2po_4pgr':

create or replace function pgr_makeDijkstraMatrix(thetbl text, sqlP text,
OUT dmatrix double precision[], OUT ids integer[])
  as
$body$
declare
    sql text;
    sql2 text;
    r record;
    r2 record;
    i integer;
    j integer;
    matrixl double precision[];
begin

   ids := '{}'::integer[];
    sql:='select node, id-1 as id from '||thetbl;
    for r in execute sql loop
            sql2:='select id1, array_agg(cost) as arow from (
          SELECT id1, id2, cost
          FROM pgr_kdijkstraCost('''||sqlP||''', '||r.node||
              ', (select array_agg(node) from '||thetbl||' where
id>'||r.id||'),
false, false)
         ) as foo group by id1 order by id1';
        for r2 in execute sql2 loop
        matrixl := '{}';
        for i in 1..r.id loop
                  matrixl := array_append(matrixl, dmatrix[i][r.id+1]);
                end loop;
                matrixl := array_cat(matrixl, r2.arow);
            dmatrix := array_cat(dmatrix, array[matrixl]);
            raise notice 'r2: %', r2;
raise notice 'r2.id1: %', r2.id1;
raise notice 'ids: %', ids;
            ids := ids || r2.id1;
        end loop;
    end loop;
end;
$body$
language plpgsql stable cost 10;


create or replace function pgr_tspDijkstra(thetbl text, sql text, start_id
integer, end_id integer default (-1))
    returns setof pgr_costResult as
$body$
declare
    sid integer;
    eid integer;

begin

    return query with dm  as (
        select * from pgr_makeDijkstraMatrix( thetbl, sql )
    ),
    ids as (
        select (row_number() over (order by id asc))-1 as rnum, id
          from (
                select unnest(ids) as id
                  from dm
                ) foo
    ),
    t as (
        select a.seq, b.rnum, b.id
          from pgr_tsp(
                   (select dmatrix from dm),
                   (select rnum from ids where id=start_id limit
1)::integer,
                   (case when end_id = -1 then -1 else (select rnum from
ids where id=end_id limit 1) end)::integer
               ) a,
               ids b
         where a.id=b.rnum
    ),
    r as (
        select array_agg(t.rnum) as rnum from t
    )
    select t.seq::integer,
           t.rnum::integer as id1,
           t.id::integer as id2,

dm.dmatrix[r.rnum[t.seq+1]+1][r.rnum[(t.seq+1)%array_length(r.rnum,
1)+1]+1]::float8 as cost
      from t, dm, r;
end;
$body$
language plpgsql volatile cost 50 rows 50;


create or replace function pgr_tspDijkstraLen(thetbl text, start_id
integer, end_id integer default (-1))
    returns setof pgr_costResult as
$body$
begin
     return query SELECT * FROM pgr_tspDijkstra(thetbl,
          'SELECT  id, source::integer, target::integer, length::double
precision AS cost FROM cgn_2po_4pgr',
           start_id, end_id);
end;
$body$
language plpgsql stable;


Maybe you've got another idea?
Thank you
Carsten


>  Thanks for you help
>> Carsten
>>
>>
>> 2014-02-20 16:24 GMT+01:00 Stephen Woodbridge <woodbri at swoodbridge.com
>> <mailto:woodbri at swoodbridge.com>>:
>>
>>
>>     On 2/20/2014 9:41 AM, Stephen Woodbridge wrote:
>>
>>         Hi Carsten,
>>
>>         The functions at
>>         http://dose.0wnz.at/scripts/__sql/pgroute_tspDijkstra.sql
>>
>>         <http://dose.0wnz.at/scripts/sql/pgroute_tspDijkstra.sql>
>>         are not part of pgRouting.
>>
>>         You need to ask this question to Ivan the author of the original
>>         article.
>>
>>
>>     Sorry, Dose was the original author.
>>
>>
>>
>>         You probably need to add some debug statements to get a better
>>         understanding of what the code is doing. For example in
>>         pgr_makeDijkstraMatrix before the statement:
>>
>>         ids := ids || array[r2.id1];
>>
>>
>>     You could try changing this line to:
>>
>>     ids := ids || r2.id1;
>>
>>     and see if that fixes the problem.
>>
>>     -Steve
>>
>>
>>         Add:
>>
>>         raise notice 'r2: %', r2;
>>         raise notice 'r2.id1: %', r2.id1;
>>         raise notice 'ids: %', ids;
>>
>>         This should show you the progression of how the array ids is
>> getting
>>         built as it appears to be compaining about that in the error
>>         message.
>>
>>         Hope this helps,
>>             -Steve
>>
>>         On 2/20/2014 9:21 AM, Carsten Hogertz wrote:
>>
>>             Hi everybody, I am new to this list and new to pgrouting.
>>
>>             I found the following TSP example
>>             http://hardwarefetish.com/513-__pgrouting-dijksta-
>> travelling-__salesman-problem-tsp-with-__openstreetmap
>>
>>             <http://hardwarefetish.com/513-pgrouting-dijksta-
>> travelling-salesman-problem-tsp-with-openstreetmap>
>>
>>             and tried to reproduce the solution.
>>
>>             Unfortunately I always get an error message.
>>             My DB is complaining about one of your functions.
>>
>>             I got my routing table from osm2po. The table has the
>> following
>>             structure:
>>
>>             1;”id”;”integer”
>>             2;”osm_id”;”bigint”
>>             3;”osm_name”;”character varying”
>>             4;”osm_meta”;”character varying”
>>             5;”osm_source_id”;”bigint”
>>             6;”osm_target_id”;”bigint”;
>>             7;”clazz”;”integer”
>>             8;”flags”;”integer”
>>             9;”source”;”integer”
>>             10;”target”;”integer”
>>             11;”length”;”double precision”
>>             12;”kmh”;”integer”
>>             13;”cost”;”double precision”
>>             14;”reverse_cost”;”double precision”
>>             15;”x1″;”double precision”
>>             16;”y1″;”double precision”
>>             17;”x2″;”double precision”
>>             18;”y2″;”double precision”
>>             19;”geom_way”;”geometry(__LineString,4326)”
>>
>>
>>             - I installed the functions as mentioned via SQL window in
>>             pgAdmin3
>>             (copy&paste then excecute)
>>             - I create and populate the my_route table
>>             - When it comes to step 5 and I try
>>
>>             select id,lon,lat from pgr_tspDijkstraLen(’my_route’, 1260
>>             ) dj, my_route rt where dj.id2=rt.node;
>>
>>             my DB warns
>>
>>             ERROR: cannot concatenate incompatible arrays
>>             DETAIL: Arrays with differing element dimensions are not
>>             compatible for
>>             concatenation.
>>             CONTEXT: PL/pgSQL function “pgr_makedijkstramatrix” line 25 at
>>             assignment PL/pgSQL function “pgr_tspdijkstra” line 8 at
>>             RETURN QUERY
>>             PL/pgSQL function “pgr_tspdijkstralen” line 3 at RETURN QUERY
>>
>>             ********** Fehler **********
>>
>>             ERROR: cannot concatenate incompatible arrays SQL Status:2202E
>>             Detail:Arrays with differing element dimensions are not
>>             compatible for
>>             concatenation.
>>             Kontext:PL/pgSQL function “pgr_makedijkstramatrix” line 25
>>             at assignment
>>             PL/pgSQL function “pgr_tspdijkstra” line 8 at RETURN QUERY
>>             PL/pgSQL function “pgr_tspdijkstralen” line 3 at RETURN QUERY
>>
>>             Do you have any idea what went wrong? I really really need
>> your
>>             solutionget working. I would appreciate a lot.
>>
>>             Thanks for your help.
>>
>>             PS: this text is a copy of what I wrote on on the comment
>>             site of the
>>             blog where I found the solution.
>>
>>             BR
>>             Carsten
>>
>>
>>             _________________________________________________
>>             Pgrouting-users mailing list
>>             Pgrouting-users at lists.osgeo.__org
>>             <mailto:Pgrouting-users at lists.osgeo.org>
>>             http://lists.osgeo.org/__mailman/listinfo/pgrouting-__users
>>             <http://lists.osgeo.org/mailman/listinfo/pgrouting-users>
>>
>>
>>         _________________________________________________
>>         Pgrouting-users mailing list
>>         Pgrouting-users at lists.osgeo.__org
>>         <mailto:Pgrouting-users at lists.osgeo.org>
>>         http://lists.osgeo.org/__mailman/listinfo/pgrouting-__users
>>         <http://lists.osgeo.org/mailman/listinfo/pgrouting-users>
>>
>>
>>     _________________________________________________
>>     Pgrouting-users mailing list
>>     Pgrouting-users at lists.osgeo.__org
>>     <mailto:Pgrouting-users at lists.osgeo.org>
>>     http://lists.osgeo.org/__mailman/listinfo/pgrouting-__users
>>
>>     <http://lists.osgeo.org/mailman/listinfo/pgrouting-users>
>>
>>
>>
>>
>> _______________________________________________
>> Pgrouting-users mailing list
>> Pgrouting-users at lists.osgeo.org
>> http://lists.osgeo.org/mailman/listinfo/pgrouting-users
>>
>>
> _______________________________________________
> Pgrouting-users mailing list
> Pgrouting-users at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/pgrouting-users
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/pgrouting-users/attachments/20140220/14e067b8/attachment-0001.html>


More information about the Pgrouting-users mailing list