Skip to content

Simplify bike priority parsers - #3227

Open
karussell wants to merge 14 commits into
masterfrom
simplify_priority
Open

karussell wants to merge 14 commits into
masterfrom
simplify_priority

Conversation

@karussell

@karussell karussell commented Nov 13, 2025 •

Copy link
Copy Markdown
Member

This PR does the following changes (via separate commits):

  • move ferry avoidance into custom model
  • replaces PriorityCode with just a double (allows us to use all the 16 values and IMO also makes reading easier when details are required, because it is very likely that you need to look up the exact value)
  • removes the weightToPrioMap (this required some logic changes but I was able to keep most tests identical)
  • the following 3 cases should all give the same prio of 1.3:
    • highway=*&cycleway=track
    • highway=cycleway
    • highway=path&surface=asphalt&bicycle=designated
  • further simplifies the code to get a single if-else-if block for a simpler reasoning
  • minor alignment of racingbike code required to get same test response

Probably for separate PR: one question I stumbled over is: why do we always avoid the avoidHighwayTags? Shouldn't we decrease the avoidance a bit in case bicycle=yes or designated? Similar question for bike=designated for preferHighwayTags and pushingSectionsHighways. And e.g. avoiding tunnel should be done always (except if bike=designated). And at the same time we could integrate the default avoidance of track for bad surfaces for bike and rcbike.

@karussell karussell added this to the 12.0 milestone Nov 13, 2025
Base automatically changed from cleanup_priority to master November 14, 2025 10:11

@ratrun ratrun Nov 14, 2025 •

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.

I don't like loosing the possibility of searching for certain priority values. I believe that we will need this soon for the first time at fixing #3219.

Therefore I suggest to define constants like this:

package com.graphhopper.routing.util;

public final class PriorityConstants {

    // Private constructor to prevent instantiation
    private PriorityConstants() {
    }

    public static final double PRIO_01 = 0.1;
    public static final double PRIO_02 = 0.2;
    public static final double PRIO_03 = 0.3;
    public static final double PRIO_04 = 0.4;
    public static final double PRIO_05 = 0.5;
    public static final double PRIO_06 = 0.6;
    public static final double PRIO_07 = 0.7;
    public static final double PRIO_08 = 0.8;
    public static final double PRIO_09 = 0.9;
    public static final double PRIO_10 = 1.0;
    public static final double PRIO_11 = 1.1;
    public static final double PRIO_12 = 1.2;
    public static final double PRIO_13 = 1.3;
    public static final double PRIO_14 = 1.4;
    public static final double PRIO_15 = 1.5;
}

and utilize these constants in the code instead of hard coding the double values.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Why is searching for a certain priority value an advantage? Also I do not understand what this has to do with #3219.

IMO the missing step for fixing #3219 is that there is still no clear concept of what should get which priority and when to boost or penalize (similar to the average speed parser). But at least we now have a single if-else block which makes it easier to reason about.

But this if-else block should be further simplified and this simplification will then hopefully fix issues like #3219 (as problems like these get more or less obvious from the code).

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.

Why is searching for a certain priority value an advantage

This is needed to analyze how other situations are prioritized in comparison to a new one. In #3219 I think that we will need to shift some existing priority deltas by a step of 0.1 in order to be able to make use of the currently left out priority values.

And hard coding these values is ugly IMHO.

@karussell karussell Nov 17, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

This is needed to analyze how other situations are prioritized in comparison to a new one.

But comparing numbers is much simpler than comparing words which you need to look up (!?)

In #3219 I think that we will need to shift some existing priority deltas by a step of 0.1 in order to be able to make use of the currently left out priority values.

We could try to use 1.1 instead of 1.2 and add 0.1 if it has a lane. (Or we could even try to use residential without any boost by default.)

IMO speaking of numbers instead of SLIGHT_PREFER and PREFER is much simpler. E.g. do you know without looking how much worse BAD is compared to AVOID?

One further argument for the numbers is that we just have numbers for the priority section of custom models too.

btw: it is unclear to me why we handle tertiary and unclassified differently. Is there a reason? And shouldn't service be 0.9 instead of getting a boost by default? (but this is yet another PR I think :) )

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.

IMO speaking of numbers instead of SLIGHT_PREFER and PREFER is much simpler. E.g. do you know without looking how much worse BAD is compared to AVOID?

I'm not proposing to keep the class with those names, but instead define constants for the double priority values:

public static final double PRIO_01 = 0.1; 
..

see above. This way you can easily search for all source code which results in a specific prio value and from the naming of the constants you can easily derive its value.

One further argument for the numbers is that we just have numbers for the priority section of custom models too.

But in Java we have the means for better readable code, which we should utilize.

Let's discuss #3219 once this PR is finished.

@karussell karussell Nov 29, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I'm still not convinced that an enum gives us any advantages. If we simplify the code then all the logic will be in one place with a single & simple if-else-block or whatever simple logic. Nothing we need to search for.

But in Java we have the means for better readable code, which we should utilize.

It is definitely not better readable because a "number" as concept for priority is immediately clear and such an enum needs to be understood first, even if it just mirrors numbers.

The only advantage would be that we can find the usage which I do not see as an advantage as this shouldn't be necessary. E.g. if we know there is just a single class and a single test class then we can as well search those two files for e.g. 0.1 too.

But I doubt that this is a real problem that we have to solve.

For example when I tried to further simplify the code I found one more inconsistency which you cannot find from a "usage search" of an enum.

The inconsistency I found is: for path, track and bridleway with a good surface and when bicycle=designated we use 1.3 (VERY_NICE), but for other highways the designated will be just 1.2 (PREFER). On the other hand a highway included in pushing section set (like path) with just bicycle=yes we give a 1.2 even if a bad surface! Here is the test which makes the inconsistency obvious:

    @Test
    public void test() {
        ReaderWay way = new ReaderWay(1);
        way.setTag("highway", "path");
        way.setTag("bicycle", "yes");
        way.setTag("surface", "ground");
        assertPriorityAndSpeed(1.2, 10, way);

        way = new ReaderWay(1);
        way.setTag("highway", "residential");
        way.setTag("bicycle", "designated");
        assertPriorityAndSpeed(1.2, 18, way);
    }

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.

The only advantage would be that we can find the usage which I do not see as an advantage as this shouldn't be necessary. E.g. if we know there is just a single class and a single test class then we can as well search those two files for e.g. 0.1 too.

Currently there are more files involved, even if one limits a search to just a single profile.
I counted five: BikeCommonPriorityParser, BikePriorityParser, AbstractBikeTagParserTester, BikeTagParserTest, BikeCustomModelTest. But if you really insist on these numbers then there is nothing I can do about it, except putting it into my fork, which I don't want in order to avoid merge conflicts.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Currently there are more files involved

Well, this will change. I plan to do this similarly to how we have it for foot and hike. Just a single parser (or later maybe even none).

But if you really insist on these numbers

I do not insist. I'm just not convinced that such an enum is a good solution in this case.

But for a moment, let's focus on the other changes: are you fine with them?

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.

Well, this will change. I plan to do this similarly to how we have it for foot and hike. Just a single parser (or later maybe even none).

If this is possible I agree with the change to the numeric factors in this PR. But please then assign this change some priority in your planning and create a separate PR for it.

But for a moment, let's focus on the other changes: are you fine with them?

I agree. I'm through now with all my comments. Don't get shocked by the big amount, it is not as bad as it might look at the first glance 😃

double collect(ReaderWay way, double wayTypeSpeed, boolean bikeDesignated) {
double prio = super.collect(way, wayTypeSpeed, bikeDesignated);

// worse grade should not mean we prefer it because it is also less fun

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.

We still should prefer tracktype=grade2, grade3, grade4 and eventually also grade5 more compared to grade1. Imagine a tracktype grade1 and parallel a highway=track with one of the other grades. From a MTB profile I would expect that the tracktype=grade1 is avoided in such a case.

@karussell karussell Dec 10, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

We probably need to clarify what 'mountainbike routing' means. IMO it means that

  1. we want to go from A to B in a short time, and
  2. accept more ways, ways that an mtb can access, and
  3. reduce the penalty on certain ways with a bad surface, and
  4. prefer "mtb ways".

I don't think highway=track&tracktype=grade5 should fall under requirement 4 but more 3. And at least we should not prefer a way just because the surface is worse like we currently do. This does not make sense to me because of requirement 1. But more importantly: a worse surface doesn't mean it makes more fun with a mtb (especially uphill) and we do not know the skill/preference of the person on the mtb.

So requirement 4 is tricky. IMO preferring a road because of its "mountainbikability" can be defined via mtb:scale and slightly boosting highway=path and highway=track (or not adding a penalty if there is no surface tag). And even a big value of mtb:scale does not mean we should prefer it more than e.g. mtb:scale==0 because of the same reason of why a worse surface shouldn't be preferred.

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.

For me it is crystal clear that 4. is the essence of a good mountainbike profile and 1. is not really a requirement. If 1. is a requirement, then I'd choose the trekking bike profile instead. But I must say that I am not a hard core mountainbike freak myself, but friends of mine are and for them this is their expectation.

I'm skeptical about AI, but as I'm not a reader of Mountainbike Forums I thought it would be interesting what the output would be. This is what I got

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.

Mountain biking is more of a leisure activity than a means of transportation. It's similar to planning a motorcycle ride with Kurviger.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

is the essence of a good mountainbike profile and 1. is not really a requirement

Hm, ok. Our disagreement once again shows that we really need to make more things easier to configure via a custom model and so we can have both :)

Because I want a mtb profile where the purpose is "just" to go from A to B with a mtb, so very similar to the hike profile. And then finding nice mtb spots or detours preferable for mtb is out of scope of the profile because we mostly do not have this data in OSM (and IMO only properly done with external/user data like you find in komoot).

@karussell karussell Dec 10, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Mountain biking is more of a leisure activity than a means of transportation

We need to differentiate between the A-to-B routing and the planning where we find the spots and then create a nice tour with multiple via points (or a round tour) to get there. IMO the scope of the GraphHopper routing engine here can be surely be "prefer tracks and mtb-alike roads" (or for kurviger "prefer curvy roads and avoid motorways") but our aim should not be to find "nice mtb spots" on the way to our goal and do big detours because of that and accept really ugly surface (it is not fun!), because there is just no good data for this in OSM (except maybe mtb:scale and scenic).

@karussell karussell Dec 13, 2025 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

actually mtb:scale and route=mtb 😀 (#3250)

Also I propose to remove the scenic=yes preference from all bikes (#3252).

String highway = way.getTag("highway");
if ("service".equals(highway) || "residential".equals(highway)) {
weightToPrioMap.put(40d, SLIGHT_AVOID);
prio *= 0.7;

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.

I think the factor 0.7 is too small, I would set it to 0.9. In the old code we fixed set it to SLIGHT_AVOID (0.9). Can you please explain your reasoning behind?

assertPriorityAndSpeed(0.9, 4, way);
way.setTag("bicycle", "designated");
assertPriorityAndSpeed(VERY_NICE, 12, way);
assertPriorityAndSpeed(1.2, 12, way);

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.

This was VERY_NICE (1.3) before. Was this change intended? What is the reasoning behind?

// tertiary without sidewalk is roughly like primary with sidewalk
way.setTag("sidewalk", "no");
assertPriority(PriorityCode.AVOID, way);
assertPriority(0.7, way);

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.

Note: This was AVOID (0.8) before, but 0.7 is also OK.


way.setTag("sidewalk", "no");
assertPriority(PriorityCode.REACH_DESTINATION, way);
assertPriority(0.2, way);

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.

Note: Was REACH_DESTINATION (0.1) before. Was this change intended?

assertEquals(10, getSpeedFromFlags(way), 0.01);

way.setTag("smoothness", "bad");
way.setTag("smoothness", "0.5");

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.

This is obviously a replace all BAD->0.5 error 😄 , please fix it back!

assertEquals(4, getSpeedFromFlags(way), 0.01);

way.setTag("smoothness", "bad");
way.setTag("smoothness", "0.5");

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.

This is obviously a replace all BAD->0.5 error 😄 , please fix it back!

assertPriority(0.5, way);
way.setTag("cycleway", "track");
assertPriority(PREFER, way);
assertPriority(1.3, way);

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.

Was PREFER (1.2) before. 1.3 is also OK


way.setTag("maxspeed", "15");
assertPriority(BEST, way);
assertPriority(1.4, way);

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.

Was 1.5 (BEST) before. Question: Does it become 1.5 for class:bicycle:touring = 3? Please add this test and make sure the result is 1.5!

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.

Well, this will change. I plan to do this similarly to how we have it for foot and hike. Just a single parser (or later maybe even none).

If this is possible I agree with the change to the numeric factors in this PR. But please then assign this change some priority in your planning and create a separate PR for it.

But for a moment, let's focus on the other changes: are you fine with them?

I agree. I'm through now with all my comments. Don't get shocked by the big amount, it is not as bad as it might look at the first glance 😃

@ratrun

ratrun commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

What is the status of this PR? Did you forget about it or did I add too many comments ❓ The discussion stalled although it is a pre-requisite for fixing the annoying bug #3219.

@karussell

Copy link
Copy Markdown
Member Author

I was actually waiting for your feedback regarding the first step here: #3251 :) (but no rush required from my side)

@ratrun

ratrun commented Feb 20, 2026

Copy link
Copy Markdown
Contributor

I was actually waiting for your feedback regarding the first step here: #3251 :) (but no rush required from my side)

Good that I asked. But I already did comment on #3251 and I don't know what I missed there.

This branch was previously deployed

1 inactive deployment
benchmarks — 63822509 Deployed Nov 14, 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