NetLogo nw扩展名:如何将nw:extension用于多个目标 [英] NetLogo nw extension: how to use nw:extension for multiple destination

查看:200
本文介绍了NetLogo nw扩展名:如何将nw:extension用于多个目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,netlogo nw:extension可以计算多个目标的路径.

Hi guys is it possible for netlogo nw:extension to calculate path for multiple destination.

我希望我的源0通过所有红色节点的目的地. 我尝试先将到所有目标的节点链接放在列表中.然后从那里,我以最小数量的节点链接作为我的第一条路径,然后将节点(turtle)和节点链接放到访问过的位置,这样它就不会检查该节点,而是再次链接.例如(节点链接0 4)(节点链接0 8),然后将链接和目标节点8添加到已访问.我不知道如何检查是否选择了节点8. 有什么想法吗?

I wanted my source 0 to pass by all the red nodes destination. I've attempt by first putting the node-links of to all destination is a list. Then from there i take the minimum number of node-links as my first path and then put the nodes(turtle) and node-link to visited so it doesn't check the node and it's link again. Eg (node-link 0 4) (node-link 0 8), then add the links and the destination node 8 to visited. I do not know how to check that the node 8 is selected. Any idea??

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

推荐答案

这有点粗糙,但可能会让您入门.使用这些扩展程序和设置:

This is sort of rough but may get you started. With these extensions and setup:

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.没有助行器的三个节点为红色,充当路径中的目标"节点.然后,按照您的问题进行网络操作:

That creates a grid of nodes, as well as a single walker randomly placed on one of the nodes. Three of the nodes without a walker are red to act as 'target' nodes in the path. Then, your network procedure as in your question:

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

这为您提供了一个随机加权的链接网络,供步行者遵循.

That gives you a randomly weighted network of links for the walker to follow.

现在,要构建路径,请步行者将红色节点识别为可能的目标.然后,生成所有可能的路径排列,始终从walker所在的节点开始.

Now, to build paths, get the walkers to recognize the red nodes as possible targets. Then, generate all possible path permutations, always starting at the node that the walker is on.

排列是使用从 现在,海龟选择的加权距离最小,而不是途中的海龟最少.

计算每个可能路径的海龟数量,然后在整个路径中选择加权距离最小的路径.

Count the number of turtles of each possible path, and select the path with the smallest weighted distance over the entire route.

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

set-path使用以下两个报告程序:

set-path uses these two reporters:

to-report turtles-on-path [ in-path ]
  ; A reporter that returns the path from the start node of a given path
  ; to the final node of that path.
  let temp-path []
  ( foreach ( but-last in-path ) ( but-first in-path ) [
    [ from to_ ] ->
    ask from [
      ifelse length temp-path = 0 [
        set temp-path nw:turtles-on-weighted-path-to to_ weight
      ] [
        set temp-path sentence temp-path but-first nw:turtles-on-weighted-path-to to_ weight
        ]
      ]
    ] )
    report temp-path
end

to-report weighted-dist-of-path [ in-path ]
  let weighted-dist 0
  ( foreach ( but-last in-path ) ( but-first in-path ) [
    [ f t ] ->
    ask f [
      set weighted-dist weighted-dist + nw:weighted-distance-to t weight
    ]
    ] )
  report weighted-dist
end

一旦海龟知道应该走什么路,它就能以某种方式遵循这条路-这是一个简单的例子.

Once the turtle knows what path it should take, it can follow that path somehow- here is a simple example.

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中,如下所示:

All that is wrapped up in go like so:

to go
  if not any? nodes with [ color = red ] [
   stop
  ] 
  ask walkers [
    set-path
    follow-path
  ]
  tick
end

使行为类似:

简单得多的选项是让步行者检查最近的(按重量计)目标节点,构建路径,遵循该路径,然后在到达该路径的末端时选择下一个最近的目标(依此类推) ).但是,这可能无法给出最短的路径-例如,请看下面的图片:

The much-simpler option is to just have the walker check the nearest (by weight) target node, build the path, follow that path, then select the next nearest target once it reaches the end of that path (and so on). However, that may not give the overall shortest path- for example, look at the image below:

绿色轨迹是路径排列行进器所采用的路径.蓝色方块表示起始节点,橙色方块表示目标节点.橙色走线是较简单的助行器所捕获的走线(如上所述).您可以看到,总体而言,较简单的助行器采用的路径的总重量成本较高,因为它仅评估到下一个目标的加权路径,而不是整个路径的整体加权成本.

The green trace is the path taken by the path-permutations walker. The blue square indicates the starting node, the orange squares designate the target nodes. The orange trace is the one taken by the simpler walker (as described above). You can see that overall, the path taken by the simpler walker has a higher overall weight cost because it is only assessing the weighted path to the next target rather than the overall weighted cost of the entire path.

这篇关于NetLogo nw扩展名:如何将nw:extension用于多个目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆