如何为NetLogo中的所有代理分配最短距离? 如何为所有代理分配最短距离 当前状态:“我的代码”仅查找1人的最短路径,但当人数超过1人时无法查找 问题:如何为每个人分配最短路径? 背景

如何为NetLogo中的所有代理分配最短距离? 如何为所有代理分配最短距离 当前状态:“我的代码”仅查找1人的最短路径,但当人数超过1人时无法查找 问题:如何为每个人分配最短路径? 背景,netlogo,shortest-path,a-star,multi-agent,Netlogo,Shortest Path,A Star,Multi Agent,我在NetLogo模型库中操纵交通网格模型,为行人创建最短路径算法(有点接近a*)。下面的界面显示一个代理站在其源站上,该源站的颜色为淡黄色,与目标站的颜色相同。它也适用于3人 1人 三个人 安装程序 下面是我作为设置编写的代码chunck。这一部分似乎没有任何问题 globals [ grid-x-inc ;; the amount of patches in between two roads in the x direction grid-y-i

我在NetLogo模型库中操纵交通网格模型,为行人创建最短路径算法(有点接近a*)。下面的界面显示一个代理站在其源站上,该源站的颜色为淡黄色,与目标站的颜色相同。它也适用于3人

  • 1人
  • 三个人
安装程序 下面是我作为设置编写的代码chunck。这一部分似乎没有任何问题

globals
[
  grid-x-inc               ;; the amount of patches in between two roads in the x direction
  grid-y-inc               ;; the amount of patches in between two roads in the y direction
  intersections ;; agentset containing the patches that are intersections
  roads         ;; agentset containing the patches that are roads
  population
]

breed[people person]

patches-own
[

  father
  navigated?
  active?
  Heuristic
  step
  total-cost
  is-intersection?
  start
  finish
]

people-own
[
  origin
  destination
  h-distance ;; short for heuristic distance. Euclidean distance to goal
  path
]

to setup
  clear-all
  setup-globals
  setup-patches
  setup-people
  setup-destination
  reset-ticks
end

to setup-globals
  set grid-x-inc world-width / grid-size-x
  set grid-y-inc world-height / grid-size-y

  ask patches
  [
    set father nobody
    set navigated? false
    set active? false
    set total-cost 0
  ]
end


to setup-patches
  ;; initialize the patch-owned variables and color the patches to a base-color
  ask patches [ set pcolor brown + 3 ]

  ;; initialize the global variables that hold patch agentsets
  set roads patches with
    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) or
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]
  set intersections roads with
    [(floor((pxcor + max-pxcor - floor(grid-x-inc - 1)) mod grid-x-inc) = 0) and
    (floor((pycor + max-pycor) mod grid-y-inc) = 0)]
  ask roads [ set pcolor white ]

  ask intersections [ set is-intersection? true
    ask neighbors [ set is-intersection? true ]]

end


to setup-people
    create-people no-of-people
  [
    set shape "person"
    set size 1
    set color black
    move-to one-of patches with [pcolor != brown + 3]
    set origin patch-here
    set destination one-of patches with [pcolor != brown + 3 and is-intersection? != true]
    set h-distance distance destination
  ]
  set population []  ;; create list of individuals
  let sort-turtles sort people set population sort-turtles
end


to setup-destination
foreach population [ individual ->
  ask individual [
   let roads_ roads with [pcolor = white]
   ask roads_ [
     set navigated? false
     set active? false
     set Heuristic 0
     set step 0
     set total-cost 0
     set start false
     set finish false
   ]]
    let current [origin] of individual
    let target [destination] of individual

ask individual [
    if target != nobody [
      ask current [
        set pcolor (10 + random 130)
        set step 0
        set start true
        set finish false
        set navigated? false
      ]
      ask target [
        set pcolor [pcolor] of current
        set navigated? true
        set start false
        set finish true
      ]]
  ]]
end
问题1:设置所有路径
set path
跟踪从原始补丁到虚拟世界中任何地方的所有步骤。该代码将道路涂成黄色,并在道路上添加标签。此步骤将重复50次,以找到所有可能的步骤

此部分出现问题,此代码仅适用于一个代理,不适用于整个代理。

to set-path

repeat 50 [
  ask patches with [pcolor = white and pcolor != yellow] [
      if any? neighbors4 with [ start = true ] or
       any? neighbors4 with [ pcolor = yellow ]
    [ set pcolor yellow
      let laststep [step] of one-of neighbors4 with [pcolor = yellow or
                                                            start = true]
        ifelse [plabel] of patches != nobody [
          set step laststep + 1
          set plabel step
          set father step
          set total-cost father + heuristic
          set plabel-color red][set plabel ""]
  ]]]

  let allroads [self] of patches with [pcolor != brown]
  let final_location one-of patches with [finish = true]

  foreach allroads [ road_patch ->
    ask road_patch [set Heuristic distance final_location]
    ]
end
问题2:设置最短路径 此过程通过查找修补程序并将其添加到名为
path
的列表中来查找最短路径。因为前面的过程只对一个代理有效,所以这个过程还将把补丁分配给一个代理,这不是我想要的

to shortest-path
  ask patches with [start = true] [
    let maxsteps 0
    ask patches with [navigated? = true]
  [ let check-steps min-one-of neighbors4 with
        [pcolor != brown + 3 and pcolor != white][step]
    set maxsteps [step] of check-steps
  ]

  let num (range maxsteps 0 -1)
  let paths []

  foreach num [ navigation ->
  ask patches with [navigated? = true] [
    let nav-patch min-one-of neighbors4 with
        [step = navigation and pcolor != brown + 3][step]
    if nav-patch != nobody [ask nav-patch [set navigated? true set paths fput self paths]]
    ]]
  ask people [set path paths]

    ]
  ask patches with [pcolor = yellow][set pcolor white set plabel ""]
end
小结:我希望模型看起来像什么
  • 如果NetLogo内有2人以上,如何为每个人分配最短路径?

  • 我希望模型看起来像什么

  • 文件:


你能描述一下它为什么只对一个代理有效吗?当您尝试使用5个代理运行它时,现在发生了什么?