【问题标题】:Random walk rerouting issue using netlogo使用 netlogo 的随机游走重新路由问题
【发布时间】:2017-05-11 11:33:31
【问题描述】:

我使用以下代码创建了一个小型网络并实现了随机游走算法。我使用一些节点作为目标,其中步行者最初放置在一个节点上。我使用带有walker 的路径(walker-own 变量)列表来保存它的位置。

问题:如何防止 walker 不返回那些已经访问并列在其内存(列表)中的节点。 我是 Netlogo 的新手,无法实现这个逻辑。

breed [nodes node]
breed [walkers walker]

walkers-own [location path]  
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
    set path (list location path)
  ]
  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
    set path lput 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


    【解决方案1】:

    您可能想要let new-location one-of [link-neighbors] of location with [not visited] 之类的东西。但是,如果没有,这将产生错误。所以试试这样的。

    to go
      ask links [ set thickness 0 ]
      ask walkers [
        let candidate-locations ([link-neighbors] of location) with [not visited?]
        ifelse any? candidate-locations
        [ let new-location one-of candidate-locations
          move-to new-location
          set location new-location
          set path lput 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 ]
          ]
        ]
        [;;whatever you want to do if all possible paths are blocked
        ]
      ]
    
      ;; 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
    

    【讨论】:

    • 尊敬的先生,我尝试了上述逻辑,方法是使用带有 [ 未访问过的位置的 let Candidate-locations [ link-neighbors ]? ],但它给出了错误“ WITH expected input to be an agentset but got the turtle (node 8) instead”。
    • 我想使用路径列表而不是 " 和 [ 未访问?],你能指导我如何使用它吗?...@JenB
    • 我已经更新了代码。 NetLogo 感到困惑,认为with 附加到location,而不是邻居。所以我添加了括号来澄清优先级。见let candidate-locations ([link-neighbors] of location) with [not visited?]一行
    • 嗨,先生,您能指导我如何检查是否会通过移动到选定的节点来创建任何圈子以及如何避免移动到该节点...@JenB
    • 这是一道逻辑题,不是编程题。为什么不简单地将visited? 变量设置为true 以作为setup 中的起始位置
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-02
    • 1970-01-01
    • 1970-01-01
    • 2014-03-26
    • 1970-01-01
    相关资源
    最近更新 更多