random walk in netlogo- stoping condition issue

2019-07-25 00:37发布

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.

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.

标签: netlogo
1条回答
Evening l夕情丶
2楼-- · 2019-07-25 01:17

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
查看更多
登录 后发表回答