基于Netlogo的复杂网络本地路由

时间:2017-08-08 04:59:45

标签: netlogo

我创建了一个小的无向网络,其中一些节点作为源,一些是目标。然后我创建了放置在源节点上的步行器。 现在,我想使用这个网络实现一个非常简单的本地路由算法。 在这里,我的算法步骤;

1 go
2 get-list-of-neighbors
3 select one-of from list of neighbors
    check is-visited:
    if yes: [remove from the list 
           check is-loop
           if yes: Die
           else go to setp 3]

  4 else Move-to selected node
  5 check is-target?
    if yes: die 
    else add to list-of-visited and Go

问题: 我是Netlog的新手,不知道如何实现这个算法。 这是我的代码。

to go
ask walkers[
set list-of-neighbors (list [link-neighbors] of location)
let selected-node one-of list-of-neighbors
if (visited?=true)[ set list-of-neighbors remove-duplicate list-of-neighbors
chek if loop? exist
     if yes:
     if no:
if(visited?=false)[ move-to selected-node]
set location selected-node
ask location[ ifelse target=true[die][set list-of-visited lput location 
go ]
end

2 个答案:

答案 0 :(得分:2)

我的回答是对我对your other question的回答稍作修改。我不确定check is-loop究竟是什么意思,所以在我的解决方案中,如果他们没有可以移动的相邻节点(因为他们已经访问过该节点),我只会让步行者死亡。此外,我建议采用略微不同的方法来实现与您概述的算法相同的想法。这里的步骤更像是:

  • Walker选择其中一个尚未移动的邻居
  • 如果没有这样的邻居(因为它已经访问过当前位置的所有邻居), walker就会死
  • 如果未访问的邻居确实存在,则助行器将移至该邻居,并将其新位置添加到其变量locations-list
  • 如果新位置是目标:
    1. 目标节点被标记为已访问
    2. 目标节点更改其颜色
    3. walker去世
  • 如果go程序结束时没有助行器,则会在源节点上生成新的助行器。
  • 如果访问了所有目标,则模型停止

显然,如果有一个线性路径上有多个目标节点,那么步行者每次到达第一个目标时都会死亡,因此永远不会访问那些更远的节点 - 但这只是一个例子。删除die块或修改要播放的其他内容。

就像我说的,这只是对上面链接的答案的一个非常小的修改,但我复制下面的整个代码以便于访问。

breed [nodes node]
breed [walkers walker]

walkers-own [location locations-list]  
nodes-own [ source? target? visited? ] 

to setup
  clear-all
  set-default-shape nodes "circle"
  create-nodes 30 [ 
    set color blue 
    set target? false
    set source? false
    set visited? false
  ]
  ask nodes [ create-link-with one-of other nodes ]
  repeat 500 [ layout ]
  ask nodes [ 
    setxy 0.95 * xcor 0.95 * ycor 
  ]
  ask n-of 3 nodes [
    set target? true
    set color white
  ]

  ask n-of 1 nodes with [ target? = false ] [
    set source? true
    set color green
  ]

  spawn-walkers

  reset-ticks
end

to layout
  layout-spring nodes links 0.5 2 1
end

to spawn-walkers

    create-walkers 1 [
    set color red
    set location one-of nodes with [ source? ]
    move-to location
    set locations-list ( list location)
  ]

end

to go
  ask links [ set thickness 0 ]
  ask walkers [
    let new-location one-of ( [link-neighbors] of location ) with [ not member? self [locations-list] of myself ]
    ifelse new-location = nobody [
      print "I'm stuck!"
      die
    ]
    [
      move-to new-location
      set location new-location
      set locations-list lput location locations-list 
      ask location [ 
        set visited? true
        if target? = true [
          set color color + 1
          ask myself [
            die
          ]
        ]
      ]
    ]
  ]

  if not any? nodes with [ target? = true and visited? = false ] [
    print ("All target nodes have been visited.")
    stop
  ]

  if count walkers < 1 [
    spawn-walkers
  ]  

  tick
end

答案 1 :(得分:0)

我的问题/算法说明:

  1. Walker从源节点开始
  2. 在每个节点,每个步行者选择其邻居的下一个节点
  3. 如果尚未访问所有相邻节点,则在未访问的节点中选择下一个邻居
  4. 如果之前已访问过所有相邻节点,则在所有邻居中均匀地选择下一个节点。步行者被迫返回先前访问过的节点
  5. 如果检测到一个循环,也就是说,助行器必须死亡
  6. 如果访问了所有目标,则模型停止
相关问题