簡體   English   中英

R igraph:在 igraph 中查找最短路徑,為其添加權重並搜索替代

[英]R igraph: Finding shortest path in igraph, adding weight to it and search for alternative

我玩弄https://rdrr.io/rforge/osmar/src/demo/navigator.R (導航器演示)。 我想找到幾條只包含一條的路徑。

看來我不能使用 function all_simple_paths因為它永遠不會終止。 在我找到一條最短路徑后是否有可能

route <- get.all.shortest.paths(gr_muc, from = as.character(hway_start_node), to = as.character(hway_end_node))[[1]]

增加整個路線的權重並使用 function get.all.shortest.paths再次搜索以找到下一個替代方案? 這是正確的方法還是有替代方法?

先感謝您!

    library(tidyverse)
library(osmdata)
library(osmar) # (geosphere is inclued in osmar)
library(sf)
library(ggmap)
library(prettymapr)
library(leaflet)
library(igraph)
library(stplanr)
library(rgeos)

### Download and extract data: #######################################

download.file("http://osmar.r-forge.r-project.org/muenchen.osm.gz",
              "muenchen.osm.gz")

system("gzip -d muenchen.osm.gz")



### Import subset based on bounding box: #############################

src <- osmsource_osmosis(file = "muenchen.osm",
                         osmosis = "osmosis")

muc_bbox <- center_bbox(11.575278, 48.137222, 60000, 60000)

muc <- get_osm(muc_bbox, src)

hways_muc<-muc
gr_muc <- as_igraph(hways_muc)

 hway_start_node <- local({
  id <- find(muc, node(tags(v == "Sendlinger Tor")))[1]
  find_nearest_node(muc, id, way(tags(k == "highway")))
})

hway_start <- subset(muc, node(hway_start_node))

hway_end_node <- local({
  id <- find(muc, node(attrs(lon > 11.59 & lat > 48.150)))[1]
  find_nearest_node(muc, id, way(tags(k == "highway")))
})
hway_end <- subset(muc, node(hway_end_node))



route <- get.all.shortest.paths(gr_muc,
                                    from = as.character(hway_start_node),
                                    to = as.character(hway_end_node),
                                    mode = "all")[[1]]

增加路徑的權重完全符合預期:

library(osmar)
library(igraph)

### Get data ----
src <- osmsource_api(url = "https://api.openstreetmap.org/api/0.6/")
muc_bbox <- center_bbox(11.575278, 48.137222, 1000, 1000)
muc <- get_osm(muc_bbox, src)

### Reduce to highways: ----
hways <- subset(muc, way_ids = find(muc, way(tags(k == "highway"))))
hways <- find(hways, way(tags(k == "name")))
hways <- find_down(muc, way(hways))
hways <- subset(muc, ids = hways)

#### Plot data ----
## Plot complete data and highways on top:
plot(muc)
plot_ways(muc, col = "lightgrey")
plot_ways(hways, col = "coral", add = TRUE)

### Define route start and end nodes: ----
id<-find(muc, node(tags(v %agrep% "Sendlinger Tor")))[1]
hway_start_node <-find_nearest_node(muc, id, way(tags(k == "highway"))) 
hway_start <- subset(muc, node(hway_start_node))

id <- find(muc, node(attrs(lon > 11.58 & lat > 48.15)))[1]
hway_end_node <- find_nearest_node(muc, id, way(tags(k == "highway")))
hway_end <- subset(muc, node(hway_end_node))

## Add the route start and and nodes to the plot:
plot_nodes(hway_start, add = TRUE, col = "red", pch = 19, cex = 2)
plot_nodes(hway_end, add = TRUE, col = "red", pch = 19, cex = 2)

### Create street graph ----
gr <- as.undirected(as_igraph(hways))

### Compute shortest route: ----
# Calculate route
route <- function(start_node,end_node) {
  get.shortest.paths(gr,
                    from = as.character(start_node),
                    to = as.character(end_node), 
                    mode = "all")[[1]][[1]]}
# Plot route
plot.route <- function(r,color) {
  r.nodes.names <- as.numeric(V(gr)[r]$name)
  r.ways <- subset(hways, ids = osmar::find_up(hways, node(r.nodes.names)))
  plot_ways(r.ways, add = TRUE, col = color, lwd = 2)
}

# Number of new ways to look for 
nways <- 10
# Weight factor applied to already found way
weightfactor <- 2

for (numway in 1:nways) {
  r <- route(hway_start_node,hway_end_node)
  color <- colorRampPalette(c("springgreen","royalblue"))(nways)[numway]
  plot.route(r,color)
  # Modify current route weight
  E(gr)[r]$weight <- E(gr)[r]$weight * weightfactor
}

結果示例

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM