nilsnolde / docker-valhalla

This is our flexible Docker repository for the Valhalla routing engine
MIT License
257 stars 74 forks source link

sources_to_targets returns significantly different results, when direction changed #168

Closed karpinskiJ closed 1 month ago

karpinskiJ commented 1 month ago

Hello! I'm sending sources_to_targets request to obtain travel_time & disctance between 2 points located in Peru.

Request input body:

{
  "costing": "auto",
  "sources": [
       {
      "lat":  -12.0665,
      "lon": -75.21226
    }

  ],
  "targets": [

 {
      "lat":  -12.06191,
      "lon": -75.1874
    }

  ]
}

Results which I obtained are (travel time and distance make no sense - checked in google maps ):

{
  "id": "my_work_route",
  "algorithm": "costmatrix",
  "units": "kilometers",
  "sources": [
    [
      {
        "lon": -75.21226,
        "lat": -12.0665
      }
    ]
  ],
  "targets": [
    [
      {
        "lon": -75.1874,
        "lat": -12.06191
      }
    ]
  ],
  "sources_to_targets": [
    [
      {
        "distance": 62.238,
        "time": 3984,
        "to_index": 0,
        "from_index": 0
      }
    ]
  ]
}

but when I change direction results are reliable:

{
  "costing": "auto",
  "sources": [
 {
      "lat":  -12.06191,
      "lon": -75.1874
    }

  ],
  "targets": [

       {
      "lat":  -12.0665,
      "lon": -75.21226
    }
  ]
}
{
  "id": "my_work_route",
  "algorithm": "costmatrix",
  "units": "kilometers",
  "sources": [
    [
      {
        "lon": -75.1874,
        "lat": -12.06191
      }
    ]
  ],
  "targets": [
    [
      {
        "lon": -75.21226,
        "lat": -12.0665
      }
    ]
  ],
  "sources_to_targets": [
    [
      {
        "distance": 3.321,
        "time": 352,
        "to_index": 0,
        "from_index": 0
      }
    ]
  ]
}

Do you have any idea what might be a reason? My only thought is that it's something regarding maps or land form, but area in nearby seems to be quite flat. There is also output from openstreetmap: https://www.openstreetmap.org/directions?engine=fossgis_valhalla_car&route=-12.066%2C-75.212%3B-12.062%2C-75.187#map=16/-12.06438/-75.20041

chrstnbwnkl commented 1 month ago

Probably helps to get a look at the geometries to reason about this. You can have those included on the response using the "shape_format" parameter. It's entirely possible that there's e.g. a one way somewhere that forces a big detour

karpinskiJ commented 1 month ago

I used parameter you mentioned, but I didn't get the shape (I guess that nearest pois were returned): Request:

{
  "costing": "auto",
  "format":"osrm",
"shape_format":"geojson",

  "targets": [

                     {
      "lat":  -4.9081,
      "lon": -80.6929
    }

  ],

  "sources": [
      {
          "lat":  -4.89552  ,
      "lon": -80.67223
    }

  ]
}

Response:

{
  "costing": "auto",
  "format":"osrm",
"shape_format":"geojson",

  "targets": [

                     {
      "lat":  -4.9081,
      "lon": -80.6929
    }

  ],

  "sources": [
      {
          "lat":  -4.89552  ,
      "lon": -80.67223
    }

  ]
}

Is there some parameter which I missed? I tried with turn by turn endpoint as well, but results there are not different when direction is changed.

chrstnbwnkl commented 1 month ago

you need to remove the format=osrm param

karpinskiJ commented 1 month ago

Hello! I resolved issue by changing source_to_target_algorithm to timedistancematrix variable in valhalla.json file.

I tried to find documentation referring to difference between costamtarix and timedistancematrix alghorithms, but I couldn't find any? @chrstnbwnkl Could you please link some sources or let me know what is the difference? Thank you in advance!

chrstnbwnkl commented 1 month ago

I can't reproduce your first result on the public Valhalla instance (which is not too different from a default Docker setup in terms of config). Would be helpful to see your valhalla config.

TimeDistanceMatrix uses a unidirectional Dijkstra algorithm, while CostMatrix uses bidirectional AStar, which is faster but can sometimes not guarantee the optimal path, and is suboptimal for time dependent requests.

karpinskiJ commented 1 month ago

My config for thor requests:

  "thor": {
    "clear_reserved_memory": false,
    "extended_search": false,
    "logging": {
      "color": true,
      "file_name": "path_to_some_file.log",
      "long_request": 110.0,
      "type": "std_out"
    },
    "max_reserved_labels_count_astar": 2000000,
    "max_reserved_labels_count_bidir_astar": 1000000,
    "max_reserved_labels_count_bidir_dijkstras": 2000000,
    "max_reserved_labels_count_dijkstras": 4000000,
    "service": {
      "proxy": "ipc:///tmp/thor"
    },
    "source_to_target_algorithm": "select_optimal" # I changed this one to timedistancematrix
  }
chrstnbwnkl commented 1 month ago

Closing here, it's definitely upstream related.