NetLogo nw扩展:如何对多个目标使用nw:扩展

NetLogo nw扩展:如何对多个目标使用nw:扩展,netlogo,Netlogo,大家好,netlogo nw:extension是否可以计算多个目的地的路径 我希望源0经过所有红色节点目标。 我尝试先将的节点链接放到所有目的地都是一个列表中。然后从那里我把最小数量的节点链接作为我的第一条路径,然后把节点(海龟)和节点链接放在访问的位置,这样它就不会再次检查节点和它的链接。例如(节点链接04)(节点链接08),然后将链接和目标节点8添加到已访问的节点。我不知道如何检查节点8是否被选中。 有什么想法吗 to setup ca crt Nodes set-defaul

大家好,netlogo nw:extension是否可以计算多个目的地的路径

我希望源0经过所有红色节点目标。 我尝试先将的节点链接放到所有目的地都是一个列表中。然后从那里我把最小数量的节点链接作为我的第一条路径,然后把节点(海龟)和节点链接放在访问的位置,这样它就不会再次检查节点和它的链接。例如(节点链接04)(节点链接08),然后将链接和目标节点8添加到已访问的节点。我不知道如何检查节点8是否被选中。 有什么想法吗

to setup
  ca
  crt Nodes
  set-default-shape turtles "circle"
  let positions [
    [-7 7] [-1 7] [5 7] [11 7] [-7 1] [-1 1] [5 1] [11 1] [-7 -5] [-1 -5] [5 -5] [11 -5]
    [-7 -11] [-1 -11] [5 -11] [11 -11]
  ]
  foreach sort turtles [
    nodePos -> ask nodePos [
      setxy (first first positions) (last first positions)
      set positions but-first positions
    ]
  ]
  ask turtles [;setxy random-xcor random-ycor
    if Show_Names? = True [show-names]]
  ;ask patches [set pcolor white]
end
to create-random-graph
  ask links [die]
  ask turtles [
    set color blue
    let neighbor-nodes other turtles in-radius 6
    create-node-links-with neighbor-nodes [
      set weight 1
      set label weight
      set color grey
      set thickness 0.1
    ]
  ]
to TEST
  let FDestin[ 9 6 8]
  let Origin 0
  let a 0
  let b []
  let i 0
  while [a < length(FDestin)  ][
    let Destin item a FDestin
    ask turtle Origin [
      set path nw:weighted-path-to turtle Destin weight
      set b lput(path ) b
    ]
    set a a + 1
  ]
let findMinPath sort-by [ [list1 list2] -> length(list1) < length (list2) ]b
let findMin []
set findMin lput item 0 findMinPath findMin
;foreach findMin [ x -> ask one-of node-links x [die]]
end
设置
ca
阴极射线管节点
将默认形状海龟设置为“圆形”
让位置[
[-7 7] [-1 7] [5 7] [11 7] [-7 1] [-1 1] [5 1] [11 1] [-7 -5] [-1 -5] [5 -5] [11 -5]
[-7 -11] [-1 -11] [5 -11] [11 -11]
]
每种海龟[
nodePos->询问nodePos[
setxy(第一个位置)(最后一个位置)
设置位置,但首先设置位置
]
]
问问海龟们,随机的,随机的
如果显示名称?=True[显示名称]]
;询问补丁[设置pcolor白色]
结束
创建随机图
询问链接[死亡]
问海龟[
设置颜色为蓝色
让邻居节点在半径6内的其他海龟
创建与邻居节点的节点链接[
设定重量1
设置标签重量
设置颜色为灰色
设置厚度为0.1
]
]
检验
让FDestin[9 6 8]
让原点为0
让一个0
让b[]
让我0
而[alength(list1)询问其中一个节点链接x[die]]
结束

这有点粗糙,但可能会让你开始。使用这些扩展和设置:

extensions [ nw ]
undirected-link-breed [ node-links node-link ] 
breed [ nodes node ]
breed [ walkers walker ]
turtles-own [ path target-nodes ]
links-own [ weight ]

to setup
  ca
  set-default-shape nodes "circle"
  set-default-shape walkers "arrow"
  let vals ( range 11 -11 -5 )
  foreach vals [ y ->
    foreach reverse vals [ x ->
      ask patch x y [
        sprout-nodes 1 [
          set color blue
          set label who
          set size 2
        ]
      ]
    ]
  ]
  create-network
  ask one-of nodes [
    hatch-walkers 1 [
      set color green
      set pen-size 5
      pd
      set target-nodes nobody
      set path []
    ]      
    ask n-of 3 other nodes [ set color red ]
  ]
  reset-ticks
end
这将创建一个节点网格,以及一个随机放置在其中一个节点上的
walker
。其中三个没有助行器的节点为红色,用作路径中的“目标”节点。然后,您的网络程序如您的问题所示:

to create-network
  ask links [die]
  ask nodes [
    set color blue
    let neighbor-nodes other turtles in-radius 5
    create-node-links-with neighbor-nodes [
      set weight one-of [ 1 2 3 ]
      set label weight      
      set color grey
      set thickness 0.1
    ]
  ]
end
这将为步行者提供一个随机加权的链接网络

现在,为了构建路径,让步行者将红色节点识别为可能的目标。然后,生成所有可能的路径置换,始终从walker所在的节点开始

使用从中修改的代码生成置换

编辑:海龟现在选择加权距离最小的路线,而不是路线上最少的海龟。

计算每条可能路径的海龟数量,并选择整个路线上加权距离最小的路径

to set-path
  if target-nodes = nobody [
    ; Designate any red nodes as targets
    set target-nodes nodes with [ color = red ]
    let start-node one-of nodes-here

    ; Get a list of nodes
    let target-node-list sort target-nodes

    ; Build all possible paths
    let possible-paths map [ i -> sentence start-node i ] path-permutations target-node-list

    ; Get the weighted distance turtles for each possible path
    let path-turtles map [ i -> turtles-on-path i ] possible-paths

    ; Keep the path with the smallest overall weighted distance 
    let shortest-path reduce [
      [ shortest next ] ->
      ifelse-value ( weighted-dist-of-path shortest < weighted-dist-of-path next ) [ shortest ] [ next ] ] path-turtles
    set path shortest-path
  ]
end
一旦海龟知道它应该走哪条路,它就可以以某种方式沿着这条路走——下面是一个简单的例子

to follow-path
  if length path > 0 [
    let target first path 
    face target
    ifelse distance target > 0.5 [
      fd 0.5 
    ] [
      move-to target 
      ask target [
        set color yellow
      ]
      set path but-first path
    ]
  ]
end
所有这些都包含在
go
中,如下所示:

to go
  if not any? nodes with [ color = red ] [
   stop
  ] 
  ask walkers [
    set-path
    follow-path
  ]
  tick
end
给行为一些类似的东西:

编辑:

更简单的选择是让步行者检查最近的(按权重)目标节点,构建路径,沿着该路径,然后在下一个最近的目标到达该路径的末端时选择它(依此类推)。但是,这可能不会给出总体最短路径-例如,请看下图:


绿色轨迹是路径置换行者所走的路径。蓝色方块表示起始节点,橙色方块表示目标节点。橙色轨迹是由简单的步行者拍摄的(如上所述)。您可以看到,总体而言,较简单的步行者所走的路径具有较高的总体权重成本,因为它只评估到下一个目标的权重路径,而不是整个路径的总体权重成本。

首先,我非常感谢您的帮助。这是我的问题,因为nw:扩展是用来寻找最短路径的,为什么你仍然需要做排列来构建所有可能的路径?对不起,代码太乱了,我还是Netlogwell的新手,你肯定可以更容易地让步行者选择最近的(按重量)目标节点并移动到那里,然后选择下一个最近的目标节点并移动到该节点,依此类推。但是,这并不能保证编辑中的最短路径-示例图像。因此,如果我使用开始节点并找到到目的地的所有最短路径,然后从那里选择最小路径,例如:第一条路径(turtle0)(turtle4)第二条路径(turtle0)(turtle1)(turtle2),情况也是一样的。然后我选择第一条路径,然后用node4(turtle4)替换开始节点,并继续这样做。我理解您的代码并感谢您的帮助,但在使用您的设置之前,我想先尝试一下我的想法。谢谢
to follow-path
  if length path > 0 [
    let target first path 
    face target
    ifelse distance target > 0.5 [
      fd 0.5 
    ] [
      move-to target 
      ask target [
        set color yellow
      ]
      set path but-first path
    ]
  ]
end
to go
  if not any? nodes with [ color = red ] [
   stop
  ] 
  ask walkers [
    set-path
    follow-path
  ]
  tick
end