Skip to content

Instantly share code, notes, and snippets.

@aoles
Created November 24, 2020 10:07
Show Gist options
  • Save aoles/94a60d67f82ceea4f586c10e00ecb244 to your computer and use it in GitHub Desktop.
Save aoles/94a60d67f82ceea4f586c10e00ecb244 to your computer and use it in GitHub Desktop.
Priority weightings compared
PRIORITY_CODE = c(
WORST = 0,
AVOID_AT_ALL_COSTS = 1,
REACH_DEST = 2,
AVOID_IF_POSSIBLE = 3,
UNCHANGED = 4,
PREFER = 5,
VERY_NICE = 6,
BEST = 7
)
## priority values returned by the priority encoder
priority <- function(code) code / unname(PRIORITY_CODE["BEST"])
sapply(PRIORITY_CODE, priority)
#> WORST AVOID_AT_ALL_COSTS REACH_DEST AVOID_IF_POSSIBLE
#> 0.0000000 0.1428571 0.2857143 0.4285714
#> UNCHANGED PREFER VERY_NICE BEST
#> 0.5714286 0.7142857 0.8571429 1.0000000
## original weight scaling function
priorityWeighting <- function(x) 1 / (0.5 + priority(x))
## current recommended weighting
optimizedPriorityWeighting <- function(x) {
best = PRIORITY_CODE["BEST"]
val = priority(x) * best - ceiling(best/2)
2^(-val/3)
}
## original weight scaling function
preferencePriorityWeighting <- function(x) {
p <- priority(x)
if (x <= PRIORITY_CODE["REACH_DEST"])
p = p / 1.5
else if (x <= PRIORITY_CODE["AVOID_IF_POSSIBLE"])
p = p / 1.25
else if (x == PRIORITY_CODE["PREFER"])
p = p * 1.5
else if (x >= PRIORITY_CODE["VERY_NICE"])
p = p * 2.2;
1 / (0.5 + p)
}
renderPlots <- function(main, log = "") {
pch = c(16, 1, 4)
col = c("black", "red", "blue")
plot(PRIORITY_CODE, sapply(PRIORITY_CODE, priorityWeighting), pch = pch[1], col = col[1], ylim = c(0.25, 4), ylab = "weight scaling factor", main = main, log = log)
points(PRIORITY_CODE, sapply(PRIORITY_CODE, optimizedPriorityWeighting), pch = pch[2], col = col[2])
points(PRIORITY_CODE, sapply(PRIORITY_CODE, preferencePriorityWeighting), pch = pch[3], col = col[3])
legend("topright", c("GraphHopper", "OptimizedPriorityWeighting", "PreferencePriorityWeighting"), pch = pch, col = col, cex = 0.8)
}
## linear scale
renderPlots("Linear scale")
## log scale
renderPlots("Logarithmic scale", "y")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment