Skip to content

scriptable turn statements: "turn_penalty" - #3174

Merged
karussell merged 25 commits into
masterfrom
turn_statements
Sep 24, 2025
Merged

karussell merged 25 commits into
masterfrom
turn_statements

Conversation

@karussell

@karussell karussell commented Jun 26, 2025 •

Copy link
Copy Markdown
Member

This PR implements scriptable statements for turns (currently the turn penalty, not turn time). I.e. one can now model advanced turn costs and there is no need for the several turn costs parameters in the config.yml (see avoid_turns.json) and instead you can model much more complex situations like a turn cost for bigger roads or different costs for left hand traffic etc. Furthmore you can try all changes out at query time directly in the custom model. However, it needs to be enabled in the config.yml via allow_turn_weight_in_request: true in the turn_costs section.

Compared to the turn cost parameters that we currently have there are no downsides, except maybe the initial setup is a bit more complicated, but even query and import speed is comparable or slightly faster for the new scriptable turn_penalty statements. Here is the example avoid_turns.json:

{
  "turn_penalty": [
    // straight:
    { "if": "change_angle > -25 && change_angle < 25", "add":  "0" },
    // right:
    { "else_if": "change_angle >= 25 && change_angle < 80", "add":  "3" },
    // sharp right:
    { "else_if": "change_angle >= 80 && change_angle <= 180", "add":  "3" },
    // left:
    { "else_if": "change_angle <= -25 && change_angle > -80", "add":  "3" },
    // sharp left:
    { "else_if": "change_angle <= -80 && change_angle >= -180", "add":  "3" },
    // uTurnCosts:
    { "else": "", "add":  "Infinity" }
  ]
}

🎉 Work is sponsored by OPTITOOL (cc @otbutz)

TODOs:

  • document this in custom-models.md
  • add to custom model editor
  • decide on naming turn_weight and add use turn_penalty instead of turn_weight
  • change CustomModelParser.createWeighting to work with turn_penalty too (for tests)
  • the config option in profile turn_costs is named differently because the costs consists of the penalty and the time. So this should be fine. Different names with different meaning.
  • move config allow_turn_penalty_in_request into TurnCostsConfig

The main driving force behind this feature was to fix the longstanding issue #733 via the idea mentioned here:

Although there are some downsides with this change the destination-only (or other avoidance) cases behave finally exactly how they should: they are avoided except if a waypoint is on an edge (or in an area) with destination-only access. The acceptable detour can be controlled with the value for add. Furthermore this change enables this "advanced avoidance" for other use cases, like hazmat_water and highway=track. Also it can be used to avoid country border crossings via the custom model without specially crafted polygons.

And in the future we can use the same approach to fix a few problems for short ferries, but we would need to modify the ETA too not only the weight. I.e. instead of turn_penalty we would need turn_time. With this more realistic ferry times are possible.

Downsides of this approach compared to just a low priority for destination-only cases:

  • enabling turn restriction is required and if CH is used this means a much slower preparation (compared to without turn restrictions). But turn restriction and turn cost support is so essential for car (where this feature is most important) this is acceptable as turn_costs support is already enabled.
  • it slows down the (edge-based) CH preparation a bit (compared to just turn restrictions without this feature). But on the other hand there might be less shortcuts and so the query speed might improve
  • minor: a missing private (or destination) tagging inside such an private (or destination-only) area will become very obvious. E.g. for point=51.11853%2C13.61062&point=51.12035%2C13.60681 but it is easy to fix this IMO tagging mistake.
  • a similar minor problem (but no tagging mistake!) is in cases like for this barrier where the ways are private but the barrier itself is not private and leads to a wrong detour nevertheless:
    image
    This is likely detectable and fixable via some pre-processing.

Instead of the turn_penalty we could add barrier edges whenever there is a change e.g. from road access "yes" to "destination". But we would have to pre-process the data and create a boolean encoded value that is true whenever there is such a change and use add in priority. This would avoid that we need to enable turn costs, but then these cases need to be known before the import (and a different solution would be required for "per-request") and also the implementation of it sounds trickier, also regarding multiple edges which needs to be joined or junctions with different values.

Note: I slightly adjusted the maxPollFactorHeuristic in EdgeBasedNodeContractor for ~10% faster preparation (compared to maxPollFactorHeuristic=5). With this the CH preparation compared to just turn restrictions is still 17% slower (95k sec vs 111k sec with private/destination avoidance; planet; car)

@karussell karussell added this to the 11.0 milestone Jun 26, 2025
@otbutz

otbutz commented Jun 27, 2025

Copy link
Copy Markdown
Contributor

I'm on vacation next week but I'm looking forward to providing feedback once I'm back. 🙂

@michaz
michaz requested review from michaz and removed request for michaz August 3, 2025 16:56
@michaz

michaz commented Aug 3, 2025

Copy link
Copy Markdown
Member

I like this.

@michaz

michaz commented Aug 3, 2025 •

Copy link
Copy Markdown
Member

My opinions on the wording question:

  • Cost and weight are synonyms. For edges and routes (aka paths), we've always called it weight. For turns, we've mostly called it cost, and sometimes weight, mostly when it appears right next to an edge weight. I think I always call it cost when it's about transportation, and weight when I'm inside a routing algorithm or something. If, in the config file and similar places, we've only ever called it cost so far (not sure), we should perhaps keep it that way.
  • We need to be a bit careful now that we give direct access to the cost: It must be clear to me what happens to the cost when I change the travel time. Do I also have to add it there? Or does the cost automatically contain the travel time component, including for any extra travel time I add here?
  • I think it may be convenient to say that the cost that I add here is an extra cost, on top of the travel time component, which it automatically contains. So in the model specification, e.g. for your turn angles, you would perhaps just add time (and it would still make the route more costly). And for the "destination" penalty, you would just add cost (and it would not make the route take longer).
  • Wait, should we say that the user can "specify a penalty" instead of "add to the cost"?
  • Then what we would have is: For turns: cost = time + penalty, and for links: cost = time + distance_influence * distance + whatever_priority_does_exactly * time (or was it distance)
  • It's yet another word, but it's something we don't have so far. And which sooner or later we may also want to have for links.
  • Unless for links I would also get direct access to the cost, so that I can say cost: if bla add 500 (adding to whatever travel time and priority are already doing), or if I insist cost: if blubb set_to 10000 (overwriting anything travel time and priority are already doing), but maybe that would be unnecessarily powerful and really an additive component, penalty, is all I should get?
  • But we could have the same without a new word by allowing only add. So it's probably all good.

@michaz

michaz commented Aug 3, 2025 •

Copy link
Copy Markdown
Member

Though I still maintain that, at least for road transportation, a 90° turn is a turn of regular sharpness, not a sharp turn. 🤪 But now that they aren't prescribed categories anymore, I shouldn't care that much.

@michaz

michaz commented Aug 4, 2025 •

Copy link
Copy Markdown
Member

And I still think it's important to be clear that turn costs in the narrower sense of e.g. "discourage left turns" are not about angles.

When some road goes left, even sharply, that's not a turn, that's just a bend.

A left turn is (in many countries) when there's conflict with oncoming traffic, and you don't see that from the angle.

Just like or even more than with estimating link travel time, I'm almost sure this ultimately isn't scriptable by looking just at local attributes. You will quickly want to look at the entire node with all its edges, and, for complicated intersections, at the entire complicated intersection.

@michaz

michaz commented Aug 4, 2025

Copy link
Copy Markdown
Member

As a solution to the destination-restriction problem, it just sounds really, really good.

Comment thread config-example.yml Resolved
"priority": [
{ "if": "hgv == NO", "multiply_by": "0" },
{ "if": "!car_access && road_access != PRIVATE && hgv != DELIVERY && hgv != DESTINATION", "multiply_by": "0" },
// also have a look into avoid_private_etc.json

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we follow the car custom model and provide a avoid_delivery.json ?

@otbutz

otbutz commented Aug 26, 2025

Copy link
Copy Markdown
Contributor

I haven't tested the angle calculation changes, but this should be doable without conditions and only using modulo operations.

@otbutz

otbutz commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

Works 🙂

master (multiply with 0.1 for private)

grafik

PR (add 300 for private)

grafik

https://graphhopper.com/maps/?point=48.58223%2C13.396052&point=48.583599%2C13.394713&profile=truck&layer=OpenStreetMap

@otbutz

otbutz commented Aug 28, 2025

Copy link
Copy Markdown
Contributor

And if you think the previous screenshot from the current master doesn't look that bad:

grafik

The effect of virtually inflating the edge sizes is quite apparent. 😅

https://graphhopper.com/maps/?point=48.582409%2C13.395431&point=48.583599%2C13.394713&profile=truck&layer=OpenStreetMap

… to slightly reduce CH prep time without much query speed slow down
@karussell
karussell merged commit 28f11e6 into master Sep 24, 2025
@karussell
karussell deleted the turn_statements branch September 24, 2025 15:56
@karussell karussell changed the title scriptable turn statements scriptable turn statements: "turn_penalty" Sep 24, 2025
chazab pushed a commit to rayanhidri/graphhopper that referenced this pull request Oct 10, 2025
* initial working state. fix for graphhopper#733

* move most of the turn cost config into new turn_time statements

* support to specify "add":"Infinity"

* rename 'turn time' to 'turn weight'

* minor fix

* turn_weight is only possible when turn_costs is configured

* by default allow the turn_weight feature only on server-side

* minor simplify

* 'add' for speed is ok

* minor comment

* removed TODO NOW

* delete file

* for now no negative adds

* minor

* improvements and review comments

* slightly reduce EdgeBasedNodeContractor.Params.maxPollFactorHeuristic to slightly reduce CH prep time without much query speed slow down

* rename turn_weight to turn_penalty

* readd bike_tc as might be usefull illustration too

* move allow_turn_penalty_in_request into TurnCostsConfig and some minor fixes to custom models

* add docs

* minor
chazab pushed a commit to rayanhidri/graphhopper that referenced this pull request Oct 10, 2025

This branch was previously deployed

1 inactive deployment
benchmarks — 358856f9 Deployed Sep 24, 2025 by karussell
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants