netlogo中的随机游走-停止条件问题 [英] random walk in netlogo- stoping condition issue

查看:222
本文介绍了netlogo中的随机游走-停止条件问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建了一个小型网络,该网络由通过链接连接的节点组成. 有些节点是源,有些是目标.我尝试实现随机游走算法.

I have created a small network consist of nodes which are connected through links. Some of the nodes are sources and some of the targets. I try to implement Random Walk algorithm.

我将步行者放置在源节点上,并且步行者在网络中随机移动.现在,我想检查沃克到达目标节点的位置,如果沃克访问了所有目标节点,然后请沃克停止或死亡. 我是NetLogo的新手,不知道如何实现此逻辑. 任何帮助或指导将不胜感激.

I place walkers on source nodes, and walkers are moving randomly through the network. Now, I want to check walker reached to targets nodes, if all target nodes are visited by walker then ask walker to stop or die. I'm new to NetLogo and don't know how to implement this logic. Any help or guide will be appreciated.

推荐答案

一种方法是让您的节点知道它们是否是目标,以及是否已被访问.这样,如果乌龟访问某个节点,则可以将该节点标记为已访问过.然后,您可以在go过程的末尾使用stop过程,以检查是否仍然存在作为目标但没有访问过 的节点.我对环行海龟"示例做了一些修改,以显示一种可以执行此操作的方法-几乎所有下面的代码都是直接从该模型中提取的.

One way to do this is to have your nodes know if they are a target, and if they have been visited. That way, if a turtle visits a node, that node can be marked as having been visited. Then, you can have a stop procedure at the end of your go procedure that checks if any nodes are still present that are a target but have not been visited. I have made slight modifications to the Link-Walking Turtles Example to show one way that you could do this- almost all the code below is directly pulled from that model.

breed [nodes node]
breed [walkers walker]

walkers-own [location]  
nodes-own [ target? visited? ] 

to setup
  clear-all
  set-default-shape nodes "circle"
  create-nodes 30 [ 
    set color blue 
    set target? 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 5 nodes [
    set target? true
    set color white
  ]

  create-walkers 1 [
    set color red
    set location one-of nodes
    move-to location
  ]
  reset-ticks
end

to layout
  layout-spring nodes links 0.5 2 1
end

to go
  ask links [ set thickness 0 ]
  ask walkers [
    let new-location one-of [link-neighbors] of location
    move-to new-location
    set location new-location
    ;; This gets turtles to ask their current location 
    ;; to set visited and target to true.
    ask location [
      set visited? true
      if target? = true [
        set color red
      ]
    ]
  ]

  ;; Check for target nodes that have NOT been visited. 
  ;; If there aren't any, stop the model.
  if not any? nodes with [ target? = true and visited? = false ] [
   print ("All target nodes have been visited.")
   stop
  ] 
  tick
end

这篇关于netlogo中的随机游走-停止条件问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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