找到具有其中一个数据的最近节点

find the nearest node that has one-of data

我有两个品种

breed[nodes]
breed [messages]
nodes-own [data]
messages-own [basedata]

我有一个有 n 个节点的网络。每个节点都有特定的数据。我选择随机海龟作为 sink.in 我的网络数据从海龟(首先所有海龟都是节点)分发到 sink.each 节点,在途中变成 message.and 每条消息都有一个内存 space 也保留从 message.messages 基础数据传递的那些数据保留原始数据。 我希望接收器找到最近的节点,例如 "D1".

to setup
setup1
setup-spatially-clustered-network ;create links
ask links [set color white]
end

to setup1

  __clear-all-and-reset-ticks
create-nodes number-of-nodes
 [
   setxy (random-xcor * 0.95) (random-ycor * 0.95)
   set shape "circle"
   set color green
   set value ["D1" "D2" "D3" "D4" "D5" "D6"]
   set data one-of value
   set label data
   set dontpick false 
   set visit false  
   ]
end

    to test1
    ask one-of turtles
    [
      set color red
      set label "sink"
      set nodenumberdestination who 
    ]
    ask min-one-of turtles with [(data = "D1") or (basedata = "D1")][distance turtle nodenumberdestination]
    [
    ]
    error : NODES breed does not own variable BASEDATA

问题是当你做

ask min-one-of turtle with [(data = "D1") or (basedata = "D1")]

它可以指代 nodesmessage,但彼此不共享同一个变量。

例如:当它在节点上运行时,检查[(basedata="D1")] 时会return 错误,因为它不拥有basedata。同样,当它在消息上运行时,它也会在检查 [(data="D1")] 时出现 return 错误。但是,由于您还没有创建任何 messages,所以它总是 returning error : NODES breed does not own variable BASEDATA

因此,在引用 agentset 时需要具体说明,因为任何包含使用的变量的命令块都将敏感于变量属于谁。

你在问题中陈述的目标与你的代码之间存在冲突,这表明你还没有完全思考你正在做的事情。 (您发布的代码也不完整;例如,它不包含您的 globals 声明,也不包含 setup-messages。)所以首先要问一个问题:您真的是想使用 turtles 而不是 nodestest1 中?如果是,则意味着您允许 message 成为您的接收器。所以我会假设不,正如你的实际问题所建议的那样。引入新的全局 sink

to test1a
  ask one-of nodes [ ;move this to setup!
    set sink self
    set label "sink" set color red
  ]
  ask sink [
    let _choice min-one-of (other nodes with [data = "D1"]) [distance myself]
    ask _choice [] ;do something
  ]
end

这回答了你的问题。如果你真的想从所有海龟中做出选择,正如你发布的代码所建议的那样,你将不得不提出一个新问题。