Skip to content

Commit

Permalink
Update penalty cals
Browse files Browse the repository at this point in the history
penalty = dplyr::case_when(
    value < 1 ~ penalty_value[1],     # Penalty of penalty_value[1] when value < 1
    value >= 1 & value <= 50 ~ penalty_value[2], 
    value > 50 & value <= 200 ~ penalty_value[3],
    TRUE ~ 1  # Default case when none of the above conditions are met
)
  • Loading branch information
wangzhao0217 authored Sep 30, 2024
1 parent 1cbc020 commit 941fc7c
Showing 1 changed file with 9 additions and 4 deletions.
13 changes: 9 additions & 4 deletions R/corenet.R
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ coherent_network_group = function(coherent_network, key_attribute = "all_fastest
#' @export

prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutch",
road_scores = list("A Road" = 1, "B Road" = 2, "Minor Road" = 100000),penalty_value = 1,
road_scores = list("A Road" = 1, "B Road" = 2, "Minor Road" = 100000),penalty_value = [10000000, 10000, 1000],
transform_crs = 27700) {
# Cast the network to LINESTRING and transform to the specified CRS
network = network |>
Expand All @@ -409,9 +409,9 @@ prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutc
sfnetworks::activate("edges") |>
dplyr::mutate(
# Handle NA values and normalize using key_attribute
value = dplyr::if_else(is.na(!!rlang::sym(key_attribute)), 0, !!rlang::sym(key_attribute)),
value = dplyr::if_else(is.na(!!rlang::sym(key_attribute)), 0.01, !!rlang::sym(key_attribute)),
max_value = max(value, na.rm = TRUE),
value = ifelse(value <= 50, 0, value),
value = ifelse(value <= 5, 0, value),
value = ifelse(value == 0, 0.01, value),
# Calculate logarithmic arterialness and round it
arterialness = ((max_value / value) ^ 3) / max(((max_value / value) ^ 3), na.rm = TRUE),
Expand All @@ -425,7 +425,12 @@ prepare_network = function(network, key_attribute = "all_fastest_bicycle_go_dutc
}),
# Calculate weight considering the road type influence
weight = round(0.95 * arterialness + 0.05 * road_score, 6),
penalty = ifelse(value <= 200, penalty_value, 1),
penalty = dplyr::case_when(
value < 1 ~ penalty_value[1], # Penalty of penalty_value[1] when value < 1
value >= 1 & value <= 50 ~ penalty_value[2],
value > 50 & value <= 200 ~ penalty_value[3],
TRUE ~ 1 # Default case when none of the above conditions are met
),
weight = weight * penalty
)
# network = sfnetworks::activate(network, "nodes")
Expand Down

0 comments on commit 941fc7c

Please sign in to comment.